src/share/classes/java/awt/Window.java

Print this page




 424      * must be displayed with a warning banner.
 425      *
 426      * @param gc the <code>GraphicsConfiguration</code> of the target screen
 427      *     device. If <code>gc</code> is <code>null</code>, the system default
 428      *     <code>GraphicsConfiguration</code> is assumed
 429      * @exception IllegalArgumentException if <code>gc</code>
 430      *    is not from a screen device
 431      * @exception HeadlessException when
 432      *     <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
 433      *
 434      * @see java.awt.GraphicsEnvironment#isHeadless
 435      * @see java.lang.SecurityManager#checkTopLevelWindow
 436      */
 437     Window(GraphicsConfiguration gc) {
 438         init(gc);
 439     }
 440 
 441     transient Object anchor = new Object();
 442     static class WindowDisposerRecord implements sun.java2d.DisposerRecord {
 443         final WeakReference<Window> owner;
 444         final WeakReference weakThis;
 445         final WeakReference<AppContext> context;
 446         WindowDisposerRecord(AppContext context, Window victim) {
 447             owner = new WeakReference<Window>(victim.getOwner());
 448             weakThis = victim.weakThis;
 449             this.context = new WeakReference<AppContext>(context);
 450         }
 451         public void dispose() {
 452             Window parent = owner.get();
 453             if (parent != null) {
 454                 parent.removeOwnedWindow(weakThis);
 455             }
 456             AppContext ac = context.get();
 457             if (null != ac) {
 458                 Window.removeFromWindowList(ac, weakThis);
 459             }
 460         }
 461     }
 462 
 463     private GraphicsConfiguration initGC(GraphicsConfiguration gc) {
 464         GraphicsEnvironment.checkHeadless();


1849     public synchronized void removeWindowFocusListener(WindowFocusListener l) {
1850         if (l == null) {
1851             return;
1852         }
1853         windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
1854     }
1855 
1856     /**
1857      * Returns an array of all the window listeners
1858      * registered on this window.
1859      *
1860      * @return all of this window's <code>WindowListener</code>s
1861      *         or an empty array if no window
1862      *         listeners are currently registered
1863      *
1864      * @see #addWindowListener
1865      * @see #removeWindowListener
1866      * @since 1.4
1867      */
1868     public synchronized WindowListener[] getWindowListeners() {
1869         return (WindowListener[])(getListeners(WindowListener.class));
1870     }
1871 
1872     /**
1873      * Returns an array of all the window focus listeners
1874      * registered on this window.
1875      *
1876      * @return all of this window's <code>WindowFocusListener</code>s
1877      *         or an empty array if no window focus
1878      *         listeners are currently registered
1879      *
1880      * @see #addWindowFocusListener
1881      * @see #removeWindowFocusListener
1882      * @since 1.4
1883      */
1884     public synchronized WindowFocusListener[] getWindowFocusListeners() {
1885         return (WindowFocusListener[])(getListeners(WindowFocusListener.class));
1886     }
1887 
1888     /**
1889      * Returns an array of all the window state listeners
1890      * registered on this window.
1891      *
1892      * @return all of this window's <code>WindowStateListener</code>s
1893      *         or an empty array if no window state
1894      *         listeners are currently registered
1895      *
1896      * @see #addWindowStateListener
1897      * @see #removeWindowStateListener
1898      * @since 1.4
1899      */
1900     public synchronized WindowStateListener[] getWindowStateListeners() {
1901         return (WindowStateListener[])(getListeners(WindowStateListener.class));
1902     }
1903 
1904 
1905     /**
1906      * Returns an array of all the objects currently registered
1907      * as <code><em>Foo</em>Listener</code>s
1908      * upon this <code>Window</code>.
1909      * <code><em>Foo</em>Listener</code>s are registered using the
1910      * <code>add<em>Foo</em>Listener</code> method.
1911      *
1912      * <p>
1913      *
1914      * You can specify the <code>listenerType</code> argument
1915      * with a class literal, such as
1916      * <code><em>Foo</em>Listener.class</code>.
1917      * For example, you can query a
1918      * <code>Window</code> <code>w</code>
1919      * for its window listeners with the following code:
1920      *
1921      * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>


1979                 return true;
1980             }
1981             return false;
1982           default:
1983             break;
1984         }
1985         return super.eventEnabled(e);
1986     }
1987 
1988     /**
1989      * Processes events on this window. If the event is an
1990      * <code>WindowEvent</code>, it invokes the
1991      * <code>processWindowEvent</code> method, else it invokes its
1992      * superclass's <code>processEvent</code>.
1993      * <p>Note that if the event parameter is <code>null</code>
1994      * the behavior is unspecified and may result in an
1995      * exception.
1996      *
1997      * @param e the event
1998      */

1999     protected void processEvent(AWTEvent e) {
2000         if (e instanceof WindowEvent) {
2001             switch (e.getID()) {
2002                 case WindowEvent.WINDOW_OPENED:
2003                 case WindowEvent.WINDOW_CLOSING:
2004                 case WindowEvent.WINDOW_CLOSED:
2005                 case WindowEvent.WINDOW_ICONIFIED:
2006                 case WindowEvent.WINDOW_DEICONIFIED:
2007                 case WindowEvent.WINDOW_ACTIVATED:
2008                 case WindowEvent.WINDOW_DEACTIVATED:
2009                     processWindowEvent((WindowEvent)e);
2010                     break;
2011                 case WindowEvent.WINDOW_GAINED_FOCUS:
2012                 case WindowEvent.WINDOW_LOST_FOCUS:
2013                     processWindowFocusEvent((WindowEvent)e);
2014                     break;
2015                 case WindowEvent.WINDOW_STATE_CHANGED:
2016                     processWindowStateEvent((WindowEvent)e);
2017                 default:
2018                     break;


2371      *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
2372      * @return the AWTKeyStroke for the specified key
2373      * @see Container#setFocusTraversalKeys
2374      * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
2375      * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
2376      * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
2377      * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
2378      * @throws IllegalArgumentException if id is not one of
2379      *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
2380      *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
2381      *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
2382      *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
2383      * @since 1.4
2384      */
2385     public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
2386         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
2387             throw new IllegalArgumentException("invalid focus traversal key identifier");
2388         }
2389 
2390         // Okay to return Set directly because it is an unmodifiable view
2391         Set keystrokes = (focusTraversalKeys != null)
2392             ? focusTraversalKeys[id]
2393             : null;
2394 
2395         if (keystrokes != null) {
2396             return keystrokes;
2397         } else {
2398             return KeyboardFocusManager.getCurrentKeyboardFocusManager().
2399                 getDefaultFocusTraversalKeys(id);
2400         }
2401     }
2402 
2403     /**
2404      * Does nothing because Windows must always be roots of a focus traversal
2405      * cycle. The passed-in value is ignored.
2406      *
2407      * @param focusCycleRoot this value is ignored
2408      * @see #isFocusCycleRoot
2409      * @see Container#setFocusTraversalPolicy
2410      * @see Container#getFocusTraversalPolicy
2411      * @since 1.4


2784             ownedWindowList.removeElement(weakWindow);
2785         }
2786     }
2787 
2788     void connectOwnedWindow(Window child) {
2789         child.parent = this;
2790         addOwnedWindow(child.weakThis);
2791     }
2792 
2793     private void addToWindowList() {
2794         synchronized (Window.class) {
2795             Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)appContext.get(Window.class);
2796             if (windowList == null) {
2797                 windowList = new Vector<WeakReference<Window>>();
2798                 appContext.put(Window.class, windowList);
2799             }
2800             windowList.add(weakThis);
2801         }
2802     }
2803 
2804     private static void removeFromWindowList(AppContext context, WeakReference weakThis) {
2805         synchronized (Window.class) {
2806             Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)context.get(Window.class);
2807             if (windowList != null) {
2808                 windowList.remove(weakThis);
2809             }
2810         }
2811     }
2812 
2813     private void removeFromWindowList() {
2814         removeFromWindowList(appContext, weakThis);
2815     }
2816 
2817     /**
2818      * Window type.
2819      *
2820      * Synchronization: ObjectLock
2821      */
2822     private Type type = Type.NORMAL;
2823 
2824     /**


2928             for (Image i : icons) {
2929                 if (i instanceof Serializable) {
2930                     s.writeObject(i);
2931                 }
2932             }
2933         }
2934         s.writeObject(null);
2935     }
2936 
2937     //
2938     // Part of deserialization procedure to be called before
2939     // user's code.
2940     //
2941     private void initDeserializedWindow() {
2942         setWarningString();
2943         inputContextLock = new Object();
2944 
2945         // Deserialized Windows are not yet visible.
2946         visible = false;
2947 
2948         weakThis = new WeakReference(this);
2949 
2950         anchor = new Object();
2951         sun.java2d.Disposer.addRecord(anchor, new WindowDisposerRecord(appContext, this));
2952 
2953         addToWindowList();
2954         initGC(null);
2955     }
2956 
2957     private void deserializeResources(ObjectInputStream s)
2958         throws ClassNotFoundException, IOException, HeadlessException {
2959             ownedWindowList = new Vector();
2960 
2961             if (windowSerializedDataVersion < 2) {
2962                 // Translate old-style focus tracking to new model. For 1.4 and
2963                 // later releases, we'll rely on the Window's initial focusable
2964                 // Component.
2965                 if (focusMgr != null) {
2966                     if (focusMgr.focusOwner != null) {
2967                         KeyboardFocusManager.
2968                             setMostRecentFocusOwner(this, focusMgr.focusOwner);
2969                     }
2970                 }
2971 
2972                 // This field is non-transient and relies on default serialization.
2973                 // However, the default value is insufficient, so we need to set
2974                 // it explicitly for object data streams prior to 1.4.
2975                 focusableWindowState = true;
2976 
2977 
2978             }
2979 




 424      * must be displayed with a warning banner.
 425      *
 426      * @param gc the <code>GraphicsConfiguration</code> of the target screen
 427      *     device. If <code>gc</code> is <code>null</code>, the system default
 428      *     <code>GraphicsConfiguration</code> is assumed
 429      * @exception IllegalArgumentException if <code>gc</code>
 430      *    is not from a screen device
 431      * @exception HeadlessException when
 432      *     <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
 433      *
 434      * @see java.awt.GraphicsEnvironment#isHeadless
 435      * @see java.lang.SecurityManager#checkTopLevelWindow
 436      */
 437     Window(GraphicsConfiguration gc) {
 438         init(gc);
 439     }
 440 
 441     transient Object anchor = new Object();
 442     static class WindowDisposerRecord implements sun.java2d.DisposerRecord {
 443         final WeakReference<Window> owner;
 444         final WeakReference<Window> weakThis;
 445         final WeakReference<AppContext> context;
 446         WindowDisposerRecord(AppContext context, Window victim) {
 447             owner = new WeakReference<Window>(victim.getOwner());
 448             weakThis = victim.weakThis;
 449             this.context = new WeakReference<AppContext>(context);
 450         }
 451         public void dispose() {
 452             Window parent = owner.get();
 453             if (parent != null) {
 454                 parent.removeOwnedWindow(weakThis);
 455             }
 456             AppContext ac = context.get();
 457             if (null != ac) {
 458                 Window.removeFromWindowList(ac, weakThis);
 459             }
 460         }
 461     }
 462 
 463     private GraphicsConfiguration initGC(GraphicsConfiguration gc) {
 464         GraphicsEnvironment.checkHeadless();


1849     public synchronized void removeWindowFocusListener(WindowFocusListener l) {
1850         if (l == null) {
1851             return;
1852         }
1853         windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
1854     }
1855 
1856     /**
1857      * Returns an array of all the window listeners
1858      * registered on this window.
1859      *
1860      * @return all of this window's <code>WindowListener</code>s
1861      *         or an empty array if no window
1862      *         listeners are currently registered
1863      *
1864      * @see #addWindowListener
1865      * @see #removeWindowListener
1866      * @since 1.4
1867      */
1868     public synchronized WindowListener[] getWindowListeners() {
1869         return getListeners(WindowListener.class);
1870     }
1871 
1872     /**
1873      * Returns an array of all the window focus listeners
1874      * registered on this window.
1875      *
1876      * @return all of this window's <code>WindowFocusListener</code>s
1877      *         or an empty array if no window focus
1878      *         listeners are currently registered
1879      *
1880      * @see #addWindowFocusListener
1881      * @see #removeWindowFocusListener
1882      * @since 1.4
1883      */
1884     public synchronized WindowFocusListener[] getWindowFocusListeners() {
1885         return getListeners(WindowFocusListener.class);
1886     }
1887 
1888     /**
1889      * Returns an array of all the window state listeners
1890      * registered on this window.
1891      *
1892      * @return all of this window's <code>WindowStateListener</code>s
1893      *         or an empty array if no window state
1894      *         listeners are currently registered
1895      *
1896      * @see #addWindowStateListener
1897      * @see #removeWindowStateListener
1898      * @since 1.4
1899      */
1900     public synchronized WindowStateListener[] getWindowStateListeners() {
1901         return getListeners(WindowStateListener.class);
1902     }
1903 
1904 
1905     /**
1906      * Returns an array of all the objects currently registered
1907      * as <code><em>Foo</em>Listener</code>s
1908      * upon this <code>Window</code>.
1909      * <code><em>Foo</em>Listener</code>s are registered using the
1910      * <code>add<em>Foo</em>Listener</code> method.
1911      *
1912      * <p>
1913      *
1914      * You can specify the <code>listenerType</code> argument
1915      * with a class literal, such as
1916      * <code><em>Foo</em>Listener.class</code>.
1917      * For example, you can query a
1918      * <code>Window</code> <code>w</code>
1919      * for its window listeners with the following code:
1920      *
1921      * <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>


1979                 return true;
1980             }
1981             return false;
1982           default:
1983             break;
1984         }
1985         return super.eventEnabled(e);
1986     }
1987 
1988     /**
1989      * Processes events on this window. If the event is an
1990      * <code>WindowEvent</code>, it invokes the
1991      * <code>processWindowEvent</code> method, else it invokes its
1992      * superclass's <code>processEvent</code>.
1993      * <p>Note that if the event parameter is <code>null</code>
1994      * the behavior is unspecified and may result in an
1995      * exception.
1996      *
1997      * @param e the event
1998      */
1999     @SuppressWarnings("fallthrough")
2000     protected void processEvent(AWTEvent e) {
2001         if (e instanceof WindowEvent) {
2002             switch (e.getID()) {
2003                 case WindowEvent.WINDOW_OPENED:
2004                 case WindowEvent.WINDOW_CLOSING:
2005                 case WindowEvent.WINDOW_CLOSED:
2006                 case WindowEvent.WINDOW_ICONIFIED:
2007                 case WindowEvent.WINDOW_DEICONIFIED:
2008                 case WindowEvent.WINDOW_ACTIVATED:
2009                 case WindowEvent.WINDOW_DEACTIVATED:
2010                     processWindowEvent((WindowEvent)e);
2011                     break;
2012                 case WindowEvent.WINDOW_GAINED_FOCUS:
2013                 case WindowEvent.WINDOW_LOST_FOCUS:
2014                     processWindowFocusEvent((WindowEvent)e);
2015                     break;
2016                 case WindowEvent.WINDOW_STATE_CHANGED:
2017                     processWindowStateEvent((WindowEvent)e);
2018                 default:
2019                     break;


2372      *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
2373      * @return the AWTKeyStroke for the specified key
2374      * @see Container#setFocusTraversalKeys
2375      * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
2376      * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
2377      * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
2378      * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
2379      * @throws IllegalArgumentException if id is not one of
2380      *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
2381      *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
2382      *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
2383      *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
2384      * @since 1.4
2385      */
2386     public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
2387         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
2388             throw new IllegalArgumentException("invalid focus traversal key identifier");
2389         }
2390 
2391         // Okay to return Set directly because it is an unmodifiable view
2392         Set<AWTKeyStroke> keystrokes = (focusTraversalKeys != null)
2393             ? focusTraversalKeys[id]
2394             : null;
2395 
2396         if (keystrokes != null) {
2397             return keystrokes;
2398         } else {
2399             return KeyboardFocusManager.getCurrentKeyboardFocusManager().
2400                 getDefaultFocusTraversalKeys(id);
2401         }
2402     }
2403 
2404     /**
2405      * Does nothing because Windows must always be roots of a focus traversal
2406      * cycle. The passed-in value is ignored.
2407      *
2408      * @param focusCycleRoot this value is ignored
2409      * @see #isFocusCycleRoot
2410      * @see Container#setFocusTraversalPolicy
2411      * @see Container#getFocusTraversalPolicy
2412      * @since 1.4


2785             ownedWindowList.removeElement(weakWindow);
2786         }
2787     }
2788 
2789     void connectOwnedWindow(Window child) {
2790         child.parent = this;
2791         addOwnedWindow(child.weakThis);
2792     }
2793 
2794     private void addToWindowList() {
2795         synchronized (Window.class) {
2796             Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)appContext.get(Window.class);
2797             if (windowList == null) {
2798                 windowList = new Vector<WeakReference<Window>>();
2799                 appContext.put(Window.class, windowList);
2800             }
2801             windowList.add(weakThis);
2802         }
2803     }
2804 
2805     private static void removeFromWindowList(AppContext context, WeakReference<Window> weakThis) {
2806         synchronized (Window.class) {
2807             Vector<WeakReference<Window>> windowList = (Vector<WeakReference<Window>>)context.get(Window.class);
2808             if (windowList != null) {
2809                 windowList.remove(weakThis);
2810             }
2811         }
2812     }
2813 
2814     private void removeFromWindowList() {
2815         removeFromWindowList(appContext, weakThis);
2816     }
2817 
2818     /**
2819      * Window type.
2820      *
2821      * Synchronization: ObjectLock
2822      */
2823     private Type type = Type.NORMAL;
2824 
2825     /**


2929             for (Image i : icons) {
2930                 if (i instanceof Serializable) {
2931                     s.writeObject(i);
2932                 }
2933             }
2934         }
2935         s.writeObject(null);
2936     }
2937 
2938     //
2939     // Part of deserialization procedure to be called before
2940     // user's code.
2941     //
2942     private void initDeserializedWindow() {
2943         setWarningString();
2944         inputContextLock = new Object();
2945 
2946         // Deserialized Windows are not yet visible.
2947         visible = false;
2948 
2949         weakThis = new WeakReference<Window>(this);
2950 
2951         anchor = new Object();
2952         sun.java2d.Disposer.addRecord(anchor, new WindowDisposerRecord(appContext, this));
2953 
2954         addToWindowList();
2955         initGC(null);
2956     }
2957 
2958     private void deserializeResources(ObjectInputStream s)
2959         throws ClassNotFoundException, IOException, HeadlessException {
2960             ownedWindowList = new Vector<WeakReference<Window>>();
2961 
2962             if (windowSerializedDataVersion < 2) {
2963                 // Translate old-style focus tracking to new model. For 1.4 and
2964                 // later releases, we'll rely on the Window's initial focusable
2965                 // Component.
2966                 if (focusMgr != null) {
2967                     if (focusMgr.focusOwner != null) {
2968                         KeyboardFocusManager.
2969                             setMostRecentFocusOwner(this, focusMgr.focusOwner);
2970                     }
2971                 }
2972 
2973                 // This field is non-transient and relies on default serialization.
2974                 // However, the default value is insufficient, so we need to set
2975                 // it explicitly for object data streams prior to 1.4.
2976                 focusableWindowState = true;
2977 
2978 
2979             }
2980