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