1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21// this code is bound to the events generated by the buttons in the dialog
22// it will close the dialog or find and highlight the text entered in the
23// dialog (depending on the button pressed)
24import com.sun.star.uno.*;
25import com.sun.star.awt.*;
26import com.sun.star.lang.*;
27import com.sun.star.beans.*;
28import com.sun.star.util.*;
29import com.sun.star.script.framework.browse.DialogFactory;
30
31// Get the ActionEvent object from the ARGUMENTS list
32ActionEvent event = (ActionEvent) ARGUMENTS[0];
33
34// Each argument is of type Any so we must use the AnyConverter class to
35// convert it into the interface or primitive type we expect
36XButton button = (XButton)AnyConverter.toObject(
37    new Type(XButton.class), event.Source);
38
39// We can now query for the model of the button and get its properties
40XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
41XControlModel cmodel = control.getModel();
42XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
43    XPropertySet.class, cmodel);
44
45if (pset.getPropertyValue("Label").equals("Exit"))
46{
47    // We can get the XDialog in which this control appears by calling
48    // getContext() on the XControl interface
49    XDialog xDialog = (XDialog)UnoRuntime.queryInterface(
50        XDialog.class, control.getContext());
51
52    // Close the dialog
53    xDialog.endExecute();
54}
55else
56{
57    // We can get the list of controls for this dialog by calling
58    // getContext() on the XControl interface of the button
59    XControlContainer controls = (XControlContainer)UnoRuntime.queryInterface(
60        XControlContainer.class, control.getContext());
61
62    // Now get the text field control from the list
63    XTextComponent textField = (XTextComponent)
64        UnoRuntime.queryInterface(
65            XTextComponent.class, controls.getControl("HighlightTextField"));
66
67    String searchKey = textField.getText();
68
69    // highlight the text in red
70    java.awt.Color cRed = new java.awt.Color(255, 0, 0);
71    int red = cRed.getRGB();
72
73    XReplaceable replaceable = (XReplaceable)
74        UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument());
75
76    XReplaceDescriptor descriptor =
77        (XReplaceDescriptor) replaceable.createReplaceDescriptor();
78
79    // Gets a XPropertyReplace object for altering the properties
80    // of the replaced text
81    XPropertyReplace xPropertyReplace = (XPropertyReplace)
82        UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
83
84    // Sets the replaced text property fontweight value to Bold
85    PropertyValue wv = new PropertyValue("CharWeight", -1,
86        new Float(com.sun.star.awt.FontWeight.BOLD),
87            com.sun.star.beans.PropertyState.DIRECT_VALUE);
88
89    // Sets the replaced text property color value to RGB parameter
90    PropertyValue cv = new PropertyValue("CharColor", -1,
91        new Integer(red),
92            com.sun.star.beans.PropertyState.DIRECT_VALUE);
93
94    // Apply the properties
95    PropertyValue[] props = new PropertyValue[] { cv, wv };
96
97    try {
98        xPropertyReplace.setReplaceAttributes(props);
99
100        // Only matches whole words and case sensitive
101        descriptor.setPropertyValue(
102            "SearchCaseSensitive", new Boolean(true));
103        descriptor.setPropertyValue("SearchWords", new Boolean(true));
104    }
105    catch (com.sun.star.beans.UnknownPropertyException upe) {
106        System.err.println("Error setting up search properties");
107        return;
108    }
109    catch (com.sun.star.beans.PropertyVetoException pve) {
110        System.err.println("Error setting up search properties");
111        return;
112    }
113    catch (com.sun.star.lang.WrappedTargetException wte) {
114        System.err.println("Error setting up search properties");
115        return;
116    }
117
118    // Replaces all instances of searchKey with new Text properties
119    // and gets the number of instances of the searchKey
120    descriptor.setSearchString(searchKey);
121    descriptor.setReplaceString(searchKey);
122    replaceable.replaceAll(descriptor);
123}
124
125// BeanShell OpenOffice.org scripts should always return 0
126return 0;
127