jdk/src/share/classes/javax/swing/SwingUtilities.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8372 786e091594a0
child 10316 8d1ca7d93fb2
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8372
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.applet.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.dnd.DropTarget;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.event.MenuDragMouseEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.awt.AppContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A collection of utility methods for Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class SwingUtilities implements SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // These states are system-wide, rather than AppContext wide.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static boolean canAccessEventQueue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static boolean eventQueueTested = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Indicates if we should change the drop target when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * {@code TransferHandler} is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static boolean suppressDropSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Indiciates if we've checked the system property for suppressing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * drop support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static boolean checkedSuppressDropSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Returns true if <code>setTransferHandler</code> should change the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <code>DropTarget</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static boolean getSuppressDropTarget() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (!checkedSuppressDropSupport) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            suppressDropSupport = Boolean.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    new GetPropertyAction("suppressSwingDropSupport")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            checkedSuppressDropSupport = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return suppressDropSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Installs a {@code DropTarget} on the component as necessary for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * {@code TransferHandler} change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    static void installSwingDropTargetAsNecessary(Component c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                                         TransferHandler t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!getSuppressDropTarget()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            DropTarget dropHandler = c.getDropTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if ((dropHandler == null) || (dropHandler instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                if (t == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    c.setDropTarget(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } else if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    c.setDropTarget(new TransferHandler.SwingDropTarget(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Return true if <code>a</code> contains <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public static final boolean isRectangleContainingRectangle(Rectangle a,Rectangle b) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   111
        return b.x >= a.x && (b.x + b.width) <= (a.x + a.width) &&
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   112
                b.y >= a.y && (b.y + b.height) <= (a.y + a.height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Return the rectangle (0,0,bounds.width,bounds.height) for the component <code>aComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static Rectangle getLocalBounds(Component aComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Rectangle b = new Rectangle(aComponent.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        b.x = b.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the first <code>Window </code> ancestor of <code>c</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * {@code null} if <code>c</code> is not contained inside a <code>Window</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param c <code>Component</code> to get <code>Window</code> ancestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *        of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the first <code>Window </code> ancestor of <code>c</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         {@code null} if <code>c</code> is not contained inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         <code>Window</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public static Window getWindowAncestor(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        for(Container p = c.getParent(); p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (p instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return (Window)p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Converts the location <code>x</code> <code>y</code> to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * parents coordinate system, returning the location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static Point convertScreenLocationToParent(Container parent,int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        for (Container p = parent; p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (p instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                Point point = new Point(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                SwingUtilities.convertPointFromScreen(point, parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                return point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        throw new Error("convertScreenLocationToParent: no window ancestor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Convert a <code>aPoint</code> in <code>source</code> coordinate system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>destination</code> coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * If <code>source</code> is {@code null}, <code>aPoint</code> is assumed to be in <code>destination</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * If <code>destination</code> is {@code null}, <code>aPoint</code> will be converted to <code>source</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>aPoint</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * without any conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public static Point convertPoint(Component source,Point aPoint,Component destination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Point p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if(source == null && destination == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return aPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if(source == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            source = getWindowAncestor(destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if(source == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw new Error("Source component not connected to component tree hierarchy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        p = new Point(aPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        convertPointToScreen(p,source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if(destination == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            destination = getWindowAncestor(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if(destination == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new Error("Destination component not connected to component tree hierarchy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        convertPointFromScreen(p,destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Convert the point <code>(x,y)</code> in <code>source</code> coordinate system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <code>destination</code> coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * If <code>source</code> is {@code null}, <code>(x,y)</code> is assumed to be in <code>destination</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * If <code>destination</code> is {@code null}, <code>(x,y)</code> will be converted to <code>source</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>(x,y)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * without any conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static Point convertPoint(Component source,int x, int y,Component destination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Point point = new Point(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return convertPoint(source,point,destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Convert the rectangle <code>aRectangle</code> in <code>source</code> coordinate system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>destination</code> coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * If <code>source</code> is {@code null}, <code>aRectangle</code> is assumed to be in <code>destination</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * If <code>destination</code> is {@code null}, <code>aRectangle</code> will be converted to <code>source</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>aRectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * without any conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public static Rectangle convertRectangle(Component source,Rectangle aRectangle,Component destination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        Point point = new Point(aRectangle.x,aRectangle.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        point =  convertPoint(source,point,destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return new Rectangle(point.x,point.y,aRectangle.width,aRectangle.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Convenience method for searching above <code>comp</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * component hierarchy and returns the first object of class <code>c</code> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * finds. Can return {@code null}, if a class <code>c</code> cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static Container getAncestorOfClass(Class<?> c, Component comp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if(comp == null || c == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Container parent = comp.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        while(parent != null && !(c.isInstance(parent)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Convenience method for searching above <code>comp</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * component hierarchy and returns the first object of <code>name</code> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * finds. Can return {@code null}, if <code>name</code> cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public static Container getAncestorNamed(String name, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if(comp == null || name == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Container parent = comp.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        while(parent != null && !(name.equals(parent.getName())))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            parent = parent.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the deepest visible descendent Component of <code>parent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * that contains the location <code>x</code>, <code>y</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * If <code>parent</code> does not contain the specified location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * then <code>null</code> is returned.  If <code>parent</code> is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * container, or none of <code>parent</code>'s visible descendents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * contain the specified location, <code>parent</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param parent the root component to begin the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param x the x target location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param y the y target location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static Component getDeepestComponentAt(Component parent, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!parent.contains(x, y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (parent instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            Component components[] = ((Container)parent).getComponents();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   272
            for (Component comp : components) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (comp != null && comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    Point loc = comp.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    if (comp instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                        comp = comp.getComponentAt(x - loc.x, y - loc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if (comp != null && comp.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns a MouseEvent similar to <code>sourceEvent</code> except that its x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * and y members have been converted to <code>destination</code>'s coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * system.  If <code>source</code> is {@code null}, <code>sourceEvent</code> x and y members
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * are assumed to be into <code>destination</code>'s root component coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * If <code>destination</code> is <code>null</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * returned MouseEvent will be in <code>source</code>'s coordinate system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <code>sourceEvent</code> will not be changed. A new event is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the <code>source</code> field of the returned event will be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * to <code>destination</code> if destination is non-{@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * use the translateMouseEvent() method to translate a mouse event from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * one component to another without changing the source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public static MouseEvent convertMouseEvent(Component source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                               MouseEvent sourceEvent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                               Component destination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        Point p = convertPoint(source,new Point(sourceEvent.getX(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                                sourceEvent.getY()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                               destination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        Component newSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if(destination != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            newSource = destination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            newSource = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        MouseEvent newEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (sourceEvent instanceof MouseWheelEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            MouseWheelEvent sourceWheelEvent = (MouseWheelEvent)sourceEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            newEvent = new MouseWheelEvent(newSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                           sourceWheelEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                           sourceWheelEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                           sourceWheelEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                           p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                           sourceWheelEvent.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                           sourceWheelEvent.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                           sourceWheelEvent.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                           sourceWheelEvent.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                           sourceWheelEvent.getScrollType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                           sourceWheelEvent.getScrollAmount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                           sourceWheelEvent.getWheelRotation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        else if (sourceEvent instanceof MenuDragMouseEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent)sourceEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            newEvent = new MenuDragMouseEvent(newSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                              sourceMenuDragEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                                              sourceMenuDragEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                                              sourceMenuDragEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                              p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                              sourceMenuDragEvent.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                              sourceMenuDragEvent.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                              sourceMenuDragEvent.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                              sourceMenuDragEvent.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                              sourceMenuDragEvent.getPath(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                                              sourceMenuDragEvent.getMenuSelectionManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            newEvent = new MouseEvent(newSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                      sourceEvent.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                      sourceEvent.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                      sourceEvent.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                      p.x,p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                      sourceEvent.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                      sourceEvent.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                      sourceEvent.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                      sourceEvent.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                      MouseEvent.NOBUTTON );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return newEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Convert a point from a component's coordinate system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * screen coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param p  a Point object (converted to the new coordinate system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param c  a Component object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public static void convertPointToScreen(Point p,Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            Rectangle b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            int x,y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if(c instanceof JComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   375
                    x = c.getX();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   376
                    y = c.getY();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                } else if(c instanceof java.applet.Applet ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                          c instanceof java.awt.Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        Point pp = c.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        x = pp.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        y = pp.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    } catch (IllegalComponentStateException icse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        x = c.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        y = c.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    x = c.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    y = c.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                p.x += x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                p.y += y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            } while(c != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Convert a point from a screen coordinates to a component's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param p  a Point object (converted to the new coordinate system)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param c  a Component object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static void convertPointFromScreen(Point p,Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        Rectangle b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int x,y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if(c instanceof JComponent) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   414
                x = c.getX();
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   415
                y = c.getY();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }  else if(c instanceof java.applet.Applet ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                       c instanceof java.awt.Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    Point pp = c.getLocationOnScreen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    x = pp.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    y = pp.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                } catch (IllegalComponentStateException icse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    x = c.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    y = c.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                x = c.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                y = c.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            p.x -= x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            p.y -= y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            c = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        } while(c != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Returns the first <code>Window </code> ancestor of <code>c</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * {@code null} if <code>c</code> is not contained inside a <code>Window</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Note: This method provides the same functionality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>getWindowAncestor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param c <code>Component</code> to get <code>Window</code> ancestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *        of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return the first <code>Window </code> ancestor of <code>c</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         {@code null} if <code>c</code> is not contained inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         <code>Window</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public static Window windowForComponent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return getWindowAncestor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Return <code>true</code> if a component <code>a</code> descends from a component <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static boolean isDescendingFrom(Component a,Component b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        if(a == b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        for(Container p = a.getParent();p!=null;p=p.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if(p == b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return false;
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
     * Convenience to calculate the intersection of two rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * without allocating a new rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * If the two rectangles don't intersect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * then the returned rectangle begins at (0,0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * and has zero width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param x       the X coordinate of the first rectangle's top-left point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param y       the Y coordinate of the first rectangle's top-left point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param width   the width of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param height  the height of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param dest    the second rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return <code>dest</code>, modified to specify the intersection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public static Rectangle computeIntersection(int x,int y,int width,int height,Rectangle dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int x1 = (x > dest.x) ? x : dest.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        int x2 = ((x+width) < (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        int y1 = (y > dest.y) ? y : dest.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        int y2 = ((y + height) < (dest.y + dest.height) ? (y+height) : (dest.y + dest.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        dest.x = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        dest.y = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        dest.width = x2 - x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        dest.height = y2 - y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // If rectangles don't intersect, return zero'd intersection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (dest.width < 0 || dest.height < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            dest.x = dest.y = dest.width = dest.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Convenience method that calculates the union of two rectangles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * without allocating a new rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param x the x-coordinate of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param y the y-coordinate of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param width the width of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @param height the height of the first rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param dest  the coordinates of the second rectangle; the union
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *    of the two rectangles is returned in this rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @return the <code>dest</code> <code>Rectangle</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public static Rectangle computeUnion(int x,int y,int width,int height,Rectangle dest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        int x1 = (x < dest.x) ? x : dest.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        int x2 = ((x+width) > (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        int y1 = (y < dest.y) ? y : dest.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int y2 = ((y+height) > (dest.y + dest.height)) ? (y+height) : (dest.y + dest.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        dest.x = x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        dest.y = y1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        dest.width = (x2 - x1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        dest.height= (y2 - y1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return dest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Convenience returning an array of rect representing the regions within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <code>rectA</code> that do not overlap with <code>rectB</code>. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * two Rects do not overlap, returns an empty array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return new Rectangle[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Rectangle t = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        Rectangle a=null,b=null,c=null,d=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        Rectangle result[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        int rectCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        /* rectA contains rectB */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (isRectangleContainingRectangle(rectA,rectB)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                c = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            t.x = rectB.x + rectB.width; t.y = rectA.y; t.width = rectA.x + rectA.width - (rectB.x + rectB.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            t.height = rectA.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                d = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            /* 1 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (rectB.x <= rectA.x && rectB.y <= rectA.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    t.x = rectA.x; t.y = rectB.y + rectB.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    t.width = rectA.width; t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        a = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    t.setBounds((rectB.x + rectB.width), rectA.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        a = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    t.setBounds((rectB.x + rectB.width), rectA.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                (rectA.x + rectA.width) - (rectB.x + rectB.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                (rectB.y + rectB.height) - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            } else if (rectB.x <= rectA.x && (rectB.y + rectB.height) >= (rectA.y + rectA.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        a = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    t.setBounds((rectB.x + rectB.width), rectB.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                (rectA.x + rectA.width) - (rectB.x + rectB.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                (rectA.y + rectA.height) - rectB.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            } else if (rectB.x <= rectA.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if ((rectB.x + rectB.width) >= (rectA.x + rectA.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    if(t.width>0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    t.setBounds((rectB.x + rectB.width), rectB.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                (rectA.x + rectA.width) - (rectB.x + rectB.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                                rectB.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                        c = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            } else if (rectB.x <= (rectA.x + rectA.width) && (rectB.x + rectB.width) > (rectA.x + rectA.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                    t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        a = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                } else if (rectB.y <= rectA.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                                (rectB.y + rectB.height) - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                (rectA.y + rectA.height) - rectB.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                rectB.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        c = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            } else if (rectB.x >= rectA.x && (rectB.x + rectB.width) <= (rectA.x + rectA.width)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                    t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    t.setBounds((rectB.x + rectB.width), rectA.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                } else if (rectB.y <= rectA.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    t.setBounds(rectB.x, (rectB.y + rectB.height),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                                rectB.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                                (rectA.y + rectA.height) - (rectB.y + rectB.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    t.setBounds((rectB.x + rectB.width), rectA.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                                (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                        c = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                        a = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    t.setBounds(rectB.x, rectA.y, rectB.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                rectB.y - rectA.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        b = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        rectCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    t.setBounds((rectB.x + rectB.width), rectA.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    if(t.width > 0 && t.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        c = new Rectangle(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                        rectCount++;
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        result = new Rectangle[rectCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        rectCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if(a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            result[rectCount++] = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if(b != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            result[rectCount++] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        if(c != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            result[rectCount++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if(d != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            result[rectCount++] = d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Returns true if the mouse event specifies the left mouse button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param anEvent  a MouseEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @return true if the left mouse button was active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    public static boolean isLeftMouseButton(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Returns true if the mouse event specifies the middle mouse button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @param anEvent  a MouseEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @return true if the middle mouse button was active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    public static boolean isMiddleMouseButton(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        return ((anEvent.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * Returns true if the mouse event specifies the right mouse button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @param anEvent  a MouseEvent object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @return true if the right mouse button was active
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public static boolean isRightMouseButton(MouseEvent anEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Compute the width of the string using a font with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * "metrics" (sizes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @param fm   a FontMetrics object to compute with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @param str  the String to compute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @return an int containing the string width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public static int computeStringWidth(FontMetrics fm,String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        // You can't assume that a string's width is the sum of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        // characters' widths in Java2D -- it may be smaller due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        // kerning, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        return SwingUtilities2.stringWidth(null, fm, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Compute and return the location of the icons origin, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * location of origin of the text baseline, and a possibly clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * version of the compound labels string.  Locations are computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * relative to the viewR rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * The JComponents orientation (LEADING/TRAILING) will also be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * into account and translated into LEFT/RIGHT values accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    public static String layoutCompoundLabel(JComponent c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                                             FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                                             String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                             Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                             int verticalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                             int horizontalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                             int verticalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                                             int horizontalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                                             Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                             Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                             Rectangle textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                                             int textIconGap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        boolean orientationIsLeftToRight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        int     hAlign = horizontalAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        int     hTextPos = horizontalTextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (!(c.getComponentOrientation().isLeftToRight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                orientationIsLeftToRight = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        // Translate LEADING/TRAILING values in horizontalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        // to LEFT/RIGHT values depending on the components orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        switch (horizontalAlignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        case LEADING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            hAlign = (orientationIsLeftToRight) ? LEFT : RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        case TRAILING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            hAlign = (orientationIsLeftToRight) ? RIGHT : LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        // Translate LEADING/TRAILING values in horizontalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        // to LEFT/RIGHT values depending on the components orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        switch (horizontalTextPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        case LEADING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            hTextPos = (orientationIsLeftToRight) ? LEFT : RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        case TRAILING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            hTextPos = (orientationIsLeftToRight) ? RIGHT : LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        return layoutCompoundLabelImpl(c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                                       fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                                       text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                                       icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                                       verticalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                                       hAlign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                       verticalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                       hTextPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                       viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                                       iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                       textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                                       textIconGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * Compute and return the location of the icons origin, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * location of origin of the text baseline, and a possibly clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * version of the compound labels string.  Locations are computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * relative to the viewR rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * values in horizontalTextPosition (they will default to RIGHT) and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * horizontalAlignment (they will default to CENTER).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Use the other version of layoutCompoundLabel() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    public static String layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        int verticalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        int horizontalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        int verticalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        int horizontalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        Rectangle textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        int textIconGap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        return layoutCompoundLabelImpl(null, fm, text, icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                                       verticalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                                       horizontalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                                       verticalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                                       horizontalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                                       viewR, iconR, textR, textIconGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Compute and return the location of the icons origin, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * location of origin of the text baseline, and a possibly clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * version of the compound labels string.  Locations are computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * relative to the viewR rectangle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * values in horizontalTextPosition (they will default to RIGHT) and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * horizontalAlignment (they will default to CENTER).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Use the other version of layoutCompoundLabel() instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    private static String layoutCompoundLabelImpl(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        JComponent c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        Icon icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        int verticalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        int horizontalAlignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        int verticalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        int horizontalTextPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        Rectangle viewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        Rectangle iconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        Rectangle textR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        int textIconGap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        /* Initialize the icon bounds rectangle iconR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            iconR.width = icon.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            iconR.height = icon.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            iconR.width = iconR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        /* Initialize the text bounds rectangle textR.  If a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * or and empty String was specified we substitute "" here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
         * and use 0,0,0,0 for textR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        boolean textIsEmpty = (text == null) || text.equals("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        int lsb = 0;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
   973
        int rsb = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        /* Unless both text and icon are non-null, we effectively ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
         * the value of textIconGap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        int gap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
   979
        View v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        if (textIsEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            textR.width = textR.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            gap = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            int availTextWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            gap = (icon == null) ? 0 : textIconGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            if (horizontalTextPosition == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                availTextWidth = viewR.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                availTextWidth = viewR.width - (iconR.width + gap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            v = (c != null) ? (View) c.getClientProperty("html") : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                textR.width = Math.min(availTextWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                                       (int) v.getPreferredSpan(View.X_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                textR.width = SwingUtilities2.stringWidth(c, fm, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                lsb = SwingUtilities2.getLeftSideBearing(c, fm, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                if (lsb < 0) {
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1004
                    // If lsb is negative, add it to the width and later
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1005
                    // adjust the x location. This gives more space than is
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1006
                    // actually needed.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1007
                    // This is done like this for two reasons:
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1008
                    // 1. If we set the width to the actual bounds all
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1009
                    //    callers would have to account for negative lsb
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1010
                    //    (pref size calculations ONLY look at width of
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1011
                    //    textR)
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1012
                    // 2. You can do a drawString at the returned location
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 2473
diff changeset
  1013
                    //    and the text won't be clipped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    textR.width -= lsb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                if (textR.width > availTextWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    text = SwingUtilities2.clipString(c, fm, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                                      availTextWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                    textR.width = SwingUtilities2.stringWidth(c, fm, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                textR.height = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        /* Compute textR.x,y given the verticalTextPosition and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
         * horizontalTextPosition properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (verticalTextPosition == TOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (horizontalTextPosition != CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                textR.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                textR.y = -(textR.height + gap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        else if (verticalTextPosition == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            textR.y = (iconR.height / 2) - (textR.height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        else { // (verticalTextPosition == BOTTOM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            if (horizontalTextPosition != CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                textR.y = iconR.height - textR.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                textR.y = (iconR.height + gap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (horizontalTextPosition == LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            textR.x = -(textR.width + gap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        else if (horizontalTextPosition == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            textR.x = (iconR.width / 2) - (textR.width / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        else { // (horizontalTextPosition == RIGHT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            textR.x = (iconR.width + gap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        // WARNING: DefaultTreeCellEditor uses a shortened version of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        // this algorithm to position it's Icon. If you change how this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        // is calculated, be sure and update DefaultTreeCellEditor too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        /* labelR is the rectangle that contains iconR and textR.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         * Move it to its proper position given the labelAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
         * properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
         * To avoid actually allocating a Rectangle, Rectangle.union
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
         * has been inlined below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        int labelR_x = Math.min(iconR.x, textR.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        int labelR_width = Math.max(iconR.x + iconR.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                                    textR.x + textR.width) - labelR_x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        int labelR_y = Math.min(iconR.y, textR.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        int labelR_height = Math.max(iconR.y + iconR.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                                     textR.y + textR.height) - labelR_y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        int dx, dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        if (verticalAlignment == TOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            dy = viewR.y - labelR_y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        else if (verticalAlignment == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            dy = (viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        else { // (verticalAlignment == BOTTOM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            dy = (viewR.y + viewR.height) - (labelR_y + labelR_height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        if (horizontalAlignment == LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            dx = viewR.x - labelR_x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        else if (horizontalAlignment == RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            dx = (viewR.x + viewR.width) - (labelR_x + labelR_width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        else { // (horizontalAlignment == CENTER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            dx = (viewR.x + (viewR.width / 2)) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                 (labelR_x + (labelR_width / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        /* Translate textR and glypyR by dx,dy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        textR.x += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        textR.y += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        iconR.x += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        iconR.y += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        if (lsb < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            // lsb is negative. Shift the x location so that the text is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            // visually drawn at the right location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            textR.x -= lsb;
1295
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
  1114
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
  1115
            textR.width += lsb;
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
  1116
        }
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
  1117
        if (rsb > 0) {
3cf2264a5743 6584657: GTK Look and Feel: Bugs in menu item layout
mlapshin
parents: 2
diff changeset
  1118
            textR.width -= rsb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Paints a component to the specified <code>Graphics</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * This method is primarily useful to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * <code>Component</code>s that don't exist as part of the visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * containment hierarchy, but are used for rendering.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * example, if you are doing your own rendering and want to render
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * some text (or even HTML), you could make use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <code>JLabel</code>'s text rendering support and have it paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * directly by way of this method, without adding the label to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * visible containment hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * This method makes use of <code>CellRendererPane</code> to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * the actual painting, and is only recommended if you use one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * component for rendering.  If you make use of multiple components
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * to handle the rendering, as <code>JTable</code> does, use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <code>CellRendererPane</code> directly.  Otherwise, as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * below, you could end up with a <code>CellRendererPane</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * per <code>Component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * If <code>c</code>'s parent is not a <code>CellRendererPane</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * a new <code>CellRendererPane</code> is created, <code>c</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * added to it, and the <code>CellRendererPane</code> is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * <code>p</code>.  If <code>c</code>'s parent is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * <code>CellRendererPane</code> and the <code>CellRendererPane</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * parent is not <code>p</code>, it is added to <code>p</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * The component should either descend from <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * or be another kind of lightweight component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * A lightweight component is one whose "lightweight" property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * (returned by the <code>Component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * <code>isLightweight</code> method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * is true. If the Component is not lightweight, bad things map happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * crashes, exceptions, painting problems...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @param g  the <code>Graphics</code> object to draw on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * @param c  the <code>Component</code> to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * @param p  the intermediate <code>Container</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @param x  an int specifying the left side of the area draw in, in pixels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *           measured from the left edge of the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @param y  an int specifying the top of the area to draw in, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *           measured down from the top edge of the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @param w  an int specifying the width of the area draw in, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @param h  an int specifying the height of the area draw in, in pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @see CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @see java.awt.Component#isLightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        getCellRendererPane(c, p).paintComponent(g, c, p, x, y, w, h,false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Paints a component to the specified <code>Graphics</code>.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * is a cover method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * {@link #paintComponent(Graphics,Component,Container,int,int,int,int)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * Refer to it for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @param g  the <code>Graphics</code> object to draw on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * @param c  the <code>Component</code> to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @param p  the intermediate <code>Container</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * @param r  the <code>Rectangle</code> to draw in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * @see #paintComponent(Graphics,Component,Container,int,int,int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @see CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    public static void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        paintComponent(g, c, p, r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Ensures that cell renderer <code>c</code> has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * <code>ComponentShell</code> parent and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * the shell's parent is p.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    private static CellRendererPane getCellRendererPane(Component c, Container p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        Container shell = c.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        if (shell instanceof CellRendererPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            if (shell.getParent() != p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                p.add(shell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            shell = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            shell.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            p.add(shell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return (CellRendererPane)shell;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * A simple minded look and feel change: ask each node in the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * to <code>updateUI()</code> -- that is, to initialize its UI property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * with the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    public static void updateComponentTreeUI(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        updateComponentTreeUI0(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        c.invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        c.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        c.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    private static void updateComponentTreeUI0(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        if (c instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            JComponent jc = (JComponent) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            jc.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
            JPopupMenu jpm =jc.getComponentPopupMenu();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            if(jpm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                updateComponentTreeUI(jpm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        Component[] children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (c instanceof JMenu) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            children = ((JMenu)c).getMenuComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        else if (c instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            children = ((Container)c).getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        if (children != null) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1243
            for (Component child : children) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1244
                updateComponentTreeUI0(child);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        }
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
     * Causes <i>doRun.run()</i> to be executed asynchronously on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * AWT event dispatching thread.  This will happen after all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * pending AWT events have been processed.  This method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * be used when an application thread needs to update the GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * In the following example the <code>invokeLater</code> call queues
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * the <code>Runnable</code> object <code>doHelloWorld</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * on the event dispatching thread and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * then prints a message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * Runnable doHelloWorld = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *     public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *         System.out.println("Hello World on " + Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * SwingUtilities.invokeLater(doHelloWorld);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * System.out.println("This might well be displayed before the other message.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * If invokeLater is called from the event dispatching thread --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * for example, from a JButton's ActionListener -- the <i>doRun.run()</i> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * still be deferred until all pending events have been processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * Note that if the <i>doRun.run()</i> throws an uncaught exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * the event dispatching thread will unwind (not the current thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * Additional documentation and examples for this method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * As of 1.3 this method is just a cover for <code>java.awt.EventQueue.invokeLater()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * Unlike the rest of Swing, this method can be invoked from any thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * @see #invokeAndWait
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    public static void invokeLater(Runnable doRun) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        EventQueue.invokeLater(doRun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Causes <code>doRun.run()</code> to be executed synchronously on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * AWT event dispatching thread.  This call blocks until
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * all pending AWT events have been processed and (then)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * <code>doRun.run()</code> returns. This method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * be used when an application thread needs to update the GUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * It shouldn't be called from the event dispatching thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Here's an example that creates a new application thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * that uses <code>invokeAndWait</code> to print a string from the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * dispatching thread and then, when that's finished, print
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * a string from the application thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * final Runnable doHelloWorld = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *     public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *         System.out.println("Hello World on " + Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * Thread appThread = new Thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *     public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *             SwingUtilities.invokeAndWait(doHelloWorld);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *         catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *             e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *         System.out.println("Finished on " + Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * appThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * Note that if the <code>Runnable.run</code> method throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * uncaught exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * (on the event dispatching thread) it's caught and rethrown, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * an <code>InvocationTargetException</code>, on the caller's thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * Additional documentation and examples for this method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * in <em>The Java Tutorial</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * As of 1.3 this method is just a cover for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * <code>java.awt.EventQueue.invokeAndWait()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @exception  InterruptedException if we're interrupted while waiting for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *             the event dispatching thread to finish excecuting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *             <code>doRun.run()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @exception  InvocationTargetException  if an exception is thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *             while running <code>doRun</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * @see #invokeLater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    public static void invokeAndWait(final Runnable doRun)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        throws InterruptedException, InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        EventQueue.invokeAndWait(doRun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * Returns true if the current thread is an AWT event dispatching thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * As of 1.3 this method is just a cover for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * <code>java.awt.EventQueue.isDispatchThread()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * @return true if the current thread is an AWT event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    public static boolean isEventDispatchThread()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        return EventQueue.isDispatchThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * Get the index of this object in its accessible parent.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * Note: as of the Java 2 platform v1.3, it is recommended that developers call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * of using this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @return -1 of this object does not have an accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * Otherwise, the index of the child in its accessible parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    public static int getAccessibleIndexInParent(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        return c.getAccessibleContext().getAccessibleIndexInParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * Returns the <code>Accessible</code> child contained at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * local coordinate <code>Point</code>, if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * Otherwise returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * @return the <code>Accessible</code> at the specified location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *    if it exists; otherwise <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    public static Accessible getAccessibleAt(Component c, Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        if (c instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            return c.getAccessibleContext().getAccessibleComponent().getAccessibleAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        } else if (c instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            Accessible a = (Accessible) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                AccessibleContext ac = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                    AccessibleComponent acmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                    Point location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                    int nchildren = ac.getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    for (int i=0; i < nchildren; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                        a = ac.getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                        if ((a != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                            ac = a.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                            if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                                acmp = ac.getAccessibleComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                                if ((acmp != null) && (acmp.isShowing())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                                    location = acmp.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                                    Point np = new Point(p.x-location.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                                                         p.y-location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                                    if (acmp.contains(np)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                                        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            return (Accessible) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * Get the state of this object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * Note: as of the Java 2 platform v1.3, it is recommended that developers call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * of using this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @return an instance of AccessibleStateSet containing the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * set of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    public static AccessibleStateSet getAccessibleStateSet(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        return c.getAccessibleContext().getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * method should return the number of children of this object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * Note: as of the Java 2 platform v1.3, it is recommended that developers call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * of using this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    public static int getAccessibleChildrenCount(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        return c.getAccessibleContext().getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * Return the nth Accessible child of the object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Note: as of the Java 2 platform v1.3, it is recommended that developers call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * of using this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    public static Accessible getAccessibleChild(Component c, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        return c.getAccessibleContext().getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * Return the child <code>Component</code> of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * <code>Component</code> that is the focus owner, if any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @param c the root of the <code>Component</code> hierarchy to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *        search for the focus owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @return the focus owner, or <code>null</code> if there is no focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     *         owner, or if the focus owner is not <code>comp</code>, or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *         descendant of <code>comp</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * @see java.awt.KeyboardFocusManager#getFocusOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * @deprecated As of 1.4, replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     *   <code>KeyboardFocusManager.getFocusOwner()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    public static Component findFocusOwner(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        Component focusOwner = KeyboardFocusManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            getCurrentKeyboardFocusManager().getFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        // verify focusOwner is a descendant of c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        for (Component temp = focusOwner; temp != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
             temp = (temp instanceof Window) ? null : temp.getParent())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            if (temp == c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                return focusOwner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * If c is a JRootPane descendant return its JRootPane ancestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * If c is a RootPaneContainer then return its JRootPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * @return the JRootPane for Component c or {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    public static JRootPane getRootPane(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        if (c instanceof RootPaneContainer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            return ((RootPaneContainer)c).getRootPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        for( ; c != null; c = c.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            if (c instanceof JRootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                return (JRootPane)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * Returns the root component for the current component tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @return the first ancestor of c that's a Window or the last Applet ancestor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    public static Component getRoot(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        Component applet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        for(Component p = c; p != null; p = p.getParent()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            if (p instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            if (p instanceof Applet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                applet = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        return applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
8372
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1535
    static JComponent getPaintingOrigin(JComponent c) {
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1536
        Container p = c;
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1537
        while ((p = p.getParent()) instanceof JComponent) {
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1538
            JComponent jp = (JComponent) p;
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1539
            if (jp.isPaintingOrigin()) {
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1540
                return jp;
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1541
            }
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1542
        }
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1543
        return null;
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1544
    }
786e091594a0 6993171: JavaTest/JDK7b114 - no help text is shown for interview questions, JavaTest HANGS UP
alexp
parents: 7668
diff changeset
  1545
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * Process the key bindings for the <code>Component</code> associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * <code>event</code>. This method is only useful if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * <code>event.getComponent()</code> does not descend from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * <code>JComponent</code>, or your are not invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * <code>super.processKeyEvent</code> from within your
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * <code>JComponent</code> subclass. <code>JComponent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * automatically processes bindings from within its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * <code>processKeyEvent</code> method, hence you rarely need
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * to directly invoke this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * @param event KeyEvent used to identify which bindings to process, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     *              well as which Component has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * @return true if a binding has found and processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    public static boolean processKeyBindings(KeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        if (event != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            if (event.isConsumed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            Component component = event.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            boolean pressed = (event.getID() == KeyEvent.KEY_PRESSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
            if (!isValidKeyEventForKeyBindings(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            // Find the first JComponent in the ancestor hierarchy, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            // invoke processKeyBindings on it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
            while (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                    return ((JComponent)component).processKeyBindings(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                                                   event, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                if ((component instanceof Applet) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    (component instanceof Window)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
                    // No JComponents, if Window or Applet parent, process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                    // WHEN_IN_FOCUSED_WINDOW bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    return JComponent.processKeyBindingsForAllComponents(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                                  event, (Container)component, pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
                component = component.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * Returns true if the <code>e</code> is a valid KeyEvent to use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * processing the key bindings associated with JComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    static boolean isValidKeyEventForKeyBindings(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Invokes <code>actionPerformed</code> on <code>action</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * <code>action</code> is enabled (and non-{@code null}). The command for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * ActionEvent is determined by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *   <li>If the action was registered via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *       <code>registerKeyboardAction</code>, then the command string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *       passed in ({@code null} will be used if {@code null} was passed in).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *   <li>Action value with name Action.ACTION_COMMAND_KEY, unless {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *   <li>String value of the KeyEvent, unless <code>getKeyChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *       returns KeyEvent.CHAR_UNDEFINED..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * This will return true if <code>action</code> is non-{@code null} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * actionPerformed is invoked on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    public static boolean notifyAction(Action action, KeyStroke ks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
                                       KeyEvent event, Object sender,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
                                       int modifiers) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        if (action instanceof UIAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            if (!((UIAction)action).isEnabled(sender)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        else if (!action.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        Object commandO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        boolean stayNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        // Get the command object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        commandO = action.getValue(Action.ACTION_COMMAND_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        if (commandO == null && (action instanceof JComponent.ActionStandin)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            // ActionStandin is used for historical reasons to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            // registerKeyboardAction with a null value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            stayNull = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            stayNull = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        // Convert it to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        String command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
        if (commandO != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            command = commandO.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        else if (!stayNull && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            command = String.valueOf(event.getKeyChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            // Do null for undefined chars, or if registerKeyboardAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            // was called with a null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            command = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        action.actionPerformed(new ActionEvent(sender,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                        ActionEvent.ACTION_PERFORMED, command, event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                        modifiers));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * Convenience method to change the UI InputMap for <code>component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * to <code>uiInputMap</code>. If <code>uiInputMap</code> is {@code null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * this removes any previously installed UI InputMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    public static void replaceUIInputMap(JComponent component, int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                                         InputMap uiInputMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        InputMap map = component.getInputMap(type, (uiInputMap != null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            InputMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            if (parent == null || (parent instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                map.setParent(uiInputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
            map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * Convenience method to change the UI ActionMap for <code>component</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * to <code>uiActionMap</code>. If <code>uiActionMap</code> is {@code null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * this removes any previously installed UI ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    public static void replaceUIActionMap(JComponent component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                                          ActionMap uiActionMap) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1699
        ActionMap map = component.getActionMap((uiActionMap != null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
            ActionMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            if (parent == null || (parent instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                map.setParent(uiActionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * Returns the InputMap provided by the UI for condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * <code>condition</code> in component <code>component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * <p>This will return {@code null} if the UI has not installed a InputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * of the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    public static InputMap getUIInputMap(JComponent component, int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        InputMap map = component.getInputMap(condition, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            InputMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
            if (parent instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * Returns the ActionMap provided by the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * in component <code>component</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * <p>This will return {@code null} if the UI has not installed an ActionMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    public static ActionMap getUIActionMap(JComponent component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        ActionMap map = component.getActionMap(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
            ActionMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
            if (parent instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    // Don't use String, as it's not guaranteed to be unique in a Hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    private static final Object sharedOwnerFrameKey =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
       new StringBuffer("SwingUtilities.sharedOwnerFrame");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
    static class SharedOwnerFrame extends Frame implements WindowListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
            installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
         * Install window listeners on owned windows to watch for displayability changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            Window[] windows = getOwnedWindows();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1767
            for (Window window : windows) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                    window.removeWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                    window.addWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
         * Watches for displayability changes and disposes shared instance if there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
         * displayable children left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        public void windowClosed(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                Window[] windows = getOwnedWindows();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1782
                for (Window window : windows) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                    if (window != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                        if (window.isDisplayable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                        window.removeWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        public void windowOpened(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        public void windowIconified(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        public void windowDeiconified(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        public void windowActivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        public void windowDeactivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        public void show() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
            // This frame can never be shown
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
                getToolkit().getSystemEventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
                super.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
                // untrusted code not allowed to dispose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * Returns a toolkit-private, shared, invisible Frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * to be the owner for JDialogs and JWindows created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * {@code null} owners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    static Frame getSharedOwnerFrame() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        Frame sharedOwnerFrame =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        if (sharedOwnerFrame == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
            sharedOwnerFrame = new SharedOwnerFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
            SwingUtilities.appContextPut(sharedOwnerFrameKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
                                         sharedOwnerFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
        return sharedOwnerFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * Returns a SharedOwnerFrame's shutdown listener to dispose the SharedOwnerFrame
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * if it has no more displayable children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    static WindowListener getSharedOwnerFrameShutdownListener() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        Frame sharedOwnerFrame = getSharedOwnerFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        return (WindowListener)sharedOwnerFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
    /* Don't make these AppContext accessors public or protected --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * since AppContext is in sun.awt in 1.2, we shouldn't expose it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * even indirectly with a public API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    // REMIND(aim): phase out use of 4 methods below since they
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    // are just private covers for AWT methods (?)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    static Object appContextGet(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        return AppContext.getAppContext().get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    static void appContextPut(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        AppContext.getAppContext().put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    static void appContextRemove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
        AppContext.getAppContext().remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 1295
diff changeset
  1870
    static Class<?> loadSystemClass(String className) throws ClassNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        return Class.forName(className, true, Thread.currentThread().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
                             getContextClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
   /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     * Convenience function for determining ComponentOrientation.  Helps us
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * avoid having Munge directives throughout the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    static boolean isLeftToRight( Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
    private SwingUtilities() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        throw new Error("SwingUtilities is just a container for static methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * Returns true if the Icon <code>icon</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * ImageIcon, and the image it contains is the same as <code>image</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    static boolean doesIconReferenceImage(Icon icon, Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        Image iconImage = (icon != null && (icon instanceof ImageIcon)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
                           ((ImageIcon)icon).getImage() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        return (iconImage == image);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * Returns index of the first occurrence of <code>mnemonic</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * within string <code>text</code>. Matching algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * case-sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * @param text The text to search through, may be {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * @param mnemonic The mnemonic to find the character for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @return index into the string if exists, otherwise -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    static int findDisplayedMnemonicIndex(String text, int mnemonic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        if (text == null || mnemonic == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        char uc = Character.toUpperCase((char)mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        char lc = Character.toLowerCase((char)mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        int uci = text.indexOf(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        int lci = text.indexOf(lc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        if (uci == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            return lci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        } else if(lci == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            return uci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            return (lci < uci) ? lci : uci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * Stores the position and size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * the inner painting area of the specified component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * in <code>r</code> and returns <code>r</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * The position and size specify the bounds of the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * adjusted so as not to include the border area (the insets).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * This method is useful for classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * that implement painting code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * @param c  the JComponent in question; if {@code null}, this method returns {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * @param r  the Rectangle instance to be modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     *           may be {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * @return {@code null} if the Component is {@code null};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     *         otherwise, returns the passed-in rectangle (if non-{@code null})
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         or a new rectangle specifying position and size information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    public static Rectangle calculateInnerArea(JComponent c, Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        Rectangle rect = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        if (rect == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            rect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        rect.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        rect.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        rect.width = c.getWidth() - insets.left - insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        rect.height = c.getHeight() - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    static void updateRendererOrEditorUI(Object rendererOrEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        if (rendererOrEditor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        Component component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        if (rendererOrEditor instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            component = (Component)rendererOrEditor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        if (rendererOrEditor instanceof DefaultCellEditor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
            component = ((DefaultCellEditor)rendererOrEditor).getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
        if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
            SwingUtilities.updateComponentTreeUI(component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
    }
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1981
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1982
    /**
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1983
     * Returns the first ancestor of the {@code component}
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1984
     * which is not an instance of {@link JLayer}.
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1985
     *
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1986
     * @param component {@code Component} to get
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1987
     * the first ancestor of, which is not a {@link JLayer} instance.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1988
     *
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1989
     * @return the first ancestor of the {@code component}
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1990
     * which is not an instance of {@link JLayer}.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1991
     * If such an ancestor can not be found, {@code null} is returned.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1992
     *
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1993
     * @throws NullPointerException if {@code component} is {@code null}
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1994
     * @see JLayer
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1995
     *
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1996
     * @since 1.7
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  1997
     */
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1998
    public static Container getUnwrappedParent(Component component) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  1999
        Container parent = component.getParent();
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2000
        while(parent instanceof JLayer) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2001
            parent = parent.getParent();
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2002
        }
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2003
        return parent;
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2004
    }
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2005
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2006
    /**
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2007
     * Returns the first {@code JViewport}'s descendant
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2008
     * which is not an instance of {@code JLayer}.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2009
     * If such a descendant can not be found, {@code null} is returned.
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2010
     *
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2011
     * If the {@code viewport}'s view component is not a {@code JLayer},
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2012
     * this method is equivalent to {@link JViewport#getView()}
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2013
     * otherwise {@link JLayer#getView()} will be recursively
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2014
     * called on all descending {@code JLayer}s.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2015
     *
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2016
     * @param viewport {@code JViewport} to get the first descendant of,
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2017
     * which in not a {@code JLayer} instance.
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2018
     *
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2019
     * @return the first {@code JViewport}'s descendant
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2020
     * which is not an instance of {@code JLayer}.
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2021
     * If such a descendant can not be found, {@code null} is returned.
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2022
     *
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2023
     * @throws NullPointerException if {@code viewport} is {@code null}
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2024
     * @see JViewport#getView()
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2025
     * @see JLayer
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2026
     *
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2027
     * @since 1.7
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2028
     */
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4284
diff changeset
  2029
    public static Component getUnwrappedView(JViewport viewport) {
4278
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2030
        Component view = viewport.getView();
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2031
        while (view instanceof JLayer) {
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2032
            view = ((JLayer)view).getView();
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2033
        }
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2034
        return view;
b7a976422d27 6878399: public SwingUtilities.getParentViewport() is required
alexp
parents: 3749
diff changeset
  2035
    }
4284
lana
parents: 4261 4278
diff changeset
  2036
lana
parents: 4261 4278
diff changeset
  2037
   /**
4261
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2038
     * Retrieves the validate root of a given container.
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2039
     *
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2040
     * If the container is contained within a {@code CellRendererPane}, this
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2041
     * method returns {@code null} due to the synthetic nature of the {@code
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2042
     * CellRendererPane}.
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2043
     * <p>
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2044
     * The component hierarchy must be displayable up to the toplevel component
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2045
     * (either a {@code Frame} or an {@code Applet} object.) Otherwise this
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2046
     * method returns {@code null}.
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2047
     * <p>
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2048
     * If the {@code visibleOnly} argument is {@code true}, the found validate
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2049
     * root and all its parents up to the toplevel component must also be
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2050
     * visible. Otherwise this method returns {@code null}.
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2051
     *
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2052
     * @return the validate root of the given container or null
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2053
     * @see java.awt.Component#isDisplayable()
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2054
     * @see java.awt.Component#isVisible()
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2055
     * @since 1.7
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2056
     */
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2057
    static Container getValidateRoot(Container c, boolean visibleOnly) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2058
        Container root = null;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2059
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2060
        for (; c != null; c = c.getParent())
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2061
        {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2062
            if (!c.isDisplayable() || c instanceof CellRendererPane) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2063
                return null;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2064
            }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2065
            if (c.isValidateRoot()) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2066
                root = c;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2067
                break;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2068
            }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2069
        }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2070
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2071
        if (root == null) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2072
            return null;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2073
        }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2074
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2075
        for (; c != null; c = c.getParent()) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2076
            if (!c.isDisplayable() || (visibleOnly && !c.isVisible())) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2077
                return null;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2078
            }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2079
            if (c instanceof Window || c instanceof Applet) {
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2080
                return root;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2081
            }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2082
        }
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2083
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2084
        return null;
126dc6fe0d7b 6852592: invalidate() must be smarter
anthony
parents: 3749
diff changeset
  2085
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
}