1 33 34 35 package swingwtx.swing; 36 37 import org.eclipse.swt.widgets.*; 38 import org.eclipse.swt.*; 39 40 import swingwt.awt.event.*; 41 42 import java.util.*; 43 44 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants, ButtonModel { 45 46 protected boolean pState = false; 47 private Shell shell = null; 48 protected Vector itemListeners = new Vector(); 49 private Object retval; 50 51 public JCheckBoxMenuItem() {setModel(this);} 52 public JCheckBoxMenuItem(Action a) { setAction(a); setModel(this);} 53 public JCheckBoxMenuItem(Icon icon) { this("", icon); } 54 public JCheckBoxMenuItem(Icon icon, boolean b) { this("", icon, b); } 55 public JCheckBoxMenuItem(String text) { this(text, null, false); } 56 public JCheckBoxMenuItem(String text, boolean b) { this(text, null, b); } 57 public JCheckBoxMenuItem(String text, Icon icon) { this(text, null, false); } 58 public JCheckBoxMenuItem(String text, Icon icon, boolean b) { pText = text; pImage = icon; pState = b; setModel(this);} 59 public JCheckBoxMenuItem(String text, int mnemonic) { pText = text; setMnemonic(mnemonic); setModel(this);} 60 public JCheckBoxMenuItem(String text, int mnemonic, boolean b) { pText = text; setMnemonic(mnemonic); pState = b; setModel(this); } 61 62 public void addItemListener(ItemListener l) { 63 itemListeners.add(l); 64 } 65 66 public void removeItemListener(ItemListener l) { 67 itemListeners.remove(l); 68 } 69 70 74 public void processActionEvent(int id) { 75 super.processActionEvent(id); 76 processItemEvent(); 77 } 78 79 82 public void processItemEvent() { 83 if (itemListeners.size() == 0) return; 84 Iterator i = itemListeners.iterator(); 85 ItemEvent e = new ItemEvent(this, 0, this, (ppeer.getSelection() ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); 86 while (i.hasNext()) { 87 ItemListener il = (ItemListener) i.next(); 88 il.itemStateChanged(e); 89 } 90 } 91 92 public Object [] getSelectedObjects() { return new Object [] { getText() };} 93 public boolean getState() { 94 SwingUtilities.invokeSync(new Runnable () { 95 public void run() { 96 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 97 retval = new Boolean (ppeer.getSelection()); 98 else 99 retval = new Boolean (pState); 100 } 101 }); 102 return ((Boolean ) retval).booleanValue(); 103 } 104 105 public void setState(boolean b) { 106 pState = b; 107 SwingUtilities.invokeSync(new Runnable () { 108 public void run() { 109 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) 110 ppeer.setSelection(pState); 111 } 112 }); 113 } 114 115 public boolean isSelected() { return getState(); } 116 public void setSelected(boolean b) { setState(b); } 117 118 public void setSwingWTParent(Menu parent, Shell shell) throws Exception { 119 this.shell = shell; 120 peer = new MenuItem(parent, SWT.CHECK); 121 peer.setSelection(pState); 122 } 123 124 } 125 | Popular Tags |