/**
   * Positions the specified frame at a relative position in the screen, where 50% is considered to
   * be the center of the screen.
   *
   * @param frame the frame.
   * @param horizontalPercent the relative horizontal position of the frame (0.0 to 1.0, where 0.5
   *     is the center of the screen).
   * @param verticalPercent the relative vertical position of the frame (0.0 to 1.0, where 0.5 is
   *     the center of the screen).
   */
  public static void positionFrameOnScreen(
      final Window frame, final double horizontalPercent, final double verticalPercent) {

    final Rectangle s = frame.getGraphicsConfiguration().getBounds();
    final Dimension f = frame.getSize();

    final int spaceOnX = Math.max(s.width - f.width, 0);
    final int spaceOnY = Math.max(s.height - f.height, 0);
    final int x = (int) (horizontalPercent * spaceOnX) + s.x;
    final int y = (int) (verticalPercent * spaceOnY) + s.y;
    frame.setBounds(x, y, f.width, f.height);
    frame.setBounds(s.intersection(frame.getBounds()));
  }
 private void adjustWindowSize(ContainerWrapper paramContainerWrapper) {
   BoundSize localBoundSize1 = this.lc.getPackWidth();
   BoundSize localBoundSize2 = this.lc.getPackHeight();
   if ((localBoundSize1 == null) && (localBoundSize2 == null)) return;
   Window localWindow =
       (Window)
           SwingUtilities.getAncestorOfClass(
               Window.class, (Component) paramContainerWrapper.getComponent());
   if (localWindow == null) return;
   Dimension localDimension = localWindow.getPreferredSize();
   int i =
       constrain(
           checkParent(localWindow),
           localWindow.getWidth(),
           localDimension.width,
           localBoundSize1);
   int j =
       constrain(
           checkParent(localWindow),
           localWindow.getHeight(),
           localDimension.height,
           localBoundSize2);
   int k =
       Math.round(
           localWindow.getX()
               - (i - localWindow.getWidth()) * (1.0F - this.lc.getPackWidthAlign()));
   int m =
       Math.round(
           localWindow.getY()
               - (j - localWindow.getHeight()) * (1.0F - this.lc.getPackHeightAlign()));
   localWindow.setBounds(k, m, i, j);
 }
Example #3
0
 public void mouseDragged(MouseEvent e) {
   Point newPos = e.getPoint();
   SwingUtilities.convertPointToScreen(newPos, SizeGrip.this);
   int xDelta = newPos.x - origPos.x;
   int yDelta = newPos.y - origPos.y;
   Window wind = SwingUtilities.getWindowAncestor(SizeGrip.this);
   if (wind != null) { // Should always be true
     if (getComponentOrientation().isLeftToRight()) {
       int w = wind.getWidth();
       if (newPos.x >= wind.getX()) {
         w += xDelta;
       }
       int h = wind.getHeight();
       if (newPos.y >= wind.getY()) {
         h += yDelta;
       }
       wind.setSize(w, h);
     } else { // RTL
       int newW = Math.max(1, wind.getWidth() - xDelta);
       int newH = Math.max(1, wind.getHeight() + yDelta);
       wind.setBounds(newPos.x, wind.getY(), newW, newH);
     }
     // invalidate()/validate() needed pre-1.6.
     wind.invalidate();
     wind.validate();
   }
   origPos.setLocation(newPos);
 }
  public void start() {
    Toolkit.getDefaultToolkit()
        .addAWTEventListener(
            new AWTEventListener() {
              public void eventDispatched(AWTEvent e) {
                System.out.println(e.toString());
              }
            },
            FocusEvent.FOCUS_EVENT_MASK
                | WindowEvent.WINDOW_FOCUS_EVENT_MASK
                | WindowEvent.WINDOW_EVENT_MASK);

    frame = new Frame("Frame");
    frame.setName("Frame-owner");
    frame.setBounds(100, 0, 100, 100);
    dialog = new Dialog(frame, "Dialog");
    dialog.setName("Dialog-owner");
    dialog.setBounds(100, 0, 100, 100);

    window1 = new Window(frame);
    window1.setName("1st child");
    window1.setBounds(100, 300, 100, 100);
    window2 = new Window(window1);
    window2.setName("2nd child");
    window2.setBounds(100, 500, 100, 100);

    test1(frame, window1);
    test2(frame, window1, window2);
    test3(frame, window1, window2);

    window1 = new Window(dialog);
    window1.setBounds(100, 300, 100, 100);
    window1.setName("1st child");
    window2 = new Window(window1);
    window2.setName("2nd child");
    window2.setBounds(100, 500, 100, 100);

    test1(dialog, window1);
    test2(dialog, window1, window2);
    test3(dialog, window1, window2);

    System.out.println("Test passed.");
  }
