1 7 8 package javax.swing; 9 10 import java.awt.AWTEvent ; 11 import java.awt.Component ; 12 import java.awt.ComponentOrientation ; 13 import java.awt.Container ; 14 import java.awt.Dimension ; 15 import java.awt.Frame ; 16 import java.awt.Graphics ; 17 import java.awt.GraphicsConfiguration ; 18 import java.awt.GraphicsDevice ; 19 import java.awt.GraphicsEnvironment ; 20 import java.awt.Insets ; 21 import java.awt.Point ; 22 import java.awt.Polygon ; 23 import java.awt.Rectangle ; 24 import java.awt.Toolkit ; 25 import java.awt.event.*; 26 import java.beans.*; 27 28 import java.util.*; 29 30 import java.io.Serializable ; 31 import java.io.ObjectOutputStream ; 32 import java.io.ObjectInputStream ; 33 import java.io.IOException ; 34 35 import javax.swing.event.*; 36 import javax.swing.plaf.*; 37 import javax.swing.plaf.basic.*; 38 import javax.accessibility.*; 39 40 import java.lang.ref.WeakReference ; 41 42 81 public class JMenu extends JMenuItem implements Accessible,MenuElement 82 { 83 87 private static final String uiClassID = "MenuUI"; 88 89 92 private JPopupMenu popupMenu; 93 94 97 private ChangeListener menuChangeListener = null; 98 99 104 private MenuEvent menuEvent = null; 105 106 111 private static Hashtable listenerRegistry = null; 112 113 117 private int delay; 118 119 123 private Point customMenuLocation = null; 124 125 126 private static final boolean TRACE = false; private static final boolean VERBOSE = false; private static final boolean DEBUG = false; 130 133 public JMenu() { 134 this(""); 135 } 136 137 143 public JMenu(String s) { 144 super(s); 145 } 146 147 154 public JMenu(Action a) { 155 this(); 156 setAction(a); 157 } 158 159 166 public JMenu(String s, boolean b) { 167 this(s); 168 } 169 170 171 183 void initFocusability() { 184 } 185 186 191 public void updateUI() { 192 setUI((MenuItemUI)UIManager.getUI(this)); 193 194 if ( popupMenu != null ) 195 { 196 popupMenu.setUI((PopupMenuUI)UIManager.getUI(popupMenu)); 197 } 198 199 } 200 201 202 209 public String getUIClassID() { 210 return uiClassID; 211 } 212 213 218 230 public void setModel(ButtonModel newModel) { 231 ButtonModel oldModel = getModel(); 232 233 super.setModel(newModel); 234 235 if (oldModel != null && menuChangeListener != null) { 236 oldModel.removeChangeListener(menuChangeListener); 237 menuChangeListener = null; 238 } 239 240 model = newModel; 241 242 if (newModel != null) { 243 menuChangeListener = createMenuChangeListener(); 244 newModel.addChangeListener(menuChangeListener); 245 } 246 } 247 248 253 public boolean isSelected() { 254 return getModel().isSelected(); 255 } 256 257 267 public void setSelected(boolean b) { 268 ButtonModel model = getModel(); 269 boolean oldValue = model.isSelected(); 270 271 279 if (b != model.isSelected()) { 280 getModel().setSelected(b); 281 } 282 } 283 284 289 public boolean isPopupMenuVisible() { 290 ensurePopupMenuCreated(); 291 return popupMenu.isVisible(); 292 } 293 294 305 public void setPopupMenuVisible(boolean b) { 306 if (DEBUG) { 307 System.out.println("in JMenu.setPopupMenuVisible " + b); 308 } 310 311 boolean isVisible = isPopupMenuVisible(); 312 if (b != isVisible && (isEnabled() || !b)) { 313 ensurePopupMenuCreated(); 314 if ((b==true) && isShowing()) { 315 Point p = getCustomMenuLocation(); 317 if (p == null) { 318 p = getPopupMenuOrigin(); 319 } 320 getPopupMenu().show(this, p.x, p.y); 321 } else { 322 getPopupMenu().setVisible(false); 323 } 324 } 325 326 } 327 328 343 protected Point getPopupMenuOrigin() { 344 int x = 0; 345 int y = 0; 346 JPopupMenu pm = getPopupMenu(); 347 Dimension s = getSize(); 349 Dimension pmSize = pm.getSize(); 350 if (pmSize.width==0) { 353 pmSize = pm.getPreferredSize(); 354 } 355 Point position = getLocationOnScreen(); 356 Toolkit toolkit = Toolkit.getDefaultToolkit(); 357 GraphicsConfiguration gc = getGraphicsConfiguration(); 358 Rectangle screenBounds = new Rectangle (toolkit.getScreenSize()); 359 GraphicsEnvironment ge = 360 GraphicsEnvironment.getLocalGraphicsEnvironment(); 361 GraphicsDevice [] gd = ge.getScreenDevices(); 362 for(int i = 0; i < gd.length; i++) { 363 if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { 364 GraphicsConfiguration dgc = 365 gd[i].getDefaultConfiguration(); 366 if(dgc.getBounds().contains(position)) { 367 gc = dgc; 368 break; 369 } 370 } 371 } 372 373 374 if (gc != null) { 375 screenBounds = gc.getBounds(); 376 Insets screenInsets = toolkit.getScreenInsets(gc); 378 379 screenBounds.width -= 380 Math.abs(screenInsets.left + screenInsets.right); 381 screenBounds.height -= 382 Math.abs(screenInsets.top + screenInsets.bottom); 383 position.x -= Math.abs(screenInsets.left); 384 position.y -= Math.abs(screenInsets.top); 385 } 386 387 Container parent = getParent(); 388 if (parent instanceof JPopupMenu ) { 389 int xOffset = UIManager.getInt("Menu.submenuPopupOffsetX"); 391 int yOffset = UIManager.getInt("Menu.submenuPopupOffsetY"); 392 393 if( SwingUtilities.isLeftToRight(this) ) { 394 x = s.width + xOffset; if (position.x + x + pmSize.width >= screenBounds.width 397 + screenBounds.x && 398 screenBounds.width - s.width < 2*(position.x 400 - screenBounds.x)) { 401 402 x = 0 - xOffset - pmSize.width; 403 } 404 } else { 405 x = 0 - xOffset - pmSize.width; if (position.x + x < screenBounds.x && 408 screenBounds.width - s.width > 2*(position.x - 410 screenBounds.x)) { 411 412 x = s.width + xOffset; 413 } 414 } 415 y = yOffset; if (position.y + y + pmSize.height >= screenBounds.height 418 + screenBounds.y && 419 screenBounds.height - s.height < 2*(position.y 421 - screenBounds.y)) { 422 423 y = s.height - yOffset - pmSize.height; 424 } 425 } else { 426 int xOffset = UIManager.getInt("Menu.menuPopupOffsetX"); 428 int yOffset = UIManager.getInt("Menu.menuPopupOffsetY"); 429 430 if( SwingUtilities.isLeftToRight(this) ) { 431 x = xOffset; if (position.x + x + pmSize.width >= screenBounds.width 434 + screenBounds.x && 435 screenBounds.width - s.width < 2*(position.x 437 - screenBounds.x)) { 438 439 x = s.width - xOffset - pmSize.width; 440 } 441 } else { 442 x = s.width - xOffset - pmSize.width; if (position.x + x < screenBounds.x && 445 screenBounds.width - s.width > 2*(position.x 447 - screenBounds.x)) { 448 449 x = xOffset; 450 } 451 } 452 y = s.height + yOffset; if (position.y + y + pmSize.height >= screenBounds.height && 455 screenBounds.height - s.height < 2*(position.y 457 - screenBounds.y)) { 458 459 y = 0 - yOffset - pmSize.height; } 461 } 462 return new Point (x,y); 463 } 464 465 466 479 public int getDelay() { 480 return delay; 481 } 482 483 498 public void setDelay(int d) { 499 if (d < 0) 500 throw new IllegalArgumentException ("Delay must be a positive integer"); 501 502 delay = d; 503 } 504 505 510 protected WinListener popupListener; 511 512 private void ensurePopupMenuCreated() { 513 if (popupMenu == null) { 514 final JMenu thisMenu = this; 515 this.popupMenu = new JPopupMenu (); 516 popupMenu.setInvoker(this); 517 popupListener = createWinListener(popupMenu); 518 } 519 } 520 521 524 private Point getCustomMenuLocation() { 525 return customMenuLocation; 526 } 527 528 534 public void setMenuLocation(int x, int y) { 535 customMenuLocation = new Point (x, y); 536 if (popupMenu != null) 537 popupMenu.setLocation(x, y); 538 } 539 540 547 public JMenuItem add(JMenuItem menuItem) { 548 AccessibleContext ac = menuItem.getAccessibleContext(); 549 ac.setAccessibleParent(this); 550 ensurePopupMenuCreated(); 551 return popupMenu.add(menuItem); 552 } 553 554 561 public Component add(Component c) { 562 if (c instanceof JComponent ) { 563 AccessibleContext ac = ((JComponent ) c).getAccessibleContext(); 564 if (ac != null) { 565 ac.setAccessibleParent(this); 566 } 567 } 568 ensurePopupMenuCreated(); 569 popupMenu.add(c); 570 return c; 571 } 572 573 583 public Component add(Component c, int index) { 584 if (c instanceof JComponent ) { 585 AccessibleContext ac = ((JComponent ) c).getAccessibleContext(); 586 if (ac != null) { 587 ac.setAccessibleParent(this); 588 } 589 } 590 ensurePopupMenuCreated(); 591 popupMenu.add(c, index); 592 return c; 593 } 594 595 601 public JMenuItem add(String s) { 602 return add(new JMenuItem (s)); 603 } 604 605 618 public JMenuItem add(Action a) { 619 JMenuItem mi = createActionComponent(a); 620 mi.setAction(a); 621 add(mi); 622 return mi; 623 } 624 625 640 protected JMenuItem createActionComponent(Action a) { 641 JMenuItem mi = new JMenuItem ((String )a.getValue(Action.NAME), 642 (Icon )a.getValue(Action.SMALL_ICON)){ 643 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 644 PropertyChangeListener pcl = createActionChangeListener(this); 645 if (pcl == null) { 646 pcl = super.createActionPropertyChangeListener(a); 647 } 648 return pcl; 649 } 650 }; 651 mi.setHorizontalTextPosition(JButton.TRAILING); 652 mi.setVerticalTextPosition(JButton.CENTER); 653 mi.setEnabled(a.isEnabled()); 654 return mi; 655 } 656 657 667 protected PropertyChangeListener createActionChangeListener(JMenuItem b) { 668 return new ActionChangedListener(b); 669 } 670 671 private class ActionChangedListener implements PropertyChangeListener { 672 WeakReference menuItem; 673 674 ActionChangedListener(JMenuItem mi) { 675 super(); 676 setTarget(mi); 677 } 678 public void propertyChange(PropertyChangeEvent e) { 679 String propertyName = e.getPropertyName(); 680 JMenuItem mi = (JMenuItem )getTarget(); 681 if (mi == null) { 682 Action action = (Action )e.getSource(); 683 action.removePropertyChangeListener(this); 684 } else { 685 if (propertyName.equals(Action.NAME)) { 686 String text = (String ) e.getNewValue(); 687 mi.setText(text); 688 } else if (propertyName.equals("enabled")) { 689 Boolean enabledState = (Boolean ) e.getNewValue(); 690 mi.setEnabled(enabledState.booleanValue()); 691 } else if (propertyName.equals(Action.SMALL_ICON)) { 692 Icon icon = (Icon ) e.getNewValue(); 693 mi.setIcon(icon); 694 mi.invalidate(); 695 mi.repaint(); 696 } else if (propertyName.equals(Action.ACTION_COMMAND_KEY)) { 697 mi.setActionCommand((String )e.getNewValue()); 698 } 699 } 700 } 701 public void setTarget(JMenuItem b) { 702 menuItem = new WeakReference (b); 703 } 704 public JMenuItem getTarget() { 705 return (JMenuItem )menuItem.get(); 706 } 707 } 708 709 712 public void addSeparator() 713 { 714 ensurePopupMenuCreated(); 715 popupMenu.addSeparator(); 716 } 717 718 728 public void insert(String s, int pos) { 729 if (pos < 0) { 730 throw new IllegalArgumentException ("index less than zero."); 731 } 732 733 ensurePopupMenuCreated(); 734 popupMenu.insert(new JMenuItem (s), pos); 735 } 736 737 747 public JMenuItem insert(JMenuItem mi, int pos) { 748 if (pos < 0) { 749 throw new IllegalArgumentException ("index less than zero."); 750 } 751 AccessibleContext ac = mi.getAccessibleContext(); 752 ac.setAccessibleParent(this); 753 ensurePopupMenuCreated(); 754 popupMenu.insert(mi, pos); 755 return mi; 756 } 757 758 768 public JMenuItem insert(Action a, int pos) { 769 if (pos < 0) { 770 throw new IllegalArgumentException ("index less than zero."); 771 } 772 773 ensurePopupMenuCreated(); 774 JMenuItem mi = new JMenuItem ((String )a.getValue(Action.NAME), 775 (Icon )a.getValue(Action.SMALL_ICON)); 776 mi.setHorizontalTextPosition(JButton.TRAILING); 777 mi.setVerticalTextPosition(JButton.CENTER); 778 mi.setEnabled(a.isEnabled()); 779 mi.setAction(a); 780 popupMenu.insert(mi, pos); 781 return mi; 782 } 783 784 792 public void insertSeparator(int index) { 793 if (index < 0) { 794 throw new IllegalArgumentException ("index less than zero."); 795 } 796 797 ensurePopupMenuCreated(); 798 popupMenu.insert( new JPopupMenu.Separator (), index ); 799 } 800 801 813 public JMenuItem getItem(int pos) { 814 if (pos < 0) { 815 throw new IllegalArgumentException ("index less than zero."); 816 } 817 818 Component c = getMenuComponent(pos); 819 if (c instanceof JMenuItem ) { 820 JMenuItem mi = (JMenuItem ) c; 821 return mi; 822 } 823 824 return null; 826 } 827 828 835 public int getItemCount() { 836 return getMenuComponentCount(); 837 } 838 839 846 public boolean isTearOff() { 847 throw new Error ("boolean isTearOff() {} not yet implemented"); 848 } 849 850 856 public void remove(JMenuItem item) { 857 if (popupMenu != null) 858 popupMenu.remove(item); 859 } 860 861 869 public void remove(int pos) { 870 if (pos < 0) { 871 throw new IllegalArgumentException ("index less than zero."); 872 } 873 if (pos > getItemCount()) { 874 throw new IllegalArgumentException ("index greater than the number of items."); 875 } 876 if (popupMenu != null) 877 popupMenu.remove(pos); 878 } 879 880 885 public void remove(Component c) { 886 if (popupMenu != null) 887 popupMenu.remove(c); 888 } 889 890 893 public void removeAll() { 894 if (popupMenu != null) 895 popupMenu.removeAll(); 896 } 897 898 903 public int getMenuComponentCount() { 904 int componentCount = 0; 905 if (popupMenu != null) 906 componentCount = popupMenu.getComponentCount(); 907 return componentCount; 908 } 909 910 918 public Component getMenuComponent(int n) { 919 if (popupMenu != null) 920 return popupMenu.getComponent(n); 921 922 return null; 923 } 924 925 933 public Component [] getMenuComponents() { 934 if (popupMenu != null) 935 return popupMenu.getComponents(); 936 937 return new Component [0]; 938 } 939 940 948 public boolean isTopLevelMenu() { 949 if (getParent() instanceof JMenuBar ) 950 return true; 951 952 return false; 953 } 954 955 962 public boolean isMenuComponent(Component c) { 963 if (c == this) 965 return true; 966 if (c instanceof JPopupMenu ) { 968 JPopupMenu comp = (JPopupMenu ) c; 969 if (comp == this.getPopupMenu()) 970 return true; 971 } 972 int ncomponents = this.getMenuComponentCount(); 974 Component [] component = this.getMenuComponents(); 975 for (int i = 0 ; i < ncomponents ; i++) { 976 Component comp = component[i]; 977 if (comp == c) 979 return true; 980 982 if (comp instanceof JMenu ) { 984 JMenu subMenu = (JMenu ) comp; 985 if (subMenu.isMenuComponent(c)) 986 return true; 987 } 988 } 989 return false; 990 } 991 992 993 1001 private Point translateToPopupMenu(Point p) { 1002 return translateToPopupMenu(p.x, p.y); 1003 } 1004 1005 1013 private Point translateToPopupMenu(int x, int y) { 1014 int newX; 1015 int newY; 1016 1017 if (getParent() instanceof JPopupMenu ) { 1018 newX = x - getSize().width; 1019 newY = y; 1020 } else { 1021 newX = x; 1022 newY = y - getSize().height; 1023 } 1024 1025 return new Point (newX, newY); 1026 } 1027 1028 1032 public JPopupMenu getPopupMenu() { 1033 ensurePopupMenuCreated(); 1034 return popupMenu; 1035 } 1036 1037 1042 public void addMenuListener(MenuListener l) { 1043 listenerList.add(MenuListener.class, l); 1044 } 1045 1046 1051 public void removeMenuListener(MenuListener l) { 1052 listenerList.remove(MenuListener.class, l); 1053 } 1054 1055 1063 public MenuListener[] getMenuListeners() { 1064 return (MenuListener[])listenerList.getListeners(MenuListener.class); 1065 } 1066 1067 1075 protected void fireMenuSelected() { 1076 if (DEBUG) { 1077 System.out.println("In JMenu.fireMenuSelected"); 1078 } 1079 Object [] listeners = listenerList.getListenerList(); 1081 for (int i = listeners.length-2; i>=0; i-=2) { 1084 if (listeners[i]==MenuListener.class) { 1085 if (listeners[i+1]== null) { 1086 throw new Error (getText() +" has a NULL Listener!! " + i); 1087 } else { 1088 if (menuEvent == null) 1090 menuEvent = new MenuEvent(this); 1091 ((MenuListener)listeners[i+1]).menuSelected(menuEvent); 1092 } 1093 } 1094 } 1095 } 1096 1097 1105 protected void fireMenuDeselected() { 1106 if (DEBUG) { 1107 System.out.println("In JMenu.fireMenuDeselected"); 1108 } 1109 Object [] listeners = listenerList.getListenerList(); 1111 for (int i = listeners.length-2; i>=0; i-=2) { 1114 if (listeners[i]==MenuListener.class) { 1115 if (listeners[i+1]== null) { 1116 throw new Error (getText() +" has a NULL Listener!! " + i); 1117 } else { 1118 if (menuEvent == null) 1120 menuEvent = new MenuEvent(this); 1121 ((MenuListener)listeners[i+1]).menuDeselected(menuEvent); 1122 } 1123 } 1124 } 1125 } 1126 1127 1135 protected void fireMenuCanceled() { 1136 if (DEBUG) { 1137 System.out.println("In JMenu.fireMenuCanceled"); 1138 } 1139 Object [] listeners = listenerList.getListenerList(); 1141 for (int i = listeners.length-2; i>=0; i-=2) { 1144 if (listeners[i]==MenuListener.class) { 1145 if (listeners[i+1]== null) { 1146 throw new Error (getText() +" has a NULL Listener!! " 1147 + i); 1148 } else { 1149 if (menuEvent == null) 1151 menuEvent = new MenuEvent(this); 1152 ((MenuListener)listeners[i+1]).menuCanceled(menuEvent); 1153 } 1154 } 1155 } 1156 } 1157 1158 1172 protected void configurePropertiesFromAction(Action a) { 1173 configurePropertiesFromAction(a, null); 1174 } 1175 1176 class MenuChangeListener implements ChangeListener, Serializable { 1177 boolean isSelected = false; 1178 public void stateChanged(ChangeEvent e) { 1179 ButtonModel model = (ButtonModel ) e.getSource(); 1180 boolean modelSelected = model.isSelected(); 1181 1182 if (modelSelected != isSelected) { 1183 if (modelSelected == true) { 1184 fireMenuSelected(); 1185 } else { 1186 fireMenuDeselected(); 1187 } 1188 isSelected = modelSelected; 1189 } 1190 } 1191 } 1192 1193 private ChangeListener createMenuChangeListener() { 1194 return new MenuChangeListener(); 1195 } 1196 1197 1198 1206 protected WinListener createWinListener(JPopupMenu p) { 1207 return new WinListener(p); 1208 } 1209 1210 1223 protected class WinListener extends WindowAdapter implements Serializable { 1224 JPopupMenu popupMenu; 1225 1228 public WinListener(JPopupMenu p) { 1229 this.popupMenu = p; 1230 } 1231 1234 public void windowClosing(WindowEvent e) { 1235 setSelected(false); 1236 } 1237 } 1238 1239 1247 public void menuSelectionChanged(boolean isIncluded) { 1248 if (DEBUG) { 1249 System.out.println("In JMenu.menuSelectionChanged to " + isIncluded); 1250 } 1251 setSelected(isIncluded); 1252 } 1253 1254 1264 public MenuElement [] getSubElements() { 1265 if(popupMenu == null) 1266 return new MenuElement [0]; 1267 else { 1268 MenuElement result[] = new MenuElement [1]; 1269 result[0] = popupMenu; 1270 return result; 1271 } 1272 } 1273 1274 1275 1282 public Component getComponent() { 1283 return this; 1284 } 1285 1286 1287 1299 public void applyComponentOrientation(ComponentOrientation o) { 1300 super.applyComponentOrientation(o); 1301 1302 if ( popupMenu != null ) { 1303 int ncomponents = getMenuComponentCount(); 1304 for (int i = 0 ; i < ncomponents ; ++i) { 1305 getMenuComponent(i).applyComponentOrientation(o); 1306 } 1307 popupMenu.setComponentOrientation(o); 1308 } 1309 } 1310 1311 public void setComponentOrientation(ComponentOrientation o) { 1312 super.setComponentOrientation(o); 1313 if ( popupMenu != null ) { 1314 popupMenu.setComponentOrientation(o); 1315 } 1316 } 1317 1318 1332 public void setAccelerator(KeyStroke keyStroke) { 1333 throw new Error ("setAccelerator() is not defined for JMenu. Use setMnemonic() instead."); 1334 } 1335 1336 1341 protected void processKeyEvent(KeyEvent evt) { 1342 MenuSelectionManager.defaultManager().processKeyEvent(evt); 1343 if (evt.isConsumed()) 1344 return; 1345 1346 super.processKeyEvent(evt); 1347 } 1348 1349 1355 public void doClick(int pressTime) { 1356 MenuElement me[] = buildMenuElementArray(this); 1357 MenuSelectionManager.defaultManager().setSelectedPath(me); 1358 } 1359 1360 1366 private MenuElement [] buildMenuElementArray(JMenu leaf) { 1367 Vector elements = new Vector(); 1368 Component current = leaf.getPopupMenu(); 1369 JPopupMenu pop; 1370 JMenu menu; 1371 JMenuBar bar; 1372 1373 while (true) { 1374 if (current instanceof JPopupMenu ) { 1375 pop = (JPopupMenu ) current; 1376 elements.insertElementAt(pop, 0); 1377 current = pop.getInvoker(); 1378 } else if (current instanceof JMenu ) { 1379 menu = (JMenu ) current; 1380 elements.insertElementAt(menu, 0); 1381 current = menu.getParent(); 1382 } else if (current instanceof JMenuBar ) { 1383 bar = (JMenuBar ) current; 1384 elements.insertElementAt(bar, 0); 1385 MenuElement me[] = new MenuElement [elements.size()]; 1386 elements.copyInto(me); 1387 return me; 1388 } 1389 } 1390 } 1391 1392 1393 1398 private void writeObject(ObjectOutputStream s) throws IOException { 1399 s.defaultWriteObject(); 1400 if (getUIClassID().equals(uiClassID)) { 1401 byte count = JComponent.getWriteObjCounter(this); 1402 JComponent.setWriteObjCounter(this, --count); 1403 if (count == 0 && ui != null) { 1404 ui.installUI(this); 1405 } 1406 } 1407 } 1408 1409 1410 1419 protected String paramString() { 1420 return super.paramString(); 1421 } 1422 1423 1424 1428 1437 public AccessibleContext getAccessibleContext() { 1438 if (accessibleContext == null) { 1439 accessibleContext = new AccessibleJMenu(); 1440 } 1441 return accessibleContext; 1442 } 1443 1444 1458 protected class AccessibleJMenu extends AccessibleJMenuItem 1459 implements AccessibleSelection { 1460 1461 1468 public int getAccessibleChildrenCount() { 1469 Component [] children = getMenuComponents(); 1470 int count = 0; 1471 for (int j = 0; j < children.length; j++) { 1472 if (children[j] instanceof Accessible) { 1473 count++; 1474 } 1475 } 1476 return count; 1477 } 1478 1479 1485 public Accessible getAccessibleChild(int i) { 1486 Component [] children = getMenuComponents(); 1487 int count = 0; 1488 for (int j = 0; j < children.length; j++) { 1489 if (children[j] instanceof Accessible) { 1490 if (count == i) { 1491 if (children[j] instanceof JComponent ) { 1492 AccessibleContext ac = ((Accessible) children[j]).getAccessibleContext(); 1497 ac.setAccessibleParent(JMenu.this); 1498 } 1499 return (Accessible) children[j]; 1500 } else { 1501 count++; 1502 } 1503 } 1504 } 1505 return null; 1506 } 1507 1508 1515 public AccessibleRole getAccessibleRole() { 1516 return AccessibleRole.MENU; 1517 } 1518 1519 1527 public AccessibleSelection getAccessibleSelection() { 1528 return this; 1529 } 1530 1531 1536 public int getAccessibleSelectionCount() { 1537 MenuElement me[] = 1538 MenuSelectionManager.defaultManager().getSelectedPath(); 1539 if (me != null) { 1540 for (int i = 0; i < me.length; i++) { 1541 if (me[i] == JMenu.this) { if (i+1 < me.length) { 1543 return 1; 1544 } 1545 } 1546 } 1547 } 1548 return 0; 1549 } 1550 1551 1557 public Accessible getAccessibleSelection(int i) { 1558 if (i < 0 || i >= getItemCount()) { 1560 return null; 1561 } 1562 MenuElement me[] = 1563 MenuSelectionManager.defaultManager().getSelectedPath(); 1564 if (me != null) { 1565 for (int j = 0; j < me.length; j++) { 1566 if (me[j] == JMenu.this) { while (++j < me.length) { 1570 if (me[j] instanceof JMenuItem ) { 1571 return (Accessible) me[j]; 1572 } 1573 } 1574 } 1575 } 1576 } 1577 return null; 1578 } 1579 1580 1588 public boolean isAccessibleChildSelected(int i) { 1589 MenuElement me[] = 1591 MenuSelectionManager.defaultManager().getSelectedPath(); 1592 if (me != null) { 1593 JMenuItem mi = JMenu.this.getItem(i); 1594 for (int j = 0; j < me.length; j++) { 1595 if (me[j] == mi) { 1596 return true; 1597 } 1598 } 1599 } 1600 return false; 1601 } 1602 1603 1604 1615 public void addAccessibleSelection(int i) { 1616 if (i < 0 || i >= getItemCount()) { 1617 return; 1618 } 1619 JMenuItem mi = getItem(i); 1620 if (mi != null) { 1621 if (mi instanceof JMenu ) { 1622 MenuElement me[] = buildMenuElementArray((JMenu ) mi); 1623 MenuSelectionManager.defaultManager().setSelectedPath(me); 1624 } else { 1625 mi.doClick(); 1626 MenuSelectionManager.defaultManager().setSelectedPath(null); 1627 } 1628 } 1629 } 1630 1631 1638 public void removeAccessibleSelection(int i) { 1639 if (i < 0 || i >= getItemCount()) { 1640 return; 1641 } 1642 JMenuItem mi = getItem(i); 1643 if (mi != null && mi instanceof JMenu ) { 1644 if (((JMenu ) mi).isSelected()) { 1645 MenuElement old[] = 1646 MenuSelectionManager.defaultManager().getSelectedPath(); 1647 MenuElement me[] = new MenuElement [old.length-2]; 1648 for (int j = 0; j < old.length -2; j++) { 1649 me[j] = old[j]; 1650 } 1651 MenuSelectionManager.defaultManager().setSelectedPath(me); 1652 } 1653 } 1654 } 1655 1656 1660 public void clearAccessibleSelection() { 1661 MenuElement old[] = 1664 MenuSelectionManager.defaultManager().getSelectedPath(); 1665 if (old != null) { 1666 for (int j = 0; j < old.length; j++) { 1667 if (old[j] == JMenu.this) { MenuElement me[] = new MenuElement [j+1]; 1669 System.arraycopy(old, 0, me, 0, j); 1670 me[j] = JMenu.this.getPopupMenu(); 1671 MenuSelectionManager.defaultManager().setSelectedPath(me); 1672 } 1673 } 1674 } 1675 } 1676 1677 1682 public void selectAllAccessibleSelection() { 1683 } 1684 } 1686} 1687 1688 | Popular Tags |