1 // this code is bound to the events generated by the buttons in the dialog
2 // it will close the dialog or find and highlight the text entered in the
3 // dialog (depending on the button pressed)
4 import com.sun.star.uno.*;
5 import com.sun.star.awt.*;
6 import com.sun.star.lang.*;
7 import com.sun.star.beans.*;
8 import com.sun.star.util.*;
9 import com.sun.star.script.framework.browse.DialogFactory;
11 // Get the ActionEvent object from the ARGUMENTS list
12 ActionEvent event = (ActionEvent) ARGUMENTS[0];
14 // Each argument is of type Any so we must use the AnyConverter class to
15 // convert it into the interface or primitive type we expect
16 XButton button = (XButton)AnyConverter.toObject(
17 new Type(XButton.class), event.Source);
19 // We can now query for the model of the button and get its properties
20 XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
21 XControlModel cmodel = control.getModel();
22 XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
23 XPropertySet.class, cmodel);
25 if (pset.getPropertyValue("Label").equals("Exit"))
27 // We can get the XDialog in which this control appears by calling
28 // getContext() on the XControl interface
29 XDialog xDialog = (XDialog)UnoRuntime.queryInterface(
30 XDialog.class, control.getContext());
37 // We can get the list of controls for this dialog by calling
38 // getContext() on the XControl interface of the button
39 XControlContainer controls = (XControlContainer)UnoRuntime.queryInterface(
40 XControlContainer.class, control.getContext());
42 // Now get the text field control from the list
43 XTextComponent textField = (XTextComponent)
44 UnoRuntime.queryInterface(
45 XTextComponent.class, controls.getControl("HighlightTextField"));
47 String searchKey = textField.getText();
49 // highlight the text in red
50 java.awt.Color cRed = new java.awt.Color(255, 0, 0);
51 int red = cRed.getRGB();
53 XReplaceable replaceable = (XReplaceable)
54 UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument());
56 XReplaceDescriptor descriptor =
57 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
59 // Gets a XPropertyReplace object for altering the properties
60 // of the replaced text
61 XPropertyReplace xPropertyReplace = (XPropertyReplace)
62 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
64 // Sets the replaced text property fontweight value to Bold
65 PropertyValue wv = new PropertyValue("CharWeight", -1,
66 new Float(com.sun.star.awt.FontWeight.BOLD),
67 com.sun.star.beans.PropertyState.DIRECT_VALUE);
69 // Sets the replaced text property color value to RGB parameter
70 PropertyValue cv = new PropertyValue("CharColor", -1,
72 com.sun.star.beans.PropertyState.DIRECT_VALUE);
74 // Apply the properties
75 PropertyValue[] props = new PropertyValue[] { cv, wv };
78 xPropertyReplace.setReplaceAttributes(props);
80 // Only matches whole words and case sensitive
81 descriptor.setPropertyValue(
82 "SearchCaseSensitive", new Boolean(true));
83 descriptor.setPropertyValue("SearchWords", new Boolean(true));
85 catch (com.sun.star.beans.UnknownPropertyException upe) {
86 System.err.println("Error setting up search properties");
89 catch (com.sun.star.beans.PropertyVetoException pve) {
90 System.err.println("Error setting up search properties");
93 catch (com.sun.star.lang.WrappedTargetException wte) {
94 System.err.println("Error setting up search properties");
98 // Replaces all instances of searchKey with new Text properties
99 // and gets the number of instances of the searchKey
100 descriptor.setSearchString(searchKey);
101 descriptor.setReplaceString(searchKey);
102 replaceable.replaceAll(descriptor);
105 // BeanShell OpenOffice.org scripts should always return 0