1 62 63 64 package swingwtx.swing; 65 66 import java.util.Vector ; 67 68 import org.eclipse.swt.widgets.Widget; 69 70 import swingwt.awt.event.ActionEvent; 71 import swingwt.awt.event.ActionListener; 72 import swingwt.awt.event.InputEvent; 73 import swingwt.awt.event.KeyEvent; 74 import swingwtx.swing.event.MenuEvent; 75 import swingwtx.swing.event.MenuListener; 76 77 public abstract class JSWTMenuComponent extends AbstractButton { 78 79 80 protected org.eclipse.swt.widgets.MenuItem peer = null; 81 82 83 protected boolean pEnabled = true; 84 85 protected Icon pImage = null; 86 87 protected char pMnemonic = ' '; 88 89 protected int pAccelerator = 0; 90 protected String pNamedAccelerator = ""; 91 92 protected String pText = ""; 93 94 protected String actionCommand = ""; 95 96 protected Action menuAction = null; 97 98 99 private Object retval; 100 101 102 protected Vector actionListeners = new Vector (); 103 104 105 protected Vector menuListeners = new Vector (); 106 107 108 protected final static int SELECTED = 0; 109 protected final static int DESELECTED = 1; 110 protected final static int CANCELED = 2; 111 112 public void setIcon(final Icon icon) { 113 pImage = icon; 114 final JSWTMenuComponent pthis = this; 115 SwingUtilities.invokeSync(new Runnable () { 116 public void run() { 117 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 118 peer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, icon)); 119 } 120 }); 121 } 122 123 public void addMenuListener(MenuListener l) { 124 menuListeners.add(l); 125 } 126 127 public void removeMenuListener(MenuListener l) { 128 menuListeners.remove(l); 129 } 130 131 public int getMnemonic() { return (int) pMnemonic; }; 132 public void setMnemonic(char mnemonic) { pMnemonic = mnemonic; showMnemonic(); } 133 public void setMnemonic(int mnemonic) { pMnemonic = (char) mnemonic; showMnemonic(); } 134 135 136 public String getLabel() { return getText(); } 137 138 public void setLabel(String label) { setText(label); } 139 140 public String getText() { 141 retval = ""; 142 SwingUtilities.invokeSync(new Runnable () { 143 public void run() { 144 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 145 retval = peer.getText(); 146 else 147 retval = pText; 148 } 149 }); 150 return retval.toString(); 151 } 152 153 public void setText(String text) { 154 if (text == null) text = ""; 155 pText = text; 156 SwingUtilities.invokeSync(new Runnable () { 157 public void run() { 158 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) { 159 peer.setText(pText); showMnemonic(); showAccelerator(); 160 } 161 } 162 }); 163 } 164 165 public String getToolTipText() { return ""; } 166 public void setToolTipText(String tip) { } 167 168 public String getActionCommand() { return actionCommand; } 169 public void setActionCommand(String command) { actionCommand = command; } 170 171 protected void showMnemonic() { 172 if (pMnemonic == ' ') return; 174 String text = ( peer == null ? pText : peer.getText()); 175 if (text.indexOf("&") != -1) return; 177 178 String lower = text.toLowerCase(); 181 String mn = new String (new char[] { pMnemonic }).toLowerCase(); 182 int pos = lower.indexOf(mn); 183 184 if (pos != -1) { 185 text = text.substring(0, pos) + "&" + text.substring(pos, text.length()); 186 setText(text); 187 } 188 } 189 190 private void showAccelerator() { 191 if (pText.indexOf("\t") != -1) return; 193 if (pNamedAccelerator == null) return; 194 if (pNamedAccelerator.equals("")) return; 195 pText += "\t" + pNamedAccelerator; 196 setText(pText); 197 } 198 199 public void setSwingWTParent(org.eclipse.swt.widgets.Menu parent, org.eclipse.swt.widgets.Shell shell) throws Exception { 200 throw new Exception ("Override in subclass"); 201 } 202 203 204 205 public void setAccelerator(final KeyStroke k) { 206 207 SwingUtilities.invokeSync(new Runnable () { 209 public void run() { 210 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 211 peer.setAccelerator( swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode()) 212 + swingwt.awt.event.InputEvent.translateAWTModifiers(k.getModifiers())); 213 else 214 pAccelerator = swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode()) 215 + swingwt.awt.event.InputEvent.translateAWTModifiers(k.getModifiers()); 216 } 217 }); 218 219 pNamedAccelerator = ""; 221 if ((k.getModifiers() & InputEvent.CTRL_MASK) > 0) pNamedAccelerator += "Ctrl+"; 222 if ((k.getModifiers() & InputEvent.ALT_MASK) > 0) 223 pNamedAccelerator += "Alt+"; 224 if ((k.getModifiers() & InputEvent.SHIFT_DOWN_MASK) > 0 || (k.getModifiers() & InputEvent.SHIFT_MASK) > 0) 225 pNamedAccelerator += "Shift+"; 226 if (k.getKeyCode() == KeyEvent.VK_SPACE) pNamedAccelerator += "Space"; 227 else if (k.getKeyCode() == KeyEvent.VK_F1) pNamedAccelerator += "F1"; 228 else if (k.getKeyCode() == KeyEvent.VK_F2) pNamedAccelerator += "F2"; 229 else if (k.getKeyCode() == KeyEvent.VK_F3) pNamedAccelerator += "F3"; 230 else if (k.getKeyCode() == KeyEvent.VK_F4) pNamedAccelerator += "F4"; 231 else if (k.getKeyCode() == KeyEvent.VK_F5) pNamedAccelerator += "F5"; 232 else if (k.getKeyCode() == KeyEvent.VK_F6) pNamedAccelerator += "F6"; 233 else if (k.getKeyCode() == KeyEvent.VK_F7) pNamedAccelerator += "F7"; 234 else if (k.getKeyCode() == KeyEvent.VK_F8) pNamedAccelerator += "F8"; 235 else if (k.getKeyCode() == KeyEvent.VK_F9) pNamedAccelerator += "F9"; 236 else if (k.getKeyCode() == KeyEvent.VK_F10) pNamedAccelerator += "F10"; 237 else if (k.getKeyCode() == KeyEvent.VK_F11) pNamedAccelerator += "F11"; 238 else if (k.getKeyCode() == KeyEvent.VK_F12) pNamedAccelerator += "F12"; 239 else 240 pNamedAccelerator += new String (new char[] {(char) swingwt.awt.event.KeyEvent.translateAWTKey(k.getKeyCode())} ); 241 242 showAccelerator(); 244 } 245 246 247 public Widget getMenuPeer() { return peer; } 248 249 250 public void setEnabled(final boolean b) { 251 pEnabled = b; 252 SwingUtilities.invokeSync(new Runnable () { 253 public void run() { 254 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 255 peer.setEnabled(b); 256 } 257 }); 258 } 259 260 261 public boolean isEnabled() { 262 SwingUtilities.invokeSync(new Runnable () { 263 public void run() { 264 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 265 retval = new Boolean (peer.isEnabled()); 266 else 267 retval = new Boolean (pEnabled); 268 } 269 }); 270 return ((Boolean ) retval).booleanValue(); 271 } 272 273 274 public void dispose() { 275 SwingUtilities.invokeSync(new Runnable () { 276 public void run() { 277 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 278 { peer.dispose(); peer = null; } 279 } 280 }); 281 } 282 283 284 public void requestFocus() {} 285 286 287 288 public void addActionListener(ActionListener l) { 289 actionListeners.add(l); 290 } 291 292 293 public void removeActionListener(ActionListener l) { 294 actionListeners.remove(l); 295 } 296 297 300 public void processActionEvent(int id) { 301 ActionEvent ae = new ActionEvent(this, id, this.getActionCommand()); 302 for (int i = 0; i < actionListeners.size(); i++) { 303 ActionListener al = (ActionListener) actionListeners.get(i); 304 al.actionPerformed(ae); 305 } 306 } 307 308 311 public void processMenuEvent(int id) { 312 MenuEvent me = new MenuEvent(this); 313 for (int i = 0; i < menuListeners.size(); i++) { 314 MenuListener ml = (MenuListener) menuListeners.get(i); 315 switch (id) { 316 case SELECTED: ml.menuSelected(me); break; 317 case DESELECTED: ml.menuDeselected(me); break; 318 case CANCELED: ml.menuCanceled(me); break; 319 } 320 } 321 } 322 323 324 330 public void registerEvents() { 331 332 SwingUtilities.invokeSync(new Runnable () { 333 334 public void run() { 335 336 peer.addSelectionListener(new org.eclipse.swt.events.SelectionListener() { 339 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 340 processActionEvent(0); 341 } 342 public void widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent e) { 343 } 345 }); 346 347 peer.addArmListener(new org.eclipse.swt.events.ArmListener() { 349 public void widgetArmed(org.eclipse.swt.events.ArmEvent e) { 350 processMenuEvent(SELECTED); 351 } 352 }); 353 354 } 355 }); 356 357 } 358 359 364 public void setCachedProperties() { 365 final JSWTMenuComponent pthis = this; 366 SwingUtilities.invokeSync(new Runnable () { 367 public void run() { 368 peer.setEnabled(pEnabled); 369 if (pImage != null) peer.setImage(SwingWTUtils.getSWTImageFromSwingIcon(pthis, pImage)); 370 if (pMnemonic != ' ') showMnemonic(); 371 if (pAccelerator != 0) { peer.setAccelerator(pAccelerator); showAccelerator(); } 372 peer.setText(pText); 373 } 374 }); 375 } 376 377 } 378 | Popular Tags |