summaryrefslogtreecommitdiff
path: root/scripting/examples
diff options
context:
space:
mode:
authorSander Vesik <svesik@openoffice.org>2004-04-19 21:59:31 +0000
committerSander Vesik <svesik@openoffice.org>2004-04-19 21:59:31 +0000
commit543a031e01c1789d86bd7b37a15105196eb31217 (patch)
treeda1686f614770475084b5cf0464cd650a1df80ae /scripting/examples
parent507ea76893b794c8dfb8409c4ac9229b4c7b6454 (diff)
INTEGRATION: CWS scriptingf5 (1.1.2); FILE ADDED
2004/02/19 17:50:36 toconnor 1.1.2.1: #i25271# Add BeanShell script to handle button click events in Highlight
Diffstat (limited to 'scripting/examples')
-rw-r--r--scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh103
1 files changed, 103 insertions, 0 deletions
diff --git a/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh b/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh
new file mode 100644
index 000000000000..acfe56e2a65f
--- /dev/null
+++ b/scripting/examples/beanshell/Highlight/ButtonPressHandler.bsh
@@ -0,0 +1,103 @@
+import com.sun.star.uno.*;
+import com.sun.star.awt.*;
+import com.sun.star.lang.*;
+import com.sun.star.beans.*;
+import com.sun.star.util.*;
+import com.sun.star.script.framework.browse.DialogFactory;
+
+// 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();
+}
+else
+{
+ // We can get the list of controls for this dialog by calling
+ // getContext() on the XControl interface of the button
+ XControlContainer controls = (XControlContainer)UnoRuntime.queryInterface(
+ XControlContainer.class, control.getContext());
+
+ // Now get the text field control from the list
+ XTextComponent textField = (XTextComponent)
+ UnoRuntime.queryInterface(
+ XTextComponent.class, controls.getControl("HighlightTextField"));
+
+ String searchKey = textField.getText();
+
+ // highlight the text in red
+ java.awt.Color cRed = new java.awt.Color(255, 0, 0);
+ int red = cRed.getRGB();
+
+ XReplaceable replaceable = (XReplaceable)
+ UnoRuntime.queryInterface(XReplaceable.class, context.getDocument());
+
+ XReplaceDescriptor descriptor =
+ (XReplaceDescriptor) replaceable.createReplaceDescriptor();
+
+ // Gets a XPropertyReplace object for altering the properties
+ // of the replaced text
+ XPropertyReplace xPropertyReplace = (XPropertyReplace)
+ UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
+
+ // Sets the replaced text property fontweight value to Bold
+ PropertyValue wv = new PropertyValue("CharWeight", -1,
+ new Float(com.sun.star.awt.FontWeight.BOLD),
+ com.sun.star.beans.PropertyState.DIRECT_VALUE);
+
+ // Sets the replaced text property color value to RGB parameter
+ PropertyValue cv = new PropertyValue("CharColor", -1,
+ new Integer(red),
+ com.sun.star.beans.PropertyState.DIRECT_VALUE);
+
+ // Apply the properties
+ PropertyValue[] props = new PropertyValue[] { cv, wv };
+
+ try {
+ xPropertyReplace.setReplaceAttributes(props);
+
+ // Only matches whole words and case sensitive
+ descriptor.setPropertyValue(
+ "SearchCaseSensitive", new Boolean(true));
+ descriptor.setPropertyValue("SearchWords", new Boolean(true));
+ }
+ catch (com.sun.star.beans.UnknownPropertyException upe) {
+ System.err.println("Error setting up search properties");
+ return;
+ }
+ catch (com.sun.star.beans.PropertyVetoException pve) {
+ System.err.println("Error setting up search properties");
+ return;
+ }
+ catch (com.sun.star.lang.WrappedTargetException wte) {
+ System.err.println("Error setting up search properties");
+ return;
+ }
+
+ // Replaces all instances of searchKey with new Text properties
+ // and gets the number of instances of the searchKey
+ descriptor.setSearchString(searchKey);
+ descriptor.setReplaceString(searchKey);
+ replaceable.replaceAll(descriptor);
+}
+
+// BeanShell OpenOffice.org scripts should always return 0
+return 0;