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

Print this page




  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;
  32 import java.io.ObjectOutputStream;
  33 import java.io.Serializable;
  34 import java.beans.*;
  35 
  36 import java.util.Locale;
  37 import java.util.Vector;
  38 import java.util.Hashtable;
  39 import javax.accessibility.*;
  40 import javax.swing.plaf.PopupMenuUI;
  41 import javax.swing.plaf.ComponentUI;
  42 import javax.swing.plaf.basic.BasicComboPopup;
  43 import javax.swing.event.*;


  44 import sun.security.util.SecurityConstants;
  45 
  46 import java.applet.Applet;
  47 
  48 /**
  49  * An implementation of a popup menu -- a small window that pops up
  50  * and displays a series of choices. A <code>JPopupMenu</code> is used for the
  51  * menu that appears when the user selects an item on the menu bar.
  52  * It is also used for "pull-right" menu that appears when the
  53  * selects a menu item that activates it. Finally, a <code>JPopupMenu</code>
  54  * can also be used anywhere else you want a menu to appear.  For
  55  * example, when the user right-clicks in a specified area.
  56  * <p>
  57  * For information and examples of using popup menus, see
  58  * <a
  59  href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>
  60  * in <em>The Java Tutorial.</em>
  61  * <p>
  62  * <strong>Warning:</strong> Swing is not thread safe. For more
  63  * information see <a


 330         }
 331 
 332         // Get screen bounds
 333         Rectangle scrBounds;
 334         GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
 335         Toolkit toolkit = Toolkit.getDefaultToolkit();
 336         if(gc != null) {
 337             // If we have GraphicsConfiguration use it to get screen bounds
 338             scrBounds = gc.getBounds();
 339         } else {
 340             // If we don't have GraphicsConfiguration use primary screen
 341             scrBounds = new Rectangle(toolkit.getScreenSize());
 342         }
 343 
 344         // Calculate the screen size that popup should fit
 345         Dimension popupSize = JPopupMenu.this.getPreferredSize();
 346         long popupRightX = (long)popupLocation.x + (long)popupSize.width;
 347         long popupBottomY = (long)popupLocation.y + (long)popupSize.height;
 348         int scrWidth = scrBounds.width;
 349         int scrHeight = scrBounds.height;

 350         if (!canPopupOverlapTaskBar()) {
 351             // Insets include the task bar. Take them into account.
 352             Insets scrInsets = toolkit.getScreenInsets(gc);
 353             scrBounds.x += scrInsets.left;
 354             scrBounds.y += scrInsets.top;
 355             scrWidth -= scrInsets.left + scrInsets.right;
 356             scrHeight -= scrInsets.top + scrInsets.bottom;
 357         }
 358         int scrRightX = scrBounds.x + scrWidth;
 359         int scrBottomY = scrBounds.y + scrHeight;
 360 
 361         // Ensure that popup menu fits the screen
 362         if (popupRightX > (long) scrRightX) {
 363             popupLocation.x = scrRightX - popupSize.width;
 364         }
 365 
 366         if (popupBottomY > (long) scrBottomY) {
 367             popupLocation.y = scrBottomY - popupSize.height;
 368         }
 369 


 390             GraphicsEnvironment.getLocalGraphicsEnvironment();
 391         GraphicsDevice[] gd = ge.getScreenDevices();
 392         for(int i = 0; i < gd.length; i++) {
 393             if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
 394                 GraphicsConfiguration dgc =
 395                     gd[i].getDefaultConfiguration();
 396                 if(dgc.getBounds().contains(popupLocation)) {
 397                     gc = dgc;
 398                     break;
 399                 }
 400             }
 401         }
 402         // If not found and we have invoker, ask invoker about his gc
 403         if(gc == null && getInvoker() != null) {
 404             gc = getInvoker().getGraphicsConfiguration();
 405         }
 406         return gc;
 407     }
 408 
 409     /**
 410      * Checks that there are enough security permissions
 411      * to make popup "always on top", which allows to show it above the task bar.
 412      */
 413     static boolean canPopupOverlapTaskBar() {
 414         boolean result = true;
 415         try {
 416             SecurityManager sm = System.getSecurityManager();
 417             if (sm != null) {
 418                 sm.checkPermission(
 419                     SecurityConstants.AWT.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION);
 420             }
 421         } catch (SecurityException se) {
 422             // There is no permission to show popups over the task bar
 423             result = false;
 424         }

 425         return result;
 426     }
 427 
 428 
 429     /**
 430      * Factory method which creates the <code>JMenuItem</code> for
 431      * <code>Actions</code> added to the <code>JPopupMenu</code>.
 432      *
 433      * @param a the <code>Action</code> for the menu item to be added
 434      * @return the new menu item
 435      * @see Action
 436      *
 437      * @since 1.3
 438      */
 439     protected JMenuItem createActionComponent(Action a) {
 440         JMenuItem mi = new JMenuItem() {
 441             protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
 442                 PropertyChangeListener pcl = createActionChangeListener(this);
 443                 if (pcl == null) {
 444                     pcl = super.createActionPropertyChangeListener(a);
 445                 }
 446                 return pcl;
 447             }
 448         };




  24  */
  25 
  26 package javax.swing;
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.io.IOException;
  31 import java.io.ObjectInputStream;
  32 import java.io.ObjectOutputStream;
  33 import java.io.Serializable;
  34 import java.beans.*;
  35 
  36 import java.util.Locale;
  37 import java.util.Vector;
  38 import java.util.Hashtable;
  39 import javax.accessibility.*;
  40 import javax.swing.plaf.PopupMenuUI;
  41 import javax.swing.plaf.ComponentUI;
  42 import javax.swing.plaf.basic.BasicComboPopup;
  43 import javax.swing.event.*;
  44 
  45 import sun.awt.SunToolkit;
  46 import sun.security.util.SecurityConstants;
  47 
  48 import java.applet.Applet;
  49 
  50 /**
  51  * An implementation of a popup menu -- a small window that pops up
  52  * and displays a series of choices. A <code>JPopupMenu</code> is used for the
  53  * menu that appears when the user selects an item on the menu bar.
  54  * It is also used for "pull-right" menu that appears when the
  55  * selects a menu item that activates it. Finally, a <code>JPopupMenu</code>
  56  * can also be used anywhere else you want a menu to appear.  For
  57  * example, when the user right-clicks in a specified area.
  58  * <p>
  59  * For information and examples of using popup menus, see
  60  * <a
  61  href="http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html">How to Use Menus</a>
  62  * in <em>The Java Tutorial.</em>
  63  * <p>
  64  * <strong>Warning:</strong> Swing is not thread safe. For more
  65  * information see <a


 332         }
 333 
 334         // Get screen bounds
 335         Rectangle scrBounds;
 336         GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
 337         Toolkit toolkit = Toolkit.getDefaultToolkit();
 338         if(gc != null) {
 339             // If we have GraphicsConfiguration use it to get screen bounds
 340             scrBounds = gc.getBounds();
 341         } else {
 342             // If we don't have GraphicsConfiguration use primary screen
 343             scrBounds = new Rectangle(toolkit.getScreenSize());
 344         }
 345 
 346         // Calculate the screen size that popup should fit
 347         Dimension popupSize = JPopupMenu.this.getPreferredSize();
 348         long popupRightX = (long)popupLocation.x + (long)popupSize.width;
 349         long popupBottomY = (long)popupLocation.y + (long)popupSize.height;
 350         int scrWidth = scrBounds.width;
 351         int scrHeight = scrBounds.height;
 352 
 353         if (!canPopupOverlapTaskBar()) {
 354             // Insets include the task bar. Take them into account.
 355             Insets scrInsets = toolkit.getScreenInsets(gc);
 356             scrBounds.x += scrInsets.left;
 357             scrBounds.y += scrInsets.top;
 358             scrWidth -= scrInsets.left + scrInsets.right;
 359             scrHeight -= scrInsets.top + scrInsets.bottom;
 360         }
 361         int scrRightX = scrBounds.x + scrWidth;
 362         int scrBottomY = scrBounds.y + scrHeight;
 363 
 364         // Ensure that popup menu fits the screen
 365         if (popupRightX > (long) scrRightX) {
 366             popupLocation.x = scrRightX - popupSize.width;
 367         }
 368 
 369         if (popupBottomY > (long) scrBottomY) {
 370             popupLocation.y = scrBottomY - popupSize.height;
 371         }
 372 


 393             GraphicsEnvironment.getLocalGraphicsEnvironment();
 394         GraphicsDevice[] gd = ge.getScreenDevices();
 395         for(int i = 0; i < gd.length; i++) {
 396             if(gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
 397                 GraphicsConfiguration dgc =
 398                     gd[i].getDefaultConfiguration();
 399                 if(dgc.getBounds().contains(popupLocation)) {
 400                     gc = dgc;
 401                     break;
 402                 }
 403             }
 404         }
 405         // If not found and we have invoker, ask invoker about his gc
 406         if(gc == null && getInvoker() != null) {
 407             gc = getInvoker().getGraphicsConfiguration();
 408         }
 409         return gc;
 410     }
 411 
 412     /**
 413      * Returns whether popup is allowed to be shown above the task bar.

 414      */
 415     static boolean canPopupOverlapTaskBar() {
 416         boolean result = true;
 417 
 418         Toolkit tk = Toolkit.getDefaultToolkit();
 419         if (tk instanceof SunToolkit) {
 420             result = ((SunToolkit)tk).canPopupOverlapTaskBar();





 421         }
 422 
 423         return result;
 424     }
 425 

 426     /**
 427      * Factory method which creates the <code>JMenuItem</code> for
 428      * <code>Actions</code> added to the <code>JPopupMenu</code>.
 429      *
 430      * @param a the <code>Action</code> for the menu item to be added
 431      * @return the new menu item
 432      * @see Action
 433      *
 434      * @since 1.3
 435      */
 436     protected JMenuItem createActionComponent(Action a) {
 437         JMenuItem mi = new JMenuItem() {
 438             protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
 439                 PropertyChangeListener pcl = createActionChangeListener(this);
 440                 if (pcl == null) {
 441                     pcl = super.createActionPropertyChangeListener(a);
 442                 }
 443                 return pcl;
 444             }
 445         };