Example #5
0
  /**
   * Listens to a JDialog to save and restore windows bounds automatically.
   *
   * <p>{@code setVisible(false)} and {@code dispose()} must not be used to close the window.
   * Instead, dispatch a window closing event.
   *
   * <PRE>
   * dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
   * </PRE>
   *
   * and the dialog must be set to dispose on close
   *
   * <PRE>
   * setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
   * </PRE>
   *
   * @param w {@code Window} to listen to
   * @param prefNode String identifier to preference node to save and restore from
   * @param key the key to save and restore from
   */
  private static void addBoundsListener(final Window w, final String prefNode, final String key) {
    String bounds = Preferences.userRoot().node(prefNode).get(key, null);

    if (bounds != null) { // restore to previous size and position

      if (w instanceof JDialog) {
        if (((JDialog) w).isResizable()) {
          w.setBounds(decodeRectangle(bounds));
        } else {
          w.setLocation(decodeRectangle(bounds).getLocation());
        }
      } else {
        w.setBounds(decodeRectangle(bounds));
      }

      Window owner = w.getOwner();

      if (owner != null) {
        w.setLocationRelativeTo(owner);
      }
    }

    /* listen for a window closing event and deal with it */
    w.addWindowListener(
        new WindowAdapter() {

          @Override
          public void windowClosing(WindowEvent evt) {
            // save position and size
            Preferences p = Preferences.userRoot().node(prefNode);

            p.put(key, encodeRectangle(w.getBounds()));
            w.removeWindowListener(this); // make GC easy
          }
        });

    if (w instanceof JDialog) {
      addEscapeListener((JDialog) w);
    }
  }
 public static boolean safeRestoreWindow(final Window frame, final Rectangle bounds) {
   final GraphicsEnvironment graphicsEnvironment =
       GraphicsEnvironment.getLocalGraphicsEnvironment();
   final GraphicsDevice[] devices = graphicsEnvironment.getScreenDevices();
   for (int i = 0; i < devices.length; i++) {
     final GraphicsDevice device = devices[i];
     final Rectangle rectangle = device.getDefaultConfiguration().getBounds();
     if (rectangle.contains(bounds) || rectangle.equals(bounds)) {
       DebugLog.log("Found a usable screen-configuration: Restoring frame to " + bounds);
       frame.setBounds(bounds);
       return true;
     }
   }
   return false;
 }
Example #7
0
 /*
 	This could live in the desktop script.
 	However we'd like to get it on the screen as quickly as possible.
 */
 public static void startSplashScreen() {
   int width = 275, height = 148;
   Window win = new Window(new Frame());
   win.pack();
   BshCanvas can = new BshCanvas();
   can.setSize(width, height); // why is this necessary?
   Toolkit tk = Toolkit.getDefaultToolkit();
   Dimension dim = tk.getScreenSize();
   win.setBounds(dim.width / 2 - width / 2, dim.height / 2 - height / 2, width, height);
   win.add("Center", can);
   Image img = tk.getImage(Interpreter.class.getResource("/bsh/util/lib/splash.gif"));
   MediaTracker mt = new MediaTracker(can);
   mt.addImage(img, 0);
   try {
     mt.waitForAll();
   } catch (Exception e) {
   }
   Graphics gr = can.getBufferedGraphics();
   gr.drawImage(img, 0, 0, can);
   win.setVisible(true);
   win.toFront();
   splashScreen = win;
 }
    public void mouseDragged(MouseEvent ev) {
      Window w = (Window) ev.getSource();
      Point pt = ev.getPoint();

      if (isMovingWindow) {
        Point windowPt;
        try {
          windowPt = (Point) AccessController.doPrivileged(getLocationAction);
          windowPt.x = windowPt.x - dragOffsetX;
          windowPt.y = windowPt.y - dragOffsetY;
          w.setLocation(windowPt);
        } catch (PrivilegedActionException e) {
        }
      } else if (dragCursor != 0) {
        Rectangle r = w.getBounds();
        Rectangle startBounds = new Rectangle(r);
        Dimension min = w.getMinimumSize();

        switch (dragCursor) {
          case Cursor.E_RESIZE_CURSOR:
            adjust(r, min, 0, 0, pt.x + (dragWidth - dragOffsetX) - r.width, 0);
            break;
          case Cursor.S_RESIZE_CURSOR:
            adjust(r, min, 0, 0, 0, pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          case Cursor.N_RESIZE_CURSOR:
            adjust(r, min, 0, pt.y - dragOffsetY, 0, -(pt.y - dragOffsetY));
            break;
          case Cursor.W_RESIZE_CURSOR:
            adjust(r, min, pt.x - dragOffsetX, 0, -(pt.x - dragOffsetX), 0);
            break;
          case Cursor.NE_RESIZE_CURSOR:
            adjust(
                r,
                min,
                0,
                pt.y - dragOffsetY,
                pt.x + (dragWidth - dragOffsetX) - r.width,
                -(pt.y - dragOffsetY));
            break;
          case Cursor.SE_RESIZE_CURSOR:
            adjust(
                r,
                min,
                0,
                0,
                pt.x + (dragWidth - dragOffsetX) - r.width,
                pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          case Cursor.NW_RESIZE_CURSOR:
            adjust(
                r,
                min,
                pt.x - dragOffsetX,
                pt.y - dragOffsetY,
                -(pt.x - dragOffsetX),
                -(pt.y - dragOffsetY));
            break;
          case Cursor.SW_RESIZE_CURSOR:
            adjust(
                r,
                min,
                pt.x - dragOffsetX,
                0,
                -(pt.x - dragOffsetX),
                pt.y + (dragHeight - dragOffsetY) - r.height);
            break;
          default:
            break;
        }
        if (!r.equals(startBounds)) {
          w.setBounds(r);
          // Defer repaint/validate on mouseReleased unless dynamic
          // layout is active.
          if (Toolkit.getDefaultToolkit().isDynamicLayoutActive()) {
            w.validate();
            getRootPane().repaint();
          }
        }
      }
    }