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.MenuPeer; 14 import java.awt.event.KeyEvent ; 15 import javax.accessibility.*; 16 17 40 public class Menu extends MenuItem implements MenuContainer , Accessible { 41 42 static { 43 44 Toolkit.loadLibraries(); 45 if (!GraphicsEnvironment.isHeadless()) { 46 initIDs(); 47 } 48 } 49 50 56 Vector items = new Vector (); 57 58 70 boolean tearOff; 71 72 80 boolean isHelpMenu; 81 82 private static final String base = "menu"; 83 private static int nameCounter = 0; 84 85 88 private static final long serialVersionUID = -8809584163345499784L; 89 90 98 public Menu() throws HeadlessException { 99 this("", false); 100 } 101 102 111 public Menu(String label) throws HeadlessException { 112 this(label, false); 113 } 114 115 131 public Menu(String label, boolean tearOff) throws HeadlessException { 132 super(label); 133 this.tearOff = tearOff; 134 } 135 136 140 String constructComponentName() { 141 synchronized (getClass()) { 142 return base + nameCounter++; 143 } 144 } 145 146 150 public void addNotify() { 151 synchronized (getTreeLock()) { 152 if (peer == null) 153 peer = Toolkit.getDefaultToolkit().createMenu(this); 154 int nitems = getItemCount(); 155 for (int i = 0 ; i < nitems ; i++) { 156 MenuItem mi = getItem(i); 157 mi.parent = this; 158 mi.addNotify(); 159 } 160 } 161 } 162 163 167 public void removeNotify() { 168 synchronized (getTreeLock()) { 169 int nitems = getItemCount(); 170 for (int i = 0 ; i < nitems ; i++) { 171 getItem(i).removeNotify(); 172 } 173 super.removeNotify(); 174 } 175 } 176 177 186 public boolean isTearOff() { 187 return tearOff; 188 } 189 190 195 public int getItemCount() { 196 return countItems(); 197 } 198 199 203 @Deprecated 204 public int countItems() { 205 return countItemsImpl(); 206 } 207 208 212 final int countItemsImpl() { 213 return items.size(); 214 } 215 216 221 public MenuItem getItem(int index) { 222 return getItemImpl(index); 223 } 224 225 229 final MenuItem getItemImpl(int index) { 230 return (MenuItem )items.elementAt(index); 231 } 232 233 243 public MenuItem add(MenuItem mi) { 244 synchronized (getTreeLock()) { 245 if (mi.parent != null) { 246 mi.parent.remove(mi); 247 } 248 items.addElement(mi); 249 mi.parent = this; 250 MenuPeer peer = (MenuPeer)this.peer; 251 if (peer != null) { 252 mi.addNotify(); 253 peer.addItem(mi); 254 } 255 return mi; 256 } 257 } 258 259 266 public void add(String label) { 267 add(new MenuItem (label)); 268 } 269 270 283 284 public void insert(MenuItem menuitem, int index) { 285 synchronized (getTreeLock()) { 286 if (index < 0) { 287 throw new IllegalArgumentException ("index less than zero."); 288 } 289 290 int nitems = getItemCount(); 291 Vector tempItems = new Vector (); 292 293 297 for (int i = index ; i < nitems; i++) { 298 tempItems.addElement(getItem(index)); 299 remove(index); 300 } 301 302 add(menuitem); 303 304 307 for (int i = 0; i < tempItems.size() ; i++) { 308 add((MenuItem )tempItems.elementAt(i)); 309 } 310 } 311 } 312 313 327 328 public void insert(String label, int index) { 329 insert(new MenuItem (label), index); 330 } 331 332 336 public void addSeparator() { 337 add("-"); 338 } 339 340 349 350 public void insertSeparator(int index) { 351 synchronized (getTreeLock()) { 352 if (index < 0) { 353 throw new IllegalArgumentException ("index less than zero."); 354 } 355 356 int nitems = getItemCount(); 357 Vector tempItems = new Vector (); 358 359 363 for (int i = index ; i < nitems; i++) { 364 tempItems.addElement(getItem(index)); 365 remove(index); 366 } 367 368 addSeparator(); 369 370 373 for (int i = 0; i < tempItems.size() ; i++) { 374 add((MenuItem )tempItems.elementAt(i)); 375 } 376 } 377 } 378 379 383 public void remove(int index) { 384 synchronized (getTreeLock()) { 385 MenuItem mi = getItem(index); 386 items.removeElementAt(index); 387 MenuPeer peer = (MenuPeer)this.peer; 388 if (peer != null) { 389 mi.removeNotify(); 390 mi.parent = null; 391 peer.delItem(index); 392 } 393 } 394 } 395 396 403 public void remove(MenuComponent item) { 404 synchronized (getTreeLock()) { 405 int index = items.indexOf(item); 406 if (index >= 0) { 407 remove(index); 408 } 409 } 410 } 411 412 416 public void removeAll() { 417 synchronized (getTreeLock()) { 418 int nitems = getItemCount(); 419 for (int i = nitems-1 ; i >= 0 ; i--) { 420 remove(i); 421 } 422 } 423 } 424 425 431 boolean handleShortcut(KeyEvent e) { 432 int nitems = getItemCount(); 433 for (int i = 0 ; i < nitems ; i++) { 434 MenuItem mi = getItem(i); 435 if (mi.handleShortcut(e)) { 436 return true; 437 } 438 } 439 return false; 440 } 441 442 MenuItem getShortcutMenuItem(MenuShortcut s) { 443 int nitems = getItemCount(); 444 for (int i = 0 ; i < nitems ; i++) { 445 MenuItem mi = getItem(i).getShortcutMenuItem(s); 446 if (mi != null) { 447 return mi; 448 } 449 } 450 return null; 451 } 452 453 synchronized Enumeration shortcuts() { 454 Vector shortcuts = new Vector (); 455 int nitems = getItemCount(); 456 for (int i = 0 ; i < nitems ; i++) { 457 MenuItem mi = getItem(i); 458 if (mi instanceof Menu ) { 459 Enumeration e = ((Menu )mi).shortcuts(); 460 while (e.hasMoreElements()) { 461 shortcuts.addElement(e.nextElement()); 462 } 463 } else { 464 MenuShortcut ms = mi.getShortcut(); 465 if (ms != null) { 466 shortcuts.addElement(ms); 467 } 468 } 469 } 470 return shortcuts.elements(); 471 } 472 473 void deleteShortcut(MenuShortcut s) { 474 int nitems = getItemCount(); 475 for (int i = 0 ; i < nitems ; i++) { 476 getItem(i).deleteShortcut(s); 477 } 478 } 479 480 481 484 485 490 private int menuSerializedDataVersion = 1; 491 492 499 private void writeObject(java.io.ObjectOutputStream s) 500 throws java.io.IOException 501 { 502 s.defaultWriteObject(); 503 } 504 505 516 private void readObject(ObjectInputStream s) 517 throws IOException , ClassNotFoundException , HeadlessException 518 { 519 s.defaultReadObject(); 521 for(int i = 0; i < items.size(); i++) { 522 MenuItem item = (MenuItem )items.elementAt(i); 523 item.parent = this; 524 } 525 } 526 527 536 public String paramString() { 537 String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu; 538 return super.paramString() + str; 539 } 540 541 544 private static native void initIDs(); 545 546 547 551 560 public AccessibleContext getAccessibleContext() { 561 if (accessibleContext == null) { 562 accessibleContext = new AccessibleAWTMenu(); 563 } 564 return accessibleContext; 565 } 566 567 570 int getAccessibleChildIndex(MenuComponent child) { 571 return items.indexOf(child); 572 } 573 574 584 protected class AccessibleAWTMenu extends AccessibleAWTMenuItem 585 { 586 589 private static final long serialVersionUID = 5228160894980069094L; 590 591 597 public AccessibleRole getAccessibleRole() { 598 return AccessibleRole.MENU; 599 } 600 601 } 603 } 604 | Popular Tags |