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