PageRenderTime 63ms CodeModel.GetById 49ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/EnhancedCheckBoxMenuItem.java

#
Java | 204 lines | 141 code | 25 blank | 38 comment | 23 complexity | 6fcaae47c4805f9f80a712fb4ed979dc MD5 | raw file
  1/*
  2 * EnhancedCheckBoxMenuItem.java - Check box menu item
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
  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;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.awt.event.*;
 28import java.awt.*;
 29import org.gjt.sp.jedit.textarea.JEditTextArea;
 30import org.gjt.sp.jedit.*;
 31import org.gjt.sp.util.Log;
 32//}}}
 33
 34/**
 35 * jEdit's custom menu item. It adds support for multi-key shortcuts.
 36 */
 37public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
 38{
 39	//{{{ EnhancedCheckBoxMenuItem constructor
 40	public EnhancedCheckBoxMenuItem(String label, EditAction action)
 41	{
 42		super(label);
 43		this.action = action;
 44
 45		if(action != null)
 46		{
 47			setEnabled(true);
 48			addActionListener(new EditAction.Wrapper(action));
 49			shortcutProp1 = action.getName() + ".shortcut";
 50			shortcutProp2 = action.getName() + ".shortcut2";
 51
 52			addMouseListener(new MouseHandler());
 53		}
 54		else
 55			setEnabled(false);
 56
 57		setModel(new Model());
 58	} //}}}
 59
 60	//{{{ getPreferredSize() method
 61	public Dimension getPreferredSize()
 62	{
 63		Dimension d = super.getPreferredSize();
 64
 65		String shortcut = getShortcut();
 66
 67		if(shortcut != null)
 68		{
 69			d.width += (getFontMetrics(acceleratorFont)
 70				.stringWidth(shortcut) + 15);
 71		}
 72		return d;
 73	} //}}}
 74
 75	//{{{ paint() method
 76	public void paint(Graphics g)
 77	{
 78		super.paint(g);
 79
 80		String shortcut = getShortcut();
 81
 82		if(shortcut != null)
 83		{
 84			g.setFont(acceleratorFont);
 85			g.setColor(getModel().isArmed() ?
 86				acceleratorSelectionForeground :
 87				acceleratorForeground);
 88			FontMetrics fm = g.getFontMetrics();
 89			Insets insets = getInsets();
 90			g.drawString(shortcut,getWidth() - (fm.stringWidth(
 91				shortcut) + insets.right + insets.left + 5),
 92				getFont().getSize() + (insets.top - 1)
 93				/* XXX magic number */);
 94		}
 95	} //}}}
 96
 97	//{{{ getActionCommand() method
 98	public String getActionCommand()
 99	{
100		return getModel().getActionCommand();
101	} //}}}
102
103	//{{{ Private members
104
105	//{{{ Instance variables
106	private String shortcutProp1;
107	private String shortcutProp2;
108	private EditAction action;
109	private static Font acceleratorFont;
110	private static Color acceleratorForeground;
111	private static Color acceleratorSelectionForeground;
112	//}}}
113
114	//{{{ getShortcut() method
115	private String getShortcut()
116	{
117		if(action == null)
118			return null;
119		else
120		{
121			String shortcut1 = jEdit.getProperty(shortcutProp1);
122			String shortcut2 = jEdit.getProperty(shortcutProp2);
123
124			if(shortcut1 == null || shortcut1.length() == 0)
125			{
126				if(shortcut2 == null || shortcut2.length() == 0)
127					return null;
128				else
129					return shortcut2;
130			}
131			else
132			{
133				if(shortcut2 == null || shortcut2.length() == 0)
134					return shortcut1;
135				else
136					return shortcut1 + " or " + shortcut2;
137			}
138		}
139	} //}}}
140
141	//{{{ Class initializer
142	static
143	{
144		acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
145		acceleratorFont = new Font("Monospaced",
146			acceleratorFont.getStyle(),
147			acceleratorFont.getSize());
148		acceleratorForeground = UIManager
149			.getColor("MenuItem.acceleratorForeground");
150		acceleratorSelectionForeground = UIManager
151			.getColor("MenuItem.acceleratorSelectionForeground");
152	} //}}}
153
154	//}}}
155
156	//{{{ Model class
157	class Model extends DefaultButtonModel
158	{
159		public boolean isSelected()
160		{
161			if(!isShowing())
162				return false;
163
164			try
165			{
166				return action.isSelected(GUIUtilities.getView(
167					EnhancedCheckBoxMenuItem.this));
168			}
169			catch(Throwable t)
170			{
171				Log.log(Log.ERROR,this,t);
172				return false;
173			}
174		}
175
176		public void setSelected(boolean b) {}
177	} //}}}
178
179	//{{{ MouseHandler class
180	class MouseHandler extends MouseAdapter
181	{
182		public void mouseReleased(MouseEvent evt)
183		{
184			GUIUtilities.getView((Component)evt.getSource())
185				.getStatus().setMessage(null);
186		}
187
188		public void mouseEntered(MouseEvent evt)
189		{
190			String msg = action.getMouseOverText();
191			if(msg != null)
192			{
193				GUIUtilities.getView((Component)evt.getSource())
194					.getStatus().setMessage(msg);
195			}
196		}
197
198		public void mouseExited(MouseEvent evt)
199		{
200			GUIUtilities.getView((Component)evt.getSource())
201				.getStatus().setMessage(null);
202		}
203	} //}}}
204}