API/Samples/Groovy/Office/PropertySet

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Groovy
Jump to: navigation, search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


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()
}

The UnoCategory API Helper

In order to let Groovy know how to make those adaptations for the UNO API, we use a Groovy Category. For this sample that code looks like this:

class UnoCategory {
    public static Object uno(Object unoObj, Class clazz) {UnoRuntime.queryInterface(clazz, unoObj)}
    public static Object getAt(XPropertySet pset, String pname) {pset.getPropertyValue(pname)}
    public static void putAt(XPropertySet pset, String pname, Object newValue) {pset.setPropertyValue(pname, newValue)}
}

To see this at work in some real code, see the API/Samples/Groovy/Writer/CleanUpForHTML example.

Personal tools