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