Example usage for java.awt Window getOwner

List of usage examples for java.awt Window getOwner

Introduction

In this page you can find the example usage for java.awt Window getOwner.

Prototype

public Window getOwner() 

Source Link

Document

Returns the owner of this window.

Usage

From source file:Utils.java

/**
 * <p>// w  w w .ja  v a 2  s.  c om
 * Returns the <code>Point</code> at which a window should be placed to
 * center that window on the screen.
 * </p>
 * <p>
 * Some thought was taken as to whether to implement a method such as this,
 * or to simply make a method that, given a window, will center it.  It was
 * decided that it is better to not alter an object within a method.
 * </p>
 *
 * @param window The window to calculate the center point for.  This object
 *               can not be null.
 *
 * @return the <code>Point</code> at which the window should be placed to
 *         center that window on the screen.
 */

private static Rectangle getUsableDeviceBounds(Window window) {
    Window owner = window.getOwner();
    GraphicsConfiguration gc = null;

    if (owner == null) {
        gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration();
    } else {
        gc = owner.getGraphicsConfiguration();
    }

    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
    Rectangle bounds = gc.getBounds();
    bounds.x += insets.left;
    bounds.y += insets.top;
    bounds.width -= (insets.left + insets.right);
    bounds.height -= (insets.top + insets.bottom);

    return bounds;
}

From source file:Main.java

static public void centerOnParent(final Window child, final boolean absolute) {
    child.pack();/*from   w ww. ja va  2s  .  co m*/
    boolean useChildsOwner = child.getOwner() != null
            ? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
            : false;
    final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
    final Point parentLocationOnScreen = useChildsOwner ? child.getOwner().getLocationOnScreen()
            : new Point(0, 0);
    final Dimension childSize = child.getSize();
    childSize.width = Math.min(childSize.width, screenSize.width);
    childSize.height = Math.min(childSize.height, screenSize.height);
    child.setSize(childSize);
    int x;
    int y;
    if ((child.getOwner() != null) && child.getOwner().isShowing()) {
        x = (parentSize.width - childSize.width) / 2;
        y = (parentSize.height - childSize.height) / 2;
        x += parentLocationOnScreen.x;
        y += parentLocationOnScreen.y;
    } else {
        x = (screenSize.width - childSize.width) / 2;
        y = (screenSize.height - childSize.height) / 2;
    }
    if (!absolute) {
        x /= 2;
        y /= 2;
    }
    child.setLocation(x, y);
}

From source file:Main.java

/**
 * Returns the frame that contains the given component.
 *
 * @param component component./*from w w w  .  java2  s  . c o  m*/
 *
 * @return frame that contains the given component or <code>null</code> if there is
 *         no parent frame.
 */
public static Frame getOwnerFrame(Component component) {
    Window window = SwingUtilities.windowForComponent(component);
    Frame mother = null;

    if (window != null) {
        Window owner = window.getOwner();

        if ((owner != null) && owner instanceof Frame) {
            mother = (Frame) owner;
        }
    }

    return mother;
}

From source file:org.eclipse.jubula.rc.swing.driver.RobotAwtImpl.java

/**
 * {@inheritDoc}/*from   ww  w.j a v a  2 s. com*/
 */
public void activateApplication(String method) throws RobotException {
    try {
        Window window = getActiveWindow();
        if (window == null) {
            return;
        }
        WindowActivationMethod wam = WindowActivationMethod.createWindowActivationMethod(method, m_robot,
                m_queuer);
        wam.activate(window);

        // Verify that window was successfully activated
        Window activeWindow = (Window) m_queuer.invokeAndWait("getActiveWindow", //$NON-NLS-1$
                new IRunnable() {
                    public Object run() throws StepExecutionException {
                        if (Frame.getFrames().length == 0) {
                            return null;
                        }
                        for (int i = 0; i < Frame.getFrames().length; ++i) {
                            Window curWindow = Frame.getFrames()[i];
                            while (curWindow.getOwner() != null) {
                                curWindow = curWindow.getOwner();
                            }
                            if (curWindow.isFocused()) {
                                return curWindow;
                            }
                        }
                        return null;
                    }
                });
        if (activeWindow != window) {
            throw new StepExecutionException(I18n.getString(TestErrorEvent.WINDOW_ACTIVATION_FAILED, true),
                    EventFactory.createActionError(TestErrorEvent.WINDOW_ACTIVATION_FAILED));
        }

    } catch (Exception exc) {
        throw new RobotException(exc);
    }
}

From source file:org.eclipse.jubula.rc.swing.driver.RobotAwtImpl.java

/**
 * Guesses the active window. Returns null if no active window is found.
 * @return the active window/*from  ww  w  .  j ava  2  s .  c o m*/
 */
private Window getActiveWindow() {
    return (Window) m_queuer.invokeAndWait("getActiveWindow", //$NON-NLS-1$
            new IRunnable() {
                public Object run() throws StepExecutionException {
                    if (Frame.getFrames().length == 0) {
                        return null;
                    }
                    for (int i = 0; i < Frame.getFrames().length; ++i) {
                        Window window = Frame.getFrames()[i];
                        while (window.getOwner() != null) {
                            window = window.getOwner();
                        }
                        if (window.isVisible()) {
                            return window;
                        }
                    }
                    return null;
                }
            });
}