Difference between revisions of "API/Samples/Groovy/Office/PropertySet"

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Groovy
Jump to: navigation, search
(New page: <noinclude>Category:API Category:Samples Category:Groovy Category:Office</noinclude> ==About the PropertySet Example== This example shows how to access properties in a Gro...)
 
Line 4: Line 4:
 
This example shows how to access properties in a Groovy macro or extension using a category (aka "mixin") UNO API helper.
 
This example shows how to access properties in a Groovy macro or extension using a category (aka "mixin") UNO API helper.
  
==The Code==
+
First let's look what the code for this sample is in Java:
 +
==The Java Code==
 +
<source lang="java">
 +
// Get the ActionEvent object from the ARGUMENTS list
 +
ActionEvent event = (ActionEvent) ARGUMENTS[0];
 +
 
 +
// Each argument is of type Any so we must use the AnyConverter class to
 +
// convert it into the interface or primitive type we expect
 +
XButton button = (XButton)AnyConverter.toObject(
 +
    new Type(XButton.class), event.Source);
 +
 
 +
// We can now query for the model of the button and get its properties
 +
XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
 +
XControlModel cmodel = control.getModel();
 +
XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
 +
    XPropertySet.class, cmodel);
 +
 
 +
if (pset.getPropertyValue("Label").equals("Exit"))
 +
{
 +
    // We can get the XDialog in which this control appears by calling
 +
    // getContext() on the XControl interface
 +
    XDialog xDialog = (XDialog)UnoRuntime.queryInterface(
 +
        XDialog.class, control.getContext());
 +
   
 +
    // Close the dialog
 +
    xDialog.endExecute();
 +
}
 +
</source>
 +
 
 +
And now the Groovy way:
 +
==The Groovy Code==
 +
<source lang="java">
 +
// Get the ActionEvent object from the ARGUMENTS list
 +
ActionEvent event = (ActionEvent) ARGUMENTS[0];
 +
 
 +
// Each argument is of type Any so we must use the AnyConverter class to
 +
// convert it into the interface or primitive type we expect
 +
XButton button = (XButton)AnyConverter.toObject(new Type(XButton), event.Source);
 +
 
 +
// We can now query for the model of the button and get its properties
 +
XControl control = button.uno(XControl)
 +
XPropertySet pset = control.getModel().uno(XPropertySet)
 +
 
 +
if (pset["Label"].equals("Exit"))
 +
{
 +
    // Close the dialog
 +
    control.getContext().uno(XDialog).endExecute()
 +
}
 +
</source>
 +
 
 +
Or here's any even shorter version that makes further use of Groovy's dynamic typing:
 +
<source lang="java">
 +
// Get the ActionEvent object from the ARGUMENTS list
 +
def event = ARGUMENTS[0]
 +
 
 +
// Each argument is of type Any so we must use the AnyConverter class to
 +
// convert it into the interface or primitive type we expect
 +
def button = AnyConverter.toObject(new Type(XButton), event.Source)
 +
 
 +
// We can now query for the model of the button and get its properties
 +
def control = button.uno(XControl)
 +
def pset = control.model.uno(XPropertySet)
 +
 
 +
if (pset["Label"].equals("Exit"))
 +
{
 +
    // Close the dialog
 +
    control.context.uno(XDialog).endExecute()
 +
}
 +
</source>

Revision as of 22:23, 21 June 2008


About the PropertySet Example

This example shows how to access properties in a Groovy macro or extension using a category (aka "mixin") UNO API helper.

First let's look what the code for this sample is in Java:

The Java Code

// Get the ActionEvent object from the ARGUMENTS list
ActionEvent event = (ActionEvent) ARGUMENTS[0];
 
// Each argument is of type Any so we must use the AnyConverter class to
// convert it into the interface or primitive type we expect
XButton button = (XButton)AnyConverter.toObject(
    new Type(XButton.class), event.Source);
 
// We can now query for the model of the button and get its properties
XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
XControlModel cmodel = control.getModel();
XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
    XPropertySet.class, cmodel);
 
if (pset.getPropertyValue("Label").equals("Exit"))
{
    // We can get the XDialog in which this control appears by calling
    // getContext() on the XControl interface
    XDialog xDialog = (XDialog)UnoRuntime.queryInterface(
        XDialog.class, control.getContext());
 
    // Close the dialog
    xDialog.endExecute();
}

And now the Groovy way:

The Groovy Code

// Get the ActionEvent object from the ARGUMENTS list
ActionEvent event = (ActionEvent) ARGUMENTS[0];
 
// Each argument is of type Any so we must use the AnyConverter class to
// convert it into the interface or primitive type we expect
XButton button = (XButton)AnyConverter.toObject(new Type(XButton), event.Source);
 
// We can now query for the model of the button and get its properties
XControl control = button.uno(XControl)
XPropertySet pset = control.getModel().uno(XPropertySet)
 
if (pset["Label"].equals("Exit"))
{
    // Close the dialog
    control.getContext().uno(XDialog).endExecute()
}

Or here's any even shorter version that makes further use of Groovy's dynamic typing:

// Get the ActionEvent object from the ARGUMENTS list
def event = ARGUMENTS[0]
 
// Each argument is of type Any so we must use the AnyConverter class to
// convert it into the interface or primitive type we expect
def button = AnyConverter.toObject(new Type(XButton), event.Source)
 
// We can now query for the model of the button and get its properties
def control = button.uno(XControl)
def pset = control.model.uno(XPropertySet)
 
if (pset["Label"].equals("Exit"))
{
    // Close the dialog
    control.context.uno(XDialog).endExecute()
}
Personal tools