highlighter.bsh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. int replaceText(searchKey, color, bold) {
  26. result = 0;
  27. try {
  28. // Create an XReplaceable object and an XReplaceDescriptor
  29. replaceable = (XReplaceable)
  30. UnoRuntime.queryInterface(XReplaceable.class, xTextDocument);
  31. descriptor =
  32. (XReplaceDescriptor) replaceable.createReplaceDescriptor();
  33. // Gets a XPropertyReplace object for altering the properties
  34. // of the replaced text
  35. xPropertyReplace = (XPropertyReplace)
  36. UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
  37. // Sets the replaced text property fontweight value to Bold or Normal
  38. wv = null;
  39. if (bold) {
  40. wv = new PropertyValue("CharWeight", -1,
  41. new Float(com.sun.star.awt.FontWeight.BOLD),
  42. com.sun.star.beans.PropertyState.DIRECT_VALUE);
  43. }
  44. else {
  45. wv = new PropertyValue("CharWeight", -1,
  46. new Float(com.sun.star.awt.FontWeight.NORMAL),
  47. com.sun.star.beans.PropertyState.DIRECT_VALUE);
  48. }
  49. // Sets the replaced text property color value to RGB color parameter
  50. cv = new PropertyValue("CharColor", -1, new Integer(color),
  51. com.sun.star.beans.PropertyState.DIRECT_VALUE);
  52. // Apply the properties
  53. PropertyValue[] props = { cv, wv };
  54. xPropertyReplace.setReplaceAttributes(props);
  55. // Only matches whole words and case sensitive
  56. descriptor.setPropertyValue("SearchCaseSensitive", new Boolean(true));
  57. descriptor.setPropertyValue("SearchWords", new Boolean(true));
  58. // Replaces all instances of searchKey with new Text properties
  59. // and gets the number of instances of the searchKey
  60. descriptor.setSearchString(searchKey);
  61. descriptor.setReplaceString(searchKey);
  62. result = replaceable.replaceAll(descriptor);
  63. }
  64. catch (Exception e) {
  65. }
  66. return result;
  67. }
  68. searchKey = "";
  69. // The XSCRIPTCONTEXT variable is of type XScriptContext and is available to
  70. // all BeanShell scripts executed by the Script Framework
  71. xTextDocument = (XTextDocument)
  72. UnoRuntime.queryInterface(XTextDocument.class, XSCRIPTCONTEXT.getDocument());
  73. // Create a JButton and add an ActionListener
  74. // When clicked the value for the searchKey is read and passed to replaceText
  75. myListener = new ActionListener() {
  76. actionPerformed(ActionEvent e) {
  77. searchKey = findTextBox.getText();
  78. if(searchKey.equalsIgnoreCase("")) {
  79. JOptionPane.showMessageDialog(null,
  80. "No text entered for search",
  81. "No text", JOptionPane.INFORMATION_MESSAGE);
  82. }
  83. else {
  84. // highlight the text in red
  85. cRed = new Color(255, 0, 0);
  86. red = cRed.getRGB();
  87. num = replaceText(searchKey, red, true);
  88. if(num > 0) {
  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);
  94. if (response == 1) {
  95. cBlack = new Color(255, 255, 255);
  96. black = cBlack.getRGB();
  97. replaceText(searchKey, black, false);
  98. }
  99. }
  100. else {
  101. JOptionPane.showMessageDialog(null,
  102. "No matches were found", "Not found",
  103. JOptionPane.INFORMATION_MESSAGE);
  104. }
  105. }
  106. }
  107. };
  108. exitListener = new ActionListener() {
  109. actionPerformed(ActionEvent e) {
  110. frame.dispose();
  111. }
  112. };
  113. searchButton = new JButton("Highlight");
  114. searchButton.addActionListener(myListener);
  115. exitButton = new JButton("Exit");
  116. exitButton.addActionListener(exitListener);
  117. buttonPanel = new JPanel();
  118. buttonPanel.setLayout(new FlowLayout());
  119. buttonPanel.add(searchButton);
  120. buttonPanel.add(exitButton);
  121. // Create a JPanel containing one JTextField for the search text.
  122. searchPanel = new JPanel();
  123. searchPanel.setLayout(new FlowLayout());
  124. findTextBox = new JTextField(20);
  125. findWhat = new JLabel("Find What: ");
  126. searchPanel.add(findWhat);
  127. searchPanel.add(findTextBox);
  128. // Create frame and add a window listener
  129. frame = new JFrame("Highlight Text");
  130. frame.setSize(350,130);
  131. frame.setLocation(430,430);
  132. frame.setResizable(false);
  133. // Add the panel and button to the frame
  134. frame.getContentPane().setLayout(new GridLayout(2,1,10,10));
  135. frame.getContentPane().add(searchPanel);
  136. frame.getContentPane().add(buttonPanel);
  137. frame.setVisible(true);
  138. frame.pack();