/**
   * Positions the specified dialog at a position relative to its parent.
   *
   * @param dialog the dialog to be positioned.
   * @param horizontalPercent the relative location.
   * @param verticalPercent the relative location.
   */
  public static void positionDialogRelativeToParent(
      final Dialog dialog, final double horizontalPercent, final double verticalPercent) {
    final Container parent = dialog.getParent();
    if (parent == null || (parent.isVisible() == false)) {
      positionFrameOnScreen(dialog, horizontalPercent, verticalPercent);
      return;
    }

    final Dimension d = dialog.getSize();
    final Dimension p = parent.getSize();

    final int baseX = parent.getX();
    final int baseY = parent.getY();

    final int parentPointX = baseX + (int) (horizontalPercent * p.width);
    final int parentPointY = baseY + (int) (verticalPercent * p.height);

    final int dialogPointX = parentPointX - (int) (horizontalPercent * d.width);
    final int dialogPointY = parentPointY - (int) (verticalPercent * d.height);

    // make sure the dialog fits completely on the screen...
    final Rectangle s = parent.getGraphicsConfiguration().getBounds();
    final Rectangle r = new Rectangle(dialogPointX, dialogPointY, d.width, d.height);
    final Rectangle intersectedDialogBounds = r.intersection(s);
    if (intersectedDialogBounds.width < d.width) {
      r.x = s.width - d.width;
      r.width = d.width;
    }
    if (intersectedDialogBounds.height < d.height) {
      r.y = s.height - d.height;
      r.height = d.height;
    }
    final Rectangle finalIntersection = r.intersection(s);
    dialog.setBounds(finalIntersection);
  }
Пример #2
0
    /**
     * Returns true if popup can fit the screen and the owner's top parent. It determines can popup
     * be lightweight or mediumweight.
     */
    boolean fitsOnScreen() {
      boolean result = false;
      Component component = getComponent();
      if (owner != null && component != null) {
        int popupWidth = component.getWidth();
        int popupHeight = component.getHeight();

        Container parent = (Container) SwingUtilities.getRoot(owner);
        if (parent instanceof JFrame || parent instanceof JDialog || parent instanceof JWindow) {

          Rectangle parentBounds = parent.getBounds();
          Insets i = parent.getInsets();
          parentBounds.x += i.left;
          parentBounds.y += i.top;
          parentBounds.width -= i.left + i.right;
          parentBounds.height -= i.top + i.bottom;

          if (JPopupMenu.canPopupOverlapTaskBar()) {
            GraphicsConfiguration gc = parent.getGraphicsConfiguration();
            Rectangle popupArea = getContainerPopupArea(gc);
            result = parentBounds.intersection(popupArea).contains(x, y, popupWidth, popupHeight);
          } else {
            result = parentBounds.contains(x, y, popupWidth, popupHeight);
          }
        } else if (parent instanceof JApplet) {
          Rectangle parentBounds = parent.getBounds();
          Point p = parent.getLocationOnScreen();
          parentBounds.x = p.x;
          parentBounds.y = p.y;
          result = parentBounds.contains(x, y, popupWidth, popupHeight);
        }
      }
      return result;
    }