1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21import com.sun.star.uno.UnoRuntime; 22import com.sun.star.util.XReplaceable; 23import com.sun.star.util.XReplaceDescriptor; 24import com.sun.star.util.XPropertyReplace; 25import com.sun.star.beans.PropertyValue; 26import com.sun.star.text.XTextDocument; 27import com.sun.star.script.provider.XScriptContext; 28 29int replaceText(searchKey, color, bold) { 30 31 result = 0; 32 33 try { 34 // Create an XReplaceable object and an XReplaceDescriptor 35 replaceable = (XReplaceable) 36 UnoRuntime.queryInterface(XReplaceable.class, xTextDocument); 37 38 descriptor = 39 (XReplaceDescriptor) replaceable.createReplaceDescriptor(); 40 41 // Gets a XPropertyReplace object for altering the properties 42 // of the replaced text 43 xPropertyReplace = (XPropertyReplace) 44 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); 45 46 // Sets the replaced text property fontweight value to Bold or Normal 47 wv = null; 48 if (bold) { 49 wv = new PropertyValue("CharWeight", -1, 50 new Float(com.sun.star.awt.FontWeight.BOLD), 51 com.sun.star.beans.PropertyState.DIRECT_VALUE); 52 } 53 else { 54 wv = new PropertyValue("CharWeight", -1, 55 new Float(com.sun.star.awt.FontWeight.NORMAL), 56 com.sun.star.beans.PropertyState.DIRECT_VALUE); 57 } 58 59 // Sets the replaced text property color value to RGB color parameter 60 cv = new PropertyValue("CharColor", -1, new Integer(color), 61 com.sun.star.beans.PropertyState.DIRECT_VALUE); 62 63 // Apply the properties 64 PropertyValue[] props = { cv, wv }; 65 xPropertyReplace.setReplaceAttributes(props); 66 67 // Only matches whole words and case sensitive 68 descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true)); 69 descriptor.setPropertyValue("SearchWords", new Boolean(true)); 70 71 // Replaces all instances of searchKey with new Text properties 72 // and gets the number of instances of the searchKey 73 descriptor.setSearchString(searchKey); 74 descriptor.setReplaceString(searchKey); 75 result = replaceable.replaceAll(descriptor); 76 77 } 78 catch (Exception e) { 79 } 80 81 return result; 82} 83 84searchKey = ""; 85 86// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to 87// all BeanShell scripts executed by the Script Framework 88xTextDocument = (XTextDocument) 89 UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument()); 90 91// Create a JButton and add an ActionListener 92// When clicked the value for the searchKey is read and passed to replaceText 93myListener = new ActionListener() { 94 actionPerformed(ActionEvent e) { 95 searchKey = findTextBox.getText(); 96 97 if(searchKey.equalsIgnoreCase("")) { 98 JOptionPane.showMessageDialog(null, 99 "No text entered for search", 100 "No text", JOptionPane.INFORMATION_MESSAGE); 101 } 102 else { 103 // highlight the text in red 104 cRed = new Color(255, 0, 0); 105 red = cRed.getRGB(); 106 num = replaceText(searchKey, red, true); 107 108 if(num > 0) { 109 int response = JOptionPane.showConfirmDialog(null, 110 searchKey + " was found " + num + 111 " times\nDo you wish to keep the text highlighted?", 112 "Confirm highlight", JOptionPane.YES_NO_OPTION, 113 JOptionPane.QUESTION_MESSAGE); 114 115 if (response == 1) { 116 cBlack = new Color(255, 255, 255); 117 black = cBlack.getRGB(); 118 replaceText(searchKey, black, false); 119 } 120 } 121 else { 122 JOptionPane.showMessageDialog(null, 123 "No matches were found", "Not found", 124 JOptionPane.INFORMATION_MESSAGE); 125 } 126 } 127 } 128}; 129 130 131exitListener = new ActionListener() { 132 actionPerformed(ActionEvent e) { 133 frame.dispose(); 134 } 135}; 136 137 138searchButton = new JButton("Highlight"); 139searchButton.addActionListener(myListener); 140 141exitButton = new JButton("Exit"); 142exitButton.addActionListener(exitListener); 143 144buttonPanel = new JPanel(); 145buttonPanel.setLayout(new FlowLayout()); 146buttonPanel.add(searchButton); 147buttonPanel.add(exitButton); 148 149 150// Create a JPanel containing one JTextField for the search text. 151searchPanel = new JPanel(); 152searchPanel.setLayout(new FlowLayout()); 153findTextBox = new JTextField(20); 154findWhat = new JLabel("Find What: "); 155searchPanel.add(findWhat); 156searchPanel.add(findTextBox); 157 158// Create frame and add a window listener 159frame = new JFrame("Highlight Text"); 160frame.setSize(350,130); 161frame.setLocation(430,430); 162frame.setResizable(false); 163// Add the panel and button to the frame 164frame.getContentPane().setLayout(new GridLayout(2,1,10,10)); 165frame.getContentPane().add(searchPanel); 166frame.getContentPane().add(buttonPanel); 167 168frame.setVisible(true); 169frame.pack(); 170