/plugins/Highlight/tags/V_1_1_0/src/gatchan/highlight/HighlightPlugin.java

# · Java · 198 lines · 126 code · 26 blank · 46 comment · 16 complexity · e00a32b7b3baf2e5c3e1ae167bd7bdea MD5 · raw file

  1. package gatchan.highlight;
  2. import gnu.regexp.REException;
  3. import org.gjt.sp.jedit.*;
  4. import org.gjt.sp.jedit.search.SearchAndReplace;
  5. import org.gjt.sp.jedit.msg.EditPaneUpdate;
  6. import org.gjt.sp.jedit.textarea.JEditTextArea;
  7. import org.gjt.sp.jedit.textarea.TextAreaPainter;
  8. import org.gjt.sp.util.Log;
  9. /**
  10. * The HighlightPlugin. This is my first plugin for jEdit, some parts of my code were inspired by the ErrorList plugin
  11. *
  12. * @author Matthieu Casanova
  13. */
  14. public final class HighlightPlugin extends EBPlugin {
  15. private static HighlightManager highlightManager;
  16. public static final String NAME = "highlight";
  17. public static final String PROPERTY_PREFIX = "plugin.Highlight.";
  18. public static final String MENU = "highlight.menu";
  19. public static final String OPTION_PREFIX = "options.highlight.";
  20. /** Initialize the plugin. When starting this plugin will add an Highlighter on each text area */
  21. public void start() {
  22. highlightManager = HighlightManagerTableModel.getManager();
  23. View view = jEdit.getFirstView();
  24. while (view != null) {
  25. final EditPane[] panes = view.getEditPanes();
  26. for (int i = 0; i < panes.length; i++) {
  27. final JEditTextArea textArea = panes[i].getTextArea();
  28. initTextArea(textArea);
  29. }
  30. view = view.getNext();
  31. }
  32. }
  33. /** uninitialize the plugin. we will remove the Highlighter on each text area */
  34. public void stop() {
  35. highlightManager.dispose();
  36. highlightManager = null;
  37. View view = jEdit.getFirstView();
  38. while (view != null) {
  39. final EditPane[] panes = view.getEditPanes();
  40. for (int i = 0; i < panes.length; i++) {
  41. final JEditTextArea textArea = panes[i].getTextArea();
  42. uninitTextArea(textArea);
  43. }
  44. view = view.getNext();
  45. }
  46. }
  47. /**
  48. * Remove the highlighter from a text area.
  49. *
  50. * @param textArea the textarea from wich we will remove the highlighter
  51. *
  52. * @see #stop()
  53. * @see #handleEditPaneMessage(org.gjt.sp.jedit.msg.EditPaneUpdate)
  54. */
  55. private static void uninitTextArea(JEditTextArea textArea) {
  56. final TextAreaPainter painter = textArea.getPainter();
  57. final Highlighter highlighter = (Highlighter) textArea.getClientProperty(Highlighter.class);
  58. if (highlighter != null) {
  59. painter.removeExtension(highlighter);
  60. textArea.putClientProperty(Highlighter.class, null);
  61. }
  62. }
  63. /**
  64. * Initialize the textarea with a highlight painter.
  65. *
  66. * @param textArea the textarea to initialize
  67. *
  68. * @return the new highlighter for the textArea
  69. */
  70. private static Highlighter initTextArea(JEditTextArea textArea) {
  71. final Highlighter highlighter = new Highlighter(textArea);
  72. final TextAreaPainter painter = textArea.getPainter();
  73. painter.addExtension(TextAreaPainter.BELOW_SELECTION_LAYER, highlighter);
  74. textArea.putClientProperty(Highlighter.class, highlighter);
  75. return highlighter;
  76. }
  77. public void handleMessage(EBMessage message) {
  78. if (message instanceof EditPaneUpdate) {
  79. handleEditPaneMessage((EditPaneUpdate) message);
  80. }
  81. }
  82. private static void handleEditPaneMessage(EditPaneUpdate message) {
  83. final JEditTextArea textArea = message.getEditPane().getTextArea();
  84. final Object what = message.getWhat();
  85. if (what == EditPaneUpdate.CREATED) {
  86. initTextArea(textArea);
  87. } else if (what == EditPaneUpdate.DESTROYED) {
  88. uninitTextArea(textArea);
  89. }
  90. }
  91. /**
  92. * Highlight a word in a textarea. If a text is selected this text will be highlighted, if no text is selected we will
  93. * ask the textarea to select a word
  94. *
  95. * @param textArea the textarea
  96. */
  97. public static void highlightThis(JEditTextArea textArea) {
  98. final String text = getCurrentWord(textArea);
  99. try {
  100. highlightManager.addElement(new Highlight(text));
  101. } catch (REException e) {
  102. Log.log(Log.MESSAGE, HighlightPlugin.class, "This should never happens here " + e.getMessage());
  103. }
  104. }
  105. /**
  106. * Get the current word. If nothing is selected, it will select it.
  107. *
  108. * @param textArea the textArea
  109. *
  110. * @return the current word
  111. */
  112. private static String getCurrentWord(JEditTextArea textArea) {
  113. String text = textArea.getSelectedText();
  114. if (text == null) {
  115. textArea.selectWord();
  116. text = textArea.getSelectedText();
  117. }
  118. return text;
  119. }
  120. /**
  121. * Highlight a word in a textarea. If a text is selected this text will be highlighted, if no text is selected we will
  122. * ask the textarea to select a word. only the entire word will be highlighted
  123. *
  124. * @param textArea the textarea
  125. */
  126. public static void highlightEntireWord(JEditTextArea textArea) {
  127. final String text = getCurrentWord(textArea);
  128. try {
  129. final Highlight highlight = new Highlight("\\<" + text + "\\>", true, false);
  130. highlightManager.addElement(highlight);
  131. } catch (REException e) {
  132. Log.log(Log.MESSAGE, HighlightPlugin.class, "This should never happens here " + e.getMessage());
  133. }
  134. }
  135. public static void highlightCurrentSearch() {
  136. try {
  137. Highlight h = new Highlight();
  138. h.init(SearchAndReplace.getSearchString(), SearchAndReplace.getRegexp(), false, Highlight.getNextColor());
  139. addHighlight(h);
  140. } catch (REException e) {
  141. Log.log(Log.WARNING, HighlightPlugin.class, "This should never happens");
  142. Log.log(Log.WARNING, HighlightPlugin.class, e);
  143. }
  144. }
  145. /**
  146. * Show an highlight dialog.
  147. *
  148. * @param view the current view
  149. */
  150. public static void highlightDialog(View view) {
  151. final HighlightDialog d = new HighlightDialog(view);
  152. d.setVisible(true);
  153. }
  154. public static void addHighlight(Highlight highlight) {
  155. highlightManager.addElement(highlight);
  156. }
  157. public static void removeAllHighlights() {
  158. highlightManager.removeAll();
  159. }
  160. public static void enableHighlights() {
  161. highlightManager.setHighlightEnable(true);
  162. }
  163. public static void disableHighlights() {
  164. highlightManager.setHighlightEnable(false);
  165. }
  166. public static void toggleHighlights() {
  167. highlightManager.setHighlightEnable(!highlightManager.isHighlightEnable());
  168. }
  169. public static boolean isHighlightEnable() {
  170. return highlightManager.isHighlightEnable();
  171. }
  172. }