| 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 |
*/ |
| 18 |
import com.sun.star.uno.UnoRuntime; |
| 19 |
import com.sun.star.util.XReplaceable; |
| 20 |
import com.sun.star.util.XReplaceDescriptor; |
| 21 |
import com.sun.star.util.XPropertyReplace; |
| 22 |
import com.sun.star.beans.PropertyValue; |
| 23 |
import com.sun.star.text.XTextDocument; |
| 24 |
import com.sun.star.script.provider.XScriptContext; |
| 25 |
|
| 26 |
int 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 |
Float.valueOf(com.sun.star.awt.FontWeight.BOLD), |
| 48 |
com.sun.star.beans.PropertyState.DIRECT_VALUE); |
| 49 |
} |
| 50 |
else { |
| 51 |
wv = new PropertyValue("CharWeight", -1, |
| 52 |
Float.valueOf(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 |
|
| 81 |
searchKey = ""; |
| 82 |
|
| 83 |
// The XSCRIPTCONTEXT variable is of type XScriptContext and is available to |
| 84 |
// all BeanShell scripts executed by the Script Framework |
| 85 |
xTextDocument = (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 |
| 90 |
myListener = 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 |
|
| 128 |
exitListener = new ActionListener() { |
| 129 |
actionPerformed(ActionEvent e) { |
| 130 |
frame.dispose(); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
searchButton = new JButton("Highlight"); |
| 136 |
searchButton.addActionListener(myListener); |
| 137 |
|
| 138 |
exitButton = new JButton("Exit"); |
| 139 |
exitButton.addActionListener(exitListener); |
| 140 |
|
| 141 |
buttonPanel = new JPanel(); |
| 142 |
buttonPanel.setLayout(new FlowLayout()); |
| 143 |
buttonPanel.add(searchButton); |
| 144 |
buttonPanel.add(exitButton); |
| 145 |
|
| 146 |
|
| 147 |
// Create a JPanel containing one JTextField for the search text. |
| 148 |
searchPanel = new JPanel(); |
| 149 |
searchPanel.setLayout(new FlowLayout()); |
| 150 |
findTextBox = new JTextField(20); |
| 151 |
findWhat = new JLabel("Find What: "); |
| 152 |
searchPanel.add(findWhat); |
| 153 |
searchPanel.add(findTextBox); |
| 154 |
|
| 155 |
// Create frame and add a window listener |
| 156 |
frame = new JFrame("Highlight Text"); |
| 157 |
frame.setSize(350,130); |
| 158 |
frame.setLocation(430,430); |
| 159 |
frame.setResizable(false); |
| 160 |
// Add the panel and button to the frame |
| 161 |
frame.getContentPane().setLayout(new GridLayout(2,1,10,10)); |
| 162 |
frame.getContentPane().add(searchPanel); |
| 163 |
frame.getContentPane().add(buttonPanel); |
| 164 |
|
| 165 |
frame.setVisible(true); |
| 166 |
frame.pack(); |