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 import com.sun.star.uno.UnoRuntime;
19 import com.sun.star.util.XReplaceable;
20 import com.sun.star.util.XReplaceDescriptor;
21 import com.sun.star.util.XPropertyReplace;
22 import com.sun.star.beans.PropertyValue;
23 import com.sun.star.text.XTextDocument;
24 import com.sun.star.script.provider.XScriptContext;
26 int replaceText(searchKey, color, bold) {
31 // Create an XReplaceable object and an XReplaceDescriptor
32 replaceable = (XReplaceable)
33 UnoRuntime.queryInterface(XReplaceable.class, xTextDocument);
36 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
38 // Gets a XPropertyReplace object for altering the properties
39 // of the replaced text
40 xPropertyReplace = (XPropertyReplace)
41 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
43 // Sets the replaced text property fontweight value to Bold or Normal
46 wv = new PropertyValue("CharWeight", -1,
47 new Float(com.sun.star.awt.FontWeight.BOLD),
48 com.sun.star.beans.PropertyState.DIRECT_VALUE);
51 wv = new PropertyValue("CharWeight", -1,
52 new Float(com.sun.star.awt.FontWeight.NORMAL),
53 com.sun.star.beans.PropertyState.DIRECT_VALUE);
56 // Sets the replaced text property color value to RGB color parameter
57 cv = new PropertyValue("CharColor", -1, new Integer(color),
58 com.sun.star.beans.PropertyState.DIRECT_VALUE);
60 // Apply the properties
61 PropertyValue[] props = { cv, wv };
62 xPropertyReplace.setReplaceAttributes(props);
64 // Only matches whole words and case sensitive
65 descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true));
66 descriptor.setPropertyValue("SearchWords", new Boolean(true));
68 // Replaces all instances of searchKey with new Text properties
69 // and gets the number of instances of the searchKey
70 descriptor.setSearchString(searchKey);
71 descriptor.setReplaceString(searchKey);
72 result = replaceable.replaceAll(descriptor);
83 // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
84 // all BeanShell scripts executed by the Script Framework
85 xTextDocument = (XTextDocument)
86 UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument());
88 // Create a JButton and add an ActionListener
89 // When clicked the value for the searchKey is read and passed to replaceText
90 myListener = new ActionListener() {
91 actionPerformed(ActionEvent e) {
92 searchKey = findTextBox.getText();
94 if(searchKey.equalsIgnoreCase("")) {
95 JOptionPane.showMessageDialog(null,
96 "No text entered for search",
97 "No text", JOptionPane.INFORMATION_MESSAGE);
100 // highlight the text in red
101 cRed = new Color(255, 0, 0);
103 num = replaceText(searchKey, red, true);
106 int response = JOptionPane.showConfirmDialog(null,
107 searchKey + " was found " + num +
108 " times\nDo you wish to keep the text highlighted?",
109 "Confirm highlight", JOptionPane.YES_NO_OPTION,
110 JOptionPane.QUESTION_MESSAGE);
113 cBlack = new Color(255, 255, 255);
114 black = cBlack.getRGB();
115 replaceText(searchKey, black, false);
119 JOptionPane.showMessageDialog(null,
120 "No matches were found", "Not found",
121 JOptionPane.INFORMATION_MESSAGE);
128 exitListener = new ActionListener() {
129 actionPerformed(ActionEvent e) {
135 searchButton = new JButton("Highlight");
136 searchButton.addActionListener(myListener);
138 exitButton = new JButton("Exit");
139 exitButton.addActionListener(exitListener);
141 buttonPanel = new JPanel();
142 buttonPanel.setLayout(new FlowLayout());
143 buttonPanel.add(searchButton);
144 buttonPanel.add(exitButton);
147 // Create a JPanel containing one JTextField for the search text.
148 searchPanel = new JPanel();
149 searchPanel.setLayout(new FlowLayout());
150 findTextBox = new JTextField(20);
151 findWhat = new JLabel("Find What: ");
152 searchPanel.add(findWhat);
153 searchPanel.add(findTextBox);
155 // Create frame and add a window listener
156 frame = new JFrame("Highlight Text");
157 frame.setSize(350,130);
158 frame.setLocation(430,430);
159 frame.setResizable(false);
160 // Add the panel and button to the frame
161 frame.getContentPane().setLayout(new GridLayout(2,1,10,10));
162 frame.getContentPane().add(searchPanel);
163 frame.getContentPane().add(buttonPanel);
165 frame.setVisible(true);