123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import com.sun.star.uno.UnoRuntime;
- import com.sun.star.util.XReplaceable;
- import com.sun.star.util.XReplaceDescriptor;
- import com.sun.star.util.XPropertyReplace;
- import com.sun.star.beans.PropertyValue;
- import com.sun.star.text.XTextDocument;
- import com.sun.star.script.provider.XScriptContext;
- int replaceText(searchKey, color, bold) {
- result = 0;
- try {
-
- replaceable = (XReplaceable)
- UnoRuntime.queryInterface(XReplaceable.class, xTextDocument);
- descriptor =
- (XReplaceDescriptor) replaceable.createReplaceDescriptor();
-
-
- xPropertyReplace = (XPropertyReplace)
- UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
-
- wv = null;
- if (bold) {
- wv = new PropertyValue("CharWeight", -1,
- new Float(com.sun.star.awt.FontWeight.BOLD),
- com.sun.star.beans.PropertyState.DIRECT_VALUE);
- }
- else {
- wv = new PropertyValue("CharWeight", -1,
- new Float(com.sun.star.awt.FontWeight.NORMAL),
- com.sun.star.beans.PropertyState.DIRECT_VALUE);
- }
-
- cv = new PropertyValue("CharColor", -1, new Integer(color),
- com.sun.star.beans.PropertyState.DIRECT_VALUE);
-
- PropertyValue[] props = { cv, wv };
- xPropertyReplace.setReplaceAttributes(props);
-
- descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true));
- descriptor.setPropertyValue("SearchWords", new Boolean(true));
-
-
- descriptor.setSearchString(searchKey);
- descriptor.setReplaceString(searchKey);
- result = replaceable.replaceAll(descriptor);
- }
- catch (Exception e) {
- }
- return result;
- }
- searchKey = "";
- xTextDocument = (XTextDocument)
- UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument());
- myListener = new ActionListener() {
- actionPerformed(ActionEvent e) {
- searchKey = findTextBox.getText();
- if(searchKey.equalsIgnoreCase("")) {
- JOptionPane.showMessageDialog(null,
- "No text entered for search",
- "No text", JOptionPane.INFORMATION_MESSAGE);
- }
- else {
-
- cRed = new Color(255, 0, 0);
- red = cRed.getRGB();
- num = replaceText(searchKey, red, true);
- if(num > 0) {
- int response = JOptionPane.showConfirmDialog(null,
- searchKey + " was found " + num +
- " times\nDo you wish to keep the text highlighted?",
- "Confirm highlight", JOptionPane.YES_NO_OPTION,
- JOptionPane.QUESTION_MESSAGE);
- if (response == 1) {
- cBlack = new Color(255, 255, 255);
- black = cBlack.getRGB();
- replaceText(searchKey, black, false);
- }
- }
- else {
- JOptionPane.showMessageDialog(null,
- "No matches were found", "Not found",
- JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- };
- exitListener = new ActionListener() {
- actionPerformed(ActionEvent e) {
- frame.dispose();
- }
- };
- searchButton = new JButton("Highlight");
- searchButton.addActionListener(myListener);
- exitButton = new JButton("Exit");
- exitButton.addActionListener(exitListener);
- buttonPanel = new JPanel();
- buttonPanel.setLayout(new FlowLayout());
- buttonPanel.add(searchButton);
- buttonPanel.add(exitButton);
- searchPanel = new JPanel();
- searchPanel.setLayout(new FlowLayout());
- findTextBox = new JTextField(20);
- findWhat = new JLabel("Find What: ");
- searchPanel.add(findWhat);
- searchPanel.add(findTextBox);
- frame = new JFrame("Highlight Text");
- frame.setSize(350,130);
- frame.setLocation(430,430);
- frame.setResizable(false);
- frame.getContentPane().setLayout(new GridLayout(2,1,10,10));
- frame.getContentPane().add(searchPanel);
- frame.getContentPane().add(buttonPanel);
- frame.setVisible(true);
- frame.pack();
|