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 import org.gjt.sp.util.Log; 31 33 36 public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem 37 { 38 47 public EnhancedCheckBoxMenuItem(String label, String 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 } 74 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 } 87 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 ); 105 } 106 } 108 110 private ActionContext context; 112 private String shortcut; 113 private String action; 114 116 private String getShortcut() 118 { 119 if(action == null) 120 return null; 121 else 122 { 123 String shortcut1 = jEdit.getProperty(action + ".shortcut"); 124 String 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 } 143 145 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 t) 166 { 167 Log.log(Log.ERROR,this,t); 168 return false; 169 } 170 } 171 172 public void setSelected(boolean b) {} 173 } 175 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 } } 212 | Popular Tags |