1 7 package java.awt; 8 9 import java.io.IOException ; 10 import java.io.ObjectInputStream ; 11 import java.util.Vector ; 12 import java.util.Enumeration ; 13 import java.awt.peer.MenuBarPeer; 14 import java.awt.event.KeyEvent ; 15 import javax.accessibility.*; 16 17 52 public class MenuBar extends MenuComponent implements MenuContainer , Accessible { 53 54 static { 55 56 Toolkit.loadLibraries(); 57 if (!GraphicsEnvironment.isHeadless()) { 58 initIDs(); 59 } 60 } 61 62 69 Vector menus = new Vector (); 70 71 81 Menu helpMenu; 82 83 private static final String base = "menubar"; 84 private static int nameCounter = 0; 85 86 89 private static final long serialVersionUID = -4930327919388951260L; 90 91 97 public MenuBar() throws HeadlessException { 98 } 99 100 104 String constructComponentName() { 105 synchronized (getClass()) { 106 return base + nameCounter++; 107 } 108 } 109 110 115 public void addNotify() { 116 synchronized (getTreeLock()) { 117 if (peer == null) 118 peer = Toolkit.getDefaultToolkit().createMenuBar(this); 119 120 int nmenus = getMenuCount(); 121 for (int i = 0 ; i < nmenus ; i++) { 122 getMenu(i).addNotify(); 123 } 124 } 125 } 126 127 132 public void removeNotify() { 133 synchronized (getTreeLock()) { 134 int nmenus = getMenuCount(); 135 for (int i = 0 ; i < nmenus ; i++) { 136 getMenu(i).removeNotify(); 137 } 138 super.removeNotify(); 139 } 140 } 141 142 146 public Menu getHelpMenu() { 147 return helpMenu; 148 } 149 150 156 public void setHelpMenu(Menu m) { 157 synchronized (getTreeLock()) { 158 if (helpMenu == m) { 159 return; 160 } 161 if (helpMenu != null) { 162 remove(helpMenu); 163 } 164 if (m.parent != this) { 165 add(m); 166 } 167 helpMenu = m; 168 if (m != null) { 169 m.isHelpMenu = true; 170 m.parent = this; 171 MenuBarPeer peer = (MenuBarPeer)this.peer; 172 if (peer != null) { 173 if (m.peer == null) { 174 m.addNotify(); 175 } 176 peer.addHelpMenu(m); 177 } 178 } 179 } 180 } 181 182 192 public Menu add(Menu m) { 193 synchronized (getTreeLock()) { 194 if (m.parent != null) { 195 m.parent.remove(m); 196 } 197 menus.addElement(m); 198 m.parent = this; 199 200 MenuBarPeer peer = (MenuBarPeer)this.peer; 201 if (peer != null) { 202 if (m.peer == null) { 203 m.addNotify(); 204 } 205 peer.addMenu(m); 206 } 207 return m; 208 } 209 } 210 211 217 public void remove(int index) { 218 synchronized (getTreeLock()) { 219 Menu m = getMenu(index); 220 menus.removeElementAt(index); 221 MenuBarPeer peer = (MenuBarPeer)this.peer; 222 if (peer != null) { 223 m.removeNotify(); 224 m.parent = null; 225 peer.delMenu(index); 226 } 227 } 228 } 229 230 235 public void remove(MenuComponent m) { 236 synchronized (getTreeLock()) { 237 int index = menus.indexOf(m); 238 if (index >= 0) { 239 remove(index); 240 } 241 } 242 } 243 244 249 public int getMenuCount() { 250 return countMenus(); 251 } 252 253 257 @Deprecated 258 public int countMenus() { 259 return getMenuCountImpl(); 260 } 261 262 266 final int getMenuCountImpl() { 267 return menus.size(); 268 } 269 270 275 public Menu getMenu(int i) { 276 return getMenuImpl(i); 277 } 278 279 283 final Menu getMenuImpl(int i) { 284 return (Menu )menus.elementAt(i); 285 } 286 287 295 public synchronized Enumeration <MenuShortcut > shortcuts() { 296 Vector shortcuts = new Vector (); 297 int nmenus = getMenuCount(); 298 for (int i = 0 ; i < nmenus ; i++) { 299 Enumeration e = getMenu(i).shortcuts(); 300 while (e.hasMoreElements()) { 301 shortcuts.addElement(e.nextElement()); 302 } 303 } 304 return shortcuts.elements(); 305 } 306 307 318 public MenuItem getShortcutMenuItem(MenuShortcut s) { 319 int nmenus = getMenuCount(); 320 for (int i = 0 ; i < nmenus ; i++) { 321 MenuItem mi = getMenu(i).getShortcutMenuItem(s); 322 if (mi != null) { 323 return mi; 324 } 325 } 326 return null; } 328 329 335 boolean handleShortcut(KeyEvent e) { 336 int id = e.getID(); 338 if (id != KeyEvent.KEY_PRESSED && id != KeyEvent.KEY_RELEASED) { 339 return false; 340 } 341 342 int accelKey = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); 344 if ((e.getModifiers() & accelKey) == 0) { 345 return false; 346 } 347 348 int nmenus = getMenuCount(); 350 for (int i = 0 ; i < nmenus ; i++) { 351 Menu m = getMenu(i); 352 if (m.handleShortcut(e)) { 353 return true; 354 } 355 } 356 return false; 357 } 358 359 364 public void deleteShortcut(MenuShortcut s) { 365 int nmenus = getMenuCount(); 366 for (int i = 0 ; i < nmenus ; i++) { 367 getMenu(i).deleteShortcut(s); 368 } 369 } 370 371 374 375 380 private int menuBarSerializedDataVersion = 1; 381 382 389 private void writeObject(java.io.ObjectOutputStream s) 390 throws java.lang.ClassNotFoundException , 391 java.io.IOException 392 { 393 s.defaultWriteObject(); 394 } 395 396 407 private void readObject(ObjectInputStream s) 408 throws ClassNotFoundException , IOException , HeadlessException 409 { 410 s.defaultReadObject(); 412 for (int i = 0; i < menus.size(); i++) { 413 Menu m = (Menu )menus.elementAt(i); 414 m.parent = this; 415 } 416 } 417 418 421 private static native void initIDs(); 422 423 424 428 437 public AccessibleContext getAccessibleContext() { 438 if (accessibleContext == null) { 439 accessibleContext = new AccessibleAWTMenuBar(); 440 } 441 return accessibleContext; 442 } 443 444 447 int getAccessibleChildIndex(MenuComponent child) { 448 return menus.indexOf(child); 449 } 450 451 461 protected class AccessibleAWTMenuBar extends AccessibleAWTMenuComponent 462 { 463 466 private static final long serialVersionUID = -8577604491830083815L; 467 468 474 public AccessibleRole getAccessibleRole() { 475 return AccessibleRole.MENU_BAR; 476 } 477 478 } 480 } 481 | Popular Tags |