final Point windowPos = window.getLocation(); pos.translate(windowPos.x, windowPos.y); final Insets windowInsets = window.getInsets(); pos.translate(windowInsets.left, windowInsets.top); bounds.setLocation(pos);
public Insets getInsets() { if( w != null ) { return w.getInsets(); } else if( jif != null ) { return jif.getInsets(); } else { throw new IllegalStateException(); } }
/** * Get the parent insets. * * @return the insets. */ private Insets getParentInsets() { if (rootParent instanceof JApplet) { return ((JApplet) rootParent).getInsets(); } return ((Window) rootParent).getInsets(); }
/** * <p/> * Returns the <code>Point</code> at which a window should be placed in * order to be staggered slightly from another "origin" window to * ensure that the title areas of both windows remain visible to the user. * </p> * * @param originWindow Window from which the staggered location will be calculated * * @return location staggered from the upper left location of the origin * window */ public static Point getPointForStaggering(Window originWindow) { Point origin = originWindow.getLocation(); Insets insets = originWindow.getInsets(); origin.x += insets.top; origin.y += insets.top; return origin; }
/** * <p/> * Returns the <code>Point</code> at which a window should be placed in * order to be staggered slightly from another "origin" window to * ensure that the title areas of both windows remain visible to the user. * </p> * * @param originWindow Window from which the staggered location will be calculated * * @return location staggered from the upper left location of the origin * window */ public static Point getPointForStaggering(Window originWindow) { Point origin = originWindow.getLocation(); Insets insets = originWindow.getInsets(); origin.x += insets.top; origin.y += insets.top; return origin; }
/** * <p/> * Returns the <code>Point</code> at which a window should be placed in * order to be staggered slightly from another "origin" window to * ensure that the title areas of both windows remain visible to the user. * </p> * * @param originWindow Window from which the staggered location will be calculated * * @return location staggered from the upper left location of the origin * window */ public static Point getPointForStaggering(Window originWindow) { Point origin = originWindow.getLocation(); Insets insets = originWindow.getInsets(); origin.x += insets.top; origin.y += insets.top; return origin; }
/** * <p/> * Returns the <code>Point</code> at which a window should be placed in * order to be staggered slightly from another "origin" window to * ensure that the title areas of both windows remain visible to the user. * </p> * * @param originWindow Window from which the staggered location will be calculated * * @return location staggered from the upper left location of the origin * window */ public static Point getPointForStaggering(Window originWindow) { Point origin = originWindow.getLocation(); Insets insets = originWindow.getInsets(); origin.x += insets.top; origin.y += insets.top; return origin; }
@RunsInCurrentThread private boolean shouldResize(@Nonnull Window window) { Insets insets = window.getInsets(); int w = window.getWidth() - (insets.left + insets.right); if (w < MINIMUM_WINDOW_SIZE.width) { return true; } int h = window.getHeight() - (insets.top + insets.bottom); return h < MINIMUM_WINDOW_SIZE.height; }
/** * Return insets of the component w/o traversing up to parent, * i.e. trying Window and JComponent. * <p> * Exception is JRootPane. * Return it's parent's Window component's insets if available, * otherwise return JRootPane's insets.<br> * This is due to <i>experience</i> that <i>some</i> JRootPane's * do not expose valid insets value. * </p> * @param topLevelOnly if true only returns insets of top-level components, i.e. Window and JRootPanel, * otherwise for JComponent as well. */ public static Insets getInsets(final Component c, final boolean topLevelOnly) { if( c instanceof Window ) { return ((Window)c).getInsets(); } if( c instanceof JRootPane ) { final Window w = getWindow(c); if( null != w ) { return w.getInsets(); } return ((JRootPane)c).getInsets(); } if( !topLevelOnly && c instanceof JComponent ) { return ((JComponent)c).getInsets(); } return null; }
@RunsInCurrentThread static @Nonnull Point absoluteCenterOf(@Nonnull Window window) { Insets insets = window.getInsets(); int w = window.getWidth() - (insets.left + insets.right); int h = window.getHeight() - (insets.top + insets.bottom); int x = window.getX() + insets.left; int y = window.getY() + insets.top; return new Point(x + (w / 2), y + (h / 2)); }
public static void makeSheet(Dialog dialog) { dialog.addNotify(); ComponentPeer peer = dialog.getPeer(); // File dialogs are CFileDialog instead. Unfortunately this means this hack // can't work for those. :( if (peer instanceof LWWindowPeer) { LWWindowPeer windowPeer = (LWWindowPeer) dialog.getPeer(); //XXX: Should check this before casting too. CPlatformWindow platformWindow = (CPlatformWindow) windowPeer.getPlatformWindow(); try { Method method = CPlatformWindow.class.getDeclaredMethod( "setStyleBits", int.class, boolean.class); method.setAccessible(true); method.invoke(platformWindow, 64 /* CPlatformWindow.SHEET */, true); Window parent = dialog.getOwner(); dialog.setLocation(dialog.getLocation().x, parent.getLocation().y + parent.getInsets().top); } catch (Exception e) { Logger.getLogger(SheetHack.class.getName()) .log(Level.WARNING, "Couldn't call setStyleBits", e); } } }
/** * Sizes the window's native host frame to match its preferred size. * * @param window */ public static void sizeHostToFit(Window window) { if (window == null) { throw new IllegalArgumentException(); } if (isFullScreen()) { throw new IllegalStateException(); } Dimensions size = window.getPreferredSize(); java.awt.Window hostWindow = window.getDisplay().getHostWindow(); java.awt.Insets frameInsets = hostWindow.getInsets(); hostWindow.setSize(size.width + (frameInsets.left + frameInsets.right), size.height + (frameInsets.top + frameInsets.bottom)); }
/** * Sizes the window's native host frame to match its preferred size. * * @param window */ public static void sizeHostToFit(Window window) { if (window == null) { throw new IllegalArgumentException(); } if (isFullScreen()) { throw new IllegalStateException(); } Dimensions size = window.getPreferredSize(); java.awt.Window hostWindow = window.getDisplay().getHostWindow(); java.awt.Insets frameInsets = hostWindow.getInsets(); hostWindow.setSize(size.width + (frameInsets.left + frameInsets.right), size.height + (frameInsets.top + frameInsets.bottom)); }
/** * Returns the corner that contains the point <code>x</code>, <code> * y</code>, or -1 if the position doesn't match a corner. * * @param w the window. * @param x the x coordinate. * @param y the y coordinate. * * @return the corner containing the (x, y) coordinate, or -1 if the * position doesn't match a corner. */ private int calculateCorner(Window w, int x, int y) { Insets insets = w.getInsets(); int xPosition = calculatePosition(x - insets.left, w.getWidth() - insets.left - insets.right); int yPosition = calculatePosition(y - insets.top, w.getHeight() - insets.top - insets.bottom); if (xPosition == -1 || yPosition == -1) { return -1; } return yPosition * 5 + xPosition; }
/** * Returns the corner that contains the point <code>x</code>, * <code>y</code>, or -1 if the position doesn't match a corner. * * @param w * Window. * @param x * X coordinate. * @param y * Y coordinate. * @return Corner that contains the specified point. */ private int calculateCorner(Window w, int x, int y) { Insets insets = w.getInsets(); int xPosition = this.calculatePosition(x - insets.left, w .getWidth() - insets.left - insets.right); int yPosition = this.calculatePosition(y - insets.top, w .getHeight() - insets.top - insets.bottom); if ((xPosition == -1) || (yPosition == -1)) { return -1; } return yPosition * 5 + xPosition; }
/** * Returns the corner that contains the point <code>x</code>, * <code>y</code>, or -1 if the position doesn't match a corner. * * @param w * Window. * @param x * X coordinate. * @param y * Y coordinate. * @return Corner that contains the specified point. */ private int calculateCorner(Window w, int x, int y) { Insets insets = w.getInsets(); int xPosition = this.calculatePosition(x - insets.left, w .getWidth() - insets.left - insets.right); int yPosition = this.calculatePosition(y - insets.top, w .getHeight() - insets.top - insets.bottom); if ((xPosition == -1) || (yPosition == -1)) { return -1; } return yPosition * 5 + xPosition; }
@Override public void componentMoved(ComponentEvent evt) { Window owner = getOwner(); Point newLocation = owner.getLocation(); if (!newLocation.equals(oldLocation)) { setLocation( newLocation.x + (owner.getWidth() - getWidth()) / 2, newLocation.y + owner.getInsets().top); shiftBackLocation = null; oldLocation = newLocation; } } };
@Override public void paint(Graphics g) { checkResizeWindow(this); super.paint(g); } public static void checkResizeWindow(Window window) { if (!window.isShowing()) { return; } Component[] components = window.getComponents(); if (components.length == 0) { return; } Component comp = components[0]; Insets insets = window.getInsets(); Dimension innerSize = window.getSize(); innerSize.width -= insets.left + insets.right; innerSize.height -= insets.top + insets.bottom; if (!innerSize.equals(comp.getSize())) { window.invalidate(); window.validate(); } }
rect.x = window.getWidth() - window.getInsets().right - 2;
sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JFrame) owner).getRootPane().getContentPane().getY()); } else if (owner instanceof JDialog) { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top + ((JDialog) owner).getRootPane().getContentPane().getY()); } else { sheetLoc = new Point( ownerLoc.x + (owner.getWidth() - getWidth()) / 2, ownerLoc.y + owner.getInsets().top);