0N/A/*
2362N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.awt.*;
0N/A
0N/Aimport sun.awt.ModalExclude;
0N/Aimport sun.awt.SunToolkit;
0N/A
0N/A/**
0N/A * Popups are used to display a <code>Component</code> to the user, typically
0N/A * on top of all the other <code>Component</code>s in a particular containment
0N/A * hierarchy. <code>Popup</code>s have a very small life cycle. Once you
0N/A * have obtained a <code>Popup</code>, and hidden it (invoked the
0N/A * <code>hide</code> method), you should no longer
0N/A * invoke any methods on it. This allows the <code>PopupFactory</code> to cache
0N/A * <code>Popup</code>s for later use.
0N/A * <p>
0N/A * The general contract is that if you need to change the size of the
0N/A * <code>Component</code>, or location of the <code>Popup</code>, you should
0N/A * obtain a new <code>Popup</code>.
0N/A * <p>
0N/A * <code>Popup</code> does not descend from <code>Component</code>, rather
0N/A * implementations of <code>Popup</code> are responsible for creating
0N/A * and maintaining their own <code>Component</code>s to render the
0N/A * requested <code>Component</code> to the user.
0N/A * <p>
0N/A * You typically do not explicitly create an instance of <code>Popup</code>,
0N/A * instead obtain one from a <code>PopupFactory</code>.
0N/A *
0N/A * @see PopupFactory
0N/A *
0N/A * @since 1.4
0N/A */
0N/Apublic class Popup {
0N/A /**
0N/A * The Component representing the Popup.
0N/A */
0N/A private Component component;
0N/A
0N/A /**
0N/A * Creates a <code>Popup</code> for the Component <code>owner</code>
0N/A * containing the Component <code>contents</code>. <code>owner</code>
0N/A * is used to determine which <code>Window</code> the new
0N/A * <code>Popup</code> will parent the <code>Component</code> the
0N/A * <code>Popup</code> creates to.
0N/A * A null <code>owner</code> implies there is no valid parent.
0N/A * <code>x</code> and
0N/A * <code>y</code> specify the preferred initial location to place
0N/A * the <code>Popup</code> at. Based on screen size, or other paramaters,
0N/A * the <code>Popup</code> may not display at <code>x</code> and
0N/A * <code>y</code>.
0N/A *
0N/A * @param owner Component mouse coordinates are relative to, may be null
0N/A * @param contents Contents of the Popup
0N/A * @param x Initial x screen coordinate
0N/A * @param y Initial y screen coordinate
0N/A * @exception IllegalArgumentException if contents is null
0N/A */
0N/A protected Popup(Component owner, Component contents, int x, int y) {
0N/A this();
0N/A if (contents == null) {
0N/A throw new IllegalArgumentException("Contents must be non-null");
0N/A }
0N/A reset(owner, contents, x, y);
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>Popup</code>. This is provided for subclasses.
0N/A */
0N/A protected Popup() {
0N/A }
0N/A
0N/A /**
0N/A * Makes the <code>Popup</code> visible. If the <code>Popup</code> is
0N/A * currently visible, this has no effect.
0N/A */
0N/A public void show() {
0N/A Component component = getComponent();
0N/A
0N/A if (component != null) {
0N/A component.show();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Hides and disposes of the <code>Popup</code>. Once a <code>Popup</code>
0N/A * has been disposed you should no longer invoke methods on it. A
0N/A * <code>dispose</code>d <code>Popup</code> may be reclaimed and later used
0N/A * based on the <code>PopupFactory</code>. As such, if you invoke methods
0N/A * on a <code>disposed</code> <code>Popup</code>, indeterminate
0N/A * behavior will result.
0N/A */
0N/A public void hide() {
0N/A Component component = getComponent();
0N/A
0N/A if (component instanceof JWindow) {
0N/A component.hide();
0N/A ((JWindow)component).getContentPane().removeAll();
0N/A }
0N/A dispose();
0N/A }
0N/A
0N/A /**
0N/A * Frees any resources the <code>Popup</code> may be holding onto.
0N/A */
0N/A void dispose() {
0N/A Component component = getComponent();
0N/A Window window = SwingUtilities.getWindowAncestor(component);
0N/A
0N/A if (component instanceof JWindow) {
0N/A ((Window)component).dispose();
0N/A component = null;
0N/A }
0N/A // If our parent is a DefaultFrame, we need to dispose it, too.
0N/A if (window instanceof DefaultFrame) {
0N/A window.dispose();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resets the <code>Popup</code> to an initial state.
0N/A */
0N/A void reset(Component owner, Component contents, int ownerX, int ownerY) {
0N/A if (getComponent() == null) {
0N/A component = createComponent(owner);
0N/A }
0N/A
0N/A Component c = getComponent();
0N/A
0N/A if (c instanceof JWindow) {
0N/A JWindow component = (JWindow)getComponent();
0N/A
0N/A component.setLocation(ownerX, ownerY);
0N/A component.getContentPane().add(contents, BorderLayout.CENTER);
0N/A component.invalidate();
0N/A component.validate();
0N/A if(component.isVisible()) {
0N/A // Do not call pack() if window is not visible to
0N/A // avoid early native peer creation
0N/A pack();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Causes the <code>Popup</code> to be sized to fit the preferred size
0N/A * of the <code>Component</code> it contains.
0N/A */
0N/A void pack() {
0N/A Component component = getComponent();
0N/A
0N/A if (component instanceof Window) {
0N/A ((Window)component).pack();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Window</code> to use as the parent of the
0N/A * <code>Window</code> created for the <code>Popup</code>. This creates
0N/A * a new <code>DefaultFrame</code>, if necessary.
0N/A */
0N/A private Window getParentWindow(Component owner) {
0N/A Window window = null;
0N/A
0N/A if (owner instanceof Window) {
0N/A window = (Window)owner;
0N/A }
0N/A else if (owner != null) {
0N/A window = SwingUtilities.getWindowAncestor(owner);
0N/A }
0N/A if (window == null) {
0N/A window = new DefaultFrame();
0N/A }
0N/A return window;
0N/A }
0N/A
0N/A /**
0N/A * Creates the Component to use as the parent of the <code>Popup</code>.
0N/A * The default implementation creates a <code>Window</code>, subclasses
0N/A * should override.
0N/A */
0N/A Component createComponent(Component owner) {
0N/A if (GraphicsEnvironment.isHeadless()) {
0N/A // Generally not useful, bail.
0N/A return null;
0N/A }
0N/A return new HeavyWeightWindow(getParentWindow(owner));
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Component</code> returned from
0N/A * <code>createComponent</code> that will hold the <code>Popup</code>.
0N/A */
0N/A Component getComponent() {
0N/A return component;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Component used to house window.
5559N/A */
0N/A static class HeavyWeightWindow extends JWindow implements ModalExclude {
0N/A HeavyWeightWindow(Window parent) {
0N/A super(parent);
0N/A setFocusableWindowState(false);
0N/A setType(Window.Type.POPUP);
0N/A
0N/A // Popups are typically transient and most likely won't benefit
5559N/A // from true double buffering. Turn it off here.
0N/A getRootPane().setUseTrueDoubleBuffering(false);
0N/A // Try to set "always-on-top" for the popup window.
0N/A // Applets usually don't have sufficient permissions to do it.
0N/A // In this case simply ignore the exception.
0N/A try {
0N/A setAlwaysOnTop(true);
0N/A } catch (SecurityException se) {
0N/A // setAlwaysOnTop is restricted,
0N/A // the exception is ignored
0N/A }
0N/A }
0N/A
0N/A public void update(Graphics g) {
0N/A paint(g);
0N/A }
0N/A
0N/A public void show() {
0N/A this.pack();
0N/A if (getWidth() > 0 && getHeight() > 0) {
0N/A super.show();
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used if no valid Window ancestor of the supplied owner is found.
0N/A * <p>
0N/A * PopupFactory uses this as a way to know when the Popup shouldn't
0N/A * be cached based on the Window.
0N/A */
0N/A static class DefaultFrame extends Frame {
0N/A }
0N/A}
0N/A