PageRenderTime 84ms CodeModel.GetById 71ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 154 lines | 104 code | 13 blank | 37 comment | 8 complexity | e03a2d650d5a3d5a19fb217b37ae5c14 MD5 | raw file
  1/*
  2 * BufferSetWidgetFactory.java - The bufferSet widget service
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2008, 2009 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 21 */
 22
 23package org.gjt.sp.jedit.gui.statusbar;
 24
 25//{{{ Imports
 26import java.awt.event.MouseAdapter;
 27import java.awt.event.MouseEvent;
 28import java.awt.*;
 29import javax.swing.JComponent;
 30import javax.swing.JLabel;
 31
 32import org.gjt.sp.jedit.*;
 33import org.gjt.sp.jedit.bufferset.BufferSetManager;
 34import org.gjt.sp.jedit.msg.EditPaneUpdate;
 35import org.gjt.sp.jedit.bufferset.BufferSet;
 36import org.gjt.sp.jedit.msg.PropertiesChanged;
 37//}}}
 38
 39/**
 40 * A Statusbar widget that show the bufferSet's scope of the current edit pane.
 41 *
 42 * @author Matthieu Casanova
 43 * @since jEdit 4.3pre15
 44 */
 45public class BufferSetWidgetFactory implements StatusWidgetFactory
 46{
 47	//{{{ getWidget() method
 48	public Widget getWidget(View view)
 49	{
 50		Widget bufferSetWidget = new BufferSetWidget();
 51		return bufferSetWidget;
 52	} //}}}
 53
 54	//{{{ BufferSetWidget class
 55	private static class BufferSetWidget implements Widget, EBComponent
 56	{
 57		private final JLabel bufferSetLabel;
 58		private BufferSet.Scope currentScope;
 59
 60		BufferSetWidget()
 61		{
 62			bufferSetLabel = new ToolTipLabel()
 63			{
 64				@Override
 65				public void addNotify()
 66				{
 67					super.addNotify();
 68					BufferSetWidget.this.update();
 69					EditBus.addToBus(BufferSetWidget.this);
 70				}
 71
 72				@Override
 73				public void removeNotify()
 74				{
 75					super.removeNotify();
 76					EditBus.removeFromBus(BufferSetWidget.this);
 77				}
 78			};
 79			update();
 80			bufferSetLabel.addMouseListener(new MouseAdapter()
 81			{
 82				@Override
 83				public void mouseClicked(MouseEvent evt)
 84				{
 85					if (evt.getClickCount() == 2)
 86					{
 87						BufferSetManager bufferSetManager = jEdit.getBufferSetManager();
 88						BufferSet.Scope scope = bufferSetManager.getScope();
 89						switch (scope)
 90						{
 91							case global:
 92								scope = BufferSet.Scope.view;
 93								break;
 94							case view:
 95								scope = BufferSet.Scope.editpane;
 96								break;
 97							case editpane:
 98								scope = BufferSet.Scope.global;
 99								break;
100						}
101						bufferSetManager.setScope(scope);
102					}
103				}
104			});
105		}
106
107		//{{{ getComponent() method
108		public JComponent getComponent()
109		{
110			return bufferSetLabel;
111		} //}}}
112
113		//{{{ update() method
114		public void update()
115		{
116			BufferSet.Scope scope = jEdit.getBufferSetManager().getScope();
117			if (currentScope == null || !currentScope.equals(scope))
118			{
119				bufferSetLabel.setText(scope.toString().substring(0,1).toUpperCase());
120				bufferSetLabel.setToolTipText(jEdit.getProperty("view.status.bufferset-tooltip", new Object[] {scope}));
121				currentScope = scope;
122			}
123		} //}}}
124
125		//{{{ propertiesChanged() method
126		public void propertiesChanged()
127		{
128			// retarded GTK look and feel!
129			Font font = new JLabel().getFont();
130			//UIManager.getFont("Label.font");
131			FontMetrics fm = bufferSetLabel.getFontMetrics(font);
132			Dimension dim = new Dimension(Math.max(fm.charWidth('E'),Math.max(fm.charWidth('V'),
133								fm.charWidth('G'))),
134								fm.getHeight());
135			bufferSetLabel.setPreferredSize(dim);
136			bufferSetLabel.setMaximumSize(dim);
137		} //}}}
138
139		//{{{ handleMessage() method
140		public void handleMessage(EBMessage message)
141		{
142			if (message instanceof PropertiesChanged)
143			{
144				PropertiesChanged propertiesChanged = (PropertiesChanged) message;
145				if (propertiesChanged.getSource() instanceof BufferSetManager)
146				{
147					update();
148				}
149			}
150		} //}}}
151
152	} //}}}
153
154}