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 */ 18import com.sun.star.uno.UnoRuntime; 19import com.sun.star.util.XReplaceable; 20import com.sun.star.util.XReplaceDescriptor; 21import com.sun.star.util.XPropertyReplace; 22import com.sun.star.beans.PropertyValue; 23import com.sun.star.text.XTextDocument; 24import com.sun.star.script.provider.XScriptContext; 25 26int replaceText(searchKey, color, bold) { 27 28 result = 0; 29 30 try { 31 // Create an XReplaceable object and an XReplaceDescriptor 32 replaceable = (XReplaceable) 33 UnoRuntime.queryInterface(XReplaceable.class, xTextDocument); 34 35 descriptor = 36 (XReplaceDescriptor) replaceable.createReplaceDescriptor(); 37 38 // Gets a XPropertyReplace object for altering the properties 39 // of the replaced text 40 xPropertyReplace = (XPropertyReplace) 41 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor); 42 43 // Sets the replaced text property fontweight value to Bold or Normal 44 wv = null; 45 if (bold) { 46 wv = new PropertyValue("CharWeight", -1, 47 new Float(com.sun.star.awt.FontWeight.BOLD), 48 com.sun.star.beans.PropertyState.DIRECT_VALUE); 49 } 50 else { 51 wv = new PropertyValue("CharWeight", -1, 52 new Float(com.sun.star.awt.FontWeight.NORMAL), 53 com.sun.star.beans.PropertyState.DIRECT_VALUE); 54 } 55 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); 59 60 // Apply the properties 61 PropertyValue[] props = { cv, wv }; 62 xPropertyReplace.setReplaceAttributes(props); 63 64 // Only matches whole words and case sensitive 65 descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true)); 66 descriptor.setPropertyValue("SearchWords", new Boolean(true)); 67 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); 73 74 } 75 catch (Exception e) { 76 } 77 78 return result; 79} 80 81searchKey = ""; 82 83// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to 84// all BeanShell scripts executed by the Script Framework 85xTextDocument = (XTextDocument) 86 UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument()); 87 88// Create a JButton and add an ActionListener 89// When clicked the value for the searchKey is read and passed to replaceText 90myListener = new ActionListener() { 91 actionPerformed(ActionEvent e) { 92 searchKey = findTextBox.getText(); 93 94 if(searchKey.equalsIgnoreCase("")) { 95 JOptionPane.showMessageDialog(null, 96 "No text entered for search", 97 "No text", JOptionPane.INFORMATION_MESSAGE); 98 } 99 else { 100 // highlight the text in red 101 cRed = new Color(255, 0, 0); 102 red = cRed.getRGB(); 103 num = replaceText(searchKey, red, true); 104 105 if(num > 0) { 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); 111 112 if (response == 1) { 113 cBlack = new Color(255, 255, 255); 114 black = cBlack.getRGB(); 115 replaceText(searchKey, black, false); 116 } 117 } 118 else { 119 JOptionPane.showMessageDialog(null, 120 "No matches were found", "Not found", 121 JOptionPane.INFORMATION_MESSAGE); 122 } 123 } 124 } 125}; 126 127 128exitListener = new ActionListener() { 129 actionPerformed(ActionEvent e) { 130 frame.dispose(); 131 } 132}; 133 134 135searchButton = new JButton("Highlight"); 136searchButton.addActionListener(myListener); 137 138exitButton = new JButton("Exit"); 139exitButton.addActionListener(exitListener); 140 141buttonPanel = new JPanel(); 142buttonPanel.setLayout(new FlowLayout()); 143buttonPanel.add(searchButton); 144buttonPanel.add(exitButton); 145 146 147// Create a JPanel containing one JTextField for the search text. 148searchPanel = new JPanel(); 149searchPanel.setLayout(new FlowLayout()); 150findTextBox = new JTextField(20); 151findWhat = new JLabel("Find What: "); 152searchPanel.add(findWhat); 153searchPanel.add(findTextBox); 154 155// Create frame and add a window listener 156frame = new JFrame("Highlight Text"); 157frame.setSize(350,130); 158frame.setLocation(430,430); 159frame.setResizable(false); 160// Add the panel and button to the frame 161frame.getContentPane().setLayout(new GridLayout(2,1,10,10)); 162frame.getContentPane().add(searchPanel); 163frame.getContentPane().add(buttonPanel); 164 165frame.setVisible(true); 166frame.pack(); 167