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