Source code: org/gjt/sp/jedit/menu/EnhancedMenuItem.java
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
23 package org.gjt.sp.jedit.menu;
24
25 //{{{ Imports
26 import javax.swing.*;
27 import java.awt.event.*;
28 import java.awt.*;
29 import org.gjt.sp.jedit.*;
30 //}}}
31
32 /**
33 * jEdit's custom menu item. It adds support for multi-key shortcuts.
34 */
35 public 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.context = context;
49 this.action = action;
50 this.shortcut = getShortcut();
51 if(OperatingSystem.hasScreenMenuBar() && shortcut != null)
52 {
53 setText(label + " (" + shortcut + ")");
54 shortcut = null;
55 }
56 else
57 setText(label);
58
59 if(action != null)
60 {
61 setEnabled(true);
62 addActionListener(new EditAction.Wrapper(context,action));
63 addMouseListener(new MouseHandler());
64 }
65 else
66 setEnabled(false);
67 } //}}}
68
69 //{{{ getPreferredSize() method
70 public Dimension getPreferredSize()
71 {
72 Dimension d = super.getPreferredSize();
73
74 if(shortcut != null)
75 {
76 d.width += (getFontMetrics(acceleratorFont)
77 .stringWidth(shortcut) + 15);
78 }
79 return d;
80 } //}}}
81
82 //{{{ paint() method
83 public void paint(Graphics g)
84 {
85 super.paint(g);
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 //{{{ Package-private members
104 static Font acceleratorFont;
105 static Color acceleratorForeground;
106 static Color acceleratorSelectionForeground;
107 //}}}
108
109 //{{{ Private members
110
111 //{{{ Instance variables
112 private ActionContext context;
113 private String shortcut;
114 private String action;
115 //}}}
116
117 //{{{ getShortcut() method
118 private String getShortcut()
119 {
120 if(action == null)
121 return null;
122 else
123 {
124 String shortcut1 = jEdit.getProperty(action + ".shortcut");
125 String shortcut2 = jEdit.getProperty(action + ".shortcut2");
126
127 if(shortcut1 == null || shortcut1.length() == 0)
128 {
129 if(shortcut2 == null || shortcut2.length() == 0)
130 return null;
131 else
132 return shortcut2;
133 }
134 else
135 {
136 if(shortcut2 == null || shortcut2.length() == 0)
137 return shortcut1;
138 else
139 return shortcut1 + " or " + shortcut2;
140 }
141 }
142 } //}}}
143
144 //{{{ Class initializer
145 static
146 {
147 String shortcutFont;
148 if (OperatingSystem.isMacOSLF())
149 shortcutFont = "Lucida Grande";
150 else
151 shortcutFont = "Monospaced";
152
153 acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
154 if(acceleratorFont == null)
155 acceleratorFont = new Font(shortcutFont,Font.PLAIN,12);
156 else
157 {
158 acceleratorFont = new Font(shortcutFont,
159 acceleratorFont.getStyle(),
160 acceleratorFont.getSize());
161 }
162 acceleratorForeground = UIManager
163 .getColor("MenuItem.acceleratorForeground");
164 if(acceleratorForeground == null)
165 acceleratorForeground = Color.black;
166
167 acceleratorSelectionForeground = UIManager
168 .getColor("MenuItem.acceleratorSelectionForeground");
169 if(acceleratorSelectionForeground == null)
170 acceleratorSelectionForeground = Color.black;
171 } //}}}
172
173 //}}}
174
175 //{{{ MouseHandler class
176 class MouseHandler extends MouseAdapter
177 {
178 boolean msgSet = false;
179
180 public void mouseReleased(MouseEvent evt)
181 {
182 if(msgSet)
183 {
184 GUIUtilities.getView((Component)evt.getSource())
185 .getStatus().setMessage(null);
186 msgSet = false;
187 }
188 }
189
190 public void mouseEntered(MouseEvent evt)
191 {
192 String msg = jEdit.getProperty(action + ".mouse-over");
193 if(msg != null)
194 {
195 GUIUtilities.getView((Component)evt.getSource())
196 .getStatus().setMessage(msg);
197 msgSet = true;
198 }
199 }
200
201 public void mouseExited(MouseEvent evt)
202 {
203 if(msgSet)
204 {
205 GUIUtilities.getView((Component)evt.getSource())
206 .getStatus().setMessage(null);
207 msgSet = false;
208 }
209 }
210 } //}}}
211 }