- /*
- * @(#)Window.java 1.184 03/01/28
- *
- * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package java.awt;
- import java.applet.Applet;
- import java.awt.peer.WindowPeer;
- import java.awt.event.*;
- import java.awt.image.BufferStrategy;
- import java.util.Vector;
- import java.util.Locale;
- import java.util.EventListener;
- import java.util.Set;
- import java.io.Serializable;
- import java.io.ObjectOutputStream;
- import java.io.ObjectInputStream;
- import java.io.IOException;
- import java.io.OptionalDataException;
- import java.awt.im.InputContext;
- import java.util.ResourceBundle;
- import java.lang.ref.WeakReference;
- import java.lang.reflect.InvocationTargetException;
- import java.security.AccessController;
- import java.security.PrivilegedAction;
- import javax.accessibility.*;
- import java.beans.PropertyChangeListener;
- import sun.security.action.GetPropertyAction;
- import sun.awt.DebugHelper;
- /**
- * 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>
- * Windows are capable of generating the following WindowEvents:
- * WindowOpened, WindowClosed, WindowGainedFocus, WindowLostFocus.
- *
- * @version 1.184, 01/28/03
- * @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 {
- /**
- * 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;
- /**
- * 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 vector containing all the windows this
- * window currently owns.
- * @since 1.2
- * @see #getOwnedWindows
- */
- transient Vector ownedWindowList = new Vector();
- private transient WeakReference weakThis;
- private transient boolean showWithParent = false;
- 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;
- private static final String base = "win";
- private static int nameCounter = 0;
- /*
- * JDK 1.1 serialVersionUID
- */
- private static final long serialVersionUID = 4497834738069338734L;
- private static final DebugHelper dbg = DebugHelper.create(Window.class);
- static {
- /* ensure that the necessary native libraries are loaded */
- Toolkit.loadLibraries();
- if (!GraphicsEnvironment.isHeadless()) {
- initIDs();
- }
- systemSyncLWRequests = ((Boolean)AccessController.doPrivileged(
- new PrivilegedAction() {
- public Object run() {
- return new Boolean("true".equals(System.
- getProperty("java.awt.syncLWRequests",
- "false")));
- }
- })).booleanValue();
- }
- /**
- * Initialize JNI field and method IDs for fields that may be
- accessed from C.
- */
- private static native void initIDs();
- /**
- * Constructs a new 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. This exception is always
- * thrown when GraphicsEnvironment.isHeadless() returns true.
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see java.lang.SecurityManager#checkTopLevelWindow
- */
- Window(GraphicsConfiguration gc) {
- init(gc);
- }
- private void init(GraphicsConfiguration gc) {
- if (GraphicsEnvironment.isHeadless()) {
- throw new IllegalArgumentException("headless environment");
- }
- syncLWRequests = systemSyncLWRequests;
- setWarningString();
- this.cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
- this.visible = false;
- if (gc == null) {
- this.graphicsConfig =
- GraphicsEnvironment.getLocalGraphicsEnvironment().
- getDefaultScreenDevice().getDefaultConfiguration();
- } else {
- this.graphicsConfig = gc;
- }
- if (graphicsConfig.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 = graphicsConfig.getBounds();
- Insets screenInsets = getToolkit().getScreenInsets(graphicsConfig);
- int x = getX() + screenBounds.x + screenInsets.left;
- int y = getY() + screenBounds.y + screenInsets.top;
- setLocation(x, y);
- }
- /**
- * Constructs a new window in the default size.
- *
- * <p>First, if there is a security manager, its
- * <code>checkTopLevelWindow</code>
- * method is called with <code>this</code>
- * as its argument
- * to see if it's ok to display the window without a warning banner.
- * If the default implementation of <code>checkTopLevelWindow</code>
- * is used (that is, that method is not overriden), then this results in
- * a call to the security manager's <code>checkPermission</code> method
- * with an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
- * permission. It that method raises a SecurityException,
- * <code>checkTopLevelWindow</code> returns false, otherwise it
- * returns true. If it returns false, a warning banner is created.
- *
- * @exception HeadlessException if GraphicsEnvironment.isHeadless()
- * returns true
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see java.lang.SecurityManager#checkTopLevelWindow
- */
- Window() throws HeadlessException {
- GraphicsEnvironment.checkHeadless();
- init((GraphicsConfiguration)null);
- }
- /**
- * Constructs a new invisible window with the specified
- * <code>Frame</code> as its owner. The Window will not be focusable
- * unless its owner is showing on the screen.
- * <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 owner the <code>Frame</code> to act as owner
- * @exception IllegalArgumentException if the <code>owner</code>'s
- * <code>GraphicsConfiguration</code> is not from a screen device
- * @exception java.lang.IllegalArgumentException if
- * <code>owner</code> is <code>null</code> this exception
- * is always thrown when <code>GraphicsEnvironment.isHeadless</code>
- * returns true
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see java.lang.SecurityManager#checkTopLevelWindow
- * @see #isShowing
- */
- public Window(Frame owner) {
- this(owner == null ? (GraphicsConfiguration)null :
- owner.getGraphicsConfiguration());
- ownedInit(owner);
- }
- /**
- * Constructs a new invisible window with the specified
- * <code>Window</code> as its owner. The Window will not be focusable
- * unless its nearest owning Frame or Dialog is showing on the screen.
- * <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 owner the <code>Window</code> to act as owner
- * @exception IllegalArgumentException if the <code>owner</code>'s
- * <code>GraphicsConfiguration</code> is not from a screen device
- * @exception java.lang.IllegalArgumentException if <code>owner</code>
- * is <code>null</code>. This exception is always thrown
- * when GraphicsEnvironment.isHeadless() returns true.
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see java.lang.SecurityManager#checkTopLevelWindow
- * @see #isShowing
- * @since 1.2
- */
- public Window(Window owner) {
- this(owner == null ? (GraphicsConfiguration)null :
- owner.getGraphicsConfiguration());
- ownedInit(owner);
- }
- /**
- * Constructs a new invisible window with the specified
- * window as its owner and a
- * <code>GraphicsConfiguration</code> of a screen device. The Window will
- * not be focusable unless its nearest owning Frame or Dialog is showing on
- * the screen.
- * <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 owner the window to act as owner
- * @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
- * @throws IllegalArgumentException if
- * <code>owner</code> is <code>null</code>
- * @throws IllegalArgumentException if <code>gc</code> is not from
- * a screen device; this exception is always thrown when
- * <code>GraphicsEnvironment.isHeadless</code> returns
- * <code>true</code>
- * @see java.awt.GraphicsEnvironment#isHeadless
- * @see java.lang.SecurityManager#checkTopLevelWindow
- * @see GraphicsConfiguration#getBounds
- * @see #isShowing
- * @since 1.3
- */
- public Window(Window owner, GraphicsConfiguration gc) {
- this(gc);
- ownedInit(owner);
- }
- private void ownedInit(Window owner) {
- if (owner == null) {
- throw new IllegalArgumentException("null owner window");
- }
- this.parent = owner;
- this.weakThis = new WeakReference(this);
- owner.addOwnedWindow(weakThis);
- }
- /**
- * Disposes of the input methods and context, and removes the WeakReference
- * which formerly pointed to this Window from the parent's owned Window
- * list.
- */
- protected void finalize() throws Throwable {
- if (parent != null) {
- ((Window)parent).removeOwnedWindow(weakThis);
- }
- super.finalize();
- }
- /**
- * Construct a name for this component. Called by getName() when the
- * name is null.
- */
- String constructComponentName() {
- synchronized (getClass()) {
- return base + nameCounter++;
- }
- }
- /**
- * Makes this Window displayable by creating the connection to its
- * native screen resource.
- * This method is called internally by the toolkit and should
- * not be called directly by programs.
- * @see Component#isDisplayable
- * @see Container#removeNotify
- * @since JDK1.0
- */
- public void addNotify() {
- synchronized (getTreeLock()) {
- Container parent = this.parent;
- if (parent != null && parent.getPeer() == null) {
- parent.addNotify();
- }
- if (peer == null)
- peer = getToolkit().createWindow(this);
- super.addNotify();
- }
- }
- /**
- * Causes this Window to be sized to fit the preferred size
- * and layouts of its subcomponents. If the window and/or its owner
- * are not yet displayable, both are made displayable before
- * calculating the preferred size. The Window will be validated
- * after the preferredSize is calculated.
- * @see Component#isDisplayable
- */
- public void pack() {
- Container parent = this.parent;
- if (parent != null && parent.getPeer() == null) {
- parent.addNotify();
- }
- if (peer == null) {
- addNotify();
- }
- setSize(getPreferredSize());
- if(beforeFirstShow) {
- isPacked = true;
- }
- validate();
- }
- /**
- * Makes the Window visible. If the Window and/or its owner
- * are not yet displayable, both are made displayable. The
- * Window will be validated prior to being made visible.
- * If the Window is already visible, this will bring the Window
- * to the front.
- * @see Component#isDisplayable
- * @see #toFront
- * @see Component#setVisible
- */
- public void show() {
- if (peer == null) {
- addNotify();
- }
- validate();
- if (visible) {
- toFront();
- } else {
- beforeFirstShow = false;
- super.show();
- for (int i = 0; i < ownedWindowList.size(); i++) {
- Window child = (Window) (((WeakReference)
- (ownedWindowList.elementAt(i))).get());
- if ((child != null) && child.showWithParent) {
- child.show();
- child.showWithParent = false;
- } // endif
- } // endfor
- }
- // If first time shown, generate WindowOpened event
- if ((state & OPENED) == 0) {
- postWindowEvent(WindowEvent.WINDOW_OPENED);
- state |= OPENED;
- }
- }
- synchronized void postWindowEvent(int id) {
- if (windowListener != null
- || (eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0
- || Toolkit.enabledOnToolkit(AWTEvent.WINDOW_EVENT_MASK)) {
- WindowEvent e = new WindowEvent(this, id);
- Toolkit.getEventQueue().postEvent(e);
- }
- }
- /**
- * Hide this Window, its subcomponents, and all of its owned children.
- * The Window and its subcomponents can be made visible again
- * with a call to <code>show</code>.
- * </p>
- * @see #show
- * @see #dispose
- */
- public void hide() {
- synchronized(ownedWindowList) {
- for (int i = 0; i < ownedWindowList.size(); i++) {
- Window child = (Window) (((WeakReference)
- (ownedWindowList.elementAt(i))).get());
- if ((child != null) && child.visible) {
- child.hide();
- child.showWithParent = true;
- }
- }
- }
- super.hide();
- }
- final void clearMostRecentFocusOwnerOnHide() {
- /* do nothing */
- }
- /**
- * Releases all of the native screen resources used by this
- * <code>Window</code>, its subcomponents, and all of its owned
- * children. That is, the resources for these <code>Component</code>s
- * will be destroyed, any memory they consume will be returned to the
- * OS, and they will be marked as undisplayable.
- * <p>
- * The <code>Window</code> and its subcomponents can be made displayable
- * again by rebuilding the native resources with a subsequent call to
- * <code>pack</code> or <code>show</code>. The states of the recreated
- * <code>Window</code> and its subcomponents will be identical to the
- * states of these objects at the point where the <code>Window</code>
- * was disposed (not accounting for additional modifications between
- * those actions).
- * <p>
- * <b>Note</b>: When the last displayable window
- * within the Java virtual machine (VM) is disposed of, the VM may
- * terminate. See <a href="doc-files/AWTThreadIssues.html">
- * AWT Threading Issues</a> for more information.
- * @see Component#isDisplayable
- * @see #pack
- * @see #show
- */
- public void dispose() {
- class DisposeAction implements Runnable {
- public void run() {
- synchronized(ownedWindowList) {
- for (int i = 0; i < ownedWindowList.size(); i++) {
- Window child = (Window) (((WeakReference)
- (ownedWindowList.elementAt(i))).get());
- if (child != null) {
- child.dispose();
- }
- }
- }
- setVisible(false);
- beforeFirstShow = true;
- removeNotify();
- synchronized (inputContextLock) {
- if (inputContext != null) {
- inputContext.dispose();
- inputContext = null;
- }
- }
- clearCurrentFocusCycleRootOnHide();
- }
- }
- DisposeAction action = new DisposeAction();
- if (EventQueue.isDispatchThread()) {
- action.run();
- }
- else {
- try {
- EventQueue.invokeAndWait(action);
- }
- catch (InterruptedException e) {
- System.err.println("Disposal was interrupted:");
- e.printStackTrace();
- }
- catch (InvocationTargetException e) {
- System.err.println("Exception during disposal:");
- e.printStackTrace();
- }
- }
- // Execute outside the Runnable because postWindowEvent is
- // synchronized on (this). We don't need to synchronize the call
- // on the EventQueue anyways.
- postWindowEvent(WindowEvent.WINDOW_CLOSED);
- }
- /*
- * Should only be called while holding the tree lock.
- * It's overridden here because parent == owner in Window,
- * and we shouldn't adjust counter on owner
- */
- void adjustListeningChildrenOnParent(long mask, int num) {
- }
- // Should only be called while holding tree lock
- void adjustDecendantsOnParent(int num) {
- // do nothing since parent == owner and we shouldn't
- // ajust counter on owner
- }
- /**
- * If this Window is visible, brings this Window to the front and may make
- * it the focused Window.
- * <p>
- * Places this Window at the top of the stacking order and shows it in
- * front of any other Windows in this VM. No action will take place if this
- * Window is not visible. Some platforms do not allow Windows which own
- * other Windows to appear on top of those owned Windows. Some platforms
- * may not permit this VM to place its Windows above windows of native
- * applications, or Windows of other VMs. This permission may depend on
- * whether a Window in this VM is already focused. Every attempt will be
- * made to move this Window as high as possible in the stacking order;
- * however, developers should not assume that this method will move this
- * Window above all other windows in every situation.
- * <p>
- * Because of variations in native windowing systems, no guarantees about
- * changes to the focused and active Windows can be made. Developers must
- * never assume that this Window is the focused or active Window until this
- * Window receives a WINDOW_GAINED_FOCUS or WINDOW_ACTIVATED event. On
- * platforms where the top-most window is the focused window, this method
- * will <b>probably</b> focus this Window, if it is not already focused. On
- * platforms where the stacking order does not typically affect the focused
- * window, this method will <b>probably</b> leave the focused and active
- * Windows unchanged.
- * <p>
- * If this method causes this Window to be focused, and this Window is a
- * Frame or a Dialog, it will also become activated. If this Window is
- * focused, but it is not a Frame or a Dialog, then the first Frame or
- * Dialog that is an owner of this Window will be activated.
- *
- * @see #toBack
- */
- public void toFront() {
- if (visible) {
- WindowPeer peer = (WindowPeer)this.peer;
- if (peer != null) {
- peer.toFront();
- }
- }
- }
- /**
- * If this Window is visible, sends this Window to the back and may cause
- * it to lose focus or activation if it is the focused or active Window.
- * <p>
- * Places this Window at the bottom of the stacking order and shows it
- * behind any other Windows in this VM. No action will take place is this
- * Window is not visible. Some platforms do not allow Windows which are
- * owned by other Windows to appear below their owners. Every attempt will
- * be made to move this Window as low as possible in the stacking order;
- * however, developers should not assume that this method will move this
- * Window below all other windows in every situation.
- * <p>
- * Because of variations in native windowing systems, no guarantees about
- * changes to the focused and active Windows can be made. Developers must
- * never assume that this Window is no longer the focused or active Window
- * until this Window receives a WINDOW_LOST_FOCUS or WINDOW_DEACTIVATED
- * event. On platforms where the top-most window is the focused window,
- * this method will <b>probably</b> cause this Window to lose focus. In
- * that case, the next highest, focusable Window in this VM will receive
- * focus. On platforms where the stacking order does not typically affect
- * the focused window, this method will <b>probably</b> leave the focused
- * and active Windows unchanged.
- *
- * @see #toFront
- */
- public void toBack() {
- if (visible) {
- WindowPeer peer = (WindowPeer)this.peer;
- if (peer != null) {
- peer.toBack();
- }
- }
- }
- /**
- * Returns the toolkit of this frame.
- * @return the toolkit of this window.
- * @see Toolkit
- * @see Toolkit#getDefaultToolkit
- * @see Component#getToolkit
- */
- public Toolkit getToolkit() {
- return Toolkit.getDefaultToolkit();
- }
- /**
- * Gets the warning string that is displayed with this window.
- * If this window is insecure, the warning string is displayed
- * somewhere in the visible area of the window. A window is
- * insecure if there is a security manager, and the security
- * manager's <code>checkTopLevelWindow</code> method returns
- * <code>false</code> when this window is passed to it as an
- * argument.
- * <p>
- * If the window is secure, then <code>getWarningString</code>
- * returns <code>null</code>. If the window is insecure, this
- * method checks for the system property
- * <code>awt.appletWarning</code>
- * and returns the string value of that property.
- * @return the warning string for this window.
- * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
- */
- public final String getWarningString() {
- return warningString;
- }
- private void setWarningString() {
- warningString = null;
- SecurityManager sm = System.getSecurityManager();
- if (sm != null) {
- if (!sm.checkTopLevelWindow(this)) {
- // make sure the privileged action is only
- // for getting the property! We don't want the
- // above checkTopLevelWindow call to always succeed!
- warningString = (String) AccessController.doPrivileged(
- new GetPropertyAction("awt.appletWarning",
- "Java Applet Window"));
- }
- }
- }
- /**
- * Gets the <code>Locale</code> object that is associated
- * with this window, if the locale has been set.
- * If no locale has been set, then the default locale
- * is returned.
- * @return the locale that is set for this window.
- * @see java.util.Locale
- * @since JDK1.1
- */
- public Locale getLocale() {
- if (this.locale == null) {
- return Locale.getDefault();
- }
- return this.locale;
- }
- /**
- * Gets the input context for this window. A window always has an input context,
- * which is shared by subcomponents unless they create and set their own.
- * @see Component#getInputContext
- * @since 1.2
- */
- public InputContext getInputContext() {
- if (inputContext == null) {
- synchronized (inputContextLock) {
- if (inputContext == null) {
- inputContext = InputContext.getInstance();
- }
- }
- }
- return inputContext;
- }
- /**
- * Set the cursor image to a specified cursor.
- * @param cursor One of the constants defined
- * by the <code>Cursor</code> class. If this parameter is null
- * then the cursor for this window will be set to the type
- * Cursor.DEFAULT_CURSOR.
- * @see Component#getCursor
- * @see Cursor
- * @since JDK1.1
- */
- public void setCursor(Cursor cursor) {
- if (cursor == null) {
- cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
- }
- super.setCursor(cursor);
- }
- /**
- * Returns the owner of this window.
- * @since 1.2
- */
- public Window getOwner() {
- return (Window)parent;
- }
- /**
- * Return an array containing all the windows this
- * window currently owns.
- * @since 1.2
- */
- public Window[] getOwnedWindows() {
- Window realCopy[];
- synchronized(ownedWindowList) {
- // Recall that ownedWindowList is actually a Vector of
- // WeakReferences and calling get() on one of these references
- // may return null. Make two arrays-- one the size of the
- // Vector (fullCopy with size fullSize), and one the size of
- // all non-null get()s (realCopy with size realSize).
- int fullSize = ownedWindowList.size();
- int realSize = 0;
- Window fullCopy[] = new Window[fullSize];
- for (int i = 0; i < fullSize; i++) {
- fullCopy[realSize] = (Window) (((WeakReference)
- (ownedWindowList.elementAt(i))).get());
- if (fullCopy[realSize] != null) {
- realSize++;
- }
- }
- if (fullSize != realSize) {
- realCopy = new Window[realSize];
- System.arraycopy(fullCopy, 0, realCopy, 0, realSize);
- } else {
- realCopy = fullCopy;
- }
- }
- return realCopy;
- }
- /**
- * Adds the specified window listener to receive window events from
- * this window.
- * If l is null, no exception is thrown and no action is performed.
- *
- * @param l the window listener
- * @see #removeWindowListener
- * @see #getWindowListeners
- */
- public synchronized void addWindowListener(WindowListener l) {
- if (l == null) {
- return;
- }
- newEventsOnly = true;
- windowListener = AWTEventMulticaster.add(windowListener, l);
- }
- /**
- * Adds the specified window state listener to receive window
- * events from this window. If <code>l</code> is </code>null</code>,
- * no exception is thrown and no action is performed.
- *
- * @param l the window state listener
- * @see #removeWindowStateListener
- * @see #getWindowStateListeners
- * @since 1.4
- */
- public synchronized void addWindowStateListener(WindowStateListener l) {
- if (l == null) {
- return;
- }
- windowStateListener = AWTEventMulticaster.add(windowStateListener, l);
- newEventsOnly = true;
- }
- /**
- * Adds the specified window focus listener to receive window events
- * from this window.
- * If l is null, no exception is thrown and no action is performed.
- *
- * @param l the window focus listener
- * @see #removeWindowFocusListener
- * @see #getWindowFocusListeners
- */
- public synchronized void addWindowFocusListener(WindowFocusListener l) {
- if (l == null) {
- return;
- }
- windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l);
- newEventsOnly = true;
- }
- /**
- * Removes the specified window listener so that it no longer
- * receives window events from this window.
- * If l is null, no exception is thrown and no action is performed.
- *
- * @param l the window listener
- * @see #addWindowListener
- * @see #getWindowListeners
- */
- public synchronized void removeWindowListener(WindowListener l) {
- if (l == null) {
- return;
- }
- windowListener = AWTEventMulticaster.remove(windowListener, l);
- }
- /**
- * Removes the specified window state listener so that it no
- * longer receives window events from this window. If
- * <code>l</code> is <code>null</code>, no exception is thrown and
- * no action is performed.
- *
- * @param l the window state listener
- * @see #addWindowStateListener
- * @see #getWindowStateListeners
- * @since 1.4
- */
- public synchronized void removeWindowStateListener(WindowStateListener l) {
- if (l == null) {
- return;
- }
- windowStateListener = AWTEventMulticaster.remove(windowStateListener, l);
- }
- /**
- * Removes the specified window focus listener so that it no longer
- * receives window events from this window.
- * If l is null, no exception is thrown and no action is performed.
- *
- * @param l the window focus listener
- * @see #addWindowFocusListener
- * @see #getWindowFocusListeners
- */
- public synchronized void removeWindowFocusListener(WindowFocusListener l) {
- if (l == null) {
- return;
- }
- windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
- }
- /**
- * Returns an array of all the window listeners
- * registered on this window.
- *
- * @return all of this window's <code>WindowListener</code>s
- * or an empty array if no window
- * listeners are currently registered
- *
- * @see #addWindowListener
- * @see #removeWindowListener
- * @since 1.4
- */
- public synchronized WindowListener[] getWindowListeners() {
- return (WindowListener[])(getListeners(WindowListener.class));
- }
- /**
- * Returns an array of all the window focus listeners
- * registered on this window.
- *
- * @return all of this window's <code>WindowFocusListener</code>s
- * or an empty array if no window focus
- * listeners are currently registered
- *
- * @see #addWindowFocusListener
- * @see #removeWindowFocusListener
- * @since 1.4
- */
- public synchronized WindowFocusListener[] getWindowFocusListeners() {
- return (WindowFocusListener[])(getListeners(WindowFocusListener.class));
- }
- /**
- * Returns an array of all the window state listeners
- * registered on this window.
- *
- * @return all of this window's <code>WindowStateListener</code>s
- * or an empty array if no window state
- * listeners are currently registered
- *
- * @see #addWindowStateListener
- * @see #removeWindowStateListener
- * @since 1.4
- */
- public synchronized WindowStateListener[] getWindowStateListeners() {
- return (WindowStateListener[])(getListeners(WindowStateListener.class));
- }
- /**
- * Returns an array of all the objects currently registered
- * as <code><em>Foo</em>Listener</code>s
- * upon this <code>Window</code>.
- * <code><em>Foo</em>Listener</code>s are registered using the
- * <code>add<em>Foo</em>Listener</code> method.
- *
- * <p>
- *
- * You can specify the <code>listenerType</code> argument
- * with a class literal, such as
- * <code><em>Foo</em>Listener.class</code>.
- * For example, you can query a
- * <code>Window</code> <code>w</code>
- * for its window listeners with the following code:
- *
- * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>
- *
- * If no such listeners exist, this method returns an empty array.
- *
- * @param listenerType the type of listeners requested; this parameter
- * should specify an interface that descends from
- * <code>java.util.EventListener</code>
- * @return an array of all objects registered as
- * <code><em>Foo</em>Listener</code>s on this window,
- * or an empty array if no such
- * listeners have been added
- * @exception ClassCastException if <code>listenerType</code>
- * doesn't specify a class or interface that implements
- * <code>java.util.EventListener</code>
- *
- * @see #getWindowListeners
- * @since 1.3
- */
- public EventListener[] getListeners(Class listenerType) {
- EventListener l = null;
- if (listenerType == WindowFocusListener.class) {
- l = windowFocusListener;
- } else if (listenerType == WindowStateListener.class) {
- l = windowStateListener;
- } else if (listenerType == WindowListener.class) {
- l = windowListener;
- } else {
- return super.getListeners(listenerType);
- }
- return (EventListener[])AWTEventMulticaster.getListeners(l, listenerType);
- }
- // REMIND: remove when filtering is handled at lower level
- boolean eventEnabled(AWTEvent e) {
- switch(e.id) {
- case WindowEvent.WINDOW_OPENED:
- case WindowEvent.WINDOW_CLOSING:
- case WindowEvent.WINDOW_CLOSED:
- case WindowEvent.WINDOW_ICONIFIED:
- case WindowEvent.WINDOW_DEICONIFIED:
- case WindowEvent.WINDOW_ACTIVATED:
- case WindowEvent.WINDOW_DEACTIVATED:
- if ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0 ||
- windowListener != null) {
- return true;
- }
- return false;
- case WindowEvent.WINDOW_GAINED_FOCUS:
- case WindowEvent.WINDOW_LOST_FOCUS:
- if ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0 ||
- windowFocusListener != null) {
- return true;
- }
- return false;
- case WindowEvent.WINDOW_STATE_CHANGED:
- if ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0 ||
- windowStateListener != null) {
- return true;
- }
- return false;
- default:
- break;
- }
- return super.eventEnabled(e);
- }
- /**
- * Processes events on this window. If the event is an
- * <code>WindowEvent</code>, it invokes the
- * <code>processWindowEvent</code> method, else it invokes its
- * superclass's <code>processEvent</code>.
- * <p>Note that if the event parameter is <code>null</code>
- * the behavior is unspecified and may result in an
- * exception.
- *
- * @param e the event
- */
- protected void processEvent(AWTEvent e) {
- if (e instanceof WindowEvent) {
- switch (e.getID()) {
- case WindowEvent.WINDOW_OPENED:
- case WindowEvent.WINDOW_CLOSING:
- case WindowEvent.WINDOW_CLOSED:
- case WindowEvent.WINDOW_ICONIFIED:
- case WindowEvent.WINDOW_DEICONIFIED:
- case WindowEvent.WINDOW_ACTIVATED:
- case WindowEvent.WINDOW_DEACTIVATED:
- processWindowEvent((WindowEvent)e);
- break;
- case WindowEvent.WINDOW_GAINED_FOCUS:
- case WindowEvent.WINDOW_LOST_FOCUS:
- processWindowFocusEvent((WindowEvent)e);
- break;
- case WindowEvent.WINDOW_STATE_CHANGED:
- processWindowStateEvent((WindowEvent)e);
- default:
- break;
- }
- return;
- }
- super.processEvent(e);
- }
- /**
- * Processes window events occurring on this window by
- * dispatching them to any registered WindowListener objects.
- * NOTE: This method will not be called unless window events
- * are enabled for this component; this happens when one of the
- * following occurs:
- * <ul>
- * <li>A WindowListener object is registered via
- * <code>addWindowListener</code>
- * <li>Window events are enabled via <code>enableEvents</code>
- * </ul>
- * <p>Note that if the event parameter is <code>null</code>
- * the behavior is unspecified and may result in an
- * exception.
- *
- * @param e the window event
- * @see Component#enableEvents
- */
- protected void processWindowEvent(WindowEvent e) {
- WindowListener listener = windowListener;
- if (listener != null) {
- switch(e.getID()) {
- case WindowEvent.WINDOW_OPENED:
- listener.windowOpened(e);
- break;
- case WindowEvent.WINDOW_CLOSING:
- listener.windowClosing(e);
- break;
- case WindowEvent.WINDOW_CLOSED:
- listener.windowClosed(e);
- break;
- case WindowEvent.WINDOW_ICONIFIED:
- listener.windowIconified(e);
- break;
- case WindowEvent.WINDOW_DEICONIFIED:
- listener.windowDeiconified(e);
- break;
- case WindowEvent.WINDOW_ACTIVATED:
- listener.windowActivated(e);
- break;
- case WindowEvent.WINDOW_DEACTIVATED:
- listener.windowDeactivated(e);
- break;
- default:
- break;
- }
- }
- }
- /**
- * Processes window focus event occuring on this window by
- * dispatching them to any registered WindowFocusListener objects.
- * NOTE: this method will not be called unless window focus events
- * are enabled for this window. This happens when one of the
- * following occurs:
- * <ul>
- * <li>a WindowFocusListener is registered via
- * <code>addWindowFocusListener</code>
- * <li>Window focus events are enabled via <code>enableEvents</code>
- * </ul>
- * <p>Note that if the event parameter is <code>null</code>
- * the behavior is unspecified and may result in an
- * exception.
- *
- * @param e the window focus event
- * @see Component#enableEvents
- */
- protected void processWindowFocusEvent(WindowEvent e) {
- WindowFocusListener listener = windowFocusListener;
- if (listener != null) {
- switch (e.getID()) {
- case WindowEvent.WINDOW_GAINED_FOCUS:
- listener.windowGainedFocus(e);
- break;
- case WindowEvent.WINDOW_LOST_FOCUS:
- listener.windowLostFocus(e);
- break;
- default:
- break;
- }
- }
- }
- /**
- * Processes window state event occuring on this window by
- * dispatching them to any registered <code>WindowStateListener</code>
- * objects.
- * NOTE: this method will not be called unless window state events
- * are enabled for this window. This happens when one of the
- * following occurs:
- * <ul>
- * <li>a <code>WindowStateListener</code> is registered via
- * <code>addWindowStateListener</code>
- * <li>window state events are enabled via <code>enableEvents</code>
- * </ul>
- * <p>Note that if the event parameter is <code>null</code>
- * the behavior is unspecified and may result in an
- * exception.
- *
- * @param e the window state event
- * @see java.awt.Component#enableEvents
- * @since 1.4
- */
- protected void processWindowStateEvent(WindowEvent e) {
- WindowStateListener listener = windowStateListener;
- if (listener != null) {
- switch (e.getID()) {
- case WindowEvent.WINDOW_STATE_CHANGED:
- listener.windowStateChanged(e);
- break;
- default:
- break;
- }
- }
- }
- /**
- * Implements a debugging hook -- checks to see if
- * the user has typed <i>control-shift-F1</i>. If so,
- * the list of child windows is dumped to <code>System.out</code>.
- * @param e the keyboard event
- */
- void preProcessKeyEvent(KeyEvent e) {
- // Dump the list of child windows to System.out.
- if (e.isActionKey() && e.getKeyCode() == KeyEvent.VK_F1 &&
- e.isControlDown() && e.isShiftDown() &&
- e.getID() == KeyEvent.KEY_PRESSED) {
- list(System.out, 0);
- }
- }
- void postProcessKeyEvent(KeyEvent e) {
- // Do nothing
- }
- /**
- * Returns the child Component of this Window that has focus if this Window
- * is focused; returns null otherwise.
- *
- * @return the child Component with focus, or null if this Window is not
- * focused
- * @see #getMostRecentFocusOwner
- * @see #isFocused
- */
- public Component getFocusOwner() {
- return (isFocused())
- ? KeyboardFocusManager.getCurrentKeyboardFocusManager().
- getFocusOwner()
- : null;
- }
- /**
- * Returns the child Component of this Window that will receive the focus
- * when this Window is focused. If this Window is currently focused, this
- * method returns the same Component as <code>getFocusOwner()</code>. If
- * this Window is not focused, then the child Component that most recently
- * requested focus will be returned. If no child Component has ever
- * requested focus, and this is a focusable Window, then this Window's
- * initial focusable Component is returned. If no child Component has ever
- * requested focus, and this is a non-focusable Window, null is returned.
- *
- * @return the child Component that will receive focus when this Window is
- * focused
- * @see #getFocusOwner
- * @see #isFocused
- * @see #isFocusableWindow
- * @since 1.4
- */
- public Component getMostRecentFocusOwner() {
- if (isFocused()) {
- return getFocusOwner();
- } else {
- Component mostRecent =
- KeyboardFocusManager.getMostRecentFocusOwner(this);
- if (mostRecent != null) {
- return mostRecent;
- } else {
- return (isFocusableWindow())
- ? getFocusTraversalPolicy().getInitialComponent(this)
- : null;
- }
- }
- }