1 30 31 32 package swingwtx.swing; 33 34 import swingwtx.swing.event.*; 35 36 import org.eclipse.swt.widgets.*; 37 import org.eclipse.swt.*; 38 39 import java.util.*; 40 41 public class JPopupMenu extends JMenuBar { 42 43 protected Vector popupListeners = new Vector(); 44 45 protected final static int CANCELED = 0; 46 protected final static int INVISIBLE = 1; 47 protected final static int VISIBLE = 2; 48 49 public JPopupMenu() { } 50 public JPopupMenu(String label) { } 51 52 53 protected JPopupMenu(Vector childComponents) { this.components = childComponents; } 54 55 public void addPopupMenuListener(PopupMenuListener l) { 56 popupListeners.add(l); 57 } 58 59 public void removePopupMenuListener(PopupMenuListener l) { 60 popupListeners.remove(l); 61 } 62 63 public void processPopupMenuEvent(int id) { 64 Iterator i = popupListeners.iterator(); 65 PopupMenuEvent e = new PopupMenuEvent(this); 66 while (i.hasNext()) { 67 PopupMenuListener l = (PopupMenuListener) i.next(); 68 switch(id) { 69 case CANCELED: l.popupMenuCanceled(e); break; 70 case INVISIBLE: l.popupMenuWillBecomeInvisible(e); break; 71 case VISIBLE: l.popupMenuWillBecomeVisible(e); break; 72 } 73 } 74 } 75 76 public void setVisible(boolean b) { 77 super.setVisible(b); 78 if (b) 79 processPopupMenuEvent(VISIBLE); 80 else { 81 processPopupMenuEvent(INVISIBLE); 82 processPopupMenuEvent(CANCELED); 83 } 84 } 85 86 public void show(swingwt.awt.Component c, int x, int y) { 87 88 boolean needToCreatePeer = menu == null; 91 if (!needToCreatePeer) 92 needToCreatePeer = menu.isDisposed(); 93 94 if (needToCreatePeer) { 95 setSwingWTParent(c.getPeer().getShell()); 96 c.getPeer().setMenu(menu); 97 } 98 99 menu.setVisible(true); 101 102 } 103 104 public void setSwingWTParent(Shell parent) { 105 shell = parent; 106 menu = new Menu(parent, SWT.POP_UP); 107 if (components.size() > 0) { 109 Iterator i = components.iterator(); 110 while (i.hasNext()) { 111 add((JSWTMenuComponent) i.next()); 112 } 113 } 114 } 115 116 } 117 | Popular Tags |