PageRenderTime 81ms CodeModel.GetById 75ms app.highlight 4ms RepoModel.GetById 1ms 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
  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
 24package org.gjt.sp.jedit.gui.statusbar;
 25
 26//{{{ Imports
 27import org.gjt.sp.jedit.*;
 28import org.gjt.sp.jedit.EditBus.EBHandler;
 29import org.gjt.sp.jedit.msg.ViewUpdate;
 30import org.gjt.sp.jedit.textarea.JEditTextArea;
 31import org.gjt.sp.jedit.textarea.TextArea;
 32import org.gjt.sp.jedit.textarea.Selection;
 33
 34import javax.swing.*;
 35import javax.swing.event.CaretListener;
 36import javax.swing.event.CaretEvent;
 37//}}}
 38
 39/**
 40 * @author Matthieu Casanova 
 41 * @since jEdit 4.3pre15
 42 */
 43public class SelectionLengthWidgetFactory implements StatusWidgetFactory
 44{
 45	//{{{ getWidget() method
 46	public Widget getWidget(View view)
 47	{
 48		Widget selectionLengthWidget = new SelectionLengthWidget(view);
 49		return selectionLengthWidget;
 50	} //}}}
 51
 52	//{{{ SelectionLengthWidget class
 53	public static class SelectionLengthWidget implements Widget
 54	{
 55		private final SelectionLength selectionLength;
 56		private final View view;
 57		private TextArea textArea;
 58
 59		SelectionLengthWidget(View view)
 60		{
 61			this.view = view;
 62			textArea = view.getTextArea();
 63			selectionLength = new SelectionLength();
 64			selectionLength.setForeground(jEdit.getColorProperty("view.status.foreground"));
 65			selectionLength.setBackground(jEdit.getColorProperty("view.status.background"));
 66			EditBus.addToBus(this);
 67		}
 68
 69		public JComponent getComponent()
 70		{
 71			return selectionLength;
 72		}
 73
 74		public void update()
 75		{
 76			Selection selection = textArea.getSelectionAtOffset(textArea.getCaretPosition());
 77			if (selection == null)
 78			{
 79				selectionLength.setText("0");
 80			}
 81			else
 82			{
 83				int selectionEnd = selection.getEnd();
 84				int selectionStart = selection.getStart();
 85				int len;
 86				if (selection instanceof Selection.Rect)
 87				{
 88					int startLine = selection.getStartLine();
 89					int endLine = selection.getEndLine();
 90					JEditTextArea textArea = view.getTextArea();
 91					int startLineOffset = textArea.getLineStartOffset(startLine);
 92					int endLineOffset = textArea.getLineStartOffset(endLine);
 93					int lines = endLine - startLine + 1;
 94					int columns = (selectionEnd - endLineOffset) -
 95						(selectionStart - startLineOffset);
 96					len = lines * columns;
 97				}
 98				else
 99					len = selectionEnd - selectionStart;
100				selectionLength.setText(Integer.toString(len));
101			}
102		}
103
104		public void propertiesChanged()
105		{
106		}
107
108		@EBHandler
109		public void handleViewUpdate(ViewUpdate viewUpdate)
110		{
111			if (viewUpdate.getView() == view && viewUpdate.getWhat() == ViewUpdate.EDIT_PANE_CHANGED)
112			{
113				if (textArea != null)
114				{
115					textArea.removeCaretListener(selectionLength);
116				}
117				textArea = view.getTextArea();
118				if (selectionLength.visible)
119					textArea.addCaretListener(selectionLength);
120			}
121		}
122
123		private class SelectionLength extends JLabel implements CaretListener
124		{
125			boolean visible;
126			//{{{ addNotify() method
127			@Override
128			public void addNotify()
129			{
130				super.addNotify();
131				visible = true;
132				textArea.addCaretListener(this);
133			} //}}}
134
135
136			//{{{ removeNotify() method
137			@Override
138			public void removeNotify()
139			{
140				visible = false;
141				textArea.removeCaretListener(this);
142				super.removeNotify();
143			} //}}}
144
145			public void caretUpdate(CaretEvent e)
146			{
147				SelectionLengthWidget.this.update();
148			}
149		}
150	} //}}}
151}