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