1 7 8 package java.awt; 9 10 import java.awt.peer.PopupMenuPeer; 11 import javax.accessibility.*; 12 13 14 27 public class PopupMenu extends Menu { 28 29 private static final String base = "popup"; 30 static int nameCounter = 0; 31 32 35 private static final long serialVersionUID = -4620452533522760060L; 36 37 43 public PopupMenu() throws HeadlessException { 44 this(""); 45 } 46 47 56 public PopupMenu(String label) throws HeadlessException { 57 super(label); 58 } 59 60 64 String constructComponentName() { 65 synchronized (getClass()) { 66 return base + nameCounter++; 67 } 68 } 69 70 75 public void addNotify() { 76 synchronized (getTreeLock()) { 77 if (parent != null && !(parent instanceof Component )) { 80 super.addNotify(); 81 } 82 else { 83 if (peer == null) 84 peer = Toolkit.getDefaultToolkit().createPopupMenu(this); 85 int nitems = getItemCount(); 86 for (int i = 0 ; i < nitems ; i++) { 87 MenuItem mi = getItem(i); 88 mi.parent = this; 89 mi.addNotify(); 90 } 91 } 92 } 93 } 94 95 116 public void show(Component origin, int x, int y) { 117 MenuContainer localParent = parent; 119 if (localParent == null) { 120 throw new NullPointerException ("parent is null"); 121 } 122 if (!(localParent instanceof Component )) { 123 throw new IllegalArgumentException ( 124 "PopupMenus with non-Component parents cannot be shown"); 125 } 126 Component compParent = (Component )localParent; 127 if (compParent != origin && 128 compParent instanceof Container && 129 !((Container )compParent).isAncestorOf(origin)) { 130 throw new IllegalArgumentException ( 131 "origin not in parent's hierarchy"); 132 } 133 if (compParent.getPeer() == null || !compParent.isShowing()) { 134 throw new RuntimeException ("parent not showing on screen"); 135 } 136 if (peer == null) { 137 addNotify(); 138 } 139 synchronized (getTreeLock()) { 140 if (peer != null) { 141 ((PopupMenuPeer)peer).show( 142 new Event (origin, 0, Event.MOUSE_DOWN, x, y, 0, 0)); 143 } 144 } 145 } 146 147 148 152 159 public AccessibleContext getAccessibleContext() { 160 if (accessibleContext == null) { 161 accessibleContext = new AccessibleAWTPopupMenu(); 162 } 163 return accessibleContext; 164 } 165 166 174 protected class AccessibleAWTPopupMenu extends AccessibleAWTMenu 175 { 176 179 private static final long serialVersionUID = -4282044795947239955L; 180 181 187 public AccessibleRole getAccessibleRole() { 188 return AccessibleRole.POPUP_MENU; 189 } 190 191 } 193 } 194 | Popular Tags |