PageRenderTime 277ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/statusbar/SelectionLengthWidgetFactory.java

#
Java | 151 lines | 105 code | 14 blank | 32 comment | 10 complexity | 005742d6f325866bcc10ea4e9616c447 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * SelectionLengthWidgetFactory.java - A status bar widget that displays
  3. * the length of the selection at caret
  4. * :tabSize=8:indentSize=8:noTabs=false:
  5. * :folding=explicit:collapseFolds=1:
  6. *
  7. * Copyright (C) 2008 Matthieu Casanova
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.gui.statusbar;
  24. //{{{ Imports
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.jedit.EditBus.EBHandler;
  27. import org.gjt.sp.jedit.msg.ViewUpdate;
  28. import org.gjt.sp.jedit.textarea.JEditTextArea;
  29. import org.gjt.sp.jedit.textarea.TextArea;
  30. import org.gjt.sp.jedit.textarea.Selection;
  31. import javax.swing.*;
  32. import javax.swing.event.CaretListener;
  33. import javax.swing.event.CaretEvent;
  34. //}}}
  35. /**
  36. * @author Matthieu Casanova
  37. * @since jEdit 4.3pre15
  38. */
  39. public class SelectionLengthWidgetFactory implements StatusWidgetFactory
  40. {
  41. //{{{ getWidget() method
  42. public Widget getWidget(View view)
  43. {
  44. Widget selectionLengthWidget = new SelectionLengthWidget(view);
  45. return selectionLengthWidget;
  46. } //}}}
  47. //{{{ SelectionLengthWidget class
  48. public static class SelectionLengthWidget implements Widget
  49. {
  50. private final SelectionLength selectionLength;
  51. private final View view;
  52. private TextArea textArea;
  53. SelectionLengthWidget(View view)
  54. {
  55. this.view = view;
  56. textArea = view.getTextArea();
  57. selectionLength = new SelectionLength();
  58. selectionLength.setForeground(jEdit.getColorProperty("view.status.foreground"));
  59. selectionLength.setBackground(jEdit.getColorProperty("view.status.background"));
  60. EditBus.addToBus(this);
  61. }
  62. public JComponent getComponent()
  63. {
  64. return selectionLength;
  65. }
  66. public void update()
  67. {
  68. Selection selection = textArea.getSelectionAtOffset(textArea.getCaretPosition());
  69. if (selection == null)
  70. {
  71. selectionLength.setText("0");
  72. }
  73. else
  74. {
  75. int selectionEnd = selection.getEnd();
  76. int selectionStart = selection.getStart();
  77. int len;
  78. if (selection instanceof Selection.Rect)
  79. {
  80. int startLine = selection.getStartLine();
  81. int endLine = selection.getEndLine();
  82. JEditTextArea textArea = view.getTextArea();
  83. int startLineOffset = textArea.getLineStartOffset(startLine);
  84. int endLineOffset = textArea.getLineStartOffset(endLine);
  85. int lines = endLine - startLine + 1;
  86. int columns = (selectionEnd - endLineOffset) -
  87. (selectionStart - startLineOffset);
  88. len = lines * columns;
  89. }
  90. else
  91. len = selectionEnd - selectionStart;
  92. selectionLength.setText(Integer.toString(len));
  93. }
  94. }
  95. public void propertiesChanged()
  96. {
  97. }
  98. @EBHandler
  99. public void handleViewUpdate(ViewUpdate viewUpdate)
  100. {
  101. if (viewUpdate.getView() == view && viewUpdate.getWhat() == ViewUpdate.EDIT_PANE_CHANGED)
  102. {
  103. if (textArea != null)
  104. {
  105. textArea.removeCaretListener(selectionLength);
  106. }
  107. textArea = view.getTextArea();
  108. if (selectionLength.visible)
  109. textArea.addCaretListener(selectionLength);
  110. }
  111. }
  112. private class SelectionLength extends JLabel implements CaretListener
  113. {
  114. boolean visible;
  115. //{{{ addNotify() method
  116. @Override
  117. public void addNotify()
  118. {
  119. super.addNotify();
  120. visible = true;
  121. textArea.addCaretListener(this);
  122. } //}}}
  123. //{{{ removeNotify() method
  124. @Override
  125. public void removeNotify()
  126. {
  127. visible = false;
  128. textArea.removeCaretListener(this);
  129. super.removeNotify();
  130. } //}}}
  131. public void caretUpdate(CaretEvent e)
  132. {
  133. SelectionLengthWidget.this.update();
  134. }
  135. }
  136. } //}}}
  137. }