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)
4import com.sun.star.uno.*;
5import com.sun.star.awt.*;
6import com.sun.star.lang.*;
7import com.sun.star.beans.*;
8import com.sun.star.util.*;
9import com.sun.star.script.framework.browse.DialogFactory;
10
11// Get the ActionEvent object from the ARGUMENTS list
12ActionEvent event = (ActionEvent) ARGUMENTS[0];
13
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
16XButton button = (XButton)AnyConverter.toObject(
17    new Type(XButton.class), event.Source);
18
19// We can now query for the model of the button and get its properties
20XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
21XControlModel cmodel = control.getModel();
22XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
23    XPropertySet.class, cmodel);
24
25if (pset.getPropertyValue("Label").equals("Exit"))
26{
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());
31
32    // Close the dialog
33    xDialog.endExecute();
34}
35else
36{
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());
41
42    // Now get the text field control from the list
43    XTextComponent textField = (XTextComponent)
44        UnoRuntime.queryInterface(
45            XTextComponent.class, controls.getControl("HighlightTextField"));
46
47    String searchKey = textField.getText();
48
49    // highlight the text in red
50    java.awt.Color cRed = new java.awt.Color(255, 0, 0);
51    int red = cRed.getRGB();
52
53    XReplaceable replaceable = (XReplaceable)
54        UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument());
55
56    XReplaceDescriptor descriptor =
57        (XReplaceDescriptor) replaceable.createReplaceDescriptor();
58
59    // Gets a XPropertyReplace object for altering the properties
60    // of the replaced text
61    XPropertyReplace xPropertyReplace = (XPropertyReplace)
62        UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
63
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);
68
69    // Sets the replaced text property color value to RGB parameter
70    PropertyValue cv = new PropertyValue("CharColor", -1,
71        new Integer(red),
72            com.sun.star.beans.PropertyState.DIRECT_VALUE);
73
74    // Apply the properties
75    PropertyValue[] props = new PropertyValue[] { cv, wv };
76
77    try {
78        xPropertyReplace.setReplaceAttributes(props);
79
80        // Only matches whole words and case sensitive
81        descriptor.setPropertyValue(
82            "SearchCaseSensitive", new Boolean(true));
83        descriptor.setPropertyValue("SearchWords", new Boolean(true));
84    }
85    catch (com.sun.star.beans.UnknownPropertyException upe) {
86        System.err.println("Error setting up search properties");
87        return;
88    }
89    catch (com.sun.star.beans.PropertyVetoException pve) {
90        System.err.println("Error setting up search properties");
91        return;
92    }
93    catch (com.sun.star.lang.WrappedTargetException wte) {
94        System.err.println("Error setting up search properties");
95        return;
96    }
97
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);
103}
104
105// BeanShell OpenOffice.org scripts should always return 0
106return 0;
107