Exemplo n.º 1
0
 static {
   /* ensure that the necessary native libraries are loaded */
   Toolkit.loadLibraries();
   if (!GraphicsEnvironment.isHeadless()) {
     initIDs();
   }
 }
Exemplo n.º 2
0
 /**
  * Tries to find GraphicsConfiguration that contains the mouse cursor position. Can return null.
  */
 private GraphicsConfiguration getCurrentGraphicsConfiguration(Point popupLocation) {
   GraphicsConfiguration gc = null;
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice[] gd = ge.getScreenDevices();
   for (int i = 0; i < gd.length; i++) {
     if (gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
       GraphicsConfiguration dgc = gd[i].getDefaultConfiguration();
       if (dgc.getBounds().contains(popupLocation)) {
         gc = dgc;
         break;
       }
     }
   }
   // If not found and we have invoker, ask invoker about his gc
   if (gc == null && getInvoker() != null) {
     gc = getInvoker().getGraphicsConfiguration();
   }
   return gc;
 }
Exemplo n.º 3
0
  /**
   * Returns an point which has been adjusted to take into account of the desktop bounds, taskbar
   * and multi-monitor configuration.
   *
   * <p>This adustment may be cancelled by invoking the application with
   * -Djavax.swing.adjustPopupLocationToFit=false
   */
  Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) {
    Point popupLocation = new Point(xPosition, yPosition);

    if (popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) {
      return popupLocation;
    }

    // Get screen bounds
    Rectangle scrBounds;
    GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    if (gc != null) {
      // If we have GraphicsConfiguration use it to get screen bounds
      scrBounds = gc.getBounds();
    } else {
      // If we don't have GraphicsConfiguration use primary screen
      scrBounds = new Rectangle(toolkit.getScreenSize());
    }

    // Calculate the screen size that popup should fit
    Dimension popupSize = JPopupMenu.this.getPreferredSize();
    long popupRightX = (long) popupLocation.x + (long) popupSize.width;
    long popupBottomY = (long) popupLocation.y + (long) popupSize.height;
    int scrWidth = scrBounds.width;
    int scrHeight = scrBounds.height;
    if (!canPopupOverlapTaskBar()) {
      // Insets include the task bar. Take them into account.
      Insets scrInsets = toolkit.getScreenInsets(gc);
      scrBounds.x += scrInsets.left;
      scrBounds.y += scrInsets.top;
      scrWidth -= scrInsets.left + scrInsets.right;
      scrHeight -= scrInsets.top + scrInsets.bottom;
    }
    int scrRightX = scrBounds.x + scrWidth;
    int scrBottomY = scrBounds.y + scrHeight;

    // Ensure that popup menu fits the screen
    if (popupRightX > (long) scrRightX) {
      popupLocation.x = scrRightX - popupSize.width;
      if (popupLocation.x < scrBounds.x) {
        popupLocation.x = scrBounds.x;
      }
    }
    if (popupBottomY > (long) scrBottomY) {
      popupLocation.y = scrBottomY - popupSize.height;
      if (popupLocation.y < scrBounds.y) {
        popupLocation.y = scrBounds.y;
      }
    }

    return popupLocation;
  }
Exemplo n.º 4
0
  /**
   * Reads the <code>ObjectInputStream</code> and if it isn't <code>null</code> adds a listener to
   * receive action events fired by the button. Unrecognized keys or values will be ignored.
   *
   * @param s the <code>ObjectInputStream</code> to read
   * @exception HeadlessException if <code>GraphicsEnvironment.isHeadless</code> returns <code>true
   *     </code>
   * @serial
   * @see #removeActionListener(ActionListener)
   * @see #addActionListener(ActionListener)
   * @see java.awt.GraphicsEnvironment#isHeadless
   * @see #writeObject(ObjectOutputStream)
   */
  private void readObject(ObjectInputStream s)
      throws ClassNotFoundException, IOException, HeadlessException {
    GraphicsEnvironment.checkHeadless();
    s.defaultReadObject();

    Object keyOrNull;
    while (null != (keyOrNull = s.readObject())) {
      String key = ((String) keyOrNull).intern();

      if (actionListenerK == key) addActionListener((ActionListener) (s.readObject()));
      else // skip value for unrecognized key
      s.readObject();
    }
  }
Exemplo n.º 5
0
 /**
  * Constructs a button with the specified label.
  *
  * @param label a string label for the button, or <code>null</code> for no label
  * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
  * @see java.awt.GraphicsEnvironment#isHeadless
  */
 public Button(String label) throws HeadlessException {
   GraphicsEnvironment.checkHeadless();
   this.label = label;
 }