1 import com.sun.star.uno.UnoRuntime;
2 import com.sun.star.util.XReplaceable;
3 import com.sun.star.util.XReplaceDescriptor;
4 import com.sun.star.util.XPropertyReplace;
5 import com.sun.star.beans.PropertyValue;
6 import com.sun.star.text.XTextDocument;
7 import com.sun.star.script.provider.XScriptContext;
9 int replaceText(searchKey, color, bold) {
14 // Create an XReplaceable object and an XReplaceDescriptor
15 replaceable = (XReplaceable)
16 UnoRuntime.queryInterface(XReplaceable.class, xTextDocument);
19 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
21 // Gets a XPropertyReplace object for altering the properties
22 // of the replaced text
23 xPropertyReplace = (XPropertyReplace)
24 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
26 // Sets the replaced text property fontweight value to Bold or Normal
29 wv = new PropertyValue("CharWeight", -1,
30 new Float(com.sun.star.awt.FontWeight.BOLD),
31 com.sun.star.beans.PropertyState.DIRECT_VALUE);
34 wv = new PropertyValue("CharWeight", -1,
35 new Float(com.sun.star.awt.FontWeight.NORMAL),
36 com.sun.star.beans.PropertyState.DIRECT_VALUE);
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);
43 // Apply the properties
44 PropertyValue[] props = { cv, wv };
45 xPropertyReplace.setReplaceAttributes(props);
47 // Only matches whole words and case sensitive
48 descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true));
49 descriptor.setPropertyValue("SearchWords", new Boolean(true));
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);
66 // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
67 // all BeanShell scripts executed by the Script Framework
68 xTextDocument = (XTextDocument)
69 UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument());
71 // Create a JButton and add an ActionListener
72 // When clicked the value for the searchKey is read and passed to replaceText
73 myListener = new ActionListener() {
74 actionPerformed(ActionEvent e) {
75 searchKey = findTextBox.getText();
77 if(searchKey.equalsIgnoreCase("")) {
78 JOptionPane.showMessageDialog(null,
79 "No text entered for search",
80 "No text", JOptionPane.INFORMATION_MESSAGE);
83 // highlight the text in red
84 cRed = new Color(255, 0, 0);
86 num = replaceText(searchKey, red, true);
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);
96 cBlack = new Color(255, 255, 255);
97 black = cBlack.getRGB();
98 replaceText(searchKey, black, false);
102 JOptionPane.showMessageDialog(null,
103 "No matches were found", "Not found",
104 JOptionPane.INFORMATION_MESSAGE);
111 exitListener = new ActionListener() {
112 actionPerformed(ActionEvent e) {
118 searchButton = new JButton("Highlight");
119 searchButton.addActionListener(myListener);
121 exitButton = new JButton("Exit");
122 exitButton.addActionListener(exitListener);
124 buttonPanel = new JPanel();
125 buttonPanel.setLayout(new FlowLayout());
126 buttonPanel.add(searchButton);
127 buttonPanel.add(exitButton);
130 // Create a JPanel containing one JTextField for the search text.
131 searchPanel = new JPanel();
132 searchPanel.setLayout(new FlowLayout());
133 findTextBox = new JTextField(20);
134 findWhat = new JLabel("Find What: ");
135 searchPanel.add(findWhat);
136 searchPanel.add(findTextBox);
138 // Create frame and add a window listener
139 frame = new JFrame("Highlight Text");
140 frame.setSize(350,130);
141 frame.setLocation(430,430);
142 frame.setResizable(false);
143 // Add the panel and button to the frame
144 frame.getContentPane().setLayout(new GridLayout(2,1,10,10));
145 frame.getContentPane().add(searchPanel);
146 frame.getContentPane().add(buttonPanel);
148 frame.setVisible(true);