1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.io.IOException ; 13 import java.io.ObjectInputStream ; 14 import java.io.ObjectOutputStream ; 15 import java.io.Serializable ; 16 import java.beans.*; 17 18 import java.util.Locale ; 19 import java.util.Vector ; 20 import java.util.Hashtable ; 21 import javax.accessibility.*; 22 import javax.swing.plaf.PopupMenuUI ; 23 import javax.swing.plaf.ComponentUI ; 24 import javax.swing.plaf.basic.BasicComboPopup ; 25 import javax.swing.event.*; 26 27 import java.applet.Applet ; 28 29 61 public class JPopupMenu extends JComponent implements Accessible,MenuElement { 62 63 67 private static final String uiClassID = "PopupMenuUI"; 68 69 72 private static final Object defaultLWPopupEnabledKey = 73 new StringBuffer ("JPopupMenu.defaultLWPopupEnabledKey"); 74 75 76 static boolean popupPostionFixDisabled = false; 77 78 static { 79 popupPostionFixDisabled = java.security.AccessController.doPrivileged( 80 new sun.security.action.GetPropertyAction( 81 "javax.swing.adjustPopupLocationToFit","")).equals("false"); 82 83 } 84 85 transient Component invoker; 86 transient Popup popup; 87 transient Frame frame; 88 private int desiredLocationX,desiredLocationY; 89 90 private String label = null; 91 private boolean paintBorder = true; 92 private Insets margin = null; 93 94 97 private boolean lightWeightPopup = true; 98 99 102 private SingleSelectionModel selectionModel; 103 104 107 private static final Object classLock = new Object (); 108 109 110 private static final boolean TRACE = false; private static final boolean VERBOSE = false; private static final boolean DEBUG = false; 114 123 public static void setDefaultLightWeightPopupEnabled(boolean aFlag) { 124 SwingUtilities.appContextPut(defaultLWPopupEnabledKey, 125 Boolean.valueOf(aFlag)); 126 } 127 128 137 public static boolean getDefaultLightWeightPopupEnabled() { 138 Boolean b = (Boolean ) 139 SwingUtilities.appContextGet(defaultLWPopupEnabledKey); 140 if (b == null) { 141 SwingUtilities.appContextPut(defaultLWPopupEnabledKey, 142 Boolean.TRUE); 143 return true; 144 } 145 return b.booleanValue(); 146 } 147 148 151 public JPopupMenu() { 152 this(null); 153 } 154 155 161 public JPopupMenu(String label) { 162 this.label = label; 163 lightWeightPopup = getDefaultLightWeightPopupEnabled(); 164 setSelectionModel(new DefaultSingleSelectionModel ()); 165 enableEvents(AWTEvent.MOUSE_EVENT_MASK); 166 setFocusTraversalKeysEnabled(false); 167 updateUI(); 168 } 169 170 171 172 177 public PopupMenuUI getUI() { 178 return (PopupMenuUI )ui; 179 } 180 181 192 public void setUI(PopupMenuUI ui) { 193 super.setUI(ui); 194 } 195 196 201 public void updateUI() { 202 setUI((PopupMenuUI )UIManager.getUI(this)); 203 } 204 205 206 213 public String getUIClassID() { 214 return uiClassID; 215 } 216 217 protected void processFocusEvent(FocusEvent evt) { 218 super.processFocusEvent(evt); 219 } 220 221 226 protected void processKeyEvent(KeyEvent evt) { 227 MenuSelectionManager.defaultManager().processKeyEvent(evt); 228 if (evt.isConsumed()) { 229 return; 230 } 231 super.processKeyEvent(evt); 232 } 233 234 235 241 public SingleSelectionModel getSelectionModel() { 242 return selectionModel; 243 } 244 245 254 public void setSelectionModel(SingleSelectionModel model) { 255 selectionModel = model; 256 } 257 258 264 public JMenuItem add(JMenuItem menuItem) { 265 super.add(menuItem); 266 return menuItem; 267 } 268 269 275 public JMenuItem add(String s) { 276 return add(new JMenuItem (s)); 277 } 278 279 293 public JMenuItem add(Action a) { 294 JMenuItem mi = createActionComponent(a); 295 mi.setAction(a); 296 add(mi); 297 return mi; 298 } 299 300 307 Point adjustPopupLocationToFitScreen(int xposition, int yposition) { 308 Point p = new Point(xposition, yposition); 309 310 if(popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) 311 return p; 312 313 Toolkit toolkit = Toolkit.getDefaultToolkit(); 314 Rectangle screenBounds; 315 Insets screenInsets; 316 GraphicsConfiguration gc = null; 317 GraphicsEnvironment ge = 320 GraphicsEnvironment.getLocalGraphicsEnvironment(); 321 GraphicsDevice[] gd = ge.getScreenDevices(); 322 for(int i = 0; i < gd.length; i++) { 323 if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) { 324 GraphicsConfiguration dgc = 325 gd[i].getDefaultConfiguration(); 326 if(dgc.getBounds().contains(p)) { 327 gc = dgc; 328 break; 329 } 330 } 331 } 332 333 if(gc == null && getInvoker() != null) { 335 gc = getInvoker().getGraphicsConfiguration(); 336 } 337 338 if(gc != null) { 339 screenInsets = toolkit.getScreenInsets(gc); 342 screenBounds = gc.getBounds(); 343 } else { 344 screenInsets = new Insets(0, 0, 0, 0); 347 screenBounds = new Rectangle(toolkit.getScreenSize()); 348 } 349 350 int scrWidth = screenBounds.width - 351 Math.abs(screenInsets.left+screenInsets.right); 352 int scrHeight = screenBounds.height - 353 Math.abs(screenInsets.top+screenInsets.bottom); 354 355 Dimension size; 356 357 size = JPopupMenu.this.getPreferredSize(); 358 359 if( (p.x + size.width) > screenBounds.x + scrWidth ) 360 p.x = screenBounds.x + scrWidth - size.width; 361 362 if( (p.y + size.height) > screenBounds.y + scrHeight) 363 p.y = screenBounds.y + scrHeight - size.height; 364 365 368 if( p.x < screenBounds.x ) 369 p.x = screenBounds.x ; 370 if( p.y < screenBounds.y ) 371 p.y = screenBounds.y; 372 373 return p; 374 } 375 376 377 392 protected JMenuItem createActionComponent(Action a) { 393 JMenuItem mi = new JMenuItem ((String )a.getValue(Action.NAME), 394 (Icon )a.getValue(Action.SMALL_ICON)){ 395 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 396 PropertyChangeListener pcl = createActionChangeListener(this); 397 if (pcl == null) { 398 pcl = super.createActionPropertyChangeListener(a); 399 } 400 return pcl; 401 } 402 }; 403 mi.setHorizontalTextPosition(JButton.TRAILING); 404 mi.setVerticalTextPosition(JButton.CENTER); 405 mi.setEnabled(a.isEnabled()); 406 return mi; 407 } 408 409 418 protected PropertyChangeListener createActionChangeListener(JMenuItem b) { 419 return new ActionChangedListener(b); 420 } 421 422 private class ActionChangedListener implements PropertyChangeListener, Serializable { 423 private JMenuItem menuItem; 424 425 public ActionChangedListener(JMenuItem mi) { 426 super(); 427 setTarget(mi); 428 } 429 public void propertyChange(PropertyChangeEvent e) { 430 String propertyName = e.getPropertyName(); 431 if (e.getPropertyName().equals(Action.NAME)) { 432 String text = (String ) e.getNewValue(); 433 menuItem.setText(text); 434 } else if (propertyName.equals("enabled")) { 435 Boolean enabledState = (Boolean ) e.getNewValue(); 436 menuItem.setEnabled(enabledState.booleanValue()); 437 } else if (e.getPropertyName().equals(Action.SMALL_ICON)) { 438 Icon icon = (Icon ) e.getNewValue(); 439 menuItem.setIcon(icon); 440 menuItem.invalidate(); 441 menuItem.repaint(); 442 } 443 } 444 public void setTarget(JMenuItem b) { 445 this.menuItem = b; 446 } 447 } 448 449 458 public void remove(int pos) { 459 if (pos < 0) { 460 throw new IllegalArgumentException ("index less than zero."); 461 } 462 if (pos > getComponentCount() -1) { 463 throw new IllegalArgumentException ("index greater than the number of items."); 464 } 465 super.remove(pos); 466 } 467 468 489 public void setLightWeightPopupEnabled(boolean aFlag) { 490 lightWeightPopup = aFlag; 493 } 494 495 501 public boolean isLightWeightPopupEnabled() { 502 return lightWeightPopup; 503 } 504 505 511 public String getLabel() { 512 return label; 513 } 514 515 526 public void setLabel(String label) { 527 String oldValue = this.label; 528 this.label = label; 529 firePropertyChange("label", oldValue, label); 530 if (accessibleContext != null) { 531 accessibleContext.firePropertyChange( 532 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 533 oldValue, label); 534 } 535 invalidate(); 536 repaint(); 537 } 538 539 542 public void addSeparator() { 543 add( new JPopupMenu.Separator () ); 544 } 545 546 556 public void insert(Action a, int index) { 557 JMenuItem mi = createActionComponent(a); 558 mi.setAction(a); 559 insert(mi, index); 560 } 561 562 571 public void insert(Component component, int index) { 572 if (index < 0) { 573 throw new IllegalArgumentException ("index less than zero."); 574 } 575 576 int nitems = getComponentCount(); 577 Vector tempItems = new Vector (); 579 580 584 for (int i = index ; i < nitems; i++) { 585 tempItems.addElement(getComponent(index)); 586 remove(index); 587 } 588 589 add(component); 590 591 594 for (int i = 0; i < tempItems.size() ; i++) { 595 add((Component)tempItems.elementAt(i)); 596 } 597 } 598 599 604 public void addPopupMenuListener(PopupMenuListener l) { 605 listenerList.add(PopupMenuListener.class,l); 606 } 607 608 613 public void removePopupMenuListener(PopupMenuListener l) { 614 listenerList.remove(PopupMenuListener.class,l); 615 } 616 617 625 public PopupMenuListener[] getPopupMenuListeners() { 626 return (PopupMenuListener[])listenerList.getListeners( 627 PopupMenuListener.class); 628 } 629 630 636 public void addMenuKeyListener(MenuKeyListener l) { 637 listenerList.add(MenuKeyListener.class, l); 638 } 639 640 646 public void removeMenuKeyListener(MenuKeyListener l) { 647 listenerList.remove(MenuKeyListener.class, l); 648 } 649 650 658 public MenuKeyListener[] getMenuKeyListeners() { 659 return (MenuKeyListener[])listenerList.getListeners( 660 MenuKeyListener.class); 661 } 662 663 667 protected void firePopupMenuWillBecomeVisible() { 668 Object [] listeners = listenerList.getListenerList(); 669 PopupMenuEvent e=null; 670 for (int i = listeners.length-2; i>=0; i-=2) { 671 if (listeners[i]==PopupMenuListener.class) { 672 if (e == null) 673 e = new PopupMenuEvent(this); 674 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e); 675 } 676 } 677 } 678 679 683 protected void firePopupMenuWillBecomeInvisible() { 684 Object [] listeners = listenerList.getListenerList(); 685 PopupMenuEvent e=null; 686 for (int i = listeners.length-2; i>=0; i-=2) { 687 if (listeners[i]==PopupMenuListener.class) { 688 if (e == null) 689 e = new PopupMenuEvent(this); 690 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e); 691 } 692 } 693 } 694 695 699 protected void firePopupMenuCanceled() { 700 Object [] listeners = listenerList.getListenerList(); 701 PopupMenuEvent e=null; 702 for (int i = listeners.length-2; i>=0; i-=2) { 703 if (listeners[i]==PopupMenuListener.class) { 704 if (e == null) 705 e = new PopupMenuEvent(this); 706 ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e); 707 } 708 } 709 } 710 711 716 boolean alwaysOnTop() { 718 return true; 719 } 720 721 725 public void pack() { 726 if(popup != null) { 727 Dimension pref = getPreferredSize(); 728 729 if (pref == null || pref.width != getWidth() || 730 pref.height != getHeight()) { 731 popup = getPopup(); 732 } else { 733 validate(); 734 } 735 } 736 } 737 738 747 public void setVisible(boolean b) { 748 if (DEBUG) { 749 System.out.println("JPopupMenu.setVisible " + b); 750 } 751 752 if (b == isVisible()) 754 return; 755 756 if (b == false) { 758 759 Boolean doCanceled = (Boolean )getClientProperty("JPopupMenu.firePopupMenuCanceled"); 764 if (doCanceled != null && doCanceled == Boolean.TRUE) { 765 putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE); 766 firePopupMenuCanceled(); 767 } 768 getSelectionModel().clearSelection(); 769 770 } else { 771 if (isPopupMenu()) { 774 if (getSubElements().length > 0) { 775 MenuElement me[] = new MenuElement [2]; 776 me[0]=(MenuElement )this; 777 me[1]=getSubElements()[0]; 778 MenuSelectionManager.defaultManager().setSelectedPath(me); 779 } else { 780 MenuElement me[] = new MenuElement [1]; 781 me[0]=(MenuElement )this; 782 MenuSelectionManager.defaultManager().setSelectedPath(me); 783 } 784 } 785 } 786 787 if(b) { 788 firePopupMenuWillBecomeVisible(); 789 popup = getPopup(); 790 firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE); 791 792 793 } else if(popup != null) { 794 firePopupMenuWillBecomeInvisible(); 795 popup.hide(); 796 popup = null; 797 firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE); 798 if (isPopupMenu()) { 801 MenuSelectionManager.defaultManager().clearSelectedPath(); 802 } 803 } 804 } 805 806 816 private Popup getPopup() { 817 Popup oldPopup = popup; 818 819 if (oldPopup != null) { 820 oldPopup.hide(); 821 } 822 PopupFactory popupFactory = PopupFactory.getSharedInstance(); 823 824 if (isLightWeightPopupEnabled()) { 825 popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP); 826 } 827 else { 828 popupFactory.setPopupType(PopupFactory.MEDIUM_WEIGHT_POPUP); 829 } 830 831 Point p = adjustPopupLocationToFitScreen(desiredLocationX,desiredLocationY); 833 desiredLocationX = p.x; 834 desiredLocationY = p.y; 835 836 Popup newPopup = getUI().getPopup(this, desiredLocationX, 837 desiredLocationY); 838 839 popupFactory.setPopupType(PopupFactory.LIGHT_WEIGHT_POPUP); 840 newPopup.show(); 841 return newPopup; 842 } 843 844 848 public boolean isVisible() { 849 if(popup != null) 850 return true; 851 else 852 return false; 853 } 854 855 866 public void setLocation(int x, int y) { 867 int oldX = desiredLocationX; 868 int oldY = desiredLocationY; 869 870 desiredLocationX = x; 871 desiredLocationY = y; 872 if(popup != null && (x != oldX || y != oldY)) { 873 popup = getPopup(); 874 } 875 } 876 877 883 private boolean isPopupMenu() { 884 return ((invoker != null) && !(invoker instanceof JMenu )); 885 } 886 887 893 public Component getInvoker() { 894 return this.invoker; 895 } 896 897 907 public void setInvoker(Component invoker) { 908 Component oldInvoker = this.invoker; 909 this.invoker = invoker; 910 if ((oldInvoker != this.invoker) && (ui != null)) { 911 ui.uninstallUI(this); 912 ui.installUI(this); 913 } 914 invalidate(); 915 } 916 917 927 public void show(Component invoker, int x, int y) { 928 if (DEBUG) { 929 System.out.println("in JPopupMenu.show " ); 930 } 931 setInvoker(invoker); 932 Frame newFrame = getFrame(invoker); 933 if (newFrame != frame) { 934 if (newFrame!=null) { 937 this.frame = newFrame; 938 if(popup != null) { 939 setVisible(false); 940 } 941 } 942 } 943 Point invokerOrigin; 944 if (invoker != null) { 945 invokerOrigin = invoker.getLocationOnScreen(); 946 setLocation(invokerOrigin.x + x, 947 invokerOrigin.y + y); 948 } else { 949 setLocation(x, y); 950 } 951 setVisible(true); 952 } 953 954 960 JPopupMenu getRootPopupMenu() { 961 JPopupMenu mp = this; 962 while((mp!=null) && (mp.isPopupMenu()!=true) && 963 (mp.getInvoker() != null) && 964 (mp.getInvoker().getParent() != null) && 965 (mp.getInvoker().getParent() instanceof JPopupMenu ) 966 ) { 967 mp = (JPopupMenu ) mp.getInvoker().getParent(); 968 } 969 return mp; 970 } 971 972 979 @Deprecated 980 public Component getComponentAtIndex(int i) { 981 return getComponent(i); 982 } 983 984 991 public int getComponentIndex(Component c) { 992 int ncomponents = this.getComponentCount(); 993 Component[] component = this.getComponents(); 994 for (int i = 0 ; i < ncomponents ; i++) { 995 Component comp = component[i]; 996 if (comp == c) 997 return i; 998 } 999 return -1; 1000 } 1001 1002 1011 public void setPopupSize(Dimension d) { 1012 Dimension oldSize = getPreferredSize(); 1013 1014 setPreferredSize(d); 1015 if (popup != null) { 1016 Dimension newSize = getPreferredSize(); 1017 1018 if (!oldSize.equals(newSize)) { 1019 popup = getPopup(); 1020 } 1021 } 1022 } 1023 1024 1034 public void setPopupSize(int width, int height) { 1035 setPopupSize(new Dimension(width, height)); 1036 } 1037 1038 1048 public void setSelected(Component sel) { 1049 SingleSelectionModel model = getSelectionModel(); 1050 int index = getComponentIndex(sel); 1051 model.setSelectedIndex(index); 1052 } 1053 1054 1060 public boolean isBorderPainted() { 1061 return paintBorder; 1062 } 1063 1064 1072 public void setBorderPainted(boolean b) { 1073 paintBorder = b; 1074 repaint(); 1075 } 1076 1077 1085 protected void paintBorder(Graphics g) { 1086 if (isBorderPainted()) { 1087 super.paintBorder(g); 1088 } 1089 } 1090 1091 1097 public Insets getMargin() { 1098 if(margin == null) { 1099 return new Insets(0,0,0,0); 1100 } else { 1101 return margin; 1102 } 1103 } 1104 1105 1106 1113 boolean isSubPopupMenu(JPopupMenu popup) { 1114 int ncomponents = this.getComponentCount(); 1115 Component[] component = this.getComponents(); 1116 for (int i = 0 ; i < ncomponents ; i++) { 1117 Component comp = component[i]; 1118 if (comp instanceof JMenu ) { 1119 JMenu menu = (JMenu )comp; 1120 JPopupMenu subPopup = menu.getPopupMenu(); 1121 if (subPopup == popup) 1122 return true; 1123 if (subPopup.isSubPopupMenu(popup)) 1124 return true; 1125 } 1126 } 1127 return false; 1128 } 1129 1130 1131 private static Frame getFrame(Component c) { 1132 Component w = c; 1133 1134 while(!(w instanceof Frame) && (w!=null)) { 1135 w = w.getParent(); 1136 } 1137 return (Frame)w; 1138 } 1139 1140 1141 1151 protected String paramString() { 1152 String labelString = (label != null ? 1153 label : ""); 1154 String paintBorderString = (paintBorder ? 1155 "true" : "false"); 1156 String marginString = (margin != null ? 1157 margin.toString() : ""); 1158 String lightWeightPopupEnabledString = (isLightWeightPopupEnabled() ? 1159 "true" : "false"); 1160 return super.paramString() + 1161 ",desiredLocationX=" + desiredLocationX + 1162 ",desiredLocationY=" + desiredLocationY + 1163 ",label=" + labelString + 1164 ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString + 1165 ",margin=" + marginString + 1166 ",paintBorder=" + paintBorderString; 1167 } 1168 1169 1173 1182 public AccessibleContext getAccessibleContext() { 1183 if (accessibleContext == null) { 1184 accessibleContext = new AccessibleJPopupMenu(); 1185 } 1186 return accessibleContext; 1187 } 1188 1189 1195 protected class AccessibleJPopupMenu extends AccessibleJComponent 1196 implements PropertyChangeListener { 1197 1198 1203 protected AccessibleJPopupMenu() { 1204 JPopupMenu.this.addPropertyChangeListener(this); 1205 } 1206 1207 1213 public AccessibleRole getAccessibleRole() { 1214 return AccessibleRole.POPUP_MENU; 1215 } 1216 1217 1225 public void propertyChange(PropertyChangeEvent e) { 1226 String propertyName = e.getPropertyName(); 1227 if (propertyName == "visible") { 1228 if (e.getOldValue() == Boolean.FALSE && 1229 e.getNewValue() == Boolean.TRUE) { 1230 handlePopupIsVisibleEvent(true); 1231 1232 } else if (e.getOldValue() == Boolean.TRUE && 1233 e.getNewValue() == Boolean.FALSE) { 1234 handlePopupIsVisibleEvent(false); 1235 } 1236 } 1237 } 1238 1239 1242 private void handlePopupIsVisibleEvent(boolean visible) { 1243 if (visible) { 1244 firePropertyChange(ACCESSIBLE_STATE_PROPERTY, 1246 null, AccessibleState.VISIBLE); 1247 fireActiveDescendant(); 1249 } else { 1250 firePropertyChange(ACCESSIBLE_STATE_PROPERTY, 1252 AccessibleState.VISIBLE, null); 1253 } 1254 } 1255 1256 1260 private void fireActiveDescendant() { 1261 if (JPopupMenu.this instanceof BasicComboPopup ) { 1262 JList popupList = ((BasicComboPopup )JPopupMenu.this).getList(); 1264 if (popupList == null) { 1265 return; 1266 } 1267 1268 AccessibleContext ac = popupList.getAccessibleContext(); 1270 AccessibleSelection selection = ac.getAccessibleSelection(); 1271 if (selection == null) { 1272 return; 1273 } 1274 Accessible a = selection.getAccessibleSelection(0); 1275 if (a == null) { 1276 return; 1277 } 1278 AccessibleContext selectedItem = a.getAccessibleContext(); 1279 1280 if (selectedItem != null && invoker != null) { 1282 AccessibleContext invokerContext = invoker.getAccessibleContext(); 1283 if (invokerContext != null) { 1284 invokerContext.firePropertyChange( 1288 ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, 1289 null, selectedItem); 1290 } 1291 } 1292 } 1293 } 1294 } 1296 1297 private void writeObject(ObjectOutputStream s) throws IOException { 1301 Vector values = new Vector (); 1302 1303 s.defaultWriteObject(); 1304 if(invoker != null && invoker instanceof Serializable ) { 1306 values.addElement("invoker"); 1307 values.addElement(invoker); 1308 } 1309 if(popup != null && popup instanceof Serializable ) { 1311 values.addElement("popup"); 1312 values.addElement(popup); 1313 } 1314 s.writeObject(values); 1315 1316 if (getUIClassID().equals(uiClassID)) { 1317 byte count = JComponent.getWriteObjCounter(this); 1318 JComponent.setWriteObjCounter(this, --count); 1319 if (count == 0 && ui != null) { 1320 ui.installUI(this); 1321 } 1322 } 1323 } 1324 1325 private void readObject(ObjectInputStream s) 1327 throws IOException , ClassNotFoundException { 1328 s.defaultReadObject(); 1329 1330 Vector values = (Vector )s.readObject(); 1331 int indexCounter = 0; 1332 int maxCounter = values.size(); 1333 1334 if(indexCounter < maxCounter && values.elementAt(indexCounter). 1335 equals("invoker")) { 1336 invoker = (Component)values.elementAt(++indexCounter); 1337 indexCounter++; 1338 } 1339 if(indexCounter < maxCounter && values.elementAt(indexCounter). 1340 equals("popup")) { 1341 popup = (Popup )values.elementAt(++indexCounter); 1342 indexCounter++; 1343 } 1344 } 1345 1346 1347 1352 public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {} 1353 1354 1366 public void processKeyEvent(KeyEvent e, MenuElement path[], 1367 MenuSelectionManager manager) { 1368 MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(), 1369 e.getWhen(), e.getModifiers(), 1370 e.getKeyCode(), e.getKeyChar(), 1371 path, manager); 1372 processMenuKeyEvent(mke); 1373 1374 if (mke.isConsumed()) { 1375 e.consume(); 1376 } 1377 } 1378 1379 1385 private void processMenuKeyEvent(MenuKeyEvent e) { 1386 switch (e.getID()) { 1387 case KeyEvent.KEY_PRESSED: 1388 fireMenuKeyPressed(e); break; 1389 case KeyEvent.KEY_RELEASED: 1390 fireMenuKeyReleased(e); break; 1391 case KeyEvent.KEY_TYPED: 1392 fireMenuKeyTyped(e); break; 1393 default: 1394 break; 1395 } 1396 } 1397 1398 1405 private void fireMenuKeyPressed(MenuKeyEvent event) { 1406 Object [] listeners = listenerList.getListenerList(); 1407 for (int i = listeners.length-2; i>=0; i-=2) { 1408 if (listeners[i]==MenuKeyListener.class) { 1409 ((MenuKeyListener)listeners[i+1]).menuKeyPressed(event); 1410 } 1411 } 1412 } 1413 1414 1421 private void fireMenuKeyReleased(MenuKeyEvent event) { 1422 Object [] listeners = listenerList.getListenerList(); 1423 for (int i = listeners.length-2; i>=0; i-=2) { 1424 if (listeners[i]==MenuKeyListener.class) { 1425 ((MenuKeyListener)listeners[i+1]).menuKeyReleased(event); 1426 } 1427 } 1428 } 1429 1430 1437 private void fireMenuKeyTyped(MenuKeyEvent event) { 1438 Object [] listeners = listenerList.getListenerList(); 1439 for (int i = listeners.length-2; i>=0; i-=2) { 1440 if (listeners[i]==MenuKeyListener.class) { 1441 ((MenuKeyListener)listeners[i+1]).menuKeyTyped(event); 1442 } 1443 } 1444 } 1445 1446 1456 public void menuSelectionChanged(boolean isIncluded) { 1457 if (DEBUG) { 1458 System.out.println("In JPopupMenu.menuSelectionChanged " + isIncluded); 1459 } 1460 if(invoker instanceof JMenu ) { 1461 JMenu m = (JMenu ) invoker; 1462 if(isIncluded) 1463 m.setPopupMenuVisible(true); 1464 else 1465 m.setPopupMenuVisible(false); 1466 } 1467 if (isPopupMenu() && !isIncluded) 1468 setVisible(false); 1469 } 1470 1471 1482 public MenuElement [] getSubElements() { 1483 MenuElement result[]; 1484 Vector tmp = new Vector (); 1485 int c = getComponentCount(); 1486 int i; 1487 Component m; 1488 1489 for(i=0 ; i < c ; i++) { 1490 m = getComponent(i); 1491 if(m instanceof MenuElement ) 1492 tmp.addElement(m); 1493 } 1494 1495 result = new MenuElement [tmp.size()]; 1496 for(i=0,c=tmp.size() ; i < c ; i++) 1497 result[i] = (MenuElement ) tmp.elementAt(i); 1498 return result; 1499 } 1500 1501 1506 public Component getComponent() { 1507 return this; 1508 } 1509 1510 1511 1514 static public class Separator extends JSeparator 1515 { 1516 public Separator( ) 1517 { 1518 super( JSeparator.HORIZONTAL ); 1519 } 1520 1521 1528 public String getUIClassID() 1529 { 1530 return "PopupMenuSeparatorUI"; 1531 1532 } 1533 } 1534 1535 1542 public boolean isPopupTrigger(MouseEvent e) { 1543 return getUI().isPopupTrigger(e); 1544 } 1545} 1546 1547 | Popular Tags |