PageRenderTime 123ms CodeModel.GetById 115ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/menu/EnhancedMenuItem.java

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