1 39 40 41 package swingwtx.swing; 42 43 import org.eclipse.swt.widgets.*; 44 import org.eclipse.swt.*; 45 46 import swingwt.awt.Component; 47 48 import java.util.*; 49 50 public class JMenu extends JSWTMenuComponent { 51 52 53 private Menu container = null; 54 56 private Shell shell = null; 57 58 59 private Vector components = new Vector(); 60 61 public JMenu() {} 62 public JMenu(Action a) { setAction(a); } 63 public JMenu(String text) { this(text, false); } 64 public JMenu(String text, boolean isTearOff) { pText = text; } 65 66 67 public JSWTMenuComponent add(final JSWTMenuComponent c) { 68 components.add(c); 69 SwingUtilities.invokeSync(new Runnable () { 70 public void run() { 71 if (SwingWTUtils.isSWTMenuControlAvailable(peer)) { 72 try { 73 c.setSwingWTParent(container, shell); 74 c.setCachedProperties(); 75 c.registerEvents(); 76 } 77 catch (Exception e) { 78 e.printStackTrace(); 79 } 80 } 81 } 82 }); 83 return c; 84 } 85 86 public JSWTMenuComponent add(JSWTMenuComponent c, String name) { 87 return add(c); 88 } 89 90 public JMenu add(JMenu c) { 91 return (JMenu) add((JSWTMenuComponent) c); 92 } 93 94 public JMenu add(JMenu c, String name) { 95 return (JMenu) add((JSWTMenuComponent) c, name); 96 } 97 98 public JMenuItem add(JMenuItem c) { 99 return (JMenuItem) add((JSWTMenuComponent) c); 100 } 101 102 public JMenuItem add(JMenuItem c, String name) { 103 return (JMenuItem) add((JSWTMenuComponent) c, name); 104 } 105 106 107 public JMenuItem add(Action a) { 108 JMenuItem jmenu = new JMenuItem(a); 109 add(jmenu); 110 return jmenu; 111 } 112 113 114 public JMenuItem add(String text) { 115 JMenuItem jmenu = new JMenuItem(text); 116 add(jmenu); 117 return jmenu; 118 } 119 120 123 public Component add(Component c) { 124 throw new IllegalArgumentException ("Sorry, SwingWT cannot support components in menus"); 125 } 126 127 130 public Component add(Component c, int index) { 131 throw new IllegalArgumentException ("Sorry, SwingWT cannot support components in menus"); 132 } 133 134 137 public void remove(Component c) { 138 throw new IllegalArgumentException ("Sorry, SwingWT cannot support components in menus"); 139 } 140 141 public void remove(JSWTMenuComponent c) { 142 c.dispose(); 143 } 144 145 146 public void remove(int index) { 147 JSWTMenuComponent c = (JSWTMenuComponent) components.get(index); 148 components.remove(index); 149 c.dispose(); 150 } 151 152 public void removeAll() { 153 Iterator i = components.iterator(); 154 while (i.hasNext()) { 155 JSWTMenuComponent c = (JSWTMenuComponent) i.next(); 156 c.dispose(); 157 } 158 159 SwingUtilities.invokeSync(new Runnable () { 160 public void run() { 161 if (peer != null && container != null) { 162 container.dispose(); 164 container = new Menu(shell, SWT.DROP_DOWN); 166 peer.setMenu(container); 167 } 168 } 169 }); 170 } 171 172 public void addSeparator() { 173 add(new JSeparator()); 174 } 175 176 181 public JPopupMenu getPopupMenu() { 182 return new JPopupMenu(components); 183 } 184 185 public void setSwingWTParent(Menu parent, Shell shell) throws Exception { 186 this.shell = shell; 187 peer = new MenuItem(parent, SWT.CASCADE); 188 container = new Menu(shell, SWT.DROP_DOWN); 189 peer.setMenu(container); 190 191 if (components.size() > 0) { 193 Object [] o = components.toArray(); 194 for (int i = 0; i < o.length; i++) 195 add((JSWTMenuComponent) o[i]); 196 } 197 198 } 199 200 public boolean isSelected() { 201 return false; 202 } 203 204 public void setSelected(boolean b) { 205 } 206 207 } 208 | Popular Tags |