jdk/src/share/classes/java/awt/Container.java
author anthony
Tue, 18 Mar 2008 14:10:28 +0300
changeset 125 079ae88eaea9
parent 121 c43b2dfab9ac
child 130 11eb29307cfb
permissions -rw-r--r--
6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock Summary: The body of the method has been wrapped into the synchronized (getTreeLock()) { } block. Reviewed-by: son, art
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1995-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.dnd.DropTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.peer.ContainerPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.peer.ComponentPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.peer.LightweightPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.PrintWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.awt.CausedFocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.awt.PeerEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import sun.awt.SunToolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.awt.dnd.SunDropTargetEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.java2d.pipe.Region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * A generic Abstract Window Toolkit(AWT) container object is a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * that can contain other AWT components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Components added to a container are tracked in a list.  The order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * of the list will define the components' front-to-back stacking order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * within the container.  If no index is specified when adding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * component to a container, it will be added to the end of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * (and hence to the bottom of the stacking order).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <b>Note</b>: For details on the focus subsystem, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * How to Use the Focus Subsystem</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * a section in <em>The Java Tutorial</em>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <a href="../../java/awt/doc-files/FocusSpec.html">Focus Specification</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author      Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see       #add(java.awt.Component, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see       #getComponent(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see       LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * @since     JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public class Container extends Component {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final Logger log = Logger.getLogger("java.awt.Container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final Logger eventLog = Logger.getLogger("java.awt.event.Container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The number of components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * This value can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #getComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see #getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see #getComponentCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * The components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see #add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see #getComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    Component component[] = new Component[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Layout manager for this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #setLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see #getLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    LayoutManager layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Event router for lightweight components.  If this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * is native, this dispatcher takes care of forwarding and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * retargeting the events to lightweight components contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private LightweightDispatcher dispatcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The focus traversal policy that will manage keyboard traversal of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Container's children, if this Container is a focus cycle root. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * value is null, this Container inherits its policy from its focus-cycle-
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * root ancestor. If all such ancestors of this Container have null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * policies, then the current KeyboardFocusManager's default policy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * used. If the value is non-null, this policy will be inherited by all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * focus-cycle-root children that have no keyboard-traversal policy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * their own (as will, recursively, their focus-cycle-root children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * If this Container is not a focus cycle root, the value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * remembered, but will not be used or inherited by this or any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Containers until this Container is made a focus cycle root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private transient FocusTraversalPolicy focusTraversalPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Indicates whether this Component is the root of a focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Once focus enters a traversal cycle, typically it cannot leave it via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * focus traversal unless one of the up- or down-cycle keys is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Normal traversal is limited to this Container, and all of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Container's descendants that are not descendants of inferior focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see #isFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private boolean focusCycleRoot = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Stores the value of focusTraversalPolicyProvider property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see #setFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private boolean focusTraversalPolicyProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    // keeps track of the threads that are printing this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private transient Set printingThreads;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // True if there is at least one thread that's printing this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private transient boolean printing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    transient ContainerListener containerListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /* HierarchyListener and HierarchyBoundsListener support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    transient int listeningChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    transient int listeningBoundsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    transient int descendantsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final long serialVersionUID = 4613797578919906343L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * A constant which toggles one of the controllable behaviors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * of <code>getMouseEventTarget</code>. It is used to specify whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the method can return the Container on which it is originally called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * in case if none of its children are the current mouse event targets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #getMouseEventTarget(int, int, boolean, boolean, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    static final boolean INCLUDE_SELF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * A constant which toggles one of the controllable behaviors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * of <code>getMouseEventTarget</code>. It is used to specify whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * the method should search only lightweight components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see #getMouseEventTarget(int, int, boolean, boolean, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static final boolean SEARCH_HEAVYWEIGHTS = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Number of HW or LW components in this container (including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * all descendant containers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    private transient int numOfHWComponents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private transient int numOfLWComponents = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private static final Logger mixingLog = Logger.getLogger("java.awt.mixing.Container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @serialField ncomponents                     int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *       The number of components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *       This value can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @serialField component                       Component[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *       The components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @serialField layoutMgr                       LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *       Layout manager for this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @serialField dispatcher                      LightweightDispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *       Event router for lightweight components.  If this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *       is native, this dispatcher takes care of forwarding and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *       retargeting the events to lightweight components contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *       (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @serialField maxSize                         Dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *       Maximum size of this Container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @serialField focusCycleRoot                  boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *       Indicates whether this Component is the root of a focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *       Once focus enters a traversal cycle, typically it cannot leave it via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *       focus traversal unless one of the up- or down-cycle keys is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *       Normal traversal is limited to this Container, and all of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *       Container's descendants that are not descendants of inferior focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *       roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @serialField containerSerializedDataVersion  int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *       Container Serial Data Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @serialField focusTraversalPolicyProvider    boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *       Stores the value of focusTraversalPolicyProvider property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        new ObjectStreamField("ncomponents", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        new ObjectStreamField("component", Component[].class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        new ObjectStreamField("layoutMgr", LayoutManager.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        new ObjectStreamField("dispatcher", LightweightDispatcher.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        new ObjectStreamField("maxSize", Dimension.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        new ObjectStreamField("focusCycleRoot", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        new ObjectStreamField("containerSerializedDataVersion", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        new ObjectStreamField("focusTraversalPolicyProvider", Boolean.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
       called from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Constructs a new Container. Containers can be extended directly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * but are lightweight in this case and must be contained by a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * somewhere higher up in the component tree that is native.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * (such as Frame for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public Container() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void initializeFocusTraversalKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        focusTraversalKeys = new Set[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Gets the number of components in this panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return    the number of components in this panel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see       #getComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public int getComponentCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return countComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * replaced by getComponentCount().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public int countComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Gets the nth component in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param      n   the index of the component to get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return     the n<sup>th</sup> component in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @exception  ArrayIndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *                 if the n<sup>th</sup> value does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public Component getComponent(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if ((n < 0) || (n >= ncomponents)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                throw new ArrayIndexOutOfBoundsException("No such child: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return component[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Gets all the components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @return    an array of all the components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public Component[] getComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return getComponents_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    // NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    //       This functionality is implemented in a package-private method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    //       to insure that it cannot be overridden by client subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    final Component[] getComponents_NoClientCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return Arrays.copyOf(component, ncomponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    } // getComponents_NoClientCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Determines the insets of this container, which indicate the size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * of the container's border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * A <code>Frame</code> object, for example, has a top inset that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * corresponds to the height of the frame's title bar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return    the insets of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see       Insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @see       LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public Insets getInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return insets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * replaced by <code>getInsets()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public Insets insets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ComponentPeer peer = this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (peer instanceof ContainerPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            ContainerPeer cpeer = (ContainerPeer)peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return (Insets)cpeer.insets().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Appends the specified component to the end of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * This is a convenience method for {@link #addImpl}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Note: If a component has been added to a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * has been displayed, <code>validate</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * called on that container to display the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * If multiple components are being added, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * efficiency by calling <code>validate</code> only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * after all the components have been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param     comp   the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see #addImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see javax.swing.JComponent#revalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @return    the component argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public Component add(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        addImpl(comp, null, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Adds the specified component to this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * This is a convenience method for {@link #addImpl}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * This method is obsolete as of 1.1.  Please use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * method <code>add(Component, Object)</code> instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see #add(Component, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public Component add(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        addImpl(comp, name, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Adds the specified component to this container at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This is a convenience method for {@link #addImpl}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Note: If a component has been added to a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * has been displayed, <code>validate</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * called on that container to display the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * If multiple components are being added, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * efficiency by calling <code>validate</code> only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * after all the components have been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param     comp   the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param     index    the position at which to insert the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *                   or <code>-1</code> to append the component to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @exception IllegalArgumentException if {@code index} is invalid (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *            {@link #addImpl} for details)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return    the component <code>comp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see #addImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @see #remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see javax.swing.JComponent#revalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public Component add(Component comp, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        addImpl(comp, null, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Checks that the component comp can be added to this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Checks :  index in bounds of container's size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * comp is not one of this container's parents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * and comp is not a window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Comp and container must be on the same GraphicsDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * if comp is container, all sub-components must be on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * same GraphicsDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private void checkAdding(Component comp, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        GraphicsConfiguration thisGC = getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (index > ncomponents || index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            throw new IllegalArgumentException("illegal component position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        if (comp.parent == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            if (index == ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                throw new IllegalArgumentException("illegal component position " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                                   index + " should be less then " + ncomponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            for (Container cn = this; cn != null; cn=cn.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (cn == comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    throw new IllegalArgumentException("adding container's parent to itself");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (comp instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                throw new IllegalArgumentException("adding a window to a container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        Window thisTopLevel = getContainingWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Window compTopLevel = comp.getContainingWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (thisTopLevel != compTopLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            throw new IllegalArgumentException("component and container should be in the same top-level window");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (thisGC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            comp.checkGD(thisGC.getDevice().getIDstring());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Removes component comp from this container without making unneccessary changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * and generating unneccessary events. This function intended to perform optimized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * remove, for example, if newParent and current parent are the same it just changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * index without calling removeNotify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Note: Should be called while holding treeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Returns whether removeNotify was invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @since: 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private boolean removeDelicately(Component comp, Container newParent, int newIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int index = getComponentZOrder(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        boolean needRemoveNotify = isRemoveNotifyNeeded(comp, this, newParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (needRemoveNotify) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            comp.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (newParent != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                layoutMgr.removeLayoutComponent(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                                    -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                    -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            adjustDescendants(-(comp.countHierarchyMembers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            comp.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            System.arraycopy(component, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                             component, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                             ncomponents - index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            component[--ncomponents] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (newIndex > index) { // 2->4: 012345 -> 013425, 2->5: 012345 -> 013452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                if (newIndex-index > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    System.arraycopy(component, index+1, component, index, newIndex-index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            } else { // 4->2: 012345 -> 014235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                if (index-newIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    System.arraycopy(component, newIndex, component, newIndex+1, index-newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            component[newIndex] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (comp.parent == null) { // was actually removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (containerListener != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                ContainerEvent e = new ContainerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                                      ContainerEvent.COMPONENT_REMOVED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                                      comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                       this, HierarchyEvent.PARENT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (peer != null && layoutMgr == null && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return needRemoveNotify;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Checks whether this container can contain component which is focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Verifies that container is enable and showing, and if it is focus cycle root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * its FTP allows component to be focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    boolean canContainFocusOwner(Component focusOwnerCandidate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (!(isEnabled() && isDisplayable()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
              && isVisible() && isFocusable()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if (isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            FocusTraversalPolicy policy = getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            if (policy instanceof DefaultFocusTraversalPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (!((DefaultFocusTraversalPolicy)policy).accept(focusOwnerCandidate)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return parent.canContainFocusOwner(focusOwnerCandidate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Checks whether or not this container has heavyweight children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Note: Should be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return true if there is at least one heavyweight children in a container, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    private boolean hasHeavyweightDescendants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return numOfHWComponents > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Checks whether or not this container has lightweight children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Note: Should be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @return true if there is at least one lightweight children in a container, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    private boolean hasLightweightDescendants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        return numOfLWComponents > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Returns closest heavyweight component to this container. If this container is heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * returns this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    Container getHeavyweightContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        if (peer != null && !(peer instanceof LightweightPeer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return getNativeContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Detects whether or not remove from current parent and adding to new parent requires call of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * removeNotify on the component. Since removeNotify destroys native window this might (not)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * be required. For example, if new container and old containers are the same we don't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * destroy native window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @since: 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    private static boolean isRemoveNotifyNeeded(Component comp, Container oldContainer, Container newContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (oldContainer == null) { // Component didn't have parent - no removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if (comp.peer == null) { // Component didn't have peer - no removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (newContainer.peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            // Component has peer but new Container doesn't - call removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        // If component is lightweight non-Container or lightweight Container with all but heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        // children there is no need to call remove notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (comp.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            boolean isContainer = comp instanceof Container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (!isContainer || (isContainer && !((Container)comp).hasHeavyweightDescendants())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // If this point is reached, then the comp is either a HW or a LW container with HW descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        // All three components have peers, check for peer change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        Container newNativeContainer = oldContainer.getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Container oldNativeContainer = newContainer.getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        if (newNativeContainer != oldNativeContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            // Native containers change - check whether or not current platform supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            // changing of widget hierarchy on native level without recreation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            // The current implementation forbids reparenting of LW containers with HW descendants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // into another native container w/o destroying the peers. Actually such an operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            // is quite rare. If we ever need to save the peers, we'll have to slightly change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            // addDelicately() method in order to handle such LW containers recursively, reparenting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            // each HW descendant independently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            return !comp.peer.isReparentSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            // if container didn't change we still might need to recreate component's window as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            // changes to zorder should be reflected in native window stacking order and it might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            // not be supported by the platform. This is important only for heavyweight child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return !((ContainerPeer)(newNativeContainer.peer)).isRestackSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Moves the specified component to the specified z-order index in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * the container. The z-order determines the order that components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * are painted; the component with the highest z-order paints first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * and the component with the lowest z-order paints last.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * Where components overlap, the component with the lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * z-order paints over the component with the higher z-order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * If the component is a child of some other container, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * removed from that container before being added to this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * The important difference between this method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <code>java.awt.Container.add(Component, int)</code> is that this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * doesn't call <code>removeNotify</code> on the component while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * removing it from its previous container unless necessary and when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * allowed by the underlying native windowing system. This way, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * component has the keyboard focus, it maintains the focus when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * moved to the new position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * This property is guaranteed to apply only to lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * non-<code>Container</code> components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <b>Note</b>: Not all platforms support changing the z-order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * heavyweight components from one container into another without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * the call to <code>removeNotify</code>. There is no way to detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * whether a platform supports this, so developers shouldn't make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * any assumptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @param     comp the component to be moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @param     index the position in the container's list to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *            insert the component, where <code>getComponentCount()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *            appends to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @exception NullPointerException if <code>comp</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *            <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @exception IllegalArgumentException if <code>comp</code> is one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *            container's parents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @exception IllegalArgumentException if <code>index</code> is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *            the range <code>[0, getComponentCount()]</code> for moving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *            between containers, or not in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *            <code>[0, getComponentCount()-1]</code> for moving inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *            a container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @exception IllegalArgumentException if adding a container to itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @exception IllegalArgumentException if adding a <code>Window</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *            to a container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @see #getComponentZOrder(java.awt.Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public void setComponentZOrder(Component comp, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
             // Store parent because remove will clear it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
             Container curParent = comp.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
             int oldZindex = getComponentZOrder(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
             if (curParent == this && index == oldZindex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                 return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
             checkAdding(comp, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
             boolean peerRecreated = (curParent != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                 curParent.removeDelicately(comp, this, index) : false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
             addDelicately(comp, curParent, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
             // If the oldZindex == -1, the component gets inserted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
             // rather than it changes its z-order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
             if (!peerRecreated && oldZindex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                 // The new 'index' cannot be == -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                 // It gets checked at the checkAdding() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                 // Therefore both oldZIndex and index denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                 // some existing positions at this point and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                 // this is actually a Z-order changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                 comp.mixOnZOrderChanging(oldZindex, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Traverses the tree of components and reparents children heavyweight component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * to new heavyweight parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    private void reparentTraverse(ContainerPeer parentPeer, Container child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        for (int i = 0; i < child.getComponentCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            Component comp = child.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (comp.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                // If components is lightweight check if it is container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                // If it is container it might contain heavyweight children we need to reparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    reparentTraverse(parentPeer, (Container)comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                // Q: Need to update NativeInLightFixer?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                comp.getPeer().reparent(parentPeer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Reparents child component peer to this container peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Container must be heavyweight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    private void reparentChild(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (comp.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // If component is lightweight container we need to reparent all its explicit  heavyweight children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                // Traverse component's tree till depth-first until encountering heavyweight component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                reparentTraverse((ContainerPeer)getPeer(), (Container)comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            comp.getPeer().reparent((ContainerPeer)getPeer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Adds component to this container. Tries to minimize side effects of this adding -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * doesn't call remove notify if it is not required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    private void addDelicately(Component comp, Container curParent, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        // Check if moving between containers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if (curParent != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            /* Add component to list; allocate new array if necessary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (ncomponents == component.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                component = Arrays.copyOf(component, ncomponents * 2 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            if (index == -1 || index == ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                component[ncomponents++] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                System.arraycopy(component, index, component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                 index + 1, ncomponents - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                component[index] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                ncomponents++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            comp.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                                    comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                    comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            adjustDescendants(comp.countHierarchyMembers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (index < ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                component[index] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (comp.peer == null) { // Remove notify was called or it didn't have peer - create new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                comp.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                // New created peer creates component on top of the stacking order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                Container newNativeContainer = getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                if (((ContainerPeer)newNativeContainer.getPeer()).isRestackSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    ((ContainerPeer)newNativeContainer.getPeer()).restack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            } else { // Both container and child have peers, it means child peer should be reparented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                // In both cases we need to reparent native widgets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                Container newNativeContainer = getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                Container oldNativeContainer = curParent.getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                if (oldNativeContainer != newNativeContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    // Native container changed - need to reparent native widgets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    newNativeContainer.reparentChild(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                // If component still has a peer and it is either container or heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                // and restack is supported we have to restack native windows since order might have changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                if ((!comp.isLightweight() || (comp instanceof Container))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    && ((ContainerPeer)newNativeContainer.getPeer()).isRestackSupported())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    ((ContainerPeer)newNativeContainer.getPeer()).restack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                if (!comp.isLightweight() && isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    // If component is heavyweight and one of the containers is lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    // some NativeInLightFixer activity should be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                    if (!curParent.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        // Moving from heavyweight container to lightweight container - should create NativeInLightFixer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                        // since addNotify does this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        comp.nativeInLightFixer = new NativeInLightFixer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        // Component already has NativeInLightFixer - just reinstall it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        // because hierarchy changed and he needs to rebuild list of parents to listen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        comp.nativeInLightFixer.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        if (curParent != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            /* Notify the layout manager of the added component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    ((LayoutManager2)layoutMgr).addLayoutComponent(comp, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    layoutMgr.addLayoutComponent(null, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            if (containerListener != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                ContainerEvent e = new ContainerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                                      ContainerEvent.COMPONENT_ADDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                                      comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                                       this, HierarchyEvent.PARENT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            // If component is focus owner or parent container of focus owner check that after reparenting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            // focus owner moved out if new container prohibit this kind of focus owner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            if (comp.isFocusOwner() && !comp.canBeFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                comp.transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            } else if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                if (focusOwner != null && isParentOf(focusOwner) && !focusOwner.canBeFocusOwner()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                    focusOwner.transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                                       this, HierarchyEvent.HIERARCHY_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if (peer != null && layoutMgr == null && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Returns the z-order index of the component inside the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * The higher a component is in the z-order hierarchy, the lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * its index.  The component with the lowest z-order index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * painted last, above all other child components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @param comp the component being queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @return  the z-order index of the component; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *          returns -1 if the component is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *          or doesn't belong to the container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @see #setComponentZOrder(java.awt.Component, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public int getComponentZOrder(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        if (comp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            // Quick check - container should be immediate parent of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (comp.parent != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            for (int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                if (component[i] == comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        // To please javac
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * Adds the specified component to the end of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Also notifies the layout manager to add the component to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * this container's layout using the specified constraints object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * This is a convenience method for {@link #addImpl}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * Note: If a component has been added to a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * has been displayed, <code>validate</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * called on that container to display the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * If multiple components are being added, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * efficiency by calling <code>validate</code> only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * after all the components have been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @param     comp the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @param     constraints an object expressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     *                  layout contraints for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @see #addImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @see javax.swing.JComponent#revalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @see       LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    public void add(Component comp, Object constraints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        addImpl(comp, constraints, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * Adds the specified component to this container with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * constraints at the specified index.  Also notifies the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * manager to add the component to the this container's layout using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * the specified constraints object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * This is a convenience method for {@link #addImpl}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Note: If a component has been added to a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * has been displayed, <code>validate</code> must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * called on that container to display the new component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * If multiple components are being added, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * efficiency by calling <code>validate</code> only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * after all the components have been added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @param comp the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param constraints an object expressing layout contraints for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @param index the position in the container's list at which to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * the component; <code>-1</code> means insert at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @exception IllegalArgumentException if {@code index} is invalid (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *            {@link #addImpl} for details)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @see #addImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @see javax.swing.JComponent#revalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @see #remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @see LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    public void add(Component comp, Object constraints, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
       addImpl(comp, constraints, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * Adds the specified component to this container at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * index. This method also notifies the layout manager to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * the component to this container's layout using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * constraints object via the <code>addLayoutComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * The constraints are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * defined by the particular layout manager being used.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * example, the <code>BorderLayout</code> class defines five
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * constraints: <code>BorderLayout.NORTH</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * <code>BorderLayout.SOUTH</code>, <code>BorderLayout.EAST</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * <code>BorderLayout.WEST</code>, and <code>BorderLayout.CENTER</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * The <code>GridBagLayout</code> class requires a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * <code>GridBagConstraints</code> object.  Failure to pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * the correct type of constraints object results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * <code>IllegalArgumentException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * If the current layout manager implements {@code LayoutManager2}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * {@link LayoutManager2#addLayoutComponent(Component,Object)} is invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * it. If the current layout manager does not implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * {@code LayoutManager2}, and constraints is a {@code String}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * {@link LayoutManager#addLayoutComponent(String,Component)} is invoked on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * If the component is not an ancestor of this container and has a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * parent, it is removed from its current parent before it is added to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * This is the method to override if a program needs to track
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * every add request to a container as all other add methods defer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * to this one. An overriding method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * usually include a call to the superclass's version of the method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * <code>super.addImpl(comp, constraints, index)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @param     comp       the component to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * @param     constraints an object expressing layout constraints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *                 for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @param     index the position in the container's list at which to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *                 insert the component, where <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *                 means append to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @exception IllegalArgumentException if {@code index} is invalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *            if {@code comp} is a child of this container, the valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *            range is {@code [-1, getComponentCount()-1]}; if component is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *            not a child of this container, the valid range is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *            {@code [-1, getComponentCount()]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @exception IllegalArgumentException if {@code comp} is an ancestor of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *                                     this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @exception IllegalArgumentException if adding a window to a container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @exception NullPointerException if {@code comp} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @see       #add(Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @see       #add(Component, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @see       #add(Component, java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @see       LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @see       LayoutManager2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    protected void addImpl(Component comp, Object constraints, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            /* Check for correct arguments:  index in bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
             * comp cannot be one of this container's parents,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
             * and comp cannot be a window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
             * comp and container must be on the same GraphicsDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
             * if comp is container, all sub-components must be on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
             * same GraphicsDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            GraphicsConfiguration thisGC = this.getGraphicsConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            if (index > ncomponents || (index < 0 && index != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                          "illegal component position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            for (Container cn = this; cn != null; cn=cn.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                if (cn == comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                      "adding container's parent to itself");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            if (comp instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                       "adding a window to a container");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (thisGC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            comp.checkGD(thisGC.getDevice().getIDstring());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            /* Reparent the component and tidy up the tree's state. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            if (comp.parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                comp.parent.remove(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    if (index > ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        throw new IllegalArgumentException("illegal component position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            /* Add component to list; allocate new array if necessary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (ncomponents == component.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                component = Arrays.copyOf(component, ncomponents * 2 + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (index == -1 || index == ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                component[ncomponents++] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                System.arraycopy(component, index, component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                                 index + 1, ncomponents - index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                component[index] = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                ncomponents++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            comp.parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            adjustDescendants(comp.countHierarchyMembers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                comp.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            /* Notify the layout manager of the added component. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    ((LayoutManager2)layoutMgr).addLayoutComponent(comp, constraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                } else if (constraints instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                    layoutMgr.addLayoutComponent((String)constraints, comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            if (containerListener != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                ContainerEvent e = new ContainerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                                     ContainerEvent.COMPONENT_ADDED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                                     comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                                       this, HierarchyEvent.PARENT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            if (peer != null && layoutMgr == null && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * Checks that all Components that this Container contains are on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * the same GraphicsDevice as this Container.  If not, throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * IllegalArgumentException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    void checkGD(String stringID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        Component tempComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        for (int i = 0; i < component.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            tempComp= component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            if (tempComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                tempComp.checkGD(stringID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Removes the component, specified by <code>index</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * from this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * This method also notifies the layout manager to remove the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * component from this container's layout via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * <code>removeLayoutComponent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * Note: If a component has been removed from a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * had been displayed, {@link #validate} must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * called on that container to reflect changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * If multiple components are being removed, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * efficiency by calling {@link #validate} only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * after all the components have been removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @param     index   the index of the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @throws ArrayIndexOutOfBoundsException if {@code index} is not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *         range {@code [0, getComponentCount()-1]}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @see #add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @see #getComponentCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    public void remove(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            if (index < 0  || index >= ncomponents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                throw new ArrayIndexOutOfBoundsException(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            Component comp = component[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                comp.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                layoutMgr.removeLayoutComponent(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                -comp.numListening(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                -comp.numListening(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            adjustDescendants(-(comp.countHierarchyMembers()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            comp.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            System.arraycopy(component, index + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                             component, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                             ncomponents - index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            component[--ncomponents] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            if (containerListener != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                ContainerEvent e = new ContainerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                                     ContainerEvent.COMPONENT_REMOVED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                     comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED, comp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                       this, HierarchyEvent.PARENT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            if (peer != null && layoutMgr == null && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * Removes the specified component from this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * This method also notifies the layout manager to remove the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * component from this container's layout via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * <code>removeLayoutComponent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * Note: If a component has been removed from a container that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * had been displayed, {@link #validate} must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * called on that container to reflect changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * If multiple components are being removed, you can improve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * efficiency by calling {@link #validate} only once,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * after all the components have been removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * @param comp the component to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * @see #add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * @see #remove(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
    public void remove(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            if (comp.parent == this)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                /* Search backwards, expect that more recent additions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                 * are more likely to be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                for (int i = ncomponents; --i >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                    if (component[i] == comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                        remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Removes all the components from this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * This method also notifies the layout manager to remove the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * components from this container's layout via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <code>removeLayoutComponent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @see #add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @see #remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    public void removeAll() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                                    -listeningChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                                    -listeningBoundsChildren);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            adjustDescendants(-descendantsCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            while (ncomponents > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                Component comp = component[--ncomponents];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                component[ncomponents] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    comp.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    layoutMgr.removeLayoutComponent(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                comp.parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                if (containerListener != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                   (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    Toolkit.enabledOnToolkit(AWTEvent.CONTAINER_EVENT_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    ContainerEvent e = new ContainerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                     ContainerEvent.COMPONENT_REMOVED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                                     comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    dispatchEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                comp.createHierarchyEvents(HierarchyEvent.HIERARCHY_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                           comp, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                                           HierarchyEvent.PARENT_CHANGED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                                           Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            if (peer != null && layoutMgr == null && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    // Should only be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    int numListening(long mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        int superListening = super.numListening(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        if (mask == AWTEvent.HIERARCHY_EVENT_MASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                // Verify listeningChildren is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                for (int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    sum += component[i].numListening(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                if (listeningChildren != sum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    eventLog.log(Level.FINE, "Assertion (listeningChildren == sum) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            return listeningChildren + superListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        } else if (mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                // Verify listeningBoundsChildren is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                for (int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    sum += component[i].numListening(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                if (listeningBoundsChildren != sum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                    eventLog.log(Level.FINE, "Assertion (listeningBoundsChildren == sum) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            return listeningBoundsChildren + superListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                eventLog.log(Level.FINE, "This code must never be reached");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            return superListening;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    // Should only be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    void adjustListeningChildren(long mask, int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        if (eventLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            boolean toAssert = (mask == AWTEvent.HIERARCHY_EVENT_MASK ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                                mask == AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                                mask == (AWTEvent.HIERARCHY_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                                         AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            if (!toAssert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                eventLog.log(Level.FINE, "Assertion failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if (num == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if ((mask & AWTEvent.HIERARCHY_EVENT_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            listeningChildren += num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        if ((mask & AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            listeningBoundsChildren += num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        adjustListeningChildrenOnParent(mask, num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    // Should only be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    void adjustDescendants(int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        if (num == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        descendantsCount += num;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        adjustDecendantsOnParent(num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    // Should only be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    void adjustDecendantsOnParent(int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            parent.adjustDescendants(num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    // Should only be called while holding tree lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    int countHierarchyMembers() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        if (log.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            // Verify descendantsCount is correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            for (int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                sum += component[i].countHierarchyMembers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
            if (descendantsCount != sum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                log.log(Level.FINE, "Assertion (descendantsCount == sum) failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        return descendantsCount + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    private int getListenersCount(int id, boolean enabledOnToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        assert Thread.holdsLock(getTreeLock());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        if (enabledOnToolkit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            return descendantsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
          case HierarchyEvent.HIERARCHY_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            return listeningChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
          case HierarchyEvent.ANCESTOR_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
          case HierarchyEvent.ANCESTOR_RESIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            return listeningBoundsChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    final int createHierarchyEvents(int id, Component changed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        Container changedParent, long changeFlags, boolean enabledOnToolkit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        assert Thread.holdsLock(getTreeLock());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        int listeners = getListenersCount(id, enabledOnToolkit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        for (int count = listeners, i = 0; count > 0; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            count -= component[i].createHierarchyEvents(id, changed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                changedParent, changeFlags, enabledOnToolkit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        return listeners +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            super.createHierarchyEvents(id, changed, changedParent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                                        changeFlags, enabledOnToolkit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    final void createChildHierarchyEvents(int id, long changeFlags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        boolean enabledOnToolkit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        assert Thread.holdsLock(getTreeLock());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        if (ncomponents == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        int listeners = getListenersCount(id, enabledOnToolkit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        for (int count = listeners, i = 0; count > 0; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            count -= component[i].createHierarchyEvents(id, this, parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                changeFlags, enabledOnToolkit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * Gets the layout manager for this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * @see #doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * @see #setLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    public LayoutManager getLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        return layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * Sets the layout manager for this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @param mgr the specified layout manager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * @see #doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @see #getLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    public void setLayout(LayoutManager mgr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        layoutMgr = mgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
            invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * Causes this container to lay out its components.  Most programs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * should not call this method directly, but should invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * the <code>validate</code> method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * @see LayoutManager#layoutContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @see #setLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    public void doLayout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        layout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * replaced by <code>doLayout()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
    public void layout() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        LayoutManager layoutMgr = this.layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            layoutMgr.layoutContainer(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * Invalidates the container.  The container and all parents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * above it are marked as needing to be laid out.  This method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * be called often, so it needs to execute quickly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * <p> If the {@code LayoutManager} installed on this container is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * an instance of {@code LayoutManager2}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * {@link LayoutManager2#invalidateLayout(Container)} is invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * it supplying this {@code Container} as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * @see #layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @see LayoutManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * @see LayoutManager2#invalidateLayout(Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
    public void invalidate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        LayoutManager layoutMgr = this.layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            LayoutManager2 lm = (LayoutManager2) layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            lm.invalidateLayout(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        super.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Validates this container and all of its subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * The <code>validate</code> method is used to cause a container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * to lay out its subcomponents again. It should be invoked when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     * this container's subcomponents are modified (added to or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * removed from the container, or layout-related information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * changed) after the container has been displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     * <p>If this {@code Container} is not valid, this method invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * the {@code validateTree} method and marks this {@code Container}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
     * as valid. Otherwise, no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * @see #add(java.awt.Component)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * @see Component#invalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
     * @see javax.swing.JComponent#revalidate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * @see #validateTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    public void validate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        /* Avoid grabbing lock unless really necessary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        if (!valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            boolean updateCur = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                if (!valid && peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    ContainerPeer p = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    if (peer instanceof ContainerPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                        p = (ContainerPeer) peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                        p.beginValidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    validateTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    if (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                        p.endValidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                        updateCur = isVisible();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
            if (updateCur) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * Recursively descends the container tree and recomputes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * layout for any subtrees marked as needing it (those marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     * invalid).  Synchronization should be provided by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
     * that calls this one:  <code>validate</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @see #doLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @see #validate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    protected void validateTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        if (!valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            if (peer instanceof ContainerPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                ((ContainerPeer)peer).beginLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
            doLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            for (int i = 0 ; i < ncomponents ; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
                Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                if (   (comp instanceof Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    && !(comp instanceof Window)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    && !comp.valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    ((Container)comp).validateTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                    comp.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            if (peer instanceof ContainerPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                ((ContainerPeer)peer).endLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * Recursively descends the container tree and invalidates all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * contained components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    void invalidateTree() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            for (int i = 0; i < ncomponents; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
                    ((Container)comp).invalidateTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
                    if (comp.valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
                        comp.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
            if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
                invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Sets the font of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @param f The font to become this container's font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @see Component#getFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @since JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        boolean shouldinvalidate = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        Font oldfont = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        super.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        Font newfont = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        if (newfont != oldfont && (oldfont == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                                   !oldfont.equals(newfont))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            invalidateTree();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * Returns the preferred size of this container.  If the preferred size has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * not been set explicitly by {@link Component#setPreferredSize(Dimension)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * and this {@code Container} has a {@code non-null} {@link LayoutManager},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * then {@link LayoutManager#preferredLayoutSize(Container)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * is used to calculate the preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * <p>Note: some implementations may cache the value returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * {@code LayoutManager}.  Implementations that cache need not invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * {@code preferredLayoutSize} on the {@code LayoutManager} every time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     * this method is invoked, rather the {@code LayoutManager} will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * be queried after the {@code Container} becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * @return    an instance of <code>Dimension</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     *                the preferred size of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * @see       #getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * @see       #getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * @see       #getLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * @see       LayoutManager#preferredLayoutSize(Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * @see       Component#getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        return preferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * replaced by <code>getPreferredSize()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    public Dimension preferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        /* Avoid grabbing the lock if a reasonable cached size value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
         * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        Dimension dim = prefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        if (dim == null || !(isPreferredSizeSet() || isValid())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                prefSize = (layoutMgr != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
                    layoutMgr.preferredLayoutSize(this) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                    super.preferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                dim = prefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (dim != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            return new Dimension(dim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     * Returns the minimum size of this container.  If the minimum size has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * not been set explicitly by {@link Component#setMinimumSize(Dimension)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * and this {@code Container} has a {@code non-null} {@link LayoutManager},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * then {@link LayoutManager#minimumLayoutSize(Container)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * is used to calculate the minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * <p>Note: some implementations may cache the value returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * {@code LayoutManager}.  Implementations that cache need not invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * {@code minimumLayoutSize} on the {@code LayoutManager} every time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * this method is invoked, rather the {@code LayoutManager} will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * be queried after the {@code Container} becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * @return    an instance of <code>Dimension</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     *                the minimum size of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * @see       #getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * @see       #getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * @see       #getLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * @see       LayoutManager#minimumLayoutSize(Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * @see       Component#getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        return minimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * replaced by <code>getMinimumSize()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    public Dimension minimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
        /* Avoid grabbing the lock if a reasonable cached size value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
         * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        Dimension dim = minSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        if (dim == null || !(isMinimumSizeSet() || isValid())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                minSize = (layoutMgr != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    layoutMgr.minimumLayoutSize(this) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    super.minimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                dim = minSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        if (dim != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            return new Dimension(dim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * Returns the maximum size of this container.  If the maximum size has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * not been set explicitly by {@link Component#setMaximumSize(Dimension)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * and the {@link LayoutManager} installed on this {@code Container}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * is an instance of {@link LayoutManager2}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * {@link LayoutManager2#maximumLayoutSize(Container)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * is used to calculate the maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * <p>Note: some implementations may cache the value returned from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * {@code LayoutManager2}.  Implementations that cache need not invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * {@code maximumLayoutSize} on the {@code LayoutManager2} every time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * this method is invoked, rather the {@code LayoutManager2} will only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * be queried after the {@code Container} becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * @return    an instance of <code>Dimension</code> that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     *                the maximum size of this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * @see       #getPreferredSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * @see       #getMinimumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * @see       #getLayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * @see       LayoutManager2#maximumLayoutSize(Container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * @see       Component#getMaximumSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    public Dimension getMaximumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        /* Avoid grabbing the lock if a reasonable cached size value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
         * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        Dimension dim = maxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        if (dim == null || !(isMaximumSizeSet() || isValid())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
               if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                    LayoutManager2 lm = (LayoutManager2) layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    maxSize = lm.maximumLayoutSize(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
               } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                    maxSize = super.getMaximumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
               dim = maxSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        if (dim != null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            return new Dimension(dim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        else{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
            return dim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * Returns the alignment along the x axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    public float getAlignmentX() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        float xAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                LayoutManager2 lm = (LayoutManager2) layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                xAlign = lm.getLayoutAlignmentX(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            xAlign = super.getAlignmentX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        return xAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * Returns the alignment along the y axis.  This specifies how
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * the component would like to be aligned relative to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * components.  The value should be a number between 0 and 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * where 0 represents alignment along the origin, 1 is aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * the furthest away from the origin, 0.5 is centered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    public float getAlignmentY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        float yAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        if (layoutMgr instanceof LayoutManager2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                LayoutManager2 lm = (LayoutManager2) layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                yAlign = lm.getLayoutAlignmentY(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
            yAlign = super.getAlignmentY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        return yAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * Paints the container. This forwards the paint to any lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * components that are children of this container. If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * reimplemented, super.paint(g) should be called so that lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * components are properly rendered. If a child component is entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     * clipped by the current clipping setting in g, paint() will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * forwarded to that child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * @param g the specified Graphics window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * @see   Component#update(Graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
                if (printing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    if (printingThreads.contains(Thread.currentThread())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            // The container is showing on screen and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            // this paint() is not called from print().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
            // Paint self and forward the paint to lightweight subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
            // super.paint(); -- Don't bother, since it's a NOP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            GraphicsCallback.PaintCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
                runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * Updates the container.  This forwards the update to any lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * components that are children of this container.  If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * reimplemented, super.update(g) should be called so that lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * components are properly rendered.  If a child component is entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * clipped by the current clipping setting in g, update() will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * forwarded to that child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * @param g the specified Graphics window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * @see   Component#update(Graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
    public void update(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            if (! (peer instanceof LightweightPeer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                g.clearRect(0, 0, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * Prints the container. This forwards the print to any lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * components that are children of this container. If this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * reimplemented, super.print(g) should be called so that lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * components are properly rendered. If a child component is entirely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * clipped by the current clipping setting in g, print() will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * forwarded to that child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * @param g the specified Graphics window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * @see   Component#update(Graphics)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    public void print(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
            Thread t = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
                    if (printingThreads == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                        printingThreads = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
                    printingThreads.add(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                    printing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                super.print(g);  // By default, Component.print() calls paint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
                    printingThreads.remove(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
                    printing = !printingThreads.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            GraphicsCallback.PrintCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * Paints each of the components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * @param     g   the graphics context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * @see       Component#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * @see       Component#paintAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    public void paintComponents(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            GraphicsCallback.PaintAllCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                runComponents(component, g, GraphicsCallback.TWO_PASSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * Simulates the peer callbacks into java.awt for printing of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * lightweight Containers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * @param     g   the graphics context to use for printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @see       Component#printAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * @see       #printComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    void lightweightPaint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        super.lightweightPaint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        paintHeavyweightComponents(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * Prints all the heavyweight subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    void paintHeavyweightComponents(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            GraphicsCallback.PaintHeavyweightComponentsCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
                                            GraphicsCallback.HEAVYWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * Prints each of the components in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * @param     g   the graphics context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @see       Component#print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * @see       Component#printAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    public void printComponents(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            GraphicsCallback.PrintAllCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                runComponents(component, g, GraphicsCallback.TWO_PASSES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * Simulates the peer callbacks into java.awt for printing of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * lightweight Containers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * @param     g   the graphics context to use for printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @see       Component#printAll
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     * @see       #printComponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    void lightweightPrint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        super.lightweightPrint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        printHeavyweightComponents(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * Prints all the heavyweight subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    void printHeavyweightComponents(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        if (isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            GraphicsCallback.PrintHeavyweightComponentsCallback.getInstance().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                runComponents(component, g, GraphicsCallback.LIGHTWEIGHTS |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                                            GraphicsCallback.HEAVYWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * Adds the specified container listener to receive container events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * from this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * @param    l the container listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * @see #removeContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * @see #getContainerListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    public synchronized void addContainerListener(ContainerListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        containerListener = AWTEventMulticaster.add(containerListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * Removes the specified container listener so it no longer receives
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * container events from this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * @param   l the container listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     * @see #addContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * @see #getContainerListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    public synchronized void removeContainerListener(ContainerListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        containerListener = AWTEventMulticaster.remove(containerListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * Returns an array of all the container listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * registered on this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * @return all of this container's <code>ContainerListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *         or an empty array if no container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * @see #addContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * @see #removeContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    public synchronized ContainerListener[] getContainerListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        return (ContainerListener[]) (getListeners(ContainerListener.class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * upon this <code>Container</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * <code>Container</code> <code>c</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * for its container listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * <pre>ContainerListener[] cls = (ContainerListener[])(c.getListeners(ContainerListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *          <code><em>Foo</em>Listener</code>s on this container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     *          or an empty array if no such listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * @see #getContainerListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
        if  (listenerType == ContainerListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
            l = containerListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            return super.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        if (id == ContainerEvent.COMPONENT_ADDED ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            id == ContainerEvent.COMPONENT_REMOVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
            if ((eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                containerListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * Processes events on this container. If the event is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * <code>ContainerEvent</code>, it invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     * <code>processContainerEvent</code> method, else it invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * its superclass's <code>processEvent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        if (e instanceof ContainerEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
            processContainerEvent((ContainerEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * Processes container events occurring on this container by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * dispatching them to any registered ContainerListener objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * NOTE: This method will not be called unless container events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * are enabled for this component; this happens when one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * following occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * <li>A ContainerListener object is registered via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *     <code>addContainerListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * <li>Container events are enabled via <code>enableEvents</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * @param e the container event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @see Component#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
    protected void processContainerEvent(ContainerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        ContainerListener listener = containerListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            switch(e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
              case ContainerEvent.COMPONENT_ADDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                listener.componentAdded(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
              case ContainerEvent.COMPONENT_REMOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                listener.componentRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * Dispatches an event to this component or one of its sub components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * Create ANCESTOR_RESIZED and ANCESTOR_MOVED events in response to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * COMPONENT_RESIZED and COMPONENT_MOVED events. We have to do this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * here instead of in processComponentEvent because ComponentEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * may not be enabled for this Container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    void dispatchEventImpl(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
        if ((dispatcher != null) && dispatcher.dispatchEvent(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            // event was sent to a lightweight component.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
            // native-produced event sent to the native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            // must be properly disposed of by the peer, so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            // gets forwarded.  If the native host has been removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            // as a result of the sending the lightweight event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
            // the peer reference will be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                peer.handleEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        super.dispatchEventImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
            switch (e.getID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
              case ComponentEvent.COMPONENT_RESIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                createChildHierarchyEvents(HierarchyEvent.ANCESTOR_RESIZED, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                                           Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
              case ComponentEvent.COMPONENT_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                createChildHierarchyEvents(HierarchyEvent.ANCESTOR_MOVED, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                                       Toolkit.enabledOnToolkit(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * Dispatches an event to this component, without trying to forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * it to any subcomponents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
    void dispatchEventToSelf(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
        super.dispatchEventImpl(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * Fetchs the top-most (deepest) lightweight component that is interested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * in receiving mouse events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
    Component getMouseEventTarget(int x, int y, boolean includeSelf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
        return getMouseEventTarget(x, y, includeSelf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                                   MouseEventTargetFilter.FILTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                                   !SEARCH_HEAVYWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * Fetches the top-most (deepest) component to receive SunDropTargetEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    Component getDropTargetEventTarget(int x, int y, boolean includeSelf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
        return getMouseEventTarget(x, y, includeSelf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                                   DropTargetEventTargetFilter.FILTER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                                   SEARCH_HEAVYWEIGHTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * A private version of getMouseEventTarget which has two additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * controllable behaviors. This method searches for the top-most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * descendant of this container that contains the given coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * and is accepted by the given filter. The search will be constrained to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * lightweight descendants if the last argument is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * @param filter EventTargetFilter instance to determine whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     *        given component is a valid target for this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * @param searchHeavyweights if <code>false</code>, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     *        will bypass heavyweight components during the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    private Component getMouseEventTarget(int x, int y, boolean includeSelf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                                          EventTargetFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                                          boolean searchHeavyweights) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        Component comp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
        if (searchHeavyweights) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
            comp = getMouseEventTargetImpl(x, y, includeSelf, filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                                           SEARCH_HEAVYWEIGHTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                                           searchHeavyweights);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        if (comp == null || comp == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
            comp = getMouseEventTargetImpl(x, y, includeSelf, filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                                           !SEARCH_HEAVYWEIGHTS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                                           searchHeavyweights);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * A private version of getMouseEventTarget which has three additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * controllable behaviors. This method searches for the top-most
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * descendant of this container that contains the given coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * and is accepted by the given filter. The search will be constrained to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * descendants of only lightweight children or only heavyweight children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * of this container depending on searchHeavyweightChildren. The search will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * be constrained to only lightweight descendants of the searched children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * of this container if searchHeavyweightDescendants is <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * @param filter EventTargetFilter instance to determine whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     *        selected component is a valid target for this event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     * @param searchHeavyweightChildren if <code>true</code>, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     *        will bypass immediate lightweight children during the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     *        If <code>false</code>, the methods will bypass immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     *        heavyweight children during the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * @param searchHeavyweightDescendants if <code>false</code>, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     *        will bypass heavyweight descendants which are not immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     *        children during the search. If <code>true</code>, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     *        will traverse both lightweight and heavyweight descendants during
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     *        the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
    private Component getMouseEventTargetImpl(int x, int y, boolean includeSelf,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                                         EventTargetFilter filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                                         boolean searchHeavyweightChildren,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                                         boolean searchHeavyweightDescendants) {
125
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2270
        synchronized (getTreeLock()) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2271
            int ncomponents = this.ncomponents;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2272
            Component component[] = this.component;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2273
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2274
            for (int i = 0 ; i < ncomponents ; i++) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2275
                Component comp = component[i];
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2276
                if (comp != null && comp.visible &&
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2277
                    ((!searchHeavyweightChildren &&
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2278
                      comp.peer instanceof LightweightPeer) ||
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2279
                     (searchHeavyweightChildren &&
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2280
                      !(comp.peer instanceof LightweightPeer))) &&
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2281
                    comp.contains(x - comp.x, y - comp.y)) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2282
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2283
                    // found a component that intersects the point, see if there
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2284
                    // is a deeper possibility.
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2285
                    if (comp instanceof Container) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2286
                        Container child = (Container) comp;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2287
                        Component deeper = child.getMouseEventTarget(
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2288
                                x - child.x,
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2289
                                y - child.y,
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2290
                                includeSelf,
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2291
                                filter,
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2292
                                searchHeavyweightDescendants);
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2293
                        if (deeper != null) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2294
                            return deeper;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2295
                        }
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2296
                    } else {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2297
                        if (filter.accept(comp)) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2298
                            // there isn't a deeper target, but this component
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2299
                            // is a target
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2300
                            return comp;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2301
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            }
125
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2305
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2306
            boolean isPeerOK;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2307
            boolean isMouseOverMe;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2308
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2309
            isPeerOK = (peer instanceof LightweightPeer) || includeSelf;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2310
            isMouseOverMe = contains(x,y);
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2311
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2312
            // didn't find a child target, return this component if it's
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2313
            // a possible target
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2314
            if (isMouseOverMe && isPeerOK && filter.accept(this)) {
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2315
                return this;
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2316
            }
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2317
            // no possible target
079ae88eaea9 6607660: java.awt.Container.getMouseEventTargetImpl should be invoked while holding the TreeLock
anthony
parents: 121
diff changeset
  2318
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    static interface EventTargetFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        boolean accept(final Component comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    static class MouseEventTargetFilter implements EventTargetFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
        static final EventTargetFilter FILTER = new MouseEventTargetFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        private MouseEventTargetFilter() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
        public boolean accept(final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
            return (comp.eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                || (comp.eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                || (comp.eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                || comp.mouseListener != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                || comp.mouseMotionListener != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                || comp.mouseWheelListener != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
    static class DropTargetEventTargetFilter implements EventTargetFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
        static final EventTargetFilter FILTER = new DropTargetEventTargetFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        private DropTargetEventTargetFilter() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        public boolean accept(final Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            DropTarget dt = comp.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            return dt != null && dt.isActive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * This is called by lightweight components that want the containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * windowed parent to enable some kind of events on their behalf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * This is needed for events that are normally only dispatched to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * windows to be accepted so that they can be forwarded downward to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     * the lightweight component that has enabled them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    void proxyEnableEvents(long events) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        if (peer instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            // this container is lightweight.... continue sending it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
            // upward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
                parent.proxyEnableEvents(events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            // This is a native container, so it needs to host
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            // one of it's children.  If this function is called before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            // a peer has been created we don't yet have a dispatcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            // because it has not yet been determined if this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
            // is lightweight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            if (dispatcher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
                dispatcher.enableEvents(events);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     * replaced by <code>dispatchEvent(AWTEvent e)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
    public void deliverEvent(Event e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        Component comp = getComponentAt(e.x, e.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        if ((comp != null) && (comp != this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
            e.translate(-comp.x, -comp.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            comp.deliverEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            postEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * Locates the component that contains the x,y position.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * top-most child component is returned in the case where there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * is overlap in the components.  This is determined by finding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * the component closest to the index 0 that claims to contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * the given point via Component.contains(), except that Components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * which have native peers take precedence over those which do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * (i.e., lightweight Components).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * @param x the <i>x</i> coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * @param y the <i>y</i> coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * @return null if the component does not contain the position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * If there is no child component at the requested point and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * point is within the bounds of the container the container itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * is returned; otherwise the top-most child is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * @see Component#contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * @since JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
    public Component getComponentAt(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        return locate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * @deprecated As of JDK version 1.1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * replaced by <code>getComponentAt(int, int)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
    public Component locate(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        if (!contains(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            // Two passes: see comment in sun.awt.SunGraphicsCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
                Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
                if (comp != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
                    !(comp.peer instanceof LightweightPeer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
                    if (comp.contains(x - comp.x, y - comp.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
                Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                if (comp != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                    comp.peer instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                    if (comp.contains(x - comp.x, y - comp.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * Gets the component that contains the specified point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * @param      p   the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * @return     returns the component that contains the point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     *                 or <code>null</code> if the component does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     *                 not contain the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * @see        Component#contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
    public Component getComponentAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        return getComponentAt(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * Returns the position of the mouse pointer in this <code>Container</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     * coordinate space if the <code>Container</code> is under the mouse pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     * otherwise returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     * This method is similar to {@link Component#getMousePosition()} with the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * that it can take the <code>Container</code>'s children into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * If <code>allowChildren</code> is <code>false</code>, this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * a non-null value only if the mouse pointer is above the <code>Container</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * directly, not above the part obscured by children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * If <code>allowChildren</code> is <code>true</code>, this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * a non-null value if the mouse pointer is above <code>Container</code> or any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * of its descendants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * @exception HeadlessException if GraphicsEnvironment.isHeadless() returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * @param     allowChildren true if children should be taken into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     * @see       Component#getMousePosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * @return    mouse coordinates relative to this <code>Component</code>, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * @since     1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
    public Point getMousePosition(boolean allowChildren) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        PointerInfo pi = (PointerInfo)java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
            new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                    return MouseInfo.getPointerInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            Component inTheSameWindow = findUnderMouseInWindow(pi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            if (isSameOrAncestorOf(inTheSameWindow, allowChildren)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                return  pointRelativeToComponent(pi.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
    boolean isSameOrAncestorOf(Component comp, boolean allowChildren) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        return this == comp || (allowChildren && isParentOf(comp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     * Locates the visible child component that contains the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * position.  The top-most child component is returned in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * where there is overlap in the components.  If the containing child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     * component is a Container, this method will continue searching for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * the deepest nested child component.  Components which are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * visible are ignored during the search.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     * The findComponentAt method is different from getComponentAt in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * that getComponentAt only searches the Container's immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     * children; if the containing component is a Container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * findComponentAt will search that child to find a nested component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     * @param x the <i>x</i> coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     * @param y the <i>y</i> coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * @return null if the component does not contain the position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * If there is no child component at the requested point and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * point is within the bounds of the container the container itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * @see Component#contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * @see #getComponentAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
    public Component findComponentAt(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            return findComponentAt(x, y, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * Private version of findComponentAt which has a controllable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * behavior. Setting 'ignoreEnabled' to 'false' bypasses disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * Components during the search. This behavior is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     * lightweight cursor support in sun.awt.GlobalCursorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     * The cursor code calls this function directly via native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     * The addition of this feature is temporary, pending the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * adoption of new, public API which exports this feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
    final Component findComponentAt(int x, int y, boolean ignoreEnabled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        if (isRecursivelyVisible()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            return findComponentAtImpl(x, y, ignoreEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
    final Component findComponentAtImpl(int x, int y, boolean ignoreEnabled){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
        if (!(contains(x, y) && visible && (ignoreEnabled || enabled))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        int ncomponents = this.ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        // Two passes: see comment in sun.awt.SunGraphicsCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            if (comp != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                !(comp.peer instanceof LightweightPeer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                    comp = ((Container)comp).findComponentAtImpl(x - comp.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
                                                             y - comp.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                                                             ignoreEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                    comp = comp.locate(x - comp.x, y - comp.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                if (comp != null && comp.visible &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                    (ignoreEnabled || comp.enabled))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
            if (comp != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                comp.peer instanceof LightweightPeer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                    comp = ((Container)comp).findComponentAtImpl(x - comp.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                                                             y - comp.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                                                             ignoreEnabled);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
                    comp = comp.locate(x - comp.x, y - comp.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
                if (comp != null && comp.visible &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                    (ignoreEnabled || comp.enabled))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     * Locates the visible child component that contains the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     * point.  The top-most child component is returned in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     * where there is overlap in the components.  If the containing child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     * component is a Container, this method will continue searching for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     * the deepest nested child component.  Components which are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     * visible are ignored during the search.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
     * The findComponentAt method is different from getComponentAt in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
     * that getComponentAt only searches the Container's immediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
     * children; if the containing component is a Container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
     * findComponentAt will search that child to find a nested component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     * @param      p   the point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * @return null if the component does not contain the position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
     * If there is no child component at the requested point and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
     * point is within the bounds of the container the container itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     * @see Component#contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * @see #getComponentAt
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
    public Component findComponentAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        return findComponentAt(p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * Makes this Container displayable by connecting it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * a native screen resource.  Making a container displayable will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     * cause all of its children to be made displayable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     * This method is called internally by the toolkit and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * not be called directly by programs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     * @see Component#isDisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     * @see #removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            // addNotify() on the children may cause proxy event enabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            // on this instance, so we first call super.addNotify() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            // possibly create an lightweight event dispatcher before calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            // addNotify() on the children which may be lightweight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
            if (! (peer instanceof LightweightPeer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                dispatcher = new LightweightDispatcher(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
            int ncomponents = this.ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                component[i].addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            // Update stacking order if native platform allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
            ContainerPeer cpeer = (ContainerPeer)peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            if (cpeer.isRestackSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                cpeer.restack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
     * Makes this Container undisplayable by removing its connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * to its native screen resource.  Making a container undisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     * will cause all of its children to be made undisplayable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * This method is called by the toolkit internally and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     * not be called directly by programs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
     * @see Component#isDisplayable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     * @see #addNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
    public void removeNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
            int ncomponents = this.ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
            Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
            for (int i = ncomponents-1 ; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                if( component[i] != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                component[i].removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            if ( dispatcher != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                dispatcher.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
        dispatcher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            super.removeNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
     * Checks if the component is contained in the component hierarchy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
     * this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
     * @param c the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
     * @return     <code>true</code> if it is an ancestor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
     *             <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
     * @since      JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    public boolean isAncestorOf(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        Container p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        if (c == null || ((p = c.getParent()) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
        while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            if (p == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            p = p.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     * The following code was added to support modal JInternalFrames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
     * Unfortunately this code has to be added here so that we can get access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
     * some private AWT classes like SequencedEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
     * The native container of the LW component has this field set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
     * to tell it that it should block Mouse events for all LW
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
     * children except for the modal component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * In the case of nested Modal components, we store the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
     * modal component in the new modal components value of modalComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
    transient Component modalComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
    transient AppContext modalAppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
    private void startLWModal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
        // Store the app context on which this component is being shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
        // Event dispatch thread of this app context will be sleeping until
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        // we wake it by any event from hideAndDisposeHandler().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
        modalAppContext = AppContext.getAppContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
        // keep the KeyEvents from being dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        // until the focus has been transfered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
        long time = Toolkit.getEventQueue().getMostRecentEventTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        Component predictedFocusOwner = (Component.isInstanceOf(this, "javax.swing.JInternalFrame")) ? ((javax.swing.JInternalFrame)(this)).getMostRecentFocusOwner() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
        if (predictedFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
            KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                enqueueKeyEvents(time, predictedFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
        // We have two mechanisms for blocking: 1. If we're on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
        // EventDispatchThread, start a new event pump. 2. If we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
        // on any other thread, call wait() on the treelock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
        final Container nativeContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
            nativeContainer = getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            if (nativeContainer.modalComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                this.modalComp =  nativeContainer.modalComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                nativeContainer.modalComp = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                nativeContainer.modalComp = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        Runnable pumpEventsForHierarchy = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                EventDispatchThread dispatchThread =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
                    (EventDispatchThread)Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
                dispatchThread.pumpEventsForHierarchy(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
                        new Conditional() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                        public boolean evaluate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
                        return ((windowClosingException == null) && (nativeContainer.modalComp != null)) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
                        }, Container.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
            SequencedEvent currentSequencedEvent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                getCurrentSequencedEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            if (currentSequencedEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
                currentSequencedEvent.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
            pumpEventsForHierarchy.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                Toolkit.getEventQueue().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                    postEvent(new PeerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                                pumpEventsForHierarchy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                                PeerEvent.PRIORITY_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                while ((windowClosingException == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
                       (nativeContainer.modalComp != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                        getTreeLock().wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
                    } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        if (windowClosingException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            windowClosingException.fillInStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
            throw windowClosingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        if (predictedFocusOwner != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
            KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                dequeueKeyEvents(time, predictedFocusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
    private void stopLWModal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
            if (modalAppContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
                Container nativeContainer = getHeavyweightContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
                if(nativeContainer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
                    if (this.modalComp !=  null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
                        nativeContainer.modalComp = this.modalComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
                        this.modalComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                        nativeContainer.modalComp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                // Wake up event dispatch thread on which the dialog was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                // initially shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                SunToolkit.postEvent(modalAppContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                        new PeerEvent(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
                                new WakingRunnable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
                                PeerEvent.PRIORITY_EVENT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
            EventQueue.invokeLater(new WakingRunnable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
            getTreeLock().notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
    final static class WakingRunnable implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    /* End of JOptionPane support code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * Returns a string representing the state of this <code>Container</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * @return    the parameter string of this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        String str = super.paramString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        LayoutManager layoutMgr = this.layoutMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        if (layoutMgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
            str += ",layout=" + layoutMgr.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
     * Prints a listing of this container to the specified output
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
     * stream. The listing starts at the specified indentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * The immediate children of the container are printed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * an indentation of <code>indent+1</code>.  The children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * of those children are printed at <code>indent+2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * @param    out      a print stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     * @param    indent   the number of spaces to indent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     * @see      Component#list(java.io.PrintStream, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
     * @since    JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
    public void list(PrintStream out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        super.list(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        int ncomponents = this.ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                comp.list(out, indent+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     * Prints out a list, starting at the specified indentation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * to the specified print writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * The immediate children of the container are printed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
     * an indentation of <code>indent+1</code>.  The children
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * of those children are printed at <code>indent+2</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     * and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
     * @param    out      a print writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
     * @param    indent   the number of spaces to indent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
     * @see      Component#list(java.io.PrintWriter, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
     * @since    JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
    public void list(PrintWriter out, int indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
        super.list(out, indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
        int ncomponents = this.ncomponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
        Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
        for (int i = 0 ; i < ncomponents ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
            Component comp = component[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
            if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                comp.list(out, indent+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
     * Sets the focus traversal keys for a given traversal operation for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
     * Container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
     * The default values for a Container's focus traversal keys are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
     * implementation-dependent. Sun recommends that all implementations for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
     * particular native platform use the same default values. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
     * recommendations for Windows and Unix are listed below. These
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
     * recommendations are used in the Sun AWT implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
     * <table border=1 summary="Recommended default values for a Container's focus traversal keys">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
     *    <th>Identifier</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
     *    <th>Meaning</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
     *    <th>Default</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
     *    <td>KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
     *    <td>Normal forward keyboard traversal</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
     *    <td>TAB on KEY_PRESSED, CTRL-TAB on KEY_PRESSED</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
     *    <td>KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
     *    <td>Normal reverse keyboard traversal</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
     *    <td>SHIFT-TAB on KEY_PRESSED, CTRL-SHIFT-TAB on KEY_PRESSED</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
     *    <td>KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
     *    <td>Go up one focus traversal cycle</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
     *    <td>none</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
     *    <td>KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS<td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
     *    <td>Go down one focus traversal cycle</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
     *    <td>none</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
     * To disable a traversal key, use an empty Set; Collections.EMPTY_SET is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
     * recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
     * Using the AWTKeyStroke API, client code can specify on which of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
     * specific KeyEvents, KEY_PRESSED or KEY_RELEASED, the focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
     * operation will occur. Regardless of which KeyEvent is specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
     * however, all KeyEvents related to the focus traversal key, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
     * associated KEY_TYPED event, will be consumed, and will not be dispatched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
     * to any Container. It is a runtime error to specify a KEY_TYPED event as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
     * mapping to a focus traversal operation, or to map the same event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
     * multiple default focus traversal operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
     * If a value of null is specified for the Set, this Container inherits the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     * Set from its parent. If all ancestors of this Container have null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
     * specified for the Set, then the current KeyboardFocusManager's default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
     * Set is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
     * @param keystrokes the Set of AWTKeyStroke for the specified operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
     * @see #getFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
     * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
     * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
     * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
     * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
     * @throws IllegalArgumentException if id is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
     *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
     *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
     *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS, or if keystrokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
     *         contains null, or if any Object in keystrokes is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
     *         AWTKeyStroke, or if any keystroke represents a KEY_TYPED event,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
     *         or if any keystroke already maps to another focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
     *         operation for this Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
    public void setFocusTraversalKeys(int id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                                      Set<? extends AWTKeyStroke> keystrokes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            throw new IllegalArgumentException("invalid focus traversal key identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        // Don't call super.setFocusTraversalKey. The Component parameter check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
        // does not allow DOWN_CYCLE_TRAVERSAL_KEYS, but we do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
        setFocusTraversalKeys_NoIDCheck(id, keystrokes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
     * Returns the Set of focus traversal keys for a given traversal operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
     * for this Container. (See
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
     * <code>setFocusTraversalKeys</code> for a full description of each key.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
     * If a Set of traversal keys has not been explicitly defined for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
     * Container, then this Container's parent's Set is returned. If no Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
     * has been explicitly defined for any of this Container's ancestors, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
     * the current KeyboardFocusManager's default Set is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
     * @return the Set of AWTKeyStrokes for the specified operation. The Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
     *         will be unmodifiable, and may be empty. null will never be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
     *         returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
     * @see #setFocusTraversalKeys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
     * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
     * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
     * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
     * @see KeyboardFocusManager#DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
     * @throws IllegalArgumentException if id is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
     *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
     *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
     *         KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
    public Set<AWTKeyStroke> getFocusTraversalKeys(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            throw new IllegalArgumentException("invalid focus traversal key identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
        // Don't call super.getFocusTraversalKey. The Component parameter check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        // does not allow DOWN_CYCLE_TRAVERSAL_KEY, but we do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
        return getFocusTraversalKeys_NoIDCheck(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
     * Returns whether the Set of focus traversal keys for the given focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
     * traversal operation has been explicitly defined for this Container. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
     * this method returns <code>false</code>, this Container is inheriting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
     * Set from an ancestor, or from the current KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
     * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
     * @return <code>true</code> if the the Set of focus traversal keys for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
     *         given focus traversal operation has been explicitly defined for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
     *         this Component; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
     * @throws IllegalArgumentException if id is not one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
     *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
     *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
     *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
     *        KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
    public boolean areFocusTraversalKeysSet(int id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
        if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
            throw new IllegalArgumentException("invalid focus traversal key identifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        return (focusTraversalKeys != null && focusTraversalKeys[id] != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
     * Returns whether the specified Container is the focus cycle root of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
     * Container's focus traversal cycle. Each focus traversal cycle has only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
     * a single focus cycle root and each Container which is not a focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
     * root belongs to only a single focus traversal cycle. Containers which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
     * are focus cycle roots belong to two cycles: one rooted at the Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
     * itself, and one rooted at the Container's nearest focus-cycle-root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
     * ancestor. This method will return <code>true</code> for both such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
     * Containers in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
     * @param container the Container to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
     * @return <code>true</code> if the specified Container is a focus-cycle-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
     *         root of this Container; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
     * @see #isFocusCycleRoot()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
    public boolean isFocusCycleRoot(Container container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
        if (isFocusCycleRoot() && container == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
            return super.isFocusCycleRoot(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
    private Container findTraversalRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
        // I potentially have two roots, myself and my root parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
        // If I am the current root, then use me
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
        // If none of my parents are roots, then use me
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
        // If my root parent is the current root, then use my root parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
        // If neither I nor my root parent is the current root, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        // use my root parent (a guess)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        Container currentFocusCycleRoot = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
            getCurrentKeyboardFocusManager().getCurrentFocusCycleRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
        Container root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
        if (currentFocusCycleRoot == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            root = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            root = getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
            if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                root = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
        if (root != currentFocusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
            KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
                setGlobalCurrentFocusCycleRoot(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
        return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
    final boolean containsFocus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
        final Component focusOwner = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
        return isParentOf(focusOwner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
     * Check if this component is the child of this container or its children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
     * Note: this function acquires treeLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
     * Note: this function traverses children tree only in one Window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
     * @param comp a component in test, must not be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
    private boolean isParentOf(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            while (comp != null && comp != this && !(comp instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
                comp = comp.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
            return (comp == this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
    void clearMostRecentFocusOwnerOnHide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
        boolean reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        Window window = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
            window = getContainingWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
            if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                Component comp = KeyboardFocusManager.getMostRecentFocusOwner(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                reset = ((comp == this) || isParentOf(comp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                // This synchronized should always be the second in a pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                // (tree lock, KeyboardFocusManager.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
                synchronized(KeyboardFocusManager.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                    Component storedComp = window.getTemporaryLostComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
                    if (isParentOf(storedComp) || storedComp == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
                        window.setTemporaryLostComponent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
        if (reset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
            KeyboardFocusManager.setMostRecentFocusOwner(window, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
    void clearCurrentFocusCycleRootOnHide() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        KeyboardFocusManager kfm =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            KeyboardFocusManager.getCurrentKeyboardFocusManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        Container cont = kfm.getCurrentFocusCycleRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        if (cont == this || isParentOf(cont)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
            kfm.setGlobalCurrentFocusCycleRoot(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
    final Container getTraversalRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
        if (isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
            return findTraversalRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
        return super.getTraversalRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
     * Sets the focus traversal policy that will manage keyboard traversal of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
     * this Container's children, if this Container is a focus cycle root. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
     * the argument is null, this Container inherits its policy from its focus-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
     * cycle-root ancestor. If the argument is non-null, this policy will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
     * inherited by all focus-cycle-root children that have no keyboard-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
     * traversal policy of their own (as will, recursively, their focus-cycle-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
     * root children).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
     * If this Container is not a focus cycle root, the policy will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
     * remembered, but will not be used or inherited by this or any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
     * Containers until this Container is made a focus cycle root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
     * @param policy the new focus traversal policy for this Container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
     * @see #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
     * @see #isFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
    public void setFocusTraversalPolicy(FocusTraversalPolicy policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        FocusTraversalPolicy oldPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            oldPolicy = this.focusTraversalPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
            this.focusTraversalPolicy = policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
        firePropertyChange("focusTraversalPolicy", oldPolicy, policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
     * Returns the focus traversal policy that will manage keyboard traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
     * of this Container's children, or null if this Container is not a focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
     * cycle root. If no traversal policy has been explicitly set for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
     * Container, then this Container's focus-cycle-root ancestor's policy is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
     * @return this Container's focus traversal policy, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
     *         Container is not a focus cycle root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
     * @see #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
     * @see #isFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
    public FocusTraversalPolicy getFocusTraversalPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
        if (!isFocusTraversalPolicyProvider() && !isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
        FocusTraversalPolicy policy = this.focusTraversalPolicy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
        if (policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            return policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        Container rootAncestor = getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        if (rootAncestor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
            return rootAncestor.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            return KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                getDefaultFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
     * Returns whether the focus traversal policy has been explicitly set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
     * this Container. If this method returns <code>false</code>, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
     * Container will inherit its focus traversal policy from an ancestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
     * @return <code>true</code> if the focus traversal policy has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
     *         explicitly set for this Container; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
    public boolean isFocusTraversalPolicySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
        return (focusTraversalPolicy != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
     * Sets whether this Container is the root of a focus traversal cycle. Once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
     * focus enters a traversal cycle, typically it cannot leave it via focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
     * traversal unless one of the up- or down-cycle keys is pressed. Normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
     * traversal is limited to this Container, and all of this Container's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
     * descendants that are not descendants of inferior focus cycle roots. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * that a FocusTraversalPolicy may bend these restrictions, however. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * example, ContainerOrderFocusTraversalPolicy supports implicit down-cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     * traversal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
     * The alternative way to specify the traversal order of this Container's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
     * children is to make this Container a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
     * <a href="doc-files/FocusSpec.html#FocusTraversalPolicyProviders">focus traversal policy provider</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
     * @param focusCycleRoot indicates whether this Container is the root of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
     *        focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
     * @see #isFocusCycleRoot()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
     * @see ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
     * @see #setFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
    public void setFocusCycleRoot(boolean focusCycleRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        boolean oldFocusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
            oldFocusCycleRoot = this.focusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
            this.focusCycleRoot = focusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
        firePropertyChange("focusCycleRoot", oldFocusCycleRoot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                           focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
     * Returns whether this Container is the root of a focus traversal cycle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
     * Once focus enters a traversal cycle, typically it cannot leave it via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
     * focus traversal unless one of the up- or down-cycle keys is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
     * Normal traversal is limited to this Container, and all of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
     * Container's descendants that are not descendants of inferior focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
     * cycle roots. Note that a FocusTraversalPolicy may bend these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
     * restrictions, however. For example, ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
     * supports implicit down-cycle traversal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
     * @return whether this Container is the root of a focus traversal cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
     * @see #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
     * @see ContainerOrderFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
    public boolean isFocusCycleRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
        return focusCycleRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
     * Sets whether this container will be used to provide focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
     * traversal policy. Container with this property as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
     * <code>true</code> will be used to acquire focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
     * instead of closest focus cycle root ancestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
     * @param provider indicates whether this container will be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
     *                provide focus traversal policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
     * @see #isFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
    public final void setFocusTraversalPolicyProvider(boolean provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
        boolean oldProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
            oldProvider = focusTraversalPolicyProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
            focusTraversalPolicyProvider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
        firePropertyChange("focusTraversalPolicyProvider", oldProvider, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
     * Returns whether this container provides focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
     * policy. If this property is set to <code>true</code> then when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
     * keyboard focus manager searches container hierarchy for focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
     * traversal policy and encounters this container before any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     * container with this property as true or focus cycle roots then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
     * its focus traversal policy will be used instead of focus cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
     * root's policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
     * @see #setFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
     * @see #getFocusTraversalPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
     * @see #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
     * @see #setFocusTraversalPolicyProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
     * @return <code>true</code> if this container provides focus traversal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
     *         policy, <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
    public final boolean isFocusTraversalPolicyProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
        return focusTraversalPolicyProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
     * Transfers the focus down one focus traversal cycle. If this Container is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
     * a focus cycle root, then the focus owner is set to this Container's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
     * default Component to focus, and the current focus cycle root is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
     * this Container. If this Container is not a focus cycle root, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
     * focus traversal operation occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
     * @see       Component#requestFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
     * @see       #isFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
     * @see       #setFocusCycleRoot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
     * @since     1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
    public void transferFocusDownCycle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
        if (isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
            KeyboardFocusManager.getCurrentKeyboardFocusManager().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                setGlobalCurrentFocusCycleRoot(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
            Component toFocus = getFocusTraversalPolicy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
                getDefaultComponent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
            if (toFocus != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                toFocus.requestFocus(CausedFocusEvent.Cause.TRAVERSAL_DOWN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    void preProcessKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
        Container parent = this.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
            parent.preProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
    void postProcessKeyEvent(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
        Container parent = this.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
            parent.postProcessKeyEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
    boolean postsOldMouseEvents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
     * Sets the <code>ComponentOrientation</code> property of this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
     * and all components contained within it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
     * @param o the new component orientation of this container and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
     *        the components contained within it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
     * @exception NullPointerException if <code>orientation</code> is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
     * @see Component#setComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
     * @see Component#getComponentOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
    public void applyComponentOrientation(ComponentOrientation o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
        super.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
        for (int i = 0 ; i < ncomponents ; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
             component[i].applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
     * Adds a PropertyChangeListener to the listener list. The listener is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
     * registered for all bound properties of this class, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
     * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
     *    <li>this Container's font ("font")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
     *    <li>this Container's background color ("background")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
     *    <li>this Container's foreground color ("foreground")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
     *    <li>this Container's focusability ("focusable")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
     *    <li>this Container's focus traversal keys enabled state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
     *        ("focusTraversalKeysEnabled")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
     *    <li>this Container's Set of FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
     *        ("forwardFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
     *    <li>this Container's Set of BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
     *        ("backwardFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
     *    <li>this Container's Set of UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
     *        ("upCycleFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
     *    <li>this Container's Set of DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
     *        ("downCycleFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
     *    <li>this Container's focus traversal policy ("focusTraversalPolicy")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
     *        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
     *    <li>this Container's focus-cycle-root state ("focusCycleRoot")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
     * Note that if this Container is inheriting a bound property, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
     * event will be fired in response to a change in the inherited property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
     * @param    listener  the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
     * @see Component#removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
     * @see #addPropertyChangeListener(java.lang.String,java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
    public void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
        super.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
     * Adds a PropertyChangeListener to the listener list for a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
     * property. The specified property may be user-defined, or one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
     * following defaults:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
     *    <li>this Container's font ("font")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
     *    <li>this Container's background color ("background")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
     *    <li>this Container's foreground color ("foreground")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
     *    <li>this Container's focusability ("focusable")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
     *    <li>this Container's focus traversal keys enabled state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
     *        ("focusTraversalKeysEnabled")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
     *    <li>this Container's Set of FORWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
     *        ("forwardFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
     *    <li>this Container's Set of BACKWARD_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
     *        ("backwardFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
     *    <li>this Container's Set of UP_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
     *        ("upCycleFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
     *    <li>this Container's Set of DOWN_CYCLE_TRAVERSAL_KEYS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
     *        ("downCycleFocusTraversalKeys")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
     *    <li>this Container's focus traversal policy ("focusTraversalPolicy")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
     *        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
     *    <li>this Container's focus-cycle-root state ("focusCycleRoot")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
     *    <li>this Container's focus-traversal-policy-provider state("focusTraversalPolicyProvider")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
     *    <li>this Container's focus-traversal-policy-provider state("focusTraversalPolicyProvider")</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
     * Note that if this Container is inheriting a bound property, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
     * event will be fired in response to a change in the inherited property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
     * If listener is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
     * @param propertyName one of the property names listed above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
     * @param listener the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
     * @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
     * @see Component#removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
    public void addPropertyChangeListener(String propertyName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
                                          PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
        super.addPropertyChangeListener(propertyName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
    // Serialization support. A Container is responsible for restoring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
    // parent fields of its component children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
     * Container Serial Data Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
    private int containerSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
     * Serializes this <code>Container</code> to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
     * <code>ObjectOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
     *    <li>Writes default serializable fields to the stream.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
     *    <li>Writes a list of serializable ContainerListener(s) as optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
     *        data. The non-serializable ContainerListner(s) are detected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
     *        no attempt is made to serialize them.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
     *    <li>Write this Container's FocusTraversalPolicy if and only if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
     *        is Serializable; otherwise, <code>null</code> is written.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
     * @param s the <code>ObjectOutputStream</code> to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
     * @serialData <code>null</code> terminated sequence of 0 or more pairs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
     *   the pair consists of a <code>String</code> and <code>Object</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
     *   the <code>String</code> indicates the type of object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
     *   is one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
     *   <code>containerListenerK</code> indicating an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
     *     <code>ContainerListener</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
     *   the <code>Container</code>'s <code>FocusTraversalPolicy</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
     *     or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
     * @see AWTEventMulticaster#save(java.io.ObjectOutputStream, java.lang.String, java.util.EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
     * @see Container#containerListenerK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
     * @see #readObject(ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
        ObjectOutputStream.PutField f = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
        f.put("ncomponents", ncomponents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
        f.put("component", component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        f.put("layoutMgr", layoutMgr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
        f.put("dispatcher", dispatcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
        f.put("maxSize", maxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        f.put("focusCycleRoot", focusCycleRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
        f.put("containerSerializedDataVersion", containerSerializedDataVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
        f.put("focusTraversalPolicyProvider", focusTraversalPolicyProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
        AWTEventMulticaster.save(s, containerListenerK, containerListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
        if (focusTraversalPolicy instanceof java.io.Serializable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
            s.writeObject(focusTraversalPolicy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
            s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * Deserializes this <code>Container</code> from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     * <code>ObjectInputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
     *    <li>Reads default serializable fields from the stream.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
     *    <li>Reads a list of serializable ContainerListener(s) as optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
     *        data. If the list is null, no Listeners are installed.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
     *    <li>Reads this Container's FocusTraversalPolicy, which may be null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
     *        as optional data.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
     * @see #addContainerListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
     * @see #writeObject(ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
        ObjectInputStream.GetField f = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
        ncomponents = f.get("ncomponents", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
        component = (Component[])f.get("component", new Component[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
        layoutMgr = (LayoutManager)f.get("layoutMgr", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
        dispatcher = (LightweightDispatcher)f.get("dispatcher", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
        // Old stream. Doesn't contain maxSize among Component's fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
        if (maxSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
            maxSize = (Dimension)f.get("maxSize", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
        focusCycleRoot = f.get("focusCycleRoot", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
        containerSerializedDataVersion = f.get("containerSerializedDataVersion", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
        focusTraversalPolicyProvider = f.get("focusTraversalPolicyProvider", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
        Component component[] = this.component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
        for(int i = 0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
            component[i].parent = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
            adjustListeningChildren(AWTEvent.HIERARCHY_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
                component[i].numListening(AWTEvent.HIERARCHY_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            adjustListeningChildren(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                component[i].numListening(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
                    AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
            adjustDescendants(component[i].countHierarchyMembers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
        Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
            String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
            if (containerListenerK == key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
                addContainerListener((ContainerListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
                s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            Object policy = s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            if (policy instanceof FocusTraversalPolicy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                focusTraversalPolicy = (FocusTraversalPolicy)policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        } catch (java.io.OptionalDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
            // JDK 1.1/1.2/1.3 instances will not have this optional data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
            // e.eof will be true to indicate that there is no more data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
            // available for this object. If e.eof is not true, throw the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
            // exception as it might have been caused by reasons unrelated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
            // focusTraversalPolicy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
            if (!e.eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
     * Inner class of Container used to provide default support for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
     * accessibility.  This class is not meant to be used directly by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
     * application developers, but is instead meant only to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
     * subclassed by container developers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
     * The class used to obtain the accessible role for this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
     * as well as implementing many of the methods in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
     * AccessibleContainer interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    protected class AccessibleAWTContainer extends AccessibleAWTComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
         * JDK1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
        private static final long serialVersionUID = 5081320404842566097L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
         * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
         * of the children of this object implement <code>Accessible</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
         * then this method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
         * @return the number of accessible children in the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
            return Container.this.getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
         * Returns the nth <code>Accessible</code> child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
         * @return the nth <code>Accessible</code> child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
            return Container.this.getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
         * Returns the <code>Accessible</code> child, if one exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
         * contained at the local coordinate <code>Point</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
         * @param p the point defining the top-left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
         *    <code>Accessible</code>, given in the coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
         *    of the object's parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
         * @return the <code>Accessible</code>, if it exists,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
         *    at the specified location; else <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
        public Accessible getAccessibleAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
            return Container.this.getAccessibleAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
        protected ContainerListener accessibleContainerHandler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
         * Fire <code>PropertyChange</code> listener, if one is registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
         * when children are added or removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
        protected class AccessibleContainerHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
            implements ContainerListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
            public void componentAdded(ContainerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                Component c = e.getChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
                if (c != null && c instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
                    AccessibleAWTContainer.this.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
                        AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
                        null, ((Accessible) c).getAccessibleContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
            public void componentRemoved(ContainerEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
                Component c = e.getChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
                if (c != null && c instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
                    AccessibleAWTContainer.this.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                        AccessibleContext.ACCESSIBLE_CHILD_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                        ((Accessible) c).getAccessibleContext(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
         * Adds a PropertyChangeListener to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
         * @param listener  the PropertyChangeListener to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
        public void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
            if (accessibleContainerHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                accessibleContainerHandler = new AccessibleContainerHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
                Container.this.addContainerListener(accessibleContainerHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            super.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
    } // inner class AccessibleAWTContainer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
     * Returns the <code>Accessible</code> child contained at the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
     * coordinate <code>Point</code>, if one exists.  Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
     * returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
     * @param p the point defining the top-left corner of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
     *    <code>Accessible</code>, given in the coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
     *    of the object's parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
     * @return the <code>Accessible</code> at the specified location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
     *    if it exists; otherwise <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
    Accessible getAccessibleAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
            if (this instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
                Accessible a = (Accessible)this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                AccessibleContext ac = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                    AccessibleComponent acmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                    Point location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                    int nchildren = ac.getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                    for (int i=0; i < nchildren; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                        a = ac.getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                        if ((a != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                            ac = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                            if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                                acmp = ac.getAccessibleComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                                if ((acmp != null) && (acmp.isShowing())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                                    location = acmp.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                                    Point np = new Point(p.x-location.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                                                         p.y-location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                                    if (acmp.contains(np)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                                        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                return (Accessible)this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                Component ret = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                if (!this.contains(p.x,p.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                    ret = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
                    int ncomponents = this.getComponentCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                    for (int i=0; i < ncomponents; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
                        Component comp = this.getComponent(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
                        if ((comp != null) && comp.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                            Point location = comp.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
                            if (comp.contains(p.x-location.x,p.y-location.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
                                ret = comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
                if (ret instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
                    return (Accessible) ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     * of the children of this object implement <code>Accessible</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     * then this method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
     * @return the number of accessible children in the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
    int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
            Component[] children = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
            for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
                if (children[i] instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                    count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
            return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
     * Returns the nth <code>Accessible</code> child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
     * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
     * @return the nth <code>Accessible</code> child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
    Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
            Component[] children = this.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
            for (int j = 0; j < children.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
                if (children[j] instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
                    if (count == i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
                        return (Accessible) children[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
    // ************************** MIXING CODE *******************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
    final void increaseComponentCount(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
            if (!c.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
                    "Peer does not exist while invoking the increaseComponentCount() method"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
            int addHW = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
            int addLW = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
            if (c instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
                addLW = ((Container)c).numOfLWComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
                addHW = ((Container)c).numOfHWComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
            if (c.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
                addLW++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
                addHW++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
            for (Container cont = this; cont != null; cont = cont.getContainer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                cont.numOfLWComponents += addLW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                cont.numOfHWComponents += addHW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
    final void decreaseComponentCount(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
            if (!c.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
                    "Peer does not exist while invoking the decreaseComponentCount() method"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
            int subHW = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
            int subLW = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
            if (c instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                subLW = ((Container)c).numOfLWComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                subHW = ((Container)c).numOfHWComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
            if (c.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
                subLW++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
                subHW++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            for (Container cont = this; cont != null; cont = cont.getContainer()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
                cont.numOfLWComponents -= subLW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
                cont.numOfHWComponents -= subHW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
    private int getTopmostComponentIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
        if (getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
    private int getBottommostComponentIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
        if (getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
            return getComponentCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
    final void recursiveSubtractAndApplyShape(Region shape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
        recursiveSubtractAndApplyShape(shape, getTopmostComponentIndex(), getBottommostComponentIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
    final void recursiveSubtractAndApplyShape(Region shape, int fromZorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
        recursiveSubtractAndApplyShape(shape, fromZorder, getBottommostComponentIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
    final void recursiveSubtractAndApplyShape(Region shape, int fromZorder, int toZorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
        if (mixingLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
            mixingLog.fine("this = " + this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
                "; shape=" + shape + "; fromZ=" + fromZorder + "; toZ=" + toZorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
        if (fromZorder == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
        for (int index = fromZorder; index <= toZorder; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
            Component comp = getComponent(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
            if (!comp.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
                comp.subtractAndApplyShape(shape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
            } else if (comp instanceof Container &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
                    ((Container)comp).hasHeavyweightDescendants() && comp.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
                ((Container)comp).recursiveSubtractAndApplyShape(shape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
    final void recursiveApplyCurrentShape() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
        recursiveApplyCurrentShape(getTopmostComponentIndex(), getBottommostComponentIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
    final void recursiveApplyCurrentShape(int fromZorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
        recursiveApplyCurrentShape(fromZorder, getBottommostComponentIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
    final void recursiveApplyCurrentShape(int fromZorder, int toZorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
        checkTreeLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
        if (mixingLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
            mixingLog.fine("this = " + this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
                "; fromZ=" + fromZorder + "; toZ=" + toZorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
        if (fromZorder == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
        for (int index = fromZorder; index <= toZorder; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
            Component comp = getComponent(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
            if (!comp.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
                comp.applyCurrentShape();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
            } else if (comp instanceof Container &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
                    ((Container)comp).hasHeavyweightDescendants()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
                ((Container)comp).recursiveApplyCurrentShape();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
    void mixOnShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
            if (mixingLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
                mixingLog.fine("this = " + this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
            boolean isLightweight = isLightweight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
            if (!isLightweight || (isLightweight && hasHeavyweightDescendants())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
                recursiveApplyCurrentShape();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
            super.mixOnShowing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
    void mixOnZOrderChanging(int oldZorder, int newZorder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3973
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3974
            if (mixingLog.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
                mixingLog.fine("this = " + this +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
                    "; oldZ=" + oldZorder + "; newZ=" + newZorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
            boolean becameHigher = newZorder < oldZorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3981
            if (becameHigher && isLightweight() && hasHeavyweightDescendants()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3982
                recursiveApplyCurrentShape();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3984
            super.mixOnZOrderChanging(oldZorder, newZorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3988
    // ****************** END OF MIXING CODE ********************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3989
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3992
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3993
 * Class to manage the dispatching of MouseEvents to the lightweight descendants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3994
 * and SunDropTargetEvents to both lightweight and heavyweight descendants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3995
 * contained by a native container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3997
 * NOTE: the class name is not appropriate anymore, but we cannot change it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3998
 * because we must keep serialization compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3999
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4000
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4001
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4002
class LightweightDispatcher implements java.io.Serializable, AWTEventListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4004
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4005
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4006
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4007
    private static final long serialVersionUID = 5184291520170872969L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4008
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4009
     * Our own mouse event for when we're dragged over from another hw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4010
     * container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4011
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4012
    private static final int  LWD_MOUSE_DRAGGED_OVER = 1500;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4014
    private static final Logger eventLog = Logger.getLogger("java.awt.event.LightweightDispatcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4016
    LightweightDispatcher(Container nativeContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4017
        this.nativeContainer = nativeContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4018
        mouseEventTarget = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4019
        eventMask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4022
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4023
     * Clean up any resources allocated when dispatcher was created;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4024
     * should be called from Container.removeNotify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4026
    void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4027
        //System.out.println("Disposing lw dispatcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4028
        stopListeningForOtherDrags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4029
        mouseEventTarget = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4032
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4033
     * Enables events to subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4035
    void enableEvents(long events) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4036
        eventMask |= events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4040
     * Dispatches an event to a sub-component if necessary, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4041
     * returns whether or not the event was forwarded to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4042
     * sub-component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4044
     * @param e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4045
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4046
    boolean dispatchEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4047
        boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4049
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4050
         * Fix for BugTraq Id 4389284.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4051
         * Dispatch SunDropTargetEvents regardless of eventMask value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4052
         * Do not update cursor on dispatching SunDropTargetEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4053
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4054
        if (e instanceof SunDropTargetEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4055
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4056
            SunDropTargetEvent sdde = (SunDropTargetEvent) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4057
            ret = processDropTargetEvent(sdde);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4059
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4060
            if (e instanceof MouseEvent && (eventMask & MOUSE_MASK) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4061
                MouseEvent me = (MouseEvent) e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4062
                ret = processMouseEvent(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4063
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4065
            if (e.getID() == MouseEvent.MOUSE_MOVED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4066
                nativeContainer.updateCursorImmediately();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4067
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4070
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4071
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4073
    /* This method effectively returns whether or not a mouse button was down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4074
     * just BEFORE the event happened.  A better method name might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     * wasAMouseButtonDownBeforeThisEvent().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    private boolean isMouseGrab(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
        int modifiers = e.getModifiersEx();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
        if(e.getID() == MouseEvent.MOUSE_PRESSED
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
            || e.getID() == MouseEvent.MOUSE_RELEASED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
            switch (e.getButton()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
            case MouseEvent.BUTTON1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
                modifiers ^= InputEvent.BUTTON1_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
            case MouseEvent.BUTTON2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
                modifiers ^= InputEvent.BUTTON2_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
            case MouseEvent.BUTTON3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
                modifiers ^= InputEvent.BUTTON3_DOWN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
        /* modifiers now as just before event */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
        return ((modifiers & (InputEvent.BUTTON1_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
                              | InputEvent.BUTTON2_DOWN_MASK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
                              | InputEvent.BUTTON3_DOWN_MASK)) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
     * This method attempts to distribute a mouse event to a lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
     * component.  It tries to avoid doing any unnecessary probes down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
     * into the component tree to minimize the overhead of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
     * where to route the event, since mouse movement events tend to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
     * come in large and frequent amounts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
    private boolean processMouseEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
        Component mouseOver =   // sensitive to mouse events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
            nativeContainer.getMouseEventTarget(e.getX(), e.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
                                                Container.INCLUDE_SELF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
        trackMouseEnterExit(mouseOver, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
    // 4508327 : MOUSE_CLICKED should only go to the recipient of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
    // the accompanying MOUSE_PRESSED, so don't reset mouseEventTarget on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
    // MOUSE_CLICKED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
    if (!isMouseGrab(e) && id != MouseEvent.MOUSE_CLICKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
            mouseEventTarget = (mouseOver != nativeContainer) ? mouseOver: null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
        if (mouseEventTarget != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
            switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
            case MouseEvent.MOUSE_ENTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
            case MouseEvent.MOUSE_EXITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
            case MouseEvent.MOUSE_PRESSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
                retargetMouseEvent(mouseEventTarget, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
        case MouseEvent.MOUSE_RELEASED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
            retargetMouseEvent(mouseEventTarget, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
        case MouseEvent.MOUSE_CLICKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
        // 4508327: MOUSE_CLICKED should never be dispatched to a Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
        // other than that which received the MOUSE_PRESSED event.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
        // mouse is now over a different Component, don't dispatch the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
        // The previous fix for a similar problem was associated with bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
        // 4155217.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
        if (mouseOver == mouseEventTarget) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
            retargetMouseEvent(mouseOver, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
            case MouseEvent.MOUSE_MOVED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
                retargetMouseEvent(mouseEventTarget, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
        case MouseEvent.MOUSE_DRAGGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            if (isMouseGrab(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
                retargetMouseEvent(mouseEventTarget, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
        case MouseEvent.MOUSE_WHEEL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
            // This may send it somewhere that doesn't have MouseWheelEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
            // enabled.  In this case, Component.dispatchEventImpl() will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
            // retarget the event to a parent that DOES have the events enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
            if (eventLog.isLoggable(Level.FINEST) && (mouseOver != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
                eventLog.log(Level.FINEST, "retargeting mouse wheel to " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
                             mouseOver.getName() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
                             mouseOver.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
            retargetMouseEvent(mouseOver, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4166
    return e.isConsumed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
    private boolean processDropTargetEvent(SunDropTargetEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
        int id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
        int x = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
        int y = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
         * Fix for BugTraq ID 4395290.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
         * It is possible that SunDropTargetEvent's Point is outside of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
         * native container bounds. In this case we truncate coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
        if (!nativeContainer.contains(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
            final Dimension d = nativeContainer.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
            if (d.width <= x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                x = d.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
            } else if (x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
            if (d.height <= y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
                y = d.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
            } else if (y < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
                y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
        Component mouseOver =   // not necessarily sensitive to mouse events
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
            nativeContainer.getDropTargetEventTarget(x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
                                                     Container.INCLUDE_SELF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
        trackMouseEnterExit(mouseOver, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
        if (mouseOver != nativeContainer && mouseOver != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
            switch (id) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
            case SunDropTargetEvent.MOUSE_ENTERED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
            case SunDropTargetEvent.MOUSE_EXITED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
                retargetMouseEvent(mouseOver, id, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
                e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
        return e.isConsumed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
     * Generates enter/exit events as mouse moves over lw components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
     * @param targetOver        Target mouse is over (including native container)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
     * @param e                 Mouse event in native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
    private void trackMouseEnterExit(Component targetOver, MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
        Component       targetEnter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
        int             id = e.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
        if (e instanceof SunDropTargetEvent &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
            id == MouseEvent.MOUSE_ENTERED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
            isMouseInNativeContainer == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
            // This can happen if a lightweight component which initiated the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
            // drag has an associated drop target. MOUSE_ENTERED comes when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
            // mouse is in the native container already. To propagate this event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
            // properly we should null out targetLastEntered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
            targetLastEntered = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
        } else if ( id != MouseEvent.MOUSE_EXITED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
             id != MouseEvent.MOUSE_DRAGGED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
             id != LWD_MOUSE_DRAGGED_OVER &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
             isMouseInNativeContainer == false ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
            // any event but an exit or drag means we're in the native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
            isMouseInNativeContainer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
            startListeningForOtherDrags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
        } else if ( id == MouseEvent.MOUSE_EXITED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
            isMouseInNativeContainer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
            stopListeningForOtherDrags();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
        if (isMouseInNativeContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4241
            targetEnter = targetOver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
        if (targetLastEntered == targetEnter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
        if (targetLastEntered != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
            retargetMouseEvent(targetLastEntered, MouseEvent.MOUSE_EXITED, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
        if (id == MouseEvent.MOUSE_EXITED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
            // consume native exit event if we generate one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
        if (targetEnter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
            retargetMouseEvent(targetEnter, MouseEvent.MOUSE_ENTERED, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
        if (id == MouseEvent.MOUSE_ENTERED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
            // consume native enter event if we generate one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
        targetLastEntered = targetEnter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
     * Listens to global mouse drag events so even drags originating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
     * from other heavyweight containers will generate enter/exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
     * events in this container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
    private void startListeningForOtherDrags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
        //System.out.println("Adding AWTEventListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
            new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4276
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4277
                    nativeContainer.getToolkit().addAWTEventListener(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4278
                        LightweightDispatcher.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4279
                        AWTEvent.MOUSE_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4280
                        AWTEvent.MOUSE_MOTION_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4281
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4284
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4287
    private void stopListeningForOtherDrags() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4288
        //System.out.println("Removing AWTEventListener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4289
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4290
            new java.security.PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4291
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4292
                    nativeContainer.getToolkit().removeAWTEventListener(LightweightDispatcher.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4293
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4296
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4300
     * (Implementation of AWTEventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4301
     * Listen for drag events posted in other hw components so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4302
     * track enter/exit regardless of where a drag originated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4304
    public void eventDispatched(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4305
        boolean isForeignDrag = (e instanceof MouseEvent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4306
                                !(e instanceof SunDropTargetEvent) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4307
                                (e.id == MouseEvent.MOUSE_DRAGGED) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4308
                                (e.getSource() != nativeContainer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4310
        if (!isForeignDrag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4311
            // only interested in drags from other hw components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4312
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4315
        MouseEvent      srcEvent = (MouseEvent)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4316
        MouseEvent      me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4318
        synchronized (nativeContainer.getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4319
            Component srcComponent = srcEvent.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4321
            // component may have disappeared since drag event posted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4322
            // (i.e. Swing hierarchical menus)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4323
            if ( !srcComponent.isShowing() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4324
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4327
            // see 5083555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4328
            // check if srcComponent is in any modal blocked window
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4329
            Component c = nativeContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4330
            while ((c != null) && !(c instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4331
                c = c.getParent_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4333
            if ((c == null) || ((Window)c).isModalBlocked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4334
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4337
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4338
            // create an internal 'dragged-over' event indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4339
            // we are being dragged over from another hw component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4340
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4341
            me = new MouseEvent(nativeContainer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4342
                               LWD_MOUSE_DRAGGED_OVER,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4343
                               srcEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4344
                               srcEvent.getModifiersEx() | srcEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4345
                               srcEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4346
                               srcEvent.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4347
                               srcEvent.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4348
                               srcEvent.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
                               srcEvent.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
                               srcEvent.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
                               srcEvent.getButton());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
            ((AWTEvent)srcEvent).copyPrivateDataInto(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
            // translate coordinates to this native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
            final Point ptSrcOrigin = srcComponent.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
            if (AppContext.getAppContext() != nativeContainer.appContext) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
                final MouseEvent mouseEvent = me;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
                Runnable r = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4359
                        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
                            if (!nativeContainer.isShowing() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
                                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
                            Point       ptDstOrigin = nativeContainer.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
                            mouseEvent.translatePoint(ptSrcOrigin.x - ptDstOrigin.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
                                              ptSrcOrigin.y - ptDstOrigin.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
                            Component targetOver =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
                                nativeContainer.getMouseEventTarget(mouseEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
                                                                    mouseEvent.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                                                                    Container.INCLUDE_SELF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
                            trackMouseEnterExit(targetOver, mouseEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4374
                SunToolkit.executeOnEventHandlerThread(nativeContainer, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4376
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
                if (!nativeContainer.isShowing() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
                Point   ptDstOrigin = nativeContainer.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
                me.translatePoint( ptSrcOrigin.x - ptDstOrigin.x, ptSrcOrigin.y - ptDstOrigin.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
        //System.out.println("Track event: " + me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
        // feed the 'dragged-over' event directly to the enter/exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
        // code (not a real event so don't pass it to dispatchEvent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
        Component targetOver =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
            nativeContainer.getMouseEventTarget(me.getX(), me.getY(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
                                                Container.INCLUDE_SELF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
        trackMouseEnterExit(targetOver, me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
     * Sends a mouse event to the current mouse event recipient using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
     * the given event (sent to the windowed host) as a srcEvent.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
     * the mouse event target is still in the component tree, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
     * coordinates of the event are translated to those of the target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
     * If the target has been removed, we don't bother to send the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
     * message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
    void retargetMouseEvent(Component target, int id, MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
            return; // mouse is over another hw component or target is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
        int x = e.getX(), y = e.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
        Component component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
        for(component = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
            component != null && component != nativeContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
            component = component.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
            x -= component.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
            y -= component.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
        MouseEvent retargeted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
        if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
            if (e instanceof SunDropTargetEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
                retargeted = new SunDropTargetEvent(target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
                                                    id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
                                                    x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
                                                    y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
                                                    ((SunDropTargetEvent)e).getDispatcher());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
            } else if (id == MouseEvent.MOUSE_WHEEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
                retargeted = new MouseWheelEvent(target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
                                      id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
                                       e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
                                       e.getModifiersEx() | e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
                                       x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
                                       y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
                                       e.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
                                       e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
                                       e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
                                       e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
                                       ((MouseWheelEvent)e).getScrollType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
                                       ((MouseWheelEvent)e).getScrollAmount(),
121
c43b2dfab9ac 6524352: support for high-resolution mouse wheel
dcherepanov
parents: 2
diff changeset
  4437
                                       ((MouseWheelEvent)e).getWheelRotation(),
c43b2dfab9ac 6524352: support for high-resolution mouse wheel
dcherepanov
parents: 2
diff changeset
  4438
                                       ((MouseWheelEvent)e).getPreciseWheelRotation());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
                retargeted = new MouseEvent(target,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
                                            id,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
                                            e.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
                                            e.getModifiersEx() | e.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
                                            x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
                                            y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
                                            e.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
                                            e.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
                                            e.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
                                            e.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
                                            e.getButton());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
            ((AWTEvent)e).copyPrivateDataInto(retargeted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
            if (target == nativeContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
                // avoid recursively calling LightweightDispatcher...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
                ((Container)target).dispatchEventToSelf(retargeted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
                assert AppContext.getAppContext() == target.appContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
                if (nativeContainer.modalComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
                    if (((Container)nativeContainer.modalComp).isAncestorOf(target)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
                        target.dispatchEvent(retargeted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
                        e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
                    target.dispatchEvent(retargeted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
    // --- member variables -------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
     * The windowed container that might be hosting events for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
     * subcomponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
    private Container nativeContainer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
     * This variable is not used, but kept for serialization compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
    private Component focus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
     * The current subcomponent being hosted by this windowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
     * component that has events being forwarded to it.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
     * is null, there are currently no events being forwarded to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
     * a subcomponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
    private transient Component mouseEventTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
     * The last component entered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
    private transient Component targetLastEntered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
     * Is the mouse over the native container
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
    private transient boolean isMouseInNativeContainer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
     * This variable is not used, but kept for serialization compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
    private Cursor nativeCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
     * The event mask for contained lightweight components.  Lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
     * components need a windowed container to host window-related
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
     * events.  This separate mask indicates events that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
     * requested by contained lightweight components without effecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
     * the mask of the windowed component itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
    private long eventMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
     * The kind of events routed to lightweight components from windowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
     * hosts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
    private static final long PROXY_EVENT_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
        AWTEvent.FOCUS_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
        AWTEvent.KEY_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
        AWTEvent.MOUSE_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
        AWTEvent.MOUSE_MOTION_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
        AWTEvent.MOUSE_WHEEL_EVENT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
    private static final long MOUSE_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
        AWTEvent.MOUSE_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
        AWTEvent.MOUSE_MOTION_EVENT_MASK |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
        AWTEvent.MOUSE_WHEEL_EVENT_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
}