Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/gjt/sp/jedit/gui/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, 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  
23  package org.gjt.sp.jedit.gui;
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 actionCommand The action command
44     */
45    public EnhancedMenuItem(String label, EditAction action)
46    {
47      super(label);
48  
49      this.action = action;
50  
51      if(action != null)
52      {
53        setEnabled(true);
54        addActionListener(new EditAction.Wrapper(action));
55        shortcutProp1 = action.getName() + ".shortcut";
56        shortcutProp2 = action.getName() + ".shortcut2";
57  
58        addMouseListener(new MouseHandler());
59      }
60      else
61        setEnabled(false);
62    } //}}}
63  
64    //{{{ getPreferredSize() method
65    public Dimension getPreferredSize()
66    {
67      Dimension d = super.getPreferredSize();
68  
69      String shortcut = getShortcut();
70  
71      if(shortcut != null)
72      {
73        d.width += (getFontMetrics(acceleratorFont)
74          .stringWidth(shortcut) + 15);
75      }
76      return d;
77    } //}}}
78  
79    //{{{ paint() method
80    public void paint(Graphics g)
81    {
82      super.paint(g);
83  
84      String shortcut = getShortcut();
85  
86      if(shortcut != null)
87      {
88        g.setFont(acceleratorFont);
89        g.setColor(getModel().isArmed() ?
90          acceleratorSelectionForeground :
91          acceleratorForeground);
92        FontMetrics fm = g.getFontMetrics();
93        Insets insets = getInsets();
94        g.drawString(shortcut,getWidth() - (fm.stringWidth(
95          shortcut) + insets.right + insets.left + 5),
96          getFont().getSize() + (insets.top - 
97          (OperatingSystem.isMacOSLF() ? 0 : 1))
98          /* XXX magic number */);
99      }
100   } //}}}
101 
102   //{{{ Private members
103 
104   //{{{ Instance variables
105   private String shortcutProp1;
106   private String shortcutProp2;
107   private EditAction action;
108   private static Font acceleratorFont;
109   private static Color acceleratorForeground;
110   private static Color acceleratorSelectionForeground;
111   //}}}
112 
113   //{{{ getShortcut() method
114   private String getShortcut()
115   {
116     if(action == null)
117       return null;
118     else
119     {
120       String shortcut1 = jEdit.getProperty(shortcutProp1);
121       String shortcut2 = jEdit.getProperty(shortcutProp2);
122 
123       if(shortcut1 == null || shortcut1.length() == 0)
124       {
125         if(shortcut2 == null || shortcut2.length() == 0)
126           return null;
127         else
128           return shortcut2;
129       }
130       else
131       {
132         if(shortcut2 == null || shortcut2.length() == 0)
133           return shortcut1;
134         else
135           return shortcut1 + " or " + shortcut2;
136       }
137     }
138   } //}}}
139 
140   //{{{ Class initializer
141   static
142   {
143     String shortcutFont;
144     if (OperatingSystem.isMacOSLF())
145       shortcutFont = "Lucida Grande";
146     else
147       shortcutFont = "Monospaced";
148     
149     acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
150     acceleratorFont = new Font(shortcutFont,
151       acceleratorFont.getStyle(),
152       acceleratorFont.getSize());
153     acceleratorForeground = UIManager
154       .getColor("MenuItem.acceleratorForeground");
155     acceleratorSelectionForeground = UIManager
156       .getColor("MenuItem.acceleratorSelectionForeground");
157   } //}}}
158 
159   //}}}
160 
161   //{{{ MouseHandler class
162   class MouseHandler extends MouseAdapter
163   {
164     public void mouseReleased(MouseEvent evt)
165     {
166       GUIUtilities.getView((Component)evt.getSource())
167         .getStatus().setMessage(null);
168     }
169 
170     public void mouseEntered(MouseEvent evt)
171     {
172       String msg = action.getMouseOverText();
173       if(msg != null)
174       {
175         GUIUtilities.getView((Component)evt.getSource())
176           .getStatus().setMessage(msg);
177       }
178     }
179 
180     public void mouseExited(MouseEvent evt)
181     {
182       GUIUtilities.getView((Component)evt.getSource())
183         .getStatus().setMessage(null);
184     }
185   } //}}}
186 }