2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
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)
21 import com.sun.star.uno.*;
22 import com.sun.star.awt.*;
23 import com.sun.star.lang.*;
24 import com.sun.star.beans.*;
25 import com.sun.star.util.*;
26 import com.sun.star.script.framework.browse.DialogFactory;
28 // Get the ActionEvent object from the ARGUMENTS list
29 ActionEvent event = (ActionEvent) ARGUMENTS[0];
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
33 XButton button = (XButton)AnyConverter.toObject(
34 new Type(XButton.class), event.Source);
36 // We can now query for the model of the button and get its properties
37 XControl control = (XControl)UnoRuntime.queryInterface(XControl.class, button);
38 XControlModel cmodel = control.getModel();
39 XPropertySet pset = (XPropertySet)UnoRuntime.queryInterface(
40 XPropertySet.class, cmodel);
42 if (pset.getPropertyValue("Label").equals("Exit"))
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());
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());
59 // Now get the text field control from the list
60 XTextComponent textField = (XTextComponent)
61 UnoRuntime.queryInterface(
62 XTextComponent.class, controls.getControl("HighlightTextField"));
64 String searchKey = textField.getText();
66 // highlight the text in red
67 java.awt.Color cRed = new java.awt.Color(255, 0, 0);
68 int red = cRed.getRGB();
70 XReplaceable replaceable = (XReplaceable)
71 UnoRuntime.queryInterface(XReplaceable.class, XSCRIPTCONTEXT.getDocument());
73 XReplaceDescriptor descriptor =
74 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
76 // Gets a XPropertyReplace object for altering the properties
77 // of the replaced text
78 XPropertyReplace xPropertyReplace = (XPropertyReplace)
79 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
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);
86 // Sets the replaced text property color value to RGB parameter
87 PropertyValue cv = new PropertyValue("CharColor", -1,
89 com.sun.star.beans.PropertyState.DIRECT_VALUE);
91 // Apply the properties
92 PropertyValue[] props = new PropertyValue[] { cv, wv };
95 xPropertyReplace.setReplaceAttributes(props);
97 // Only matches whole words and case sensitive
98 descriptor.setPropertyValue(
99 "SearchCaseSensitive", new Boolean(true));
100 descriptor.setPropertyValue("SearchWords", new Boolean(true));
102 catch (com.sun.star.beans.UnknownPropertyException upe) {
103 System.err.println("Error setting up search properties");
106 catch (com.sun.star.beans.PropertyVetoException pve) {
107 System.err.println("Error setting up search properties");
110 catch (com.sun.star.lang.WrappedTargetException wte) {
111 System.err.println("Error setting up search properties");
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);
122 // BeanShell OpenOffice.org scripts should always return 0