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