1 7 8 package javax.swing; 9 10 import java.awt.Component ; 11 import java.awt.Dimension ; 12 import java.awt.Graphics ; 13 import java.awt.Insets ; 14 import java.awt.Point ; 15 import java.awt.Rectangle ; 16 import java.awt.event.*; 17 import java.util.Vector ; 18 import java.util.Enumeration ; 19 20 import java.io.Serializable ; 21 import java.io.ObjectOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.IOException ; 24 25 import javax.swing.event.*; 26 import javax.swing.border.Border ; 27 import javax.swing.plaf.*; 28 import javax.accessibility.*; 29 30 62 public class JMenuBar extends JComponent implements Accessible,MenuElement 63 { 64 68 private static final String uiClassID = "MenuBarUI"; 69 70 73 private transient SingleSelectionModel selectionModel; 74 75 private boolean paintBorder = true; 76 private Insets margin = null; 77 78 79 private static final boolean TRACE = false; private static final boolean VERBOSE = false; private static final boolean DEBUG = false; 83 86 public JMenuBar() { 87 super(); 88 setFocusTraversalKeysEnabled(false); 89 setSelectionModel(new DefaultSingleSelectionModel ()); 90 updateUI(); 91 } 92 93 97 public MenuBarUI getUI() { 98 return (MenuBarUI)ui; 99 } 100 101 112 public void setUI(MenuBarUI ui) { 113 super.setUI(ui); 114 } 115 116 121 public void updateUI() { 122 setUI((MenuBarUI)UIManager.getUI(this)); 123 } 124 125 126 133 public String getUIClassID() { 134 return uiClassID; 135 } 136 137 138 144 public SingleSelectionModel getSelectionModel() { 145 return selectionModel; 146 } 147 148 157 public void setSelectionModel(SingleSelectionModel model) { 158 SingleSelectionModel oldValue = selectionModel; 159 this.selectionModel = model; 160 firePropertyChange("selectionModel", oldValue, selectionModel); 161 } 162 163 164 170 public JMenu add(JMenu c) { 171 super.add(c); 172 return c; 173 } 174 175 184 public JMenu getMenu(int index) { 185 Component c = getComponentAtIndex(index); 186 if (c instanceof JMenu ) 187 return (JMenu ) c; 188 return null; 189 } 190 191 196 public int getMenuCount() { 197 return getComponentCount(); 198 } 199 200 207 public void setHelpMenu(JMenu menu) { 208 throw new Error ("setHelpMenu() not yet implemented."); 209 } 210 211 217 public JMenu getHelpMenu() { 218 throw new Error ("getHelpMenu() not yet implemented."); 219 } 220 221 229 @Deprecated 230 public Component getComponentAtIndex(int i) { 231 return getComponent(i); 232 } 233 234 241 public int getComponentIndex(Component c) { 242 int ncomponents = this.getComponentCount(); 243 Component [] component = this.getComponents(); 244 for (int i = 0 ; i < ncomponents ; i++) { 245 Component comp = component[i]; 246 if (comp == c) 247 return i; 248 } 249 return -1; 250 } 251 252 258 public void setSelected(Component sel) { 259 SingleSelectionModel model = getSelectionModel(); 260 int index = getComponentIndex(sel); 261 model.setSelectedIndex(index); 262 } 263 264 269 public boolean isSelected() { 270 return selectionModel.isSelected(); 271 } 272 273 278 public boolean isBorderPainted() { 279 return paintBorder; 280 } 281 282 293 public void setBorderPainted(boolean b) { 294 boolean oldValue = paintBorder; 295 paintBorder = b; 296 firePropertyChange("borderPainted", oldValue, paintBorder); 297 if (b != oldValue) { 298 revalidate(); 299 repaint(); 300 } 301 } 302 303 311 protected void paintBorder(Graphics g) { 312 if (isBorderPainted()) { 313 super.paintBorder(g); 314 } 315 } 316 317 329 public void setMargin(Insets m) { 330 Insets old = margin; 331 this.margin = m; 332 firePropertyChange("margin", old, m); 333 if (old == null || !old.equals(m)) { 334 revalidate(); 335 repaint(); 336 } 337 } 338 339 347 public Insets getMargin() { 348 if(margin == null) { 349 return new Insets (0,0,0,0); 350 } else { 351 return margin; 352 } 353 } 354 355 356 361 public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) { 362 } 363 364 369 public void processKeyEvent(KeyEvent e,MenuElement path[],MenuSelectionManager manager) { 370 } 371 372 377 public void menuSelectionChanged(boolean isIncluded) { 378 } 379 380 388 public MenuElement [] getSubElements() { 389 MenuElement result[]; 390 Vector tmp = new Vector (); 391 int c = getComponentCount(); 392 int i; 393 Component m; 394 395 for(i=0 ; i < c ; i++) { 396 m = getComponent(i); 397 if(m instanceof MenuElement ) 398 tmp.addElement(m); 399 } 400 401 result = new MenuElement [tmp.size()]; 402 for(i=0,c=tmp.size() ; i < c ; i++) 403 result[i] = (MenuElement ) tmp.elementAt(i); 404 return result; 405 } 406 407 413 public Component getComponent() { 414 return this; 415 } 416 417 418 428 protected String paramString() { 429 String paintBorderString = (paintBorder ? 430 "true" : "false"); 431 String marginString = (margin != null ? 432 margin.toString() : ""); 433 434 return super.paramString() + 435 ",margin=" + marginString + 436 ",paintBorder=" + paintBorderString; 437 } 438 439 443 452 public AccessibleContext getAccessibleContext() { 453 if (accessibleContext == null) { 454 accessibleContext = new AccessibleJMenuBar(); 455 } 456 return accessibleContext; 457 } 458 459 474 protected class AccessibleJMenuBar extends AccessibleJComponent 475 implements AccessibleSelection { 476 477 483 public AccessibleStateSet getAccessibleStateSet() { 484 AccessibleStateSet states = super.getAccessibleStateSet(); 485 return states; 486 } 487 488 494 public AccessibleRole getAccessibleRole() { 495 return AccessibleRole.MENU_BAR; 496 } 497 498 506 public AccessibleSelection getAccessibleSelection() { 507 return this; 508 } 509 510 515 public int getAccessibleSelectionCount() { 516 if (isSelected()) { 517 return 1; 518 } else { 519 return 0; 520 } 521 } 522 523 527 public Accessible getAccessibleSelection(int i) { 528 if (isSelected()) { 529 if (i != 0) { return null; 531 } 532 int j = getSelectionModel().getSelectedIndex(); 533 if (getComponentAtIndex(j) instanceof Accessible) { 534 return (Accessible) getComponentAtIndex(j); 535 } 536 } 537 return null; 538 } 539 540 547 public boolean isAccessibleChildSelected(int i) { 548 return (i == getSelectionModel().getSelectedIndex()); 549 } 550 551 560 public void addAccessibleSelection(int i) { 561 int j = getSelectionModel().getSelectedIndex(); 563 if (i == j) { 564 return; 565 } 566 if (j >= 0 && j < getMenuCount()) { 567 JMenu menu = getMenu(j); 568 if (menu != null) { 569 MenuSelectionManager.defaultManager().setSelectedPath(null); 570 } 572 } 573 getSelectionModel().setSelectedIndex(i); 575 JMenu menu = getMenu(i); 576 if (menu != null) { 577 MenuElement me[] = new MenuElement [3]; 578 me[0] = JMenuBar.this; 579 me[1] = menu; 580 me[2] = menu.getPopupMenu(); 581 MenuSelectionManager.defaultManager().setSelectedPath(me); 582 } 584 } 585 586 593 public void removeAccessibleSelection(int i) { 594 if (i >= 0 && i < getMenuCount()) { 595 JMenu menu = getMenu(i); 596 if (menu != null) { 597 MenuSelectionManager.defaultManager().setSelectedPath(null); 598 } 600 getSelectionModel().setSelectedIndex(-1); 601 } 602 } 603 604 608 public void clearAccessibleSelection() { 609 int i = getSelectionModel().getSelectedIndex(); 610 if (i >= 0 && i < getMenuCount()) { 611 JMenu menu = getMenu(i); 612 if (menu != null) { 613 MenuSelectionManager.defaultManager().setSelectedPath(null); 614 } 616 } 617 getSelectionModel().setSelectedIndex(-1); 618 } 619 620 625 public void selectAllAccessibleSelection() { 626 } 627 } 629 630 633 protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, 634 int condition, boolean pressed) { 635 boolean retValue = super.processKeyBinding(ks, e, condition, pressed); 637 if (!retValue) { 638 MenuElement [] subElements = getSubElements(); 639 for (int i=0; i<subElements.length; i++) { 640 if (processBindingForKeyStrokeRecursive( 641 subElements[i], ks, e, condition, pressed)) { 642 return true; 643 } 644 } 645 } 646 return retValue; 647 } 648 649 static boolean processBindingForKeyStrokeRecursive(MenuElement elem, 650 KeyStroke ks, KeyEvent e, int condition, boolean pressed) { 651 if (elem == null) { 652 return false; 653 } 654 655 Component c = elem.getComponent(); 656 if (c != null && c instanceof JComponent && 657 ((JComponent )c).processKeyBinding(ks, e, condition, pressed)) { 658 659 return true; 660 } 661 662 MenuElement [] subElements = elem.getSubElements(); 663 for(int i=0; i<subElements.length; i++) { 664 if (processBindingForKeyStrokeRecursive(subElements[i], ks, e, 665 condition, pressed)) { 666 return true; 667 } 669 } 670 return false; 671 } 672 673 677 public void addNotify() { 678 super.addNotify(); 679 KeyboardManager.getCurrentManager().registerMenuBar(this); 680 } 681 682 686 public void removeNotify() { 687 super.removeNotify(); 688 KeyboardManager.getCurrentManager().unregisterMenuBar(this); 689 } 690 691 692 private void writeObject(ObjectOutputStream s) throws IOException { 693 s.defaultWriteObject(); 694 if (getUIClassID().equals(uiClassID)) { 695 byte count = JComponent.getWriteObjCounter(this); 696 JComponent.setWriteObjCounter(this, --count); 697 if (count == 0 && ui != null) { 698 ui.installUI(this); 699 } 700 } 701 702 Object [] kvData = new Object [4]; 703 int n = 0; 704 705 if (selectionModel instanceof Serializable ) { 706 kvData[n++] = "selectionModel"; 707 kvData[n++] = selectionModel; 708 } 709 710 s.writeObject(kvData); 711 } 712 713 714 718 private void readObject(ObjectInputStream s) throws IOException , ClassNotFoundException 719 { 720 s.defaultReadObject(); 721 Object [] kvData = (Object [])(s.readObject()); 722 723 for(int i = 0; i < kvData.length; i += 2) { 724 if (kvData[i] == null) { 725 break; 726 } 727 else if (kvData[i].equals("selectionModel")) { 728 selectionModel = (SingleSelectionModel )kvData[i + 1]; 729 } 730 } 731 732 } 733 } 734 735 | Popular Tags |