KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > menu > EnhancedCheckBoxMenuItem


1 /*
2  * EnhancedCheckBoxMenuItem.java - Check box menu item
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 import org.gjt.sp.util.Log;
31 //}}}
32

33 /**
34  * jEdit's custom menu item. It adds support for multi-key shortcuts.
35  */

36 public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
37 {
38     //{{{ EnhancedCheckBoxMenuItem constructor
39
/**
40      * Creates a new menu item. Most plugins should call
41      * GUIUtilities.loadMenuItem() instead.
42      * @param label The menu item label
43      * @param action The edit action
44      * @param context An action context
45      * @since jEdit 4.2pre1
46      */

47     public EnhancedCheckBoxMenuItem(String JavaDoc label, String JavaDoc action,
48         ActionContext context)
49     {
50         this.context = context;
51         this.action = action;
52         this.shortcut = getShortcut();
53         if(OperatingSystem.hasScreenMenuBar() && shortcut != null)
54         {
55             setText(label + " (" + shortcut + ")");
56             shortcut = null;
57         }
58         else
59             setText(label);
60
61         if(action != null)
62         {
63             setEnabled(true);
64             addActionListener(new EditAction.Wrapper(context,action));
65
66             addMouseListener(new MouseHandler());
67         }
68         else
69             setEnabled(false);
70
71         setModel(new Model());
72     } //}}}
73

74     //{{{ getPreferredSize() method
75
public Dimension getPreferredSize()
76     {
77         Dimension d = super.getPreferredSize();
78
79         if(shortcut != null)
80         {
81             d.width += (getFontMetrics(EnhancedMenuItem.acceleratorFont)
82                 .stringWidth(shortcut) + 15);
83         }
84         return d;
85     } //}}}
86

87     //{{{ paint() method
88
public void paint(Graphics g)
89     {
90         super.paint(g);
91
92         if(shortcut != null)
93         {
94             g.setFont(EnhancedMenuItem.acceleratorFont);
95             g.setColor(getModel().isArmed() ?
96                 EnhancedMenuItem.acceleratorSelectionForeground :
97                 EnhancedMenuItem.acceleratorForeground);
98             FontMetrics fm = g.getFontMetrics();
99             Insets insets = getInsets();
100             g.drawString(shortcut,getWidth() - (fm.stringWidth(
101                 shortcut) + insets.right + insets.left + 5),
102                 getFont().getSize() + (insets.top -
103                 (OperatingSystem.isMacOSLF() ? 0 : 1))
104                 /* XXX magic number */);
105         }
106     } //}}}
107

108     //{{{ Private members
109

110     //{{{ Instance variables
111
private ActionContext context;
112     private String JavaDoc shortcut;
113     private String JavaDoc action;
114     //}}}
115

116     //{{{ getShortcut() method
117
private String JavaDoc getShortcut()
118     {
119         if(action == null)
120             return null;
121         else
122         {
123             String JavaDoc shortcut1 = jEdit.getProperty(action + ".shortcut");
124             String JavaDoc shortcut2 = jEdit.getProperty(action + ".shortcut2");
125
126             if(shortcut1 == null || shortcut1.length() == 0)
127             {
128                 if(shortcut2 == null || shortcut2.length() == 0)
129                     return null;
130                 else
131                     return shortcut2;
132             }
133             else
134             {
135                 if(shortcut2 == null || shortcut2.length() == 0)
136                     return shortcut1;
137                 else
138                     return shortcut1 + " or " + shortcut2;
139             }
140         }
141     } //}}}
142

143     //}}}
144

145     //{{{ Model class
146
class Model extends DefaultButtonModel
147     {
148         public boolean isSelected()
149         {
150             if(!isShowing())
151                 return false;
152
153             EditAction a = context.getAction(action);
154             if(a == null)
155             {
156                 Log.log(Log.WARNING,this,"Unknown action: "
157                     + action);
158                 return false;
159             }
160
161             try
162             {
163                 return a.isSelected(EnhancedCheckBoxMenuItem.this);
164             }
165             catch(Throwable JavaDoc t)
166             {
167                 Log.log(Log.ERROR,this,t);
168                 return false;
169             }
170         }
171
172         public void setSelected(boolean b) {}
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 JavaDoc 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
}
212
Popular Tags