001: /*
002: * EnhancedCheckBoxMenuItem.java - Check box menu item
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2003 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.menu;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import org.gjt.sp.jedit.*;
030: import org.gjt.sp.util.Log;
031:
032: //}}}
033:
034: /**
035: * jEdit's custom menu item. It adds support for multi-key shortcuts.
036: */
037: public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem {
038: //{{{ EnhancedCheckBoxMenuItem constructor
039: /**
040: * Creates a new menu item. Most plugins should call
041: * GUIUtilities.loadMenuItem() instead.
042: * @param label The menu item label
043: * @param action The edit action
044: * @param context An action context
045: * @since jEdit 4.2pre1
046: */
047: public EnhancedCheckBoxMenuItem(String label, String action,
048: ActionContext context) {
049: this .context = context;
050: this .action = action;
051: this .shortcut = GUIUtilities.getShortcutLabel(action);
052: if (OperatingSystem.hasScreenMenuBar() && shortcut != null) {
053: setText(label + " (" + shortcut + ")");
054: shortcut = null;
055: } else
056: setText(label);
057:
058: if (action != null) {
059: setEnabled(true);
060: addActionListener(new EditAction.Wrapper(context, action));
061:
062: addMouseListener(new MouseHandler());
063: } else
064: setEnabled(false);
065:
066: setModel(new Model());
067: } //}}}
068:
069: //{{{ getPreferredSize() method
070: public Dimension getPreferredSize() {
071: Dimension d = super .getPreferredSize();
072:
073: if (shortcut != null) {
074: d.width += (getFontMetrics(EnhancedMenuItem.acceleratorFont)
075: .stringWidth(shortcut) + 15);
076: }
077: return d;
078: } //}}}
079:
080: //{{{ paint() method
081: public void paint(Graphics g) {
082: super .paint(g);
083:
084: if (shortcut != null) {
085: g.setFont(EnhancedMenuItem.acceleratorFont);
086: g
087: .setColor(getModel().isArmed() ? EnhancedMenuItem.acceleratorSelectionForeground
088: : EnhancedMenuItem.acceleratorForeground);
089: FontMetrics fm = g.getFontMetrics();
090: Insets insets = getInsets();
091: g.drawString(shortcut, getWidth()
092: - (fm.stringWidth(shortcut) + insets.right
093: + insets.left + 5), getFont().getSize()
094: + (insets.top - (OperatingSystem.isMacOSLF() ? 0
095: : 1))
096: /* XXX magic number */);
097: }
098: } //}}}
099:
100: //{{{ Private members
101:
102: //{{{ Instance variables
103: private ActionContext context;
104: private String shortcut;
105: private String action;
106:
107: //}}}
108:
109: //}}}
110:
111: //{{{ Model class
112: class Model extends DefaultButtonModel {
113: public boolean isSelected() {
114: if (!isShowing())
115: return false;
116:
117: EditAction a = context.getAction(action);
118: if (a == null) {
119: Log.log(Log.WARNING, this , "Unknown action: " + action);
120: return false;
121: }
122:
123: try {
124: return a.isSelected(EnhancedCheckBoxMenuItem.this );
125: } catch (Throwable t) {
126: Log.log(Log.ERROR, this , t);
127: return false;
128: }
129: }
130:
131: public void setSelected(boolean b) {
132: }
133: } //}}}
134:
135: //{{{ MouseHandler class
136: class MouseHandler extends MouseAdapter {
137: boolean msgSet = false;
138:
139: public void mouseReleased(MouseEvent evt) {
140: if (msgSet) {
141: GUIUtilities.getView((Component) evt.getSource())
142: .getStatus().setMessage(null);
143: msgSet = false;
144: }
145: }
146:
147: public void mouseEntered(MouseEvent evt) {
148: String msg = jEdit.getProperty(action + ".mouse-over");
149: if (msg != null) {
150: GUIUtilities.getView((Component) evt.getSource())
151: .getStatus().setMessage(msg);
152: msgSet = true;
153: }
154: }
155:
156: public void mouseExited(MouseEvent evt) {
157: if (msgSet) {
158: GUIUtilities.getView((Component) evt.getSource())
159: .getStatus().setMessage(null);
160: msgSet = false;
161: }
162: }
163: } //}}}
164: }
|