4088 lines · 151.3 KB · Java
RawDownload
/* * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */package java.awt; import java.awt.event.*;import java.awt.geom.Path2D;import java.awt.geom.Point2D;import java.awt.im.InputContext;import java.awt.image.BufferStrategy;import java.awt.image.BufferedImage;import java.awt.peer.ComponentPeer;import java.awt.peer.WindowPeer;import java.beans.PropertyChangeListener;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OptionalDataException;import java.io.Serializable;import java.lang.ref.WeakReference;import java.lang.reflect.InvocationTargetException;import java.security.AccessController;import java.util.ArrayList;import java.util.Arrays;import java.util.EventListener;import java.util.Locale;import java.util.ResourceBundle;import java.util.Set;import java.util.Vector;import java.util.concurrent.atomic.AtomicBoolean;import javax.accessibility.*;import sun.awt.AWTAccessor;import sun.awt.AppContext;import sun.awt.CausedFocusEvent;import sun.awt.SunToolkit;import sun.awt.util.IdentityArrayList;import sun.java2d.Disposer;import sun.java2d.pipe.Region;import sun.security.action.GetPropertyAction;import sun.security.util.SecurityConstants;import sun.util.logging.PlatformLogger; /** * A <code>Window</code> object is a top-level window with no borders and no * menubar. * The default layout for a window is <code>BorderLayout</code>. * <p> * A window must have either a frame, dialog, or another window defined as its * owner when it's constructed. * <p> * In a multi-screen environment, you can create a <code>Window</code> * on a different screen device by constructing the <code>Window</code> * with {@link #Window(Window, GraphicsConfiguration)}.  The * <code>GraphicsConfiguration</code> object is one of the * <code>GraphicsConfiguration</code> objects of the target screen device. * <p> * In a virtual device multi-screen environment in which the desktop * area could span multiple physical screen devices, the bounds of all * configurations are relative to the virtual device coordinate system. * The origin of the virtual-coordinate system is at the upper left-hand * corner of the primary physical screen.  Depending on the location of * the primary screen in the virtual device, negative coordinates are * possible, as shown in the following figure. * <p> * <img src="doc-files/MultiScreen.gif" * alt="Diagram shows virtual device containing 4 physical screens. Primary physical screen shows coords (0,0), other screen shows (-80,-100)." * ALIGN=center HSPACE=10 VSPACE=7> * <p> * In such an environment, when calling <code>setLocation</code>, * you must pass a virtual coordinate to this method.  Similarly, * calling <code>getLocationOnScreen</code> on a <code>Window</code> returns * virtual device coordinates.  Call the <code>getBounds</code> method * of a <code>GraphicsConfiguration</code> to find its origin in the virtual * coordinate system. * <p> * The following code sets the location of a <code>Window</code> * at (10, 10) relative to the origin of the physical screen * of the corresponding <code>GraphicsConfiguration</code>.  If the * bounds of the <code>GraphicsConfiguration</code> is not taken * into account, the <code>Window</code> location would be set * at (10, 10) relative to the virtual-coordinate system and would appear * on the primary physical screen, which might be different from the * physical screen of the specified <code>GraphicsConfiguration</code>. * * <pre> *      Window w = new Window(Window owner, GraphicsConfiguration gc); *      Rectangle bounds = gc.getBounds(); *      w.setLocation(10 + bounds.x, 10 + bounds.y); * </pre> * * <p> * Note: the location and size of top-level windows (including * <code>Window</code>s, <code>Frame</code>s, and <code>Dialog</code>s) * are under the control of the desktop's window management system. * Calls to <code>setLocation</code>, <code>setSize</code>, and * <code>setBounds</code> are requests (not directives) which are * forwarded to the window management system.  Every effort will be * made to honor such requests.  However, in some cases the window * management system may ignore such requests, or modify the requested * geometry in order to place and size the <code>Window</code> in a way * that more closely matches the desktop settings. * <p> * Due to the asynchronous nature of native event handling, the results * returned by <code>getBounds</code>, <code>getLocation</code>, * <code>getLocationOnScreen</code>, and <code>getSize</code> might not * reflect the actual geometry of the Window on screen until the last * request has been processed.  During the processing of subsequent * requests these values might change accordingly while the window * management system fulfills the requests. * <p> * An application may set the size and location of an invisible * {@code Window} arbitrarily, but the window management system may * subsequently change its size and/or location when the * {@code Window} is made visible. One or more {@code ComponentEvent}s * will be generated to indicate the new geometry. * <p> * Windows are capable of generating the following WindowEvents: * WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus. * * @author      Sami Shaio * @author      Arthur van Hoff * @see WindowEvent * @see #addWindowListener * @see java.awt.BorderLayout * @since       JDK1.0 */public class Window extends Container implements Accessible {     /**     * Enumeration of available <i>window types</i>.     *     * A window type defines the generic visual appearance and behavior of a     * top-level window. For example, the type may affect the kind of     * decorations of a decorated {@code Frame} or {@code Dialog} instance.     * <p>     * Some platforms may not fully support a certain window type. Depending on     * the level of support, some properties of the window type may be     * disobeyed.     *     * @see   #getType     * @see   #setType     * @since 1.7     */    public static enum Type {        /**         * Represents a <i>normal</i> window.         *         * This is the default type for objects of the {@code Window} class or         * its descendants. Use this type for regular top-level windows.         */        NORMAL,         /**         * Represents a <i>utility</i> window.         *         * A utility window is usually a small window such as a toolbar or a         * palette. The native system may render the window with smaller         * title-bar if the window is either a {@code Frame} or a {@code         * Dialog} object, and if it has its decorations enabled.         */        UTILITY,         /**         * Represents a <i>popup</i> window.         *         * A popup window is a temporary window such as a drop-down menu or a         * tooltip. On some platforms, windows of that type may be forcibly         * made undecorated even if they are instances of the {@code Frame} or         * {@code Dialog} class, and have decorations enabled.         */        POPUP    }     /**     * This represents the warning message that is     * to be displayed in a non secure window. ie :     * a window that has a security manager installed for     * which calling SecurityManager.checkTopLevelWindow()     * is false.  This message can be displayed anywhere in     * the window.     *     * @serial     * @see #getWarningString     */    String      warningString;     /**     * {@code icons} is the graphical way we can     * represent the frames and dialogs.     * {@code Window} can't display icon but it's     * being inherited by owned {@code Dialog}s.     *     * @serial     * @see #getIconImages     * @see #setIconImages(List<? extends Image>)     */    transient java.util.List<Image> icons;     /**     * Holds the reference to the component which last had focus in this window     * before it lost focus.     */    private transient Component temporaryLostComponent;     static boolean systemSyncLWRequests = false;    boolean     syncLWRequests = false;    transient boolean beforeFirstShow = true;     static final int OPENED = 0x01;     /**     * An Integer value representing the Window State.     *     * @serial     * @since 1.2     * @see #show     */    int state;     /**     * A boolean value representing Window always-on-top state     * @since 1.5     * @serial     * @see #setAlwaysOnTop     * @see #isAlwaysOnTop     */    private boolean alwaysOnTop;     /**     * Contains all the windows that have a peer object associated,     * i. e. between addNotify() and removeNotify() calls. The list     * of all Window instances can be obtained from AppContext object.     *     * @since 1.6     */    private static final IdentityArrayList<Window> allWindows = new IdentityArrayList<Window>();     /**     * A vector containing all the windows this     * window currently owns.     * @since 1.2     * @see #getOwnedWindows     */    transient Vector<WeakReference<Window>> ownedWindowList =                                            new Vector<WeakReference<Window>>();     /*     * We insert a weak reference into the Vector of all Windows in AppContext     * instead of 'this' so that garbage collection can still take place     * correctly.     */    private transient WeakReference<Window> weakThis;     transient boolean showWithParent;     /**     * Contains the modal dialog that blocks this window, or null     * if the window is unblocked.     *     * @since 1.6     */    transient Dialog modalBlocker;     /**     * @serial     *     * @see java.awt.Dialog.ModalExclusionType     * @see #getModalExclusionType     * @see #setModalExclusionType     *     * @since 1.6     */    Dialog.ModalExclusionType modalExclusionType;     transient WindowListener windowListener;    transient WindowStateListener windowStateListener;    transient WindowFocusListener windowFocusListener;     transient InputContext inputContext;    private transient Object inputContextLock = new Object();     /**     * Unused. Maintained for serialization backward-compatibility.     *     * @serial     * @since 1.2     */    private FocusManager focusMgr;     /**     * Indicates whether this Window can become the focused Window.     *     * @serial     * @see #getFocusableWindowState     * @see #setFocusableWindowState     * @since 1.4     */    private boolean focusableWindowState = true;     /**     * Indicates whether this window should receive focus on     * subsequently being shown (with a call to {@code setVisible(true)}), or     * being moved to the front (with a call to {@code toFront()}).     *     * @serial     * @see #setAutoRequestFocus     * @see #isAutoRequestFocus     * @since 1.7     */    private volatile boolean autoRequestFocus = true;     /*     * Indicates that this window is being shown. This flag is set to true at     * the beginning of show() and to false at the end of show().     *     * @see #show()     * @see Dialog#shouldBlock     */    transient boolean isInShow = false;     /*     * The opacity level of the window     *     * @serial     * @see #setOpacity(float)     * @see #getOpacity()     * @since 1.7     */    private float opacity = 1.0f;     /*     * The shape assigned to this window. This field is set to {@code null} if     * no shape is set (rectangular window).     *     * @serial     * @see #getShape()     * @see #setShape(Shape)     * @since 1.7     */    private Shape shape = null;     private static final String base = "win";    private static int nameCounter = 0;     /*     * JDK 1.1 serialVersionUID     */    private static final long serialVersionUID = 4497834738069338734L;     private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.Window");     private static final boolean locationByPlatformProp;     transient boolean isTrayIconWindow = false;     /**     * These fields are initialized in the native peer code     * or via AWTAccessor's WindowAccessor.     */    private transient volatile int securityWarningWidth = 0;    private transient volatile int securityWarningHeight = 0;     /**     * These fields represent the desired location for the security     * warning if this window is untrusted.     * See com.sun.awt.SecurityWarning for more details.     */    private transient double securityWarningPointX = 2.0;    private transient double securityWarningPointY = 0.0;    private transient float securityWarningAlignmentX = RIGHT_ALIGNMENT;    private transient float securityWarningAlignmentY = TOP_ALIGNMENT;     static {        /* ensure that the necessary native libraries are loaded */        Toolkit.loadLibraries();        if (!GraphicsEnvironment.isHeadless()) {            initIDs();        }         String s = (String) java.security.AccessController.doPrivileged(            new GetPropertyAction("java.awt.syncLWRequests"));        systemSyncLWRequests = (s != null && s.equals("true"));        s = (String) java.security.AccessController.doPrivileged(            new GetPropertyAction("java.awt.Window.locationByPlatform"));        locationByPlatformProp = (s != null && s.equals("true"));    }     /**     * Initialize JNI field and method IDs for fields that may be       accessed from C.     */    private static native void initIDs();     /**     * Constructs a new, initially invisible window in default size with the     * specified <code>GraphicsConfiguration</code>.     * <p>     * If there is a security manager, this method first calls     * the security manager's <code>checkTopLevelWindow</code>     * method with <code>this</code>     * as its argument to determine whether or not the window     * must be displayed with a warning banner.     *     * @param gc the <code>GraphicsConfiguration</code> of the target screen     *     device. If <code>gc</code> is <code>null</code>, the system default     *     <code>GraphicsConfiguration</code> is assumed     * @exception IllegalArgumentException if <code>gc</code>     *    is not from a screen device     * @exception HeadlessException when     *     <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>     *     * @see java.awt.GraphicsEnvironment#isHeadless     * @see java.lang.SecurityManager#checkTopLevelWindow     */    Window(GraphicsConfiguration gc) {        init(gc);    }     transient Object anchor = new Object();    static class WindowDisposerRecord implements sun.java2d.DisposerRecord {        final WeakReference<Window> owner;        final WeakReference weakThis;        final WeakReference<AppContext> context;        WindowDisposerRecord(AppContext context, Window victim) {            owner = new WeakReference<Window>(victim.getOwner());            weakThis = victim.weakThis;            this.context = new WeakReference<AppContext>(context);        }        public void dispose() {            Window parent = owner.get();            if (parent != null) {                parent.removeOwnedWindow(weakThis);            }            AppContext ac = context.get();            if (null != ac) {                Window.removeFromWindowList(ac, weakThis);            }        }    }     private GraphicsConfiguration initGC(GraphicsConfiguration gc) {        GraphicsEnvironment.checkHeadless();         if (gc == null) {            gc = GraphicsEnvironment.getLocalGraphicsEnvironment().                getDefaultScreenDevice().getDefaultConfiguration();        }        setGraphicsConfiguration(gc);         return gc;    }     private void init(GraphicsConfiguration gc) {        GraphicsEnvironment.checkHeadless();         syncLWRequests = systemSyncLWRequests;         weakThis = new WeakReference<Window>(this);        addToWindowList();         setWarningString();        this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);        this.visible = false;         gc = initGC(gc);         if (gc.getDevice().getType() !=            GraphicsDevice.TYPE_RASTER_SCREEN) {            throw new IllegalArgumentException("not a screen device");        }        setLayout(new BorderLayout());         /* offset the initial location with the original of the screen */        /* and any insets                                              */        Rectangle screenBounds = gc.getBounds();        Insets screenInsets = getToolkit().getScreenInsets(gc);        int x = getX() + screenBounds.x + screenInsets.left;        int y = getY() + screenBounds.y + screenInsets.top;        if (x != this.x || y != this.y) {

Lines 1–500 of 4088.