/**
   * 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);
  }
Ejemplo n.º 2
0
  public void centerDialog(Dialog dialog) {
    // For centering dialogs

    Dimension size = dialog.getSize();
    Dimension parentSize = getSize();
    Point parentLocation = getLocationOnScreen();
    if ((parentSize.width <= size.width) || (parentSize.height <= size.height))
      // If this window is bigger than the parent window,
      // place it at the same coordinates as the parent.
      dialog.setLocation(parentLocation.x, parentLocation.y);
    else
      // Otherwise, place it centered within the parent window.
      dialog.setLocation(
          (((parentSize.width - size.width) / 2) + parentLocation.x),
          (((parentSize.height - size.height) / 2) + parentLocation.y));
  }