PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/EnhancedCheckBoxMenuItem.java

#
Java | 159 lines | 118 code | 19 blank | 22 comment | 21 complexity | 544059dde9eb0e0d226daefb77223372 MD5 | raw file
  1/*
  2 * EnhancedCheckBoxMenuItem.java - Check box menu item
  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.*;
 27import org.gjt.sp.util.Log;
 28
 29/**
 30 * jEdit's custom menu item. It adds support for multi-key shortcuts.
 31 */
 32public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
 33{
 34	public EnhancedCheckBoxMenuItem(String label, EditAction action)
 35	{
 36		super(label);
 37		this.action = action;
 38
 39		if(action != null)
 40		{
 41			setEnabled(true);
 42			addActionListener(new EditAction.Wrapper(action));
 43			shortcutProp1 = action.getName() + ".shortcut";
 44			shortcutProp2 = action.getName() + ".shortcut2";
 45		}
 46		else
 47			setEnabled(false);
 48
 49		setModel(new Model());
 50	}
 51
 52	public Dimension getPreferredSize()
 53	{
 54		Dimension d = super.getPreferredSize();
 55
 56		String shortcut = getShortcut();
 57
 58		if(shortcut != null)
 59		{
 60			d.width += (getFontMetrics(acceleratorFont)
 61				.stringWidth(shortcut) + 10);
 62		}
 63		return d;
 64	}
 65
 66	public void paint(Graphics g)
 67	{
 68		super.paint(g);
 69
 70		String shortcut = getShortcut();
 71
 72		if(shortcut != null)
 73		{
 74			g.setFont(acceleratorFont);
 75			g.setColor(getModel().isArmed() ?
 76				acceleratorSelectionForeground :
 77				acceleratorForeground);
 78			FontMetrics fm = g.getFontMetrics();
 79			Insets insets = getInsets();
 80			g.drawString(shortcut,getWidth() - (fm.stringWidth(
 81				shortcut) + insets.right + insets.left),
 82				getFont().getSize() + (insets.top - 1)
 83				/* XXX magic number */);
 84		}
 85	}
 86
 87	public String getActionCommand()
 88	{
 89		return getModel().getActionCommand();
 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
138	class Model extends DefaultButtonModel
139	{
140		public boolean isSelected()
141		{
142			if(!isShowing())
143				return false;
144
145			try
146			{
147				return action.isSelected(GUIUtilities.getView(
148					EnhancedCheckBoxMenuItem.this));
149			}
150			catch(Throwable t)
151			{
152				Log.log(Log.ERROR,this,t);
153				return false;
154			}
155		}
156
157		public void setSelected(boolean b) {}
158	}
159}