< prev index next >

src/java.desktop/share/classes/javax/swing/JPopupMenu.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 757             return;
 758 
 759         // if closing, first close all Submenus
 760         if (b == false) {
 761 
 762             // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
 763             // a protected method and cannot be called from BasicPopupMenuUI directly
 764             // The real solution could be to make
 765             // firePopupMenuCanceled public and call it directly.
 766             Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
 767             if (doCanceled != null && doCanceled == Boolean.TRUE) {
 768                 putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
 769                 firePopupMenuCanceled();
 770             }
 771             getSelectionModel().clearSelection();
 772 
 773         } else {
 774             // This is a popup menu with MenuElement children,
 775             // set selection path before popping up!
 776             if (isPopupMenu()) {
 777                 MenuElement me[] = new MenuElement[1];
 778                 me[0] = this;
 779                 MenuSelectionManager.defaultManager().setSelectedPath(me);
 780             }
 781         }
 782 
 783         if(b) {
 784             firePopupMenuWillBecomeVisible();
 785             showPopup();
 786             firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
 787 
 788 
 789         } else if(popup != null) {
 790             firePopupMenuWillBecomeInvisible();
 791             popup.hide();
 792             popup = null;
 793             firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
 794             // 4694797: When popup menu is made invisible, selected path
 795             // should be cleared
 796             if (isPopupMenu()) {
 797                 MenuSelectionManager.defaultManager().clearSelectedPath();


1354         int             maxCounter = values.size();
1355 
1356         if(indexCounter < maxCounter && values.elementAt(indexCounter).
1357            equals("invoker")) {
1358             invoker = (Component)values.elementAt(++indexCounter);
1359             indexCounter++;
1360         }
1361         if(indexCounter < maxCounter && values.elementAt(indexCounter).
1362            equals("popup")) {
1363             popup = (Popup)values.elementAt(++indexCounter);
1364             indexCounter++;
1365         }
1366     }
1367 
1368 
1369     /**
1370      * This method is required to conform to the
1371      * <code>MenuElement</code> interface, but it not implemented.
1372      * @see MenuElement#processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
1373      */
1374     public void processMouseEvent(MouseEvent event,MenuElement path[],MenuSelectionManager manager) {}
1375 
1376     /**
1377      * Processes a key event forwarded from the
1378      * <code>MenuSelectionManager</code> and changes the menu selection,
1379      * if necessary, by using <code>MenuSelectionManager</code>'s API.
1380      * <p>
1381      * Note: you do not have to forward the event to sub-components.
1382      * This is done automatically by the <code>MenuSelectionManager</code>.
1383      *
1384      * @param e  a <code>KeyEvent</code>
1385      * @param path the <code>MenuElement</code> path array
1386      * @param manager   the <code>MenuSelectionManager</code>
1387      */
1388     @SuppressWarnings("deprecation")
1389     public void processKeyEvent(KeyEvent e, MenuElement path[],
1390                                 MenuSelectionManager manager) {
1391         MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
1392                                              e.getWhen(), e.getModifiers(),
1393                                              e.getKeyCode(), e.getKeyChar(),
1394                                              path, manager);
1395         processMenuKeyEvent(mke);
1396 
1397         if (mke.isConsumed())  {
1398             e.consume();
1399     }
1400     }
1401 
1402     /**
1403      * Handles a keystroke in a menu.
1404      *
1405      * @param e  a <code>MenuKeyEvent</code> object
1406      * @since 1.5
1407      */
1408     private void processMenuKeyEvent(MenuKeyEvent e) {
1409         switch (e.getID()) {


1487             else
1488                 m.setPopupMenuVisible(false);
1489         }
1490         if (isPopupMenu() && !isIncluded)
1491           setVisible(false);
1492     }
1493 
1494     /**
1495      * Returns an array of <code>MenuElement</code>s containing the submenu
1496      * for this menu component.  It will only return items conforming to
1497      * the <code>JMenuElement</code> interface.
1498      * If popup menu is <code>null</code> returns
1499      * an empty array.  This method is required to conform to the
1500      * <code>MenuElement</code> interface.
1501      *
1502      * @return an array of <code>MenuElement</code> objects
1503      * @see MenuElement#getSubElements
1504      */
1505     @BeanProperty(bound = false)
1506     public MenuElement[] getSubElements() {
1507         MenuElement result[];
1508         Vector<MenuElement> tmp = new Vector<MenuElement>();
1509         int c = getComponentCount();
1510         int i;
1511         Component m;
1512 
1513         for(i=0 ; i < c ; i++) {
1514             m = getComponent(i);
1515             if(m instanceof MenuElement)
1516                 tmp.addElement((MenuElement) m);
1517         }
1518 
1519         result = new MenuElement[tmp.size()];
1520         for(i=0,c=tmp.size() ; i < c ; i++)
1521             result[i] = tmp.elementAt(i);
1522         return result;
1523     }
1524 
1525     /**
1526      * Returns this <code>JPopupMenu</code> component.
1527      * @return this <code>JPopupMenu</code> object


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 757             return;
 758 
 759         // if closing, first close all Submenus
 760         if (b == false) {
 761 
 762             // 4234793: This is a workaround because JPopupMenu.firePopupMenuCanceled is
 763             // a protected method and cannot be called from BasicPopupMenuUI directly
 764             // The real solution could be to make
 765             // firePopupMenuCanceled public and call it directly.
 766             Boolean doCanceled = (Boolean)getClientProperty("JPopupMenu.firePopupMenuCanceled");
 767             if (doCanceled != null && doCanceled == Boolean.TRUE) {
 768                 putClientProperty("JPopupMenu.firePopupMenuCanceled", Boolean.FALSE);
 769                 firePopupMenuCanceled();
 770             }
 771             getSelectionModel().clearSelection();
 772 
 773         } else {
 774             // This is a popup menu with MenuElement children,
 775             // set selection path before popping up!
 776             if (isPopupMenu()) {
 777                 MenuElement[] me = new MenuElement[1];
 778                 me[0] = this;
 779                 MenuSelectionManager.defaultManager().setSelectedPath(me);
 780             }
 781         }
 782 
 783         if(b) {
 784             firePopupMenuWillBecomeVisible();
 785             showPopup();
 786             firePropertyChange("visible", Boolean.FALSE, Boolean.TRUE);
 787 
 788 
 789         } else if(popup != null) {
 790             firePopupMenuWillBecomeInvisible();
 791             popup.hide();
 792             popup = null;
 793             firePropertyChange("visible", Boolean.TRUE, Boolean.FALSE);
 794             // 4694797: When popup menu is made invisible, selected path
 795             // should be cleared
 796             if (isPopupMenu()) {
 797                 MenuSelectionManager.defaultManager().clearSelectedPath();


1354         int             maxCounter = values.size();
1355 
1356         if(indexCounter < maxCounter && values.elementAt(indexCounter).
1357            equals("invoker")) {
1358             invoker = (Component)values.elementAt(++indexCounter);
1359             indexCounter++;
1360         }
1361         if(indexCounter < maxCounter && values.elementAt(indexCounter).
1362            equals("popup")) {
1363             popup = (Popup)values.elementAt(++indexCounter);
1364             indexCounter++;
1365         }
1366     }
1367 
1368 
1369     /**
1370      * This method is required to conform to the
1371      * <code>MenuElement</code> interface, but it not implemented.
1372      * @see MenuElement#processMouseEvent(MouseEvent, MenuElement[], MenuSelectionManager)
1373      */
1374     public void processMouseEvent(MouseEvent event,MenuElement[] path,MenuSelectionManager manager) {}
1375 
1376     /**
1377      * Processes a key event forwarded from the
1378      * <code>MenuSelectionManager</code> and changes the menu selection,
1379      * if necessary, by using <code>MenuSelectionManager</code>'s API.
1380      * <p>
1381      * Note: you do not have to forward the event to sub-components.
1382      * This is done automatically by the <code>MenuSelectionManager</code>.
1383      *
1384      * @param e  a <code>KeyEvent</code>
1385      * @param path the <code>MenuElement</code> path array
1386      * @param manager   the <code>MenuSelectionManager</code>
1387      */
1388     @SuppressWarnings("deprecation")
1389     public void processKeyEvent(KeyEvent e, MenuElement[] path,
1390                                 MenuSelectionManager manager) {
1391         MenuKeyEvent mke = new MenuKeyEvent(e.getComponent(), e.getID(),
1392                                              e.getWhen(), e.getModifiers(),
1393                                              e.getKeyCode(), e.getKeyChar(),
1394                                              path, manager);
1395         processMenuKeyEvent(mke);
1396 
1397         if (mke.isConsumed())  {
1398             e.consume();
1399     }
1400     }
1401 
1402     /**
1403      * Handles a keystroke in a menu.
1404      *
1405      * @param e  a <code>MenuKeyEvent</code> object
1406      * @since 1.5
1407      */
1408     private void processMenuKeyEvent(MenuKeyEvent e) {
1409         switch (e.getID()) {


1487             else
1488                 m.setPopupMenuVisible(false);
1489         }
1490         if (isPopupMenu() && !isIncluded)
1491           setVisible(false);
1492     }
1493 
1494     /**
1495      * Returns an array of <code>MenuElement</code>s containing the submenu
1496      * for this menu component.  It will only return items conforming to
1497      * the <code>JMenuElement</code> interface.
1498      * If popup menu is <code>null</code> returns
1499      * an empty array.  This method is required to conform to the
1500      * <code>MenuElement</code> interface.
1501      *
1502      * @return an array of <code>MenuElement</code> objects
1503      * @see MenuElement#getSubElements
1504      */
1505     @BeanProperty(bound = false)
1506     public MenuElement[] getSubElements() {
1507         MenuElement[] result;
1508         Vector<MenuElement> tmp = new Vector<MenuElement>();
1509         int c = getComponentCount();
1510         int i;
1511         Component m;
1512 
1513         for(i=0 ; i < c ; i++) {
1514             m = getComponent(i);
1515             if(m instanceof MenuElement)
1516                 tmp.addElement((MenuElement) m);
1517         }
1518 
1519         result = new MenuElement[tmp.size()];
1520         for(i=0,c=tmp.size() ; i < c ; i++)
1521             result[i] = tmp.elementAt(i);
1522         return result;
1523     }
1524 
1525     /**
1526      * Returns this <code>JPopupMenu</code> component.
1527      * @return this <code>JPopupMenu</code> object


< prev index next >