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

Print this page


   1 /*
   2  * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1435      * The method may have no visual effect if the Java platform
1436      * implementation and/or the native system do not support
1437      * changing the mouse cursor shape.
1438      * @param     cursor One of the constants defined
1439      *            by the {@code Cursor} class. If this parameter is null
1440      *            then the cursor for this window will be set to the type
1441      *            Cursor.DEFAULT_CURSOR.
1442      * @see       Component#getCursor
1443      * @see       Cursor
1444      * @since     JDK1.1
1445      */
1446     public void setCursor(Cursor cursor) {
1447         if (cursor == null) {
1448             cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
1449         }
1450         super.setCursor(cursor);
1451     }
1452 
1453     /**
1454      * Returns the owner of this window.

1455      * @since 1.2
1456      */
1457     public Window getOwner() {
1458         return getOwner_NoClientCode();
1459     }
1460     final Window getOwner_NoClientCode() {
1461         return (Window)parent;
1462     }
1463 
1464     /**
1465      * Return an array containing all the windows this
1466      * window currently owns.

1467      * @since 1.2
1468      */
1469     public Window[] getOwnedWindows() {
1470         return getOwnedWindows_NoClientCode();
1471     }
1472     final Window[] getOwnedWindows_NoClientCode() {
1473         Window realCopy[];
1474 
1475         synchronized(ownedWindowList) {
1476             // Recall that ownedWindowList is actually a Vector of
1477             // WeakReferences and calling get() on one of these references
1478             // may return null. Make two arrays-- one the size of the
1479             // Vector (fullCopy with size fullSize), and one the size of
1480             // all non-null get()s (realCopy with size realSize).
1481             int fullSize = ownedWindowList.size();
1482             int realSize = 0;
1483             Window fullCopy[] = new Window[fullSize];
1484 
1485             for (int i = 0; i < fullSize; i++) {
1486                 fullCopy[realSize] = ownedWindowList.elementAt(i).get();


1569                 }
1570             } else {
1571                 realCopy = new Window[0];
1572             }
1573             return realCopy;
1574         }
1575     }
1576 
1577     /**
1578      * Returns an array of all {@code Window}s, both owned and ownerless,
1579      * created by this application.
1580      * If called from an applet, the array includes only the {@code Window}s
1581      * accessible by that applet.
1582      * <p>
1583      * <b>Warning:</b> this method may return system created windows, such
1584      * as a print dialog. Applications should not assume the existence of
1585      * these dialogs, nor should an application assume anything about these
1586      * dialogs such as component positions, {@code LayoutManager}s
1587      * or serialization.
1588      *

1589      * @see Frame#getFrames
1590      * @see Window#getOwnerlessWindows
1591      *
1592      * @since 1.6
1593      */
1594     public static Window[] getWindows() {
1595         return getWindows(AppContext.getAppContext());
1596     }
1597 
1598     /**
1599      * Returns an array of all {@code Window}s created by this application
1600      * that have no owner. They include {@code Frame}s and ownerless
1601      * {@code Dialog}s and {@code Window}s.
1602      * If called from an applet, the array includes only the {@code Window}s
1603      * accessible by that applet.
1604      * <p>
1605      * <b>Warning:</b> this method may return system created windows, such
1606      * as a print dialog. Applications should not assume the existence of
1607      * these dialogs, nor should an application assume anything about these
1608      * dialogs such as component positions, {@code LayoutManager}s
1609      * or serialization.
1610      *


1611      * @see Frame#getFrames
1612      * @see Window#getWindows()
1613      *
1614      * @since 1.6
1615      */
1616     public static Window[] getOwnerlessWindows() {
1617         Window[] allWindows = Window.getWindows();
1618 
1619         int ownerlessCount = 0;
1620         for (Window w : allWindows) {
1621             if (w.getOwner() == null) {
1622                 ownerlessCount++;
1623             }
1624         }
1625 
1626         Window[] ownerless = new Window[ownerlessCount];
1627         int c = 0;
1628         for (Window w : allWindows) {
1629             if (w.getOwner() == null) {
1630                 ownerless[c++] = w;


2832             }
2833         }
2834     }
2835 
2836     private void removeFromWindowList() {
2837         removeFromWindowList(appContext, weakThis);
2838     }
2839 
2840     /**
2841      * Window type.
2842      *
2843      * Synchronization: ObjectLock
2844      */
2845     private Type type = Type.NORMAL;
2846 
2847     /**
2848      * Sets the type of the window.
2849      *
2850      * This method can only be called while the window is not displayable.
2851      *

2852      * @throws IllegalComponentStateException if the window
2853      *         is displayable.
2854      * @throws IllegalArgumentException if the type is {@code null}
2855      * @see    Component#isDisplayable
2856      * @see    #getType
2857      * @since 1.7
2858      */
2859     public void setType(Type type) {
2860         if (type == null) {
2861             throw new IllegalArgumentException("type should not be null.");
2862         }
2863         synchronized (getTreeLock()) {
2864             if (isDisplayable()) {
2865                 throw new IllegalComponentStateException(
2866                         "The window is displayable.");
2867             }
2868             synchronized (getObjectLock()) {
2869                 this.type = type;
2870             }
2871         }
2872     }
2873 
2874     /**
2875      * Returns the type of the window.
2876      *
2877      * @see   #setType
2878      * @since 1.7
2879      */
2880     public Type getType() {
2881         synchronized (getObjectLock()) {
2882             return type;
2883         }
2884     }
2885 
2886     /**
2887      * The window serialized data version.
2888      *
2889      * @serial
2890      */
2891     private int windowSerializedDataVersion = 2;
2892 
2893     /**
2894      * Writes default serializable fields to stream.  Writes
2895      * a list of serializable {@code WindowListener}s and
2896      * {@code WindowFocusListener}s as optional data.


   1 /*
   2  * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1435      * The method may have no visual effect if the Java platform
1436      * implementation and/or the native system do not support
1437      * changing the mouse cursor shape.
1438      * @param     cursor One of the constants defined
1439      *            by the {@code Cursor} class. If this parameter is null
1440      *            then the cursor for this window will be set to the type
1441      *            Cursor.DEFAULT_CURSOR.
1442      * @see       Component#getCursor
1443      * @see       Cursor
1444      * @since     JDK1.1
1445      */
1446     public void setCursor(Cursor cursor) {
1447         if (cursor == null) {
1448             cursor = Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR);
1449         }
1450         super.setCursor(cursor);
1451     }
1452 
1453     /**
1454      * Returns the owner of this window.
1455      * @return  the owner of this window
1456      * @since 1.2
1457      */
1458     public Window getOwner() {
1459         return getOwner_NoClientCode();
1460     }
1461     final Window getOwner_NoClientCode() {
1462         return (Window)parent;
1463     }
1464 
1465     /**
1466      * Return an array containing all the windows this
1467      * window currently owns.
1468      * @return  the array of all the owned windows
1469      * @since 1.2
1470      */
1471     public Window[] getOwnedWindows() {
1472         return getOwnedWindows_NoClientCode();
1473     }
1474     final Window[] getOwnedWindows_NoClientCode() {
1475         Window realCopy[];
1476 
1477         synchronized(ownedWindowList) {
1478             // Recall that ownedWindowList is actually a Vector of
1479             // WeakReferences and calling get() on one of these references
1480             // may return null. Make two arrays-- one the size of the
1481             // Vector (fullCopy with size fullSize), and one the size of
1482             // all non-null get()s (realCopy with size realSize).
1483             int fullSize = ownedWindowList.size();
1484             int realSize = 0;
1485             Window fullCopy[] = new Window[fullSize];
1486 
1487             for (int i = 0; i < fullSize; i++) {
1488                 fullCopy[realSize] = ownedWindowList.elementAt(i).get();


1571                 }
1572             } else {
1573                 realCopy = new Window[0];
1574             }
1575             return realCopy;
1576         }
1577     }
1578 
1579     /**
1580      * Returns an array of all {@code Window}s, both owned and ownerless,
1581      * created by this application.
1582      * If called from an applet, the array includes only the {@code Window}s
1583      * accessible by that applet.
1584      * <p>
1585      * <b>Warning:</b> this method may return system created windows, such
1586      * as a print dialog. Applications should not assume the existence of
1587      * these dialogs, nor should an application assume anything about these
1588      * dialogs such as component positions, {@code LayoutManager}s
1589      * or serialization.
1590      *
1591      * @return  the array of all the {@code Window}s created by the application
1592      * @see Frame#getFrames
1593      * @see Window#getOwnerlessWindows
1594      *
1595      * @since 1.6
1596      */
1597     public static Window[] getWindows() {
1598         return getWindows(AppContext.getAppContext());
1599     }
1600 
1601     /**
1602      * Returns an array of all {@code Window}s created by this application
1603      * that have no owner. They include {@code Frame}s and ownerless
1604      * {@code Dialog}s and {@code Window}s.
1605      * If called from an applet, the array includes only the {@code Window}s
1606      * accessible by that applet.
1607      * <p>
1608      * <b>Warning:</b> this method may return system created windows, such
1609      * as a print dialog. Applications should not assume the existence of
1610      * these dialogs, nor should an application assume anything about these
1611      * dialogs such as component positions, {@code LayoutManager}s
1612      * or serialization.
1613      *
1614      * @return  the array of all the ownerless {@code Window}s
1615      *          created by this application
1616      * @see Frame#getFrames
1617      * @see Window#getWindows()
1618      *
1619      * @since 1.6
1620      */
1621     public static Window[] getOwnerlessWindows() {
1622         Window[] allWindows = Window.getWindows();
1623 
1624         int ownerlessCount = 0;
1625         for (Window w : allWindows) {
1626             if (w.getOwner() == null) {
1627                 ownerlessCount++;
1628             }
1629         }
1630 
1631         Window[] ownerless = new Window[ownerlessCount];
1632         int c = 0;
1633         for (Window w : allWindows) {
1634             if (w.getOwner() == null) {
1635                 ownerless[c++] = w;


2837             }
2838         }
2839     }
2840 
2841     private void removeFromWindowList() {
2842         removeFromWindowList(appContext, weakThis);
2843     }
2844 
2845     /**
2846      * Window type.
2847      *
2848      * Synchronization: ObjectLock
2849      */
2850     private Type type = Type.NORMAL;
2851 
2852     /**
2853      * Sets the type of the window.
2854      *
2855      * This method can only be called while the window is not displayable.
2856      *
2857      * @param  type  the window type
2858      * @throws IllegalComponentStateException if the window
2859      *         is displayable.
2860      * @throws IllegalArgumentException if the type is {@code null}
2861      * @see    Component#isDisplayable
2862      * @see    #getType
2863      * @since 1.7
2864      */
2865     public void setType(Type type) {
2866         if (type == null) {
2867             throw new IllegalArgumentException("type should not be null.");
2868         }
2869         synchronized (getTreeLock()) {
2870             if (isDisplayable()) {
2871                 throw new IllegalComponentStateException(
2872                         "The window is displayable.");
2873             }
2874             synchronized (getObjectLock()) {
2875                 this.type = type;
2876             }
2877         }
2878     }
2879 
2880     /**
2881      * Returns the type of the window.
2882      * @return  the type of the window
2883      * @see   #setType
2884      * @since 1.7
2885      */
2886     public Type getType() {
2887         synchronized (getObjectLock()) {
2888             return type;
2889         }
2890     }
2891 
2892     /**
2893      * The window serialized data version.
2894      *
2895      * @serial
2896      */
2897     private int windowSerializedDataVersion = 2;
2898 
2899     /**
2900      * Writes default serializable fields to stream.  Writes
2901      * a list of serializable {@code WindowListener}s and
2902      * {@code WindowFocusListener}s as optional data.