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