Difference between revisions of "API/Samples/Groovy/Office/PropertySet"
From Apache OpenOffice Wiki
(Add link to CleanUpForHTML sample) |
|||
(One intermediate revision by the same user not shown) | |||
Line 6: | Line 6: | ||
First let's look what the code for this sample is in Java: | First let's look what the code for this sample is in Java: | ||
==The Java Code== | ==The Java Code== | ||
− | < | + | <syntaxhighlight lang="java"> |
// Get the ActionEvent object from the ARGUMENTS list | // Get the ActionEvent object from the ARGUMENTS list | ||
ActionEvent event = (ActionEvent) ARGUMENTS[0]; | ActionEvent event = (ActionEvent) ARGUMENTS[0]; | ||
Line 31: | Line 31: | ||
xDialog.endExecute(); | xDialog.endExecute(); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
And now the Groovy way: | And now the Groovy way: | ||
==The Groovy Code== | ==The Groovy Code== | ||
− | < | + | <syntaxhighlight lang="groovy"> |
// Get the ActionEvent object from the ARGUMENTS list | // Get the ActionEvent object from the ARGUMENTS list | ||
ActionEvent event = (ActionEvent) ARGUMENTS[0]; | ActionEvent event = (ActionEvent) ARGUMENTS[0]; | ||
Line 52: | Line 52: | ||
control.getContext().uno(XDialog).endExecute() | control.getContext().uno(XDialog).endExecute() | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
Or here's any even shorter version that makes further use of Groovy's dynamic typing: | Or here's any even shorter version that makes further use of Groovy's dynamic typing: | ||
− | < | + | <syntaxhighlight lang="groovy"> |
// Get the ActionEvent object from the ARGUMENTS list | // Get the ActionEvent object from the ARGUMENTS list | ||
def event = ARGUMENTS[0] | def event = ARGUMENTS[0] | ||
Line 72: | Line 72: | ||
control.context.uno(XDialog).endExecute() | control.context.uno(XDialog).endExecute() | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
==The UnoCategory API Helper== | ==The UnoCategory API Helper== | ||
In order to let Groovy know how to make those adaptations for the UNO API, we use a [http://groovy.codehaus.org/Groovy+Categories Groovy Category]. For this sample that code looks like this: | In order to let Groovy know how to make those adaptations for the UNO API, we use a [http://groovy.codehaus.org/Groovy+Categories Groovy Category]. For this sample that code looks like this: | ||
− | < | + | <syntaxhighlight lang="java"> |
class UnoCategory { | class UnoCategory { | ||
public static Object uno(Object unoObj, Class clazz) {UnoRuntime.queryInterface(clazz, unoObj)} | public static Object uno(Object unoObj, Class clazz) {UnoRuntime.queryInterface(clazz, unoObj)} | ||
Line 82: | Line 82: | ||
public static void putAt(XPropertySet pset, String pname, Object newValue) {pset.setPropertyValue(pname, newValue)} | public static void putAt(XPropertySet pset, String pname, Object newValue) {pset.setPropertyValue(pname, newValue)} | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
To see this at work in some real code, see the [[API/Samples/Groovy/Writer/CleanUpForHTML]] example. | To see this at work in some real code, see the [[API/Samples/Groovy/Writer/CleanUpForHTML]] example. |
Latest revision as of 17:13, 30 January 2021
Contents
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.