Difference between revisions of "API/Samples/Groovy/Office/PropertySet"
From Apache OpenOffice Wiki
(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...) |
|||
| (5 intermediate revisions by 2 users not shown) | |||
| 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== | ||
| + | <syntaxhighlight 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(); | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | And now the Groovy way: | ||
| + | ==The Groovy Code== | ||
| + | <syntaxhighlight lang="groovy"> | ||
| + | // 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() | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | 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 | ||
| + | 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() | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ==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: | ||
| + | <syntaxhighlight lang="java"> | ||
| + | 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)} | ||
| + | } | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | 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
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.