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