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