PageRenderTime 73ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 187 lines | 122 code | 22 blank | 43 comment | 23 complexity | 5e2bdaf986b9df7d1fba4abdbdbbef54 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, 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.*;
 31//}}}
 32
 33/**
 34 * jEdit's custom menu item. It adds support for multi-key shortcuts.
 35 */
 36public class EnhancedMenuItem extends JMenuItem
 37{
 38	//{{{ EnhancedMenuItem constructor
 39	/**
 40	 * Creates a new menu item. Most plugins should call
 41	 * GUIUtilities.loadMenuItem() instead.
 42	 * @param label The menu item label
 43	 * @param action The edit action
 44	 * @param actionCommand The action command
 45	 */
 46	public EnhancedMenuItem(String label, EditAction action)
 47	{
 48		super(label);
 49
 50		this.action = action;
 51
 52		if(action != null)
 53		{
 54			setEnabled(true);
 55			addActionListener(new EditAction.Wrapper(action));
 56			shortcutProp1 = action.getName() + ".shortcut";
 57			shortcutProp2 = action.getName() + ".shortcut2";
 58
 59			addMouseListener(new MouseHandler());
 60		}
 61		else
 62			setEnabled(false);
 63	} //}}}
 64
 65	//{{{ getPreferredSize() method
 66	public Dimension getPreferredSize()
 67	{
 68		Dimension d = super.getPreferredSize();
 69
 70		String shortcut = getShortcut();
 71
 72		if(shortcut != null)
 73		{
 74			d.width += (getFontMetrics(acceleratorFont)
 75				.stringWidth(shortcut) + 15);
 76		}
 77		return d;
 78	} //}}}
 79
 80	//{{{ paint() method
 81	public void paint(Graphics g)
 82	{
 83		super.paint(g);
 84
 85		String shortcut = getShortcut();
 86
 87		if(shortcut != null)
 88		{
 89			g.setFont(acceleratorFont);
 90			g.setColor(getModel().isArmed() ?
 91				acceleratorSelectionForeground :
 92				acceleratorForeground);
 93			FontMetrics fm = g.getFontMetrics();
 94			Insets insets = getInsets();
 95			g.drawString(shortcut,getWidth() - (fm.stringWidth(
 96				shortcut) + insets.right + insets.left + 5),
 97				getFont().getSize() + (insets.top - 
 98				(OperatingSystem.isMacOSLF() ? 0 : 1))
 99				/* XXX magic number */);
100		}
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		String shortcutFont;
145		if (OperatingSystem.isMacOSLF())
146			shortcutFont = "Lucida Grande";
147		else
148			shortcutFont = "Monospaced";
149		
150		acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
151		acceleratorFont = new Font(shortcutFont,
152			acceleratorFont.getStyle(),
153			acceleratorFont.getSize());
154		acceleratorForeground = UIManager
155			.getColor("MenuItem.acceleratorForeground");
156		acceleratorSelectionForeground = UIManager
157			.getColor("MenuItem.acceleratorSelectionForeground");
158	} //}}}
159
160	//}}}
161
162	//{{{ MouseHandler class
163	class MouseHandler extends MouseAdapter
164	{
165		public void mouseReleased(MouseEvent evt)
166		{
167			GUIUtilities.getView((Component)evt.getSource())
168				.getStatus().setMessage(null);
169		}
170
171		public void mouseEntered(MouseEvent evt)
172		{
173			String msg = action.getMouseOverText();
174			if(msg != null)
175			{
176				GUIUtilities.getView((Component)evt.getSource())
177					.getStatus().setMessage(msg);
178			}
179		}
180
181		public void mouseExited(MouseEvent evt)
182		{
183			GUIUtilities.getView((Component)evt.getSource())
184				.getStatus().setMessage(null);
185		}
186	} //}}}
187}