PageRenderTime 108ms CodeModel.GetById 96ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 137 lines | 93 code | 15 blank | 29 comment | 20 complexity | e3e6b95305eb6015a7fb06f53b9d04f3 MD5 | raw file
  1/*
  2 * EnhancedMenuItem.java - Menu item with user-specified accelerator string
  3 * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package org.gjt.sp.jedit.gui;
 21
 22import javax.swing.*;
 23import java.awt.event.*;
 24import java.awt.*;
 25import org.gjt.sp.jedit.textarea.JEditTextArea;
 26import org.gjt.sp.jedit.*;
 27
 28/**
 29 * jEdit's custom menu item. It adds support for multi-key shortcuts.
 30 */
 31public class EnhancedMenuItem extends JMenuItem
 32{
 33	/**
 34	 * Creates a new menu item. Most plugins should call
 35	 * GUIUtilities.loadMenuItem() instead.
 36	 * @param label The menu item label
 37	 * @param action The edit action
 38	 * @param actionCommand The action command
 39	 */
 40	public EnhancedMenuItem(String label, EditAction action)
 41	{
 42		super(label);
 43
 44		this.action = action;
 45
 46		if(action != null)
 47		{
 48			setEnabled(true);
 49			addActionListener(new EditAction.Wrapper(action));
 50			shortcutProp1 = action.getName() + ".shortcut";
 51			shortcutProp2 = action.getName() + ".shortcut2";
 52		}
 53		else
 54			setEnabled(false);
 55	}
 56
 57	public Dimension getPreferredSize()
 58	{
 59		Dimension d = super.getPreferredSize();
 60
 61		String shortcut = getShortcut();
 62
 63		if(shortcut != null)
 64		{
 65			d.width += (getFontMetrics(acceleratorFont)
 66				.stringWidth(shortcut) + 15);
 67		}
 68		return d;
 69	}
 70
 71	public void paint(Graphics g)
 72	{
 73		super.paint(g);
 74
 75		String shortcut = getShortcut();
 76
 77		if(shortcut != null)
 78		{
 79			g.setFont(acceleratorFont);
 80			g.setColor(getModel().isArmed() ?
 81				acceleratorSelectionForeground :
 82				acceleratorForeground);
 83			FontMetrics fm = g.getFontMetrics();
 84			Insets insets = getInsets();
 85			g.drawString(shortcut,getWidth() - (fm.stringWidth(
 86				shortcut) + insets.right + insets.left + 5),
 87				getFont().getSize() + (insets.top - 1)
 88				/* XXX magic number */);
 89		}
 90	}
 91
 92	// private members
 93	private String shortcutProp1;
 94	private String shortcutProp2;
 95	private EditAction action;
 96	private static Font acceleratorFont;
 97	private static Color acceleratorForeground;
 98	private static Color acceleratorSelectionForeground;
 99
100	private String getShortcut()
101	{
102		if(action == null)
103			return null;
104		else
105		{
106			String shortcut1 = jEdit.getProperty(shortcutProp1);
107			String shortcut2 = jEdit.getProperty(shortcutProp2);
108
109			if(shortcut1 == null || shortcut1.length() == 0)
110			{
111				if(shortcut2 == null || shortcut2.length() == 0)
112					return null;
113				else
114					return shortcut2;
115			}
116			else
117			{
118				if(shortcut2 == null || shortcut2.length() == 0)
119					return shortcut1;
120				else
121					return shortcut1 + " or " + shortcut2;
122			}
123		}
124	}
125
126	static
127	{
128		acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
129		acceleratorFont = new Font("Monospaced",
130			acceleratorFont.getStyle(),
131			acceleratorFont.getSize());
132		acceleratorForeground = UIManager
133			.getColor("MenuItem.acceleratorForeground");
134		acceleratorSelectionForeground = UIManager
135			.getColor("MenuItem.acceleratorSelectionForeground");
136	}
137}