0N/A/*
3909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport sun.swing.UIAction;
0N/A
0N/Aimport java.applet.*;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.dnd.DropTarget;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport java.util.Hashtable;
0N/A
0N/Aimport java.lang.reflect.*;
0N/A
0N/Aimport javax.accessibility.*;
0N/Aimport javax.swing.event.MenuDragMouseEvent;
0N/Aimport javax.swing.plaf.UIResource;
0N/Aimport javax.swing.text.View;
0N/Aimport java.security.AccessController;
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/Aimport sun.awt.AppContext;
0N/A
0N/A/**
0N/A * A collection of utility methods for Swing.
0N/A *
0N/A * @author unknown
0N/A */
0N/Apublic class SwingUtilities implements SwingConstants
0N/A{
0N/A // These states are system-wide, rather than AppContext wide.
0N/A private static boolean canAccessEventQueue = false;
0N/A private static boolean eventQueueTested = false;
0N/A
0N/A /**
0N/A * Indicates if we should change the drop target when a
0N/A * {@code TransferHandler} is set.
0N/A */
0N/A private static boolean suppressDropSupport;
0N/A
0N/A /**
0N/A * Indiciates if we've checked the system property for suppressing
0N/A * drop support.
0N/A */
0N/A private static boolean checkedSuppressDropSupport;
0N/A
0N/A
0N/A /**
0N/A * Returns true if <code>setTransferHandler</code> should change the
0N/A * <code>DropTarget</code>.
0N/A */
0N/A private static boolean getSuppressDropTarget() {
0N/A if (!checkedSuppressDropSupport) {
0N/A suppressDropSupport = Boolean.valueOf(
0N/A AccessController.doPrivileged(
0N/A new GetPropertyAction("suppressSwingDropSupport")));
0N/A checkedSuppressDropSupport = true;
0N/A }
0N/A return suppressDropSupport;
0N/A }
0N/A
0N/A /**
0N/A * Installs a {@code DropTarget} on the component as necessary for a
0N/A * {@code TransferHandler} change.
0N/A */
0N/A static void installSwingDropTargetAsNecessary(Component c,
0N/A TransferHandler t) {
0N/A
0N/A if (!getSuppressDropTarget()) {
0N/A DropTarget dropHandler = c.getDropTarget();
0N/A if ((dropHandler == null) || (dropHandler instanceof UIResource)) {
0N/A if (t == null) {
0N/A c.setDropTarget(null);
0N/A } else if (!GraphicsEnvironment.isHeadless()) {
0N/A c.setDropTarget(new TransferHandler.SwingDropTarget(c));
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return true if <code>a</code> contains <code>b</code>
0N/A */
0N/A public static final boolean isRectangleContainingRectangle(Rectangle a,Rectangle b) {
625N/A return b.x >= a.x && (b.x + b.width) <= (a.x + a.width) &&
625N/A b.y >= a.y && (b.y + b.height) <= (a.y + a.height);
0N/A }
0N/A
0N/A /**
0N/A * Return the rectangle (0,0,bounds.width,bounds.height) for the component <code>aComponent</code>
0N/A */
0N/A public static Rectangle getLocalBounds(Component aComponent) {
0N/A Rectangle b = new Rectangle(aComponent.getBounds());
0N/A b.x = b.y = 0;
0N/A return b;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the first <code>Window </code> ancestor of <code>c</code>, or
0N/A * {@code null} if <code>c</code> is not contained inside a <code>Window</code>.
0N/A *
0N/A * @param c <code>Component</code> to get <code>Window</code> ancestor
0N/A * of.
0N/A * @return the first <code>Window </code> ancestor of <code>c</code>, or
0N/A * {@code null} if <code>c</code> is not contained inside a
0N/A * <code>Window</code>.
0N/A * @since 1.3
0N/A */
0N/A public static Window getWindowAncestor(Component c) {
0N/A for(Container p = c.getParent(); p != null; p = p.getParent()) {
0N/A if (p instanceof Window) {
0N/A return (Window)p;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Converts the location <code>x</code> <code>y</code> to the
0N/A * parents coordinate system, returning the location.
0N/A */
0N/A static Point convertScreenLocationToParent(Container parent,int x, int y) {
0N/A for (Container p = parent; p != null; p = p.getParent()) {
0N/A if (p instanceof Window) {
0N/A Point point = new Point(x, y);
0N/A
0N/A SwingUtilities.convertPointFromScreen(point, parent);
0N/A return point;
0N/A }
0N/A }
0N/A throw new Error("convertScreenLocationToParent: no window ancestor");
0N/A }
0N/A
0N/A /**
0N/A * Convert a <code>aPoint</code> in <code>source</code> coordinate system to
0N/A * <code>destination</code> coordinate system.
0N/A * If <code>source</code> is {@code null}, <code>aPoint</code> is assumed to be in <code>destination</code>'s
0N/A * root component coordinate system.
0N/A * If <code>destination</code> is {@code null}, <code>aPoint</code> will be converted to <code>source</code>'s
0N/A * root component coordinate system.
0N/A * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>aPoint</code>
0N/A * without any conversion.
0N/A */
0N/A public static Point convertPoint(Component source,Point aPoint,Component destination) {
0N/A Point p;
0N/A
0N/A if(source == null && destination == null)
0N/A return aPoint;
0N/A if(source == null) {
0N/A source = getWindowAncestor(destination);
0N/A if(source == null)
0N/A throw new Error("Source component not connected to component tree hierarchy");
0N/A }
0N/A p = new Point(aPoint);
0N/A convertPointToScreen(p,source);
0N/A if(destination == null) {
0N/A destination = getWindowAncestor(source);
0N/A if(destination == null)
0N/A throw new Error("Destination component not connected to component tree hierarchy");
0N/A }
0N/A convertPointFromScreen(p,destination);
0N/A return p;
0N/A }
0N/A
0N/A /**
0N/A * Convert the point <code>(x,y)</code> in <code>source</code> coordinate system to
0N/A * <code>destination</code> coordinate system.
0N/A * If <code>source</code> is {@code null}, <code>(x,y)</code> is assumed to be in <code>destination</code>'s
0N/A * root component coordinate system.
0N/A * If <code>destination</code> is {@code null}, <code>(x,y)</code> will be converted to <code>source</code>'s
0N/A * root component coordinate system.
0N/A * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>(x,y)</code>
0N/A * without any conversion.
0N/A */
0N/A public static Point convertPoint(Component source,int x, int y,Component destination) {
0N/A Point point = new Point(x,y);
0N/A return convertPoint(source,point,destination);
0N/A }
0N/A
0N/A /**
0N/A * Convert the rectangle <code>aRectangle</code> in <code>source</code> coordinate system to
0N/A * <code>destination</code> coordinate system.
0N/A * If <code>source</code> is {@code null}, <code>aRectangle</code> is assumed to be in <code>destination</code>'s
0N/A * root component coordinate system.
0N/A * If <code>destination</code> is {@code null}, <code>aRectangle</code> will be converted to <code>source</code>'s
0N/A * root component coordinate system.
0N/A * If both <code>source</code> and <code>destination</code> are {@code null}, return <code>aRectangle</code>
0N/A * without any conversion.
0N/A */
0N/A public static Rectangle convertRectangle(Component source,Rectangle aRectangle,Component destination) {
0N/A Point point = new Point(aRectangle.x,aRectangle.y);
0N/A point = convertPoint(source,point,destination);
0N/A return new Rectangle(point.x,point.y,aRectangle.width,aRectangle.height);
0N/A }
0N/A
0N/A /**
0N/A * Convenience method for searching above <code>comp</code> in the
0N/A * component hierarchy and returns the first object of class <code>c</code> it
0N/A * finds. Can return {@code null}, if a class <code>c</code> cannot be found.
0N/A */
0N/A public static Container getAncestorOfClass(Class<?> c, Component comp)
0N/A {
0N/A if(comp == null || c == null)
0N/A return null;
0N/A
0N/A Container parent = comp.getParent();
0N/A while(parent != null && !(c.isInstance(parent)))
0N/A parent = parent.getParent();
0N/A return parent;
0N/A }
0N/A
0N/A /**
0N/A * Convenience method for searching above <code>comp</code> in the
0N/A * component hierarchy and returns the first object of <code>name</code> it
0N/A * finds. Can return {@code null}, if <code>name</code> cannot be found.
0N/A */
0N/A public static Container getAncestorNamed(String name, Component comp) {
0N/A if(comp == null || name == null)
0N/A return null;
0N/A
0N/A Container parent = comp.getParent();
0N/A while(parent != null && !(name.equals(parent.getName())))
0N/A parent = parent.getParent();
0N/A return parent;
0N/A }
0N/A
0N/A /**
0N/A * Returns the deepest visible descendent Component of <code>parent</code>
0N/A * that contains the location <code>x</code>, <code>y</code>.
0N/A * If <code>parent</code> does not contain the specified location,
0N/A * then <code>null</code> is returned. If <code>parent</code> is not a
0N/A * container, or none of <code>parent</code>'s visible descendents
0N/A * contain the specified location, <code>parent</code> is returned.
0N/A *
0N/A * @param parent the root component to begin the search
0N/A * @param x the x target location
0N/A * @param y the y target location
0N/A */
0N/A public static Component getDeepestComponentAt(Component parent, int x, int y) {
0N/A if (!parent.contains(x, y)) {
0N/A return null;
0N/A }
0N/A if (parent instanceof Container) {
0N/A Component components[] = ((Container)parent).getComponents();
625N/A for (Component comp : components) {
0N/A if (comp != null && comp.isVisible()) {
0N/A Point loc = comp.getLocation();
0N/A if (comp instanceof Container) {
0N/A comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y);
0N/A } else {
0N/A comp = comp.getComponentAt(x - loc.x, y - loc.y);
0N/A }
0N/A if (comp != null && comp.isVisible()) {
0N/A return comp;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return parent;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a MouseEvent similar to <code>sourceEvent</code> except that its x
0N/A * and y members have been converted to <code>destination</code>'s coordinate
0N/A * system. If <code>source</code> is {@code null}, <code>sourceEvent</code> x and y members
0N/A * are assumed to be into <code>destination</code>'s root component coordinate system.
0N/A * If <code>destination</code> is <code>null</code>, the
0N/A * returned MouseEvent will be in <code>source</code>'s coordinate system.
0N/A * <code>sourceEvent</code> will not be changed. A new event is returned.
0N/A * the <code>source</code> field of the returned event will be set
0N/A * to <code>destination</code> if destination is non-{@code null}
0N/A * use the translateMouseEvent() method to translate a mouse event from
0N/A * one component to another without changing the source.
0N/A */
0N/A public static MouseEvent convertMouseEvent(Component source,
0N/A MouseEvent sourceEvent,
0N/A Component destination) {
0N/A Point p = convertPoint(source,new Point(sourceEvent.getX(),
0N/A sourceEvent.getY()),
0N/A destination);
0N/A Component newSource;
0N/A
0N/A if(destination != null)
0N/A newSource = destination;
0N/A else
0N/A newSource = source;
0N/A
0N/A MouseEvent newEvent;
0N/A if (sourceEvent instanceof MouseWheelEvent) {
0N/A MouseWheelEvent sourceWheelEvent = (MouseWheelEvent)sourceEvent;
0N/A newEvent = new MouseWheelEvent(newSource,
0N/A sourceWheelEvent.getID(),
0N/A sourceWheelEvent.getWhen(),
5216N/A sourceWheelEvent.getModifiers()
5216N/A | sourceWheelEvent.getModifiersEx(),
0N/A p.x,p.y,
0N/A sourceWheelEvent.getXOnScreen(),
0N/A sourceWheelEvent.getYOnScreen(),
0N/A sourceWheelEvent.getClickCount(),
0N/A sourceWheelEvent.isPopupTrigger(),
0N/A sourceWheelEvent.getScrollType(),
0N/A sourceWheelEvent.getScrollAmount(),
0N/A sourceWheelEvent.getWheelRotation());
0N/A }
0N/A else if (sourceEvent instanceof MenuDragMouseEvent) {
0N/A MenuDragMouseEvent sourceMenuDragEvent = (MenuDragMouseEvent)sourceEvent;
0N/A newEvent = new MenuDragMouseEvent(newSource,
0N/A sourceMenuDragEvent.getID(),
0N/A sourceMenuDragEvent.getWhen(),
5216N/A sourceMenuDragEvent.getModifiers()
5216N/A | sourceMenuDragEvent.getModifiersEx(),
0N/A p.x,p.y,
0N/A sourceMenuDragEvent.getXOnScreen(),
0N/A sourceMenuDragEvent.getYOnScreen(),
0N/A sourceMenuDragEvent.getClickCount(),
0N/A sourceMenuDragEvent.isPopupTrigger(),
0N/A sourceMenuDragEvent.getPath(),
0N/A sourceMenuDragEvent.getMenuSelectionManager());
0N/A }
0N/A else {
0N/A newEvent = new MouseEvent(newSource,
0N/A sourceEvent.getID(),
0N/A sourceEvent.getWhen(),
5216N/A sourceEvent.getModifiers()
5216N/A | sourceEvent.getModifiersEx(),
0N/A p.x,p.y,
0N/A sourceEvent.getXOnScreen(),
0N/A sourceEvent.getYOnScreen(),
0N/A sourceEvent.getClickCount(),
0N/A sourceEvent.isPopupTrigger(),
6069N/A sourceEvent.getButton());
0N/A }
0N/A return newEvent;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Convert a point from a component's coordinate system to
0N/A * screen coordinates.
0N/A *
0N/A * @param p a Point object (converted to the new coordinate system)
0N/A * @param c a Component object
0N/A */
0N/A public static void convertPointToScreen(Point p,Component c) {
0N/A Rectangle b;
0N/A int x,y;
0N/A
0N/A do {
0N/A if(c instanceof JComponent) {
625N/A x = c.getX();
625N/A y = c.getY();
0N/A } else if(c instanceof java.applet.Applet ||
0N/A c instanceof java.awt.Window) {
0N/A try {
0N/A Point pp = c.getLocationOnScreen();
0N/A x = pp.x;
0N/A y = pp.y;
0N/A } catch (IllegalComponentStateException icse) {
0N/A x = c.getX();
0N/A y = c.getY();
0N/A }
0N/A } else {
0N/A x = c.getX();
0N/A y = c.getY();
0N/A }
0N/A
0N/A p.x += x;
0N/A p.y += y;
0N/A
0N/A if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
0N/A break;
0N/A c = c.getParent();
0N/A } while(c != null);
0N/A }
0N/A
0N/A /**
0N/A * Convert a point from a screen coordinates to a component's
0N/A * coordinate system
0N/A *
0N/A * @param p a Point object (converted to the new coordinate system)
0N/A * @param c a Component object
0N/A */
0N/A public static void convertPointFromScreen(Point p,Component c) {
0N/A Rectangle b;
0N/A int x,y;
0N/A
0N/A do {
0N/A if(c instanceof JComponent) {
625N/A x = c.getX();
625N/A y = c.getY();
0N/A } else if(c instanceof java.applet.Applet ||
0N/A c instanceof java.awt.Window) {
0N/A try {
0N/A Point pp = c.getLocationOnScreen();
0N/A x = pp.x;
0N/A y = pp.y;
0N/A } catch (IllegalComponentStateException icse) {
0N/A x = c.getX();
0N/A y = c.getY();
0N/A }
0N/A } else {
0N/A x = c.getX();
0N/A y = c.getY();
0N/A }
0N/A
0N/A p.x -= x;
0N/A p.y -= y;
0N/A
0N/A if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
0N/A break;
0N/A c = c.getParent();
0N/A } while(c != null);
0N/A }
0N/A
0N/A /**
0N/A * Returns the first <code>Window </code> ancestor of <code>c</code>, or
0N/A * {@code null} if <code>c</code> is not contained inside a <code>Window</code>.
0N/A * <p>
0N/A * Note: This method provides the same functionality as
0N/A * <code>getWindowAncestor</code>.
0N/A *
0N/A * @param c <code>Component</code> to get <code>Window</code> ancestor
0N/A * of.
0N/A * @return the first <code>Window </code> ancestor of <code>c</code>, or
0N/A * {@code null} if <code>c</code> is not contained inside a
0N/A * <code>Window</code>.
0N/A */
0N/A public static Window windowForComponent(Component c) {
0N/A return getWindowAncestor(c);
0N/A }
0N/A
0N/A /**
0N/A * Return <code>true</code> if a component <code>a</code> descends from a component <code>b</code>
0N/A */
0N/A public static boolean isDescendingFrom(Component a,Component b) {
0N/A if(a == b)
0N/A return true;
0N/A for(Container p = a.getParent();p!=null;p=p.getParent())
0N/A if(p == b)
0N/A return true;
0N/A return false;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Convenience to calculate the intersection of two rectangles
0N/A * without allocating a new rectangle.
0N/A * If the two rectangles don't intersect,
0N/A * then the returned rectangle begins at (0,0)
0N/A * and has zero width and height.
0N/A *
0N/A * @param x the X coordinate of the first rectangle's top-left point
0N/A * @param y the Y coordinate of the first rectangle's top-left point
0N/A * @param width the width of the first rectangle
0N/A * @param height the height of the first rectangle
0N/A * @param dest the second rectangle
0N/A *
0N/A * @return <code>dest</code>, modified to specify the intersection
0N/A */
0N/A public static Rectangle computeIntersection(int x,int y,int width,int height,Rectangle dest) {
0N/A int x1 = (x > dest.x) ? x : dest.x;
0N/A int x2 = ((x+width) < (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
0N/A int y1 = (y > dest.y) ? y : dest.y;
0N/A int y2 = ((y + height) < (dest.y + dest.height) ? (y+height) : (dest.y + dest.height));
0N/A
0N/A dest.x = x1;
0N/A dest.y = y1;
0N/A dest.width = x2 - x1;
0N/A dest.height = y2 - y1;
0N/A
0N/A // If rectangles don't intersect, return zero'd intersection.
0N/A if (dest.width < 0 || dest.height < 0) {
0N/A dest.x = dest.y = dest.width = dest.height = 0;
0N/A }
0N/A
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * Convenience method that calculates the union of two rectangles
0N/A * without allocating a new rectangle.
0N/A *
0N/A * @param x the x-coordinate of the first rectangle
0N/A * @param y the y-coordinate of the first rectangle
0N/A * @param width the width of the first rectangle
0N/A * @param height the height of the first rectangle
0N/A * @param dest the coordinates of the second rectangle; the union
0N/A * of the two rectangles is returned in this rectangle
0N/A * @return the <code>dest</code> <code>Rectangle</code>
0N/A */
0N/A public static Rectangle computeUnion(int x,int y,int width,int height,Rectangle dest) {
0N/A int x1 = (x < dest.x) ? x : dest.x;
0N/A int x2 = ((x+width) > (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
0N/A int y1 = (y < dest.y) ? y : dest.y;
0N/A int y2 = ((y+height) > (dest.y + dest.height)) ? (y+height) : (dest.y + dest.height);
0N/A
0N/A dest.x = x1;
0N/A dest.y = y1;
0N/A dest.width = (x2 - x1);
0N/A dest.height= (y2 - y1);
0N/A return dest;
0N/A }
0N/A
0N/A /**
0N/A * Convenience returning an array of rect representing the regions within
0N/A * <code>rectA</code> that do not overlap with <code>rectB</code>. If the
0N/A * two Rects do not overlap, returns an empty array
0N/A */
0N/A public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
0N/A if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
0N/A return new Rectangle[0];
0N/A }
0N/A
0N/A Rectangle t = new Rectangle();
0N/A Rectangle a=null,b=null,c=null,d=null;
0N/A Rectangle result[];
0N/A int rectCount = 0;
0N/A
0N/A /* rectA contains rectB */
0N/A if (isRectangleContainingRectangle(rectA,rectB)) {
0N/A t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
0N/A t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A c = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.x = rectB.x + rectB.width; t.y = rectA.y; t.width = rectA.x + rectA.width - (rectB.x + rectB.width);
0N/A t.height = rectA.height;
0N/A if(t.width > 0 && t.height > 0) {
0N/A d = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A /* 1 */
0N/A if (rectB.x <= rectA.x && rectB.y <= rectA.y) {
0N/A if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
0N/A
0N/A t.x = rectA.x; t.y = rectB.y + rectB.height;
0N/A t.width = rectA.width; t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = t;
0N/A rectCount++;
0N/A }
0N/A } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
0N/A t.setBounds((rectB.x + rectB.width), rectA.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = t;
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A t.setBounds((rectB.x + rectB.width), rectA.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width),
0N/A (rectB.y + rectB.height) - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A }
0N/A } else if (rectB.x <= rectA.x && (rectB.y + rectB.height) >= (rectA.y + rectA.height)) {
0N/A if ((rectB.x + rectB.width) > (rectA.x + rectA.width)) {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = t;
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A t.setBounds((rectB.x + rectB.width), rectB.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width),
0N/A (rectA.y + rectA.height) - rectB.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A }
0N/A } else if (rectB.x <= rectA.x) {
0N/A if ((rectB.x + rectB.width) >= (rectA.x + rectA.width)) {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width>0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds((rectB.x + rectB.width), rectB.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width),
0N/A rectB.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A c = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A }
0N/A } else if (rectB.x <= (rectA.x + rectA.width) && (rectB.x + rectB.width) > (rectA.x + rectA.width)) {
0N/A if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
0N/A t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = t;
0N/A rectCount++;
0N/A }
0N/A } else if (rectB.y <= rectA.y) {
0N/A t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x,
0N/A (rectB.y + rectB.height) - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else if ((rectB.y + rectB.height) > (rectA.y + rectA.height)) {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
0N/A (rectA.y + rectA.height) - rectB.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A t.setBounds(rectA.x, rectA.y, rectA.width, rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, rectB.y, rectB.x - rectA.x,
0N/A rectB.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectA.x, (rectB.y + rectB.height), rectA.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A c = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A }
0N/A } else if (rectB.x >= rectA.x && (rectB.x + rectB.width) <= (rectA.x + rectA.width)) {
0N/A if (rectB.y <= rectA.y && (rectB.y + rectB.height) > (rectA.y + rectA.height)) {
0N/A t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A t.setBounds((rectB.x + rectB.width), rectA.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else if (rectB.y <= rectA.y) {
0N/A t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectB.x, (rectB.y + rectB.height),
0N/A rectB.width,
0N/A (rectA.y + rectA.height) - (rectB.y + rectB.height));
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds((rectB.x + rectB.width), rectA.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A c = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A } else {
0N/A t.setBounds(rectA.x, rectA.y, rectB.x - rectA.x, rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A a = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds(rectB.x, rectA.y, rectB.width,
0N/A rectB.y - rectA.y);
0N/A if(t.width > 0 && t.height > 0) {
0N/A b = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A
0N/A t.setBounds((rectB.x + rectB.width), rectA.y,
0N/A (rectA.x + rectA.width) - (rectB.x + rectB.width), rectA.height);
0N/A if(t.width > 0 && t.height > 0) {
0N/A c = new Rectangle(t);
0N/A rectCount++;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A result = new Rectangle[rectCount];
0N/A rectCount = 0;
0N/A if(a != null)
0N/A result[rectCount++] = a;
0N/A if(b != null)
0N/A result[rectCount++] = b;
0N/A if(c != null)
0N/A result[rectCount++] = c;
0N/A if(d != null)
0N/A result[rectCount++] = d;
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the mouse event specifies the left mouse button.
0N/A *
0N/A * @param anEvent a MouseEvent object
0N/A * @return true if the left mouse button was active
0N/A */
0N/A public static boolean isLeftMouseButton(MouseEvent anEvent) {
0N/A return ((anEvent.getModifiers() & InputEvent.BUTTON1_MASK) != 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the mouse event specifies the middle mouse button.
0N/A *
0N/A * @param anEvent a MouseEvent object
0N/A * @return true if the middle mouse button was active
0N/A */
0N/A public static boolean isMiddleMouseButton(MouseEvent anEvent) {
0N/A return ((anEvent.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the mouse event specifies the right mouse button.
0N/A *
0N/A * @param anEvent a MouseEvent object
0N/A * @return true if the right mouse button was active
0N/A */
0N/A public static boolean isRightMouseButton(MouseEvent anEvent) {
0N/A return ((anEvent.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK);
0N/A }
0N/A
0N/A /**
0N/A * Compute the width of the string using a font with the specified
0N/A * "metrics" (sizes).
0N/A *
0N/A * @param fm a FontMetrics object to compute with
0N/A * @param str the String to compute
0N/A * @return an int containing the string width
0N/A */
0N/A public static int computeStringWidth(FontMetrics fm,String str) {
0N/A // You can't assume that a string's width is the sum of its
0N/A // characters' widths in Java2D -- it may be smaller due to
0N/A // kerning, etc.
0N/A return SwingUtilities2.stringWidth(null, fm, str);
0N/A }
0N/A
0N/A /**
0N/A * Compute and return the location of the icons origin, the
0N/A * location of origin of the text baseline, and a possibly clipped
0N/A * version of the compound labels string. Locations are computed
0N/A * relative to the viewR rectangle.
0N/A * The JComponents orientation (LEADING/TRAILING) will also be taken
0N/A * into account and translated into LEFT/RIGHT values accordingly.
0N/A */
0N/A public static String layoutCompoundLabel(JComponent c,
0N/A FontMetrics fm,
0N/A String text,
0N/A Icon icon,
0N/A int verticalAlignment,
0N/A int horizontalAlignment,
0N/A int verticalTextPosition,
0N/A int horizontalTextPosition,
0N/A Rectangle viewR,
0N/A Rectangle iconR,
0N/A Rectangle textR,
0N/A int textIconGap)
0N/A {
0N/A boolean orientationIsLeftToRight = true;
0N/A int hAlign = horizontalAlignment;
0N/A int hTextPos = horizontalTextPosition;
0N/A
0N/A if (c != null) {
0N/A if (!(c.getComponentOrientation().isLeftToRight())) {
0N/A orientationIsLeftToRight = false;
0N/A }
0N/A }
0N/A
0N/A // Translate LEADING/TRAILING values in horizontalAlignment
0N/A // to LEFT/RIGHT values depending on the components orientation
0N/A switch (horizontalAlignment) {
0N/A case LEADING:
0N/A hAlign = (orientationIsLeftToRight) ? LEFT : RIGHT;
0N/A break;
0N/A case TRAILING:
0N/A hAlign = (orientationIsLeftToRight) ? RIGHT : LEFT;
0N/A break;
0N/A }
0N/A
0N/A // Translate LEADING/TRAILING values in horizontalTextPosition
0N/A // to LEFT/RIGHT values depending on the components orientation
0N/A switch (horizontalTextPosition) {
0N/A case LEADING:
0N/A hTextPos = (orientationIsLeftToRight) ? LEFT : RIGHT;
0N/A break;
0N/A case TRAILING:
0N/A hTextPos = (orientationIsLeftToRight) ? RIGHT : LEFT;
0N/A break;
0N/A }
0N/A
0N/A return layoutCompoundLabelImpl(c,
0N/A fm,
0N/A text,
0N/A icon,
0N/A verticalAlignment,
0N/A hAlign,
0N/A verticalTextPosition,
0N/A hTextPos,
0N/A viewR,
0N/A iconR,
0N/A textR,
0N/A textIconGap);
0N/A }
0N/A
0N/A /**
0N/A * Compute and return the location of the icons origin, the
0N/A * location of origin of the text baseline, and a possibly clipped
0N/A * version of the compound labels string. Locations are computed
0N/A * relative to the viewR rectangle.
0N/A * This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
0N/A * values in horizontalTextPosition (they will default to RIGHT) and in
0N/A * horizontalAlignment (they will default to CENTER).
0N/A * Use the other version of layoutCompoundLabel() instead.
0N/A */
0N/A public static String layoutCompoundLabel(
0N/A FontMetrics fm,
0N/A String text,
0N/A Icon icon,
0N/A int verticalAlignment,
0N/A int horizontalAlignment,
0N/A int verticalTextPosition,
0N/A int horizontalTextPosition,
0N/A Rectangle viewR,
0N/A Rectangle iconR,
0N/A Rectangle textR,
0N/A int textIconGap)
0N/A {
0N/A return layoutCompoundLabelImpl(null, fm, text, icon,
0N/A verticalAlignment,
0N/A horizontalAlignment,
0N/A verticalTextPosition,
0N/A horizontalTextPosition,
0N/A viewR, iconR, textR, textIconGap);
0N/A }
0N/A
0N/A /**
0N/A * Compute and return the location of the icons origin, the
0N/A * location of origin of the text baseline, and a possibly clipped
0N/A * version of the compound labels string. Locations are computed
0N/A * relative to the viewR rectangle.
0N/A * This layoutCompoundLabel() does not know how to handle LEADING/TRAILING
0N/A * values in horizontalTextPosition (they will default to RIGHT) and in
0N/A * horizontalAlignment (they will default to CENTER).
0N/A * Use the other version of layoutCompoundLabel() instead.
0N/A */
0N/A private static String layoutCompoundLabelImpl(
0N/A JComponent c,
0N/A FontMetrics fm,
0N/A String text,
0N/A Icon icon,
0N/A int verticalAlignment,
0N/A int horizontalAlignment,
0N/A int verticalTextPosition,
0N/A int horizontalTextPosition,
0N/A Rectangle viewR,
0N/A Rectangle iconR,
0N/A Rectangle textR,
0N/A int textIconGap)
0N/A {
0N/A /* Initialize the icon bounds rectangle iconR.
0N/A */
0N/A
0N/A if (icon != null) {
0N/A iconR.width = icon.getIconWidth();
0N/A iconR.height = icon.getIconHeight();
0N/A }
0N/A else {
0N/A iconR.width = iconR.height = 0;
0N/A }
0N/A
0N/A /* Initialize the text bounds rectangle textR. If a null
0N/A * or and empty String was specified we substitute "" here
0N/A * and use 0,0,0,0 for textR.
0N/A */
0N/A
0N/A boolean textIsEmpty = (text == null) || text.equals("");
0N/A int lsb = 0;
619N/A int rsb = 0;
0N/A /* Unless both text and icon are non-null, we effectively ignore
0N/A * the value of textIconGap.
0N/A */
0N/A int gap;
0N/A
625N/A View v;
0N/A if (textIsEmpty) {
0N/A textR.width = textR.height = 0;
0N/A text = "";
0N/A gap = 0;
0N/A }
0N/A else {
0N/A int availTextWidth;
0N/A gap = (icon == null) ? 0 : textIconGap;
0N/A
0N/A if (horizontalTextPosition == CENTER) {
0N/A availTextWidth = viewR.width;
0N/A }
0N/A else {
0N/A availTextWidth = viewR.width - (iconR.width + gap);
0N/A }
0N/A v = (c != null) ? (View) c.getClientProperty("html") : null;
0N/A if (v != null) {
0N/A textR.width = Math.min(availTextWidth,
0N/A (int) v.getPreferredSpan(View.X_AXIS));
0N/A textR.height = (int) v.getPreferredSpan(View.Y_AXIS);
0N/A } else {
0N/A textR.width = SwingUtilities2.stringWidth(c, fm, text);
0N/A lsb = SwingUtilities2.getLeftSideBearing(c, fm, text);
0N/A if (lsb < 0) {
1637N/A // If lsb is negative, add it to the width and later
1637N/A // adjust the x location. This gives more space than is
1637N/A // actually needed.
1637N/A // This is done like this for two reasons:
1637N/A // 1. If we set the width to the actual bounds all
1637N/A // callers would have to account for negative lsb
1637N/A // (pref size calculations ONLY look at width of
1637N/A // textR)
1637N/A // 2. You can do a drawString at the returned location
1637N/A // and the text won't be clipped.
0N/A textR.width -= lsb;
0N/A }
0N/A if (textR.width > availTextWidth) {
0N/A text = SwingUtilities2.clipString(c, fm, text,
0N/A availTextWidth);
0N/A textR.width = SwingUtilities2.stringWidth(c, fm, text);
0N/A }
0N/A textR.height = fm.getHeight();
0N/A }
0N/A }
0N/A
0N/A
0N/A /* Compute textR.x,y given the verticalTextPosition and
0N/A * horizontalTextPosition properties
0N/A */
0N/A
0N/A if (verticalTextPosition == TOP) {
0N/A if (horizontalTextPosition != CENTER) {
0N/A textR.y = 0;
0N/A }
0N/A else {
0N/A textR.y = -(textR.height + gap);
0N/A }
0N/A }
0N/A else if (verticalTextPosition == CENTER) {
0N/A textR.y = (iconR.height / 2) - (textR.height / 2);
0N/A }
0N/A else { // (verticalTextPosition == BOTTOM)
0N/A if (horizontalTextPosition != CENTER) {
0N/A textR.y = iconR.height - textR.height;
0N/A }
0N/A else {
0N/A textR.y = (iconR.height + gap);
0N/A }
0N/A }
0N/A
0N/A if (horizontalTextPosition == LEFT) {
0N/A textR.x = -(textR.width + gap);
0N/A }
0N/A else if (horizontalTextPosition == CENTER) {
0N/A textR.x = (iconR.width / 2) - (textR.width / 2);
0N/A }
0N/A else { // (horizontalTextPosition == RIGHT)
0N/A textR.x = (iconR.width + gap);
0N/A }
0N/A
0N/A // WARNING: DefaultTreeCellEditor uses a shortened version of
0N/A // this algorithm to position it's Icon. If you change how this
0N/A // is calculated, be sure and update DefaultTreeCellEditor too.
0N/A
0N/A /* labelR is the rectangle that contains iconR and textR.
0N/A * Move it to its proper position given the labelAlignment
0N/A * properties.
0N/A *
0N/A * To avoid actually allocating a Rectangle, Rectangle.union
0N/A * has been inlined below.
0N/A */
0N/A int labelR_x = Math.min(iconR.x, textR.x);
0N/A int labelR_width = Math.max(iconR.x + iconR.width,
0N/A textR.x + textR.width) - labelR_x;
0N/A int labelR_y = Math.min(iconR.y, textR.y);
0N/A int labelR_height = Math.max(iconR.y + iconR.height,
0N/A textR.y + textR.height) - labelR_y;
0N/A
0N/A int dx, dy;
0N/A
0N/A if (verticalAlignment == TOP) {
0N/A dy = viewR.y - labelR_y;
0N/A }
0N/A else if (verticalAlignment == CENTER) {
0N/A dy = (viewR.y + (viewR.height / 2)) - (labelR_y + (labelR_height / 2));
0N/A }
0N/A else { // (verticalAlignment == BOTTOM)
0N/A dy = (viewR.y + viewR.height) - (labelR_y + labelR_height);
0N/A }
0N/A
0N/A if (horizontalAlignment == LEFT) {
0N/A dx = viewR.x - labelR_x;
0N/A }
0N/A else if (horizontalAlignment == RIGHT) {
0N/A dx = (viewR.x + viewR.width) - (labelR_x + labelR_width);
0N/A }
0N/A else { // (horizontalAlignment == CENTER)
0N/A dx = (viewR.x + (viewR.width / 2)) -
0N/A (labelR_x + (labelR_width / 2));
0N/A }
0N/A
0N/A /* Translate textR and glypyR by dx,dy.
0N/A */
0N/A
0N/A textR.x += dx;
0N/A textR.y += dy;
0N/A
0N/A iconR.x += dx;
0N/A iconR.y += dy;
0N/A
0N/A if (lsb < 0) {
0N/A // lsb is negative. Shift the x location so that the text is
0N/A // visually drawn at the right location.
0N/A textR.x -= lsb;
619N/A
619N/A textR.width += lsb;
619N/A }
619N/A if (rsb > 0) {
619N/A textR.width -= rsb;
0N/A }
0N/A
0N/A return text;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Paints a component to the specified <code>Graphics</code>.
0N/A * This method is primarily useful to render
0N/A * <code>Component</code>s that don't exist as part of the visible
0N/A * containment hierarchy, but are used for rendering. For
0N/A * example, if you are doing your own rendering and want to render
0N/A * some text (or even HTML), you could make use of
0N/A * <code>JLabel</code>'s text rendering support and have it paint
0N/A * directly by way of this method, without adding the label to the
0N/A * visible containment hierarchy.
0N/A * <p>
0N/A * This method makes use of <code>CellRendererPane</code> to handle
0N/A * the actual painting, and is only recommended if you use one
0N/A * component for rendering. If you make use of multiple components
0N/A * to handle the rendering, as <code>JTable</code> does, use
0N/A * <code>CellRendererPane</code> directly. Otherwise, as described
0N/A * below, you could end up with a <code>CellRendererPane</code>
0N/A * per <code>Component</code>.
0N/A * <p>
0N/A * If <code>c</code>'s parent is not a <code>CellRendererPane</code>,
0N/A * a new <code>CellRendererPane</code> is created, <code>c</code> is
0N/A * added to it, and the <code>CellRendererPane</code> is added to
0N/A * <code>p</code>. If <code>c</code>'s parent is a
0N/A * <code>CellRendererPane</code> and the <code>CellRendererPane</code>s
0N/A * parent is not <code>p</code>, it is added to <code>p</code>.
0N/A * <p>
0N/A * The component should either descend from <code>JComponent</code>
0N/A * or be another kind of lightweight component.
0N/A * A lightweight component is one whose "lightweight" property
0N/A * (returned by the <code>Component</code>
0N/A * <code>isLightweight</code> method)
0N/A * is true. If the Component is not lightweight, bad things map happen:
0N/A * crashes, exceptions, painting problems...
0N/A *
0N/A * @param g the <code>Graphics</code> object to draw on
0N/A * @param c the <code>Component</code> to draw
0N/A * @param p the intermediate <code>Container</code>
0N/A * @param x an int specifying the left side of the area draw in, in pixels,
0N/A * measured from the left edge of the graphics context
0N/A * @param y an int specifying the top of the area to draw in, in pixels
0N/A * measured down from the top edge of the graphics context
0N/A * @param w an int specifying the width of the area draw in, in pixels
0N/A * @param h an int specifying the height of the area draw in, in pixels
0N/A *
0N/A * @see CellRendererPane
0N/A * @see java.awt.Component#isLightweight
0N/A */
0N/A public static void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
0N/A getCellRendererPane(c, p).paintComponent(g, c, p, x, y, w, h,false);
0N/A }
0N/A
0N/A /**
0N/A * Paints a component to the specified <code>Graphics</code>. This
0N/A * is a cover method for
0N/A * {@link #paintComponent(Graphics,Component,Container,int,int,int,int)}.
0N/A * Refer to it for more information.
0N/A *
0N/A * @param g the <code>Graphics</code> object to draw on
0N/A * @param c the <code>Component</code> to draw
0N/A * @param p the intermediate <code>Container</code>
0N/A * @param r the <code>Rectangle</code> to draw in
0N/A *
0N/A * @see #paintComponent(Graphics,Component,Container,int,int,int,int)
0N/A * @see CellRendererPane
0N/A */
0N/A public static void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
0N/A paintComponent(g, c, p, r.x, r.y, r.width, r.height);
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Ensures that cell renderer <code>c</code> has a
0N/A * <code>ComponentShell</code> parent and that
0N/A * the shell's parent is p.
0N/A */
0N/A private static CellRendererPane getCellRendererPane(Component c, Container p) {
0N/A Container shell = c.getParent();
0N/A if (shell instanceof CellRendererPane) {
0N/A if (shell.getParent() != p) {
0N/A p.add(shell);
0N/A }
0N/A } else {
0N/A shell = new CellRendererPane();
0N/A shell.add(c);
0N/A p.add(shell);
0N/A }
0N/A return (CellRendererPane)shell;
0N/A }
0N/A
0N/A /**
0N/A * A simple minded look and feel change: ask each node in the tree
0N/A * to <code>updateUI()</code> -- that is, to initialize its UI property
0N/A * with the current look and feel.
0N/A */
0N/A public static void updateComponentTreeUI(Component c) {
0N/A updateComponentTreeUI0(c);
0N/A c.invalidate();
0N/A c.validate();
0N/A c.repaint();
0N/A }
0N/A
0N/A private static void updateComponentTreeUI0(Component c) {
0N/A if (c instanceof JComponent) {
0N/A JComponent jc = (JComponent) c;
0N/A jc.updateUI();
0N/A JPopupMenu jpm =jc.getComponentPopupMenu();
0N/A if(jpm != null) {
0N/A updateComponentTreeUI(jpm);
0N/A }
0N/A }
0N/A Component[] children = null;
0N/A if (c instanceof JMenu) {
0N/A children = ((JMenu)c).getMenuComponents();
0N/A }
0N/A else if (c instanceof Container) {
0N/A children = ((Container)c).getComponents();
0N/A }
0N/A if (children != null) {
625N/A for (Component child : children) {
625N/A updateComponentTreeUI0(child);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Causes <i>doRun.run()</i> to be executed asynchronously on the
0N/A * AWT event dispatching thread. This will happen after all
0N/A * pending AWT events have been processed. This method should
0N/A * be used when an application thread needs to update the GUI.
0N/A * In the following example the <code>invokeLater</code> call queues
0N/A * the <code>Runnable</code> object <code>doHelloWorld</code>
0N/A * on the event dispatching thread and
0N/A * then prints a message.
0N/A * <pre>
0N/A * Runnable doHelloWorld = new Runnable() {
0N/A * public void run() {
0N/A * System.out.println("Hello World on " + Thread.currentThread());
0N/A * }
0N/A * };
0N/A *
0N/A * SwingUtilities.invokeLater(doHelloWorld);
0N/A * System.out.println("This might well be displayed before the other message.");
0N/A * </pre>
0N/A * If invokeLater is called from the event dispatching thread --
0N/A * for example, from a JButton's ActionListener -- the <i>doRun.run()</i> will
0N/A * still be deferred until all pending events have been processed.
0N/A * Note that if the <i>doRun.run()</i> throws an uncaught exception
0N/A * the event dispatching thread will unwind (not the current thread).
0N/A * <p>
0N/A * Additional documentation and examples for this method can be
0N/A * found in
0N/A * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>,
0N/A * in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * As of 1.3 this method is just a cover for <code>java.awt.EventQueue.invokeLater()</code>.
0N/A * <p>
0N/A * Unlike the rest of Swing, this method can be invoked from any thread.
0N/A *
0N/A * @see #invokeAndWait
0N/A */
0N/A public static void invokeLater(Runnable doRun) {
0N/A EventQueue.invokeLater(doRun);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Causes <code>doRun.run()</code> to be executed synchronously on the
0N/A * AWT event dispatching thread. This call blocks until
0N/A * all pending AWT events have been processed and (then)
0N/A * <code>doRun.run()</code> returns. This method should
0N/A * be used when an application thread needs to update the GUI.
0N/A * It shouldn't be called from the event dispatching thread.
0N/A * Here's an example that creates a new application thread
0N/A * that uses <code>invokeAndWait</code> to print a string from the event
0N/A * dispatching thread and then, when that's finished, print
0N/A * a string from the application thread.
0N/A * <pre>
0N/A * final Runnable doHelloWorld = new Runnable() {
0N/A * public void run() {
0N/A * System.out.println("Hello World on " + Thread.currentThread());
0N/A * }
0N/A * };
0N/A *
0N/A * Thread appThread = new Thread() {
0N/A * public void run() {
0N/A * try {
0N/A * SwingUtilities.invokeAndWait(doHelloWorld);
0N/A * }
0N/A * catch (Exception e) {
0N/A * e.printStackTrace();
0N/A * }
0N/A * System.out.println("Finished on " + Thread.currentThread());
0N/A * }
0N/A * };
0N/A * appThread.start();
0N/A * </pre>
0N/A * Note that if the <code>Runnable.run</code> method throws an
0N/A * uncaught exception
0N/A * (on the event dispatching thread) it's caught and rethrown, as
0N/A * an <code>InvocationTargetException</code>, on the caller's thread.
0N/A * <p>
0N/A * Additional documentation and examples for this method can be
0N/A * found in
0N/A * <A HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>,
0N/A * in <em>The Java Tutorial</em>.
0N/A * <p>
0N/A * As of 1.3 this method is just a cover for
0N/A * <code>java.awt.EventQueue.invokeAndWait()</code>.
0N/A *
0N/A * @exception InterruptedException if we're interrupted while waiting for
0N/A * the event dispatching thread to finish excecuting
0N/A * <code>doRun.run()</code>
0N/A * @exception InvocationTargetException if an exception is thrown
0N/A * while running <code>doRun</code>
0N/A *
0N/A * @see #invokeLater
0N/A */
0N/A public static void invokeAndWait(final Runnable doRun)
0N/A throws InterruptedException, InvocationTargetException
0N/A {
0N/A EventQueue.invokeAndWait(doRun);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current thread is an AWT event dispatching thread.
0N/A * <p>
0N/A * As of 1.3 this method is just a cover for
0N/A * <code>java.awt.EventQueue.isDispatchThread()</code>.
0N/A *
0N/A * @return true if the current thread is an AWT event dispatching thread
0N/A */
0N/A public static boolean isEventDispatchThread()
0N/A {
0N/A return EventQueue.isDispatchThread();
0N/A }
0N/A
0N/A
0N/A /*
0N/A * --- Accessibility Support ---
0N/A *
0N/A */
0N/A
0N/A /**
0N/A * Get the index of this object in its accessible parent.<p>
0N/A *
0N/A * Note: as of the Java 2 platform v1.3, it is recommended that developers call
0N/A * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
0N/A * of using this method.
0N/A *
0N/A * @return -1 of this object does not have an accessible parent.
0N/A * Otherwise, the index of the child in its accessible parent.
0N/A */
0N/A public static int getAccessibleIndexInParent(Component c) {
0N/A return c.getAccessibleContext().getAccessibleIndexInParent();
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Accessible</code> child contained at the
0N/A * local coordinate <code>Point</code>, if one exists.
0N/A * Otherwise returns <code>null</code>.
0N/A *
0N/A * @return the <code>Accessible</code> at the specified location,
0N/A * if it exists; otherwise <code>null</code>
0N/A */
0N/A public static Accessible getAccessibleAt(Component c, Point p) {
0N/A if (c instanceof Container) {
0N/A return c.getAccessibleContext().getAccessibleComponent().getAccessibleAt(p);
0N/A } else if (c instanceof Accessible) {
0N/A Accessible a = (Accessible) c;
0N/A if (a != null) {
0N/A AccessibleContext ac = a.getAccessibleContext();
0N/A if (ac != null) {
0N/A AccessibleComponent acmp;
0N/A Point location;
0N/A int nchildren = ac.getAccessibleChildrenCount();
0N/A for (int i=0; i < nchildren; i++) {
0N/A a = ac.getAccessibleChild(i);
0N/A if ((a != null)) {
0N/A ac = a.getAccessibleContext();
0N/A if (ac != null) {
0N/A acmp = ac.getAccessibleComponent();
0N/A if ((acmp != null) && (acmp.isShowing())) {
0N/A location = acmp.getLocation();
0N/A Point np = new Point(p.x-location.x,
0N/A p.y-location.y);
0N/A if (acmp.contains(np)){
0N/A return a;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return (Accessible) c;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Get the state of this object. <p>
0N/A *
0N/A * Note: as of the Java 2 platform v1.3, it is recommended that developers call
0N/A * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
0N/A * of using this method.
0N/A *
0N/A * @return an instance of AccessibleStateSet containing the current state
0N/A * set of the object
0N/A * @see AccessibleState
0N/A */
0N/A public static AccessibleStateSet getAccessibleStateSet(Component c) {
0N/A return c.getAccessibleContext().getAccessibleStateSet();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object. If all
0N/A * of the children of this object implement Accessible, than this
0N/A * method should return the number of children of this object. <p>
0N/A *
0N/A * Note: as of the Java 2 platform v1.3, it is recommended that developers call
0N/A * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
0N/A * of using this method.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public static int getAccessibleChildrenCount(Component c) {
0N/A return c.getAccessibleContext().getAccessibleChildrenCount();
0N/A }
0N/A
0N/A /**
0N/A * Return the nth Accessible child of the object. <p>
0N/A *
0N/A * Note: as of the Java 2 platform v1.3, it is recommended that developers call
0N/A * Component.AccessibleAWTComponent.getAccessibleIndexInParent() instead
0N/A * of using this method.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public static Accessible getAccessibleChild(Component c, int i) {
0N/A return c.getAccessibleContext().getAccessibleChild(i);
0N/A }
0N/A
0N/A /**
0N/A * Return the child <code>Component</code> of the specified
0N/A * <code>Component</code> that is the focus owner, if any.
0N/A *
0N/A * @param c the root of the <code>Component</code> hierarchy to
0N/A * search for the focus owner
0N/A * @return the focus owner, or <code>null</code> if there is no focus
0N/A * owner, or if the focus owner is not <code>comp</code>, or a
0N/A * descendant of <code>comp</code>
0N/A *
0N/A * @see java.awt.KeyboardFocusManager#getFocusOwner
0N/A * @deprecated As of 1.4, replaced by
0N/A * <code>KeyboardFocusManager.getFocusOwner()</code>.
0N/A */
0N/A @Deprecated
0N/A public static Component findFocusOwner(Component c) {
0N/A Component focusOwner = KeyboardFocusManager.
0N/A getCurrentKeyboardFocusManager().getFocusOwner();
0N/A
0N/A // verify focusOwner is a descendant of c
0N/A for (Component temp = focusOwner; temp != null;
0N/A temp = (temp instanceof Window) ? null : temp.getParent())
0N/A {
0N/A if (temp == c) {
0N/A return focusOwner;
0N/A }
0N/A }
0N/A
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * If c is a JRootPane descendant return its JRootPane ancestor.
0N/A * If c is a RootPaneContainer then return its JRootPane.
0N/A * @return the JRootPane for Component c or {@code null}.
0N/A */
0N/A public static JRootPane getRootPane(Component c) {
0N/A if (c instanceof RootPaneContainer) {
0N/A return ((RootPaneContainer)c).getRootPane();
0N/A }
0N/A for( ; c != null; c = c.getParent()) {
0N/A if (c instanceof JRootPane) {
0N/A return (JRootPane)c;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the root component for the current component tree.
0N/A * @return the first ancestor of c that's a Window or the last Applet ancestor
0N/A */
0N/A public static Component getRoot(Component c) {
0N/A Component applet = null;
0N/A for(Component p = c; p != null; p = p.getParent()) {
0N/A if (p instanceof Window) {
0N/A return p;
0N/A }
0N/A if (p instanceof Applet) {
0N/A applet = p;
0N/A }
0N/A }
0N/A return applet;
0N/A }
0N/A
3555N/A static JComponent getPaintingOrigin(JComponent c) {
3555N/A Container p = c;
3555N/A while ((p = p.getParent()) instanceof JComponent) {
3555N/A JComponent jp = (JComponent) p;
3555N/A if (jp.isPaintingOrigin()) {
3555N/A return jp;
3555N/A }
3555N/A }
3555N/A return null;
3555N/A }
3555N/A
0N/A /**
0N/A * Process the key bindings for the <code>Component</code> associated with
0N/A * <code>event</code>. This method is only useful if
0N/A * <code>event.getComponent()</code> does not descend from
0N/A * <code>JComponent</code>, or your are not invoking
0N/A * <code>super.processKeyEvent</code> from within your
0N/A * <code>JComponent</code> subclass. <code>JComponent</code>
0N/A * automatically processes bindings from within its
0N/A * <code>processKeyEvent</code> method, hence you rarely need
0N/A * to directly invoke this method.
0N/A *
0N/A * @param event KeyEvent used to identify which bindings to process, as
0N/A * well as which Component has focus.
0N/A * @return true if a binding has found and processed
0N/A * @since 1.4
0N/A */
0N/A public static boolean processKeyBindings(KeyEvent event) {
0N/A if (event != null) {
0N/A if (event.isConsumed()) {
0N/A return false;
0N/A }
0N/A
0N/A Component component = event.getComponent();
0N/A boolean pressed = (event.getID() == KeyEvent.KEY_PRESSED);
0N/A
0N/A if (!isValidKeyEventForKeyBindings(event)) {
0N/A return false;
0N/A }
0N/A // Find the first JComponent in the ancestor hierarchy, and
0N/A // invoke processKeyBindings on it
0N/A while (component != null) {
0N/A if (component instanceof JComponent) {
0N/A return ((JComponent)component).processKeyBindings(
0N/A event, pressed);
0N/A }
0N/A if ((component instanceof Applet) ||
0N/A (component instanceof Window)) {
0N/A // No JComponents, if Window or Applet parent, process
0N/A // WHEN_IN_FOCUSED_WINDOW bindings.
0N/A return JComponent.processKeyBindingsForAllComponents(
0N/A event, (Container)component, pressed);
0N/A }
0N/A component = component.getParent();
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the <code>e</code> is a valid KeyEvent to use in
0N/A * processing the key bindings associated with JComponents.
0N/A */
0N/A static boolean isValidKeyEventForKeyBindings(KeyEvent e) {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Invokes <code>actionPerformed</code> on <code>action</code> if
0N/A * <code>action</code> is enabled (and non-{@code null}). The command for the
0N/A * ActionEvent is determined by:
0N/A * <ol>
0N/A * <li>If the action was registered via
0N/A * <code>registerKeyboardAction</code>, then the command string
0N/A * passed in ({@code null} will be used if {@code null} was passed in).
0N/A * <li>Action value with name Action.ACTION_COMMAND_KEY, unless {@code null}.
0N/A * <li>String value of the KeyEvent, unless <code>getKeyChar</code>
0N/A * returns KeyEvent.CHAR_UNDEFINED..
0N/A * </ol>
0N/A * This will return true if <code>action</code> is non-{@code null} and
0N/A * actionPerformed is invoked on it.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static boolean notifyAction(Action action, KeyStroke ks,
0N/A KeyEvent event, Object sender,
0N/A int modifiers) {
0N/A if (action == null) {
0N/A return false;
0N/A }
0N/A if (action instanceof UIAction) {
0N/A if (!((UIAction)action).isEnabled(sender)) {
0N/A return false;
0N/A }
0N/A }
0N/A else if (!action.isEnabled()) {
0N/A return false;
0N/A }
0N/A Object commandO;
0N/A boolean stayNull;
0N/A
0N/A // Get the command object.
0N/A commandO = action.getValue(Action.ACTION_COMMAND_KEY);
0N/A if (commandO == null && (action instanceof JComponent.ActionStandin)) {
0N/A // ActionStandin is used for historical reasons to support
0N/A // registerKeyboardAction with a null value.
0N/A stayNull = true;
0N/A }
0N/A else {
0N/A stayNull = false;
0N/A }
0N/A
0N/A // Convert it to a string.
0N/A String command;
0N/A
0N/A if (commandO != null) {
0N/A command = commandO.toString();
0N/A }
0N/A else if (!stayNull && event.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
0N/A command = String.valueOf(event.getKeyChar());
0N/A }
0N/A else {
0N/A // Do null for undefined chars, or if registerKeyboardAction
0N/A // was called with a null.
0N/A command = null;
0N/A }
0N/A action.actionPerformed(new ActionEvent(sender,
0N/A ActionEvent.ACTION_PERFORMED, command, event.getWhen(),
0N/A modifiers));
0N/A return true;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Convenience method to change the UI InputMap for <code>component</code>
0N/A * to <code>uiInputMap</code>. If <code>uiInputMap</code> is {@code null},
0N/A * this removes any previously installed UI InputMap.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static void replaceUIInputMap(JComponent component, int type,
0N/A InputMap uiInputMap) {
0N/A InputMap map = component.getInputMap(type, (uiInputMap != null));
0N/A
0N/A while (map != null) {
0N/A InputMap parent = map.getParent();
0N/A if (parent == null || (parent instanceof UIResource)) {
0N/A map.setParent(uiInputMap);
0N/A return;
0N/A }
0N/A map = parent;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Convenience method to change the UI ActionMap for <code>component</code>
0N/A * to <code>uiActionMap</code>. If <code>uiActionMap</code> is {@code null},
0N/A * this removes any previously installed UI ActionMap.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static void replaceUIActionMap(JComponent component,
0N/A ActionMap uiActionMap) {
625N/A ActionMap map = component.getActionMap((uiActionMap != null));
0N/A
0N/A while (map != null) {
0N/A ActionMap parent = map.getParent();
0N/A if (parent == null || (parent instanceof UIResource)) {
0N/A map.setParent(uiActionMap);
0N/A return;
0N/A }
0N/A map = parent;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the InputMap provided by the UI for condition
0N/A * <code>condition</code> in component <code>component</code>.
0N/A * <p>This will return {@code null} if the UI has not installed a InputMap
0N/A * of the specified type.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static InputMap getUIInputMap(JComponent component, int condition) {
0N/A InputMap map = component.getInputMap(condition, false);
0N/A while (map != null) {
0N/A InputMap parent = map.getParent();
0N/A if (parent instanceof UIResource) {
0N/A return parent;
0N/A }
0N/A map = parent;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ActionMap provided by the UI
0N/A * in component <code>component</code>.
0N/A * <p>This will return {@code null} if the UI has not installed an ActionMap.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static ActionMap getUIActionMap(JComponent component) {
0N/A ActionMap map = component.getActionMap(false);
0N/A while (map != null) {
0N/A ActionMap parent = map.getParent();
0N/A if (parent instanceof UIResource) {
0N/A return parent;
0N/A }
0N/A map = parent;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A
0N/A // Don't use String, as it's not guaranteed to be unique in a Hashtable.
0N/A private static final Object sharedOwnerFrameKey =
0N/A new StringBuffer("SwingUtilities.sharedOwnerFrame");
0N/A
0N/A static class SharedOwnerFrame extends Frame implements WindowListener {
0N/A public void addNotify() {
0N/A super.addNotify();
0N/A installListeners();
0N/A }
0N/A
0N/A /**
0N/A * Install window listeners on owned windows to watch for displayability changes
0N/A */
0N/A void installListeners() {
0N/A Window[] windows = getOwnedWindows();
625N/A for (Window window : windows) {
0N/A if (window != null) {
0N/A window.removeWindowListener(this);
0N/A window.addWindowListener(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Watches for displayability changes and disposes shared instance if there are no
0N/A * displayable children left.
0N/A */
0N/A public void windowClosed(WindowEvent e) {
0N/A synchronized(getTreeLock()) {
0N/A Window[] windows = getOwnedWindows();
625N/A for (Window window : windows) {
0N/A if (window != null) {
0N/A if (window.isDisplayable()) {
0N/A return;
0N/A }
0N/A window.removeWindowListener(this);
0N/A }
0N/A }
0N/A dispose();
0N/A }
0N/A }
0N/A public void windowOpened(WindowEvent e) {
0N/A }
0N/A public void windowClosing(WindowEvent e) {
0N/A }
0N/A public void windowIconified(WindowEvent e) {
0N/A }
0N/A public void windowDeiconified(WindowEvent e) {
0N/A }
0N/A public void windowActivated(WindowEvent e) {
0N/A }
0N/A public void windowDeactivated(WindowEvent e) {
0N/A }
0N/A
0N/A public void show() {
0N/A // This frame can never be shown
0N/A }
0N/A public void dispose() {
0N/A try {
0N/A getToolkit().getSystemEventQueue();
0N/A super.dispose();
0N/A } catch (Exception e) {
0N/A // untrusted code not allowed to dispose
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a toolkit-private, shared, invisible Frame
0N/A * to be the owner for JDialogs and JWindows created with
0N/A * {@code null} owners.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A static Frame getSharedOwnerFrame() throws HeadlessException {
0N/A Frame sharedOwnerFrame =
0N/A (Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey);
0N/A if (sharedOwnerFrame == null) {
0N/A sharedOwnerFrame = new SharedOwnerFrame();
0N/A SwingUtilities.appContextPut(sharedOwnerFrameKey,
0N/A sharedOwnerFrame);
0N/A }
0N/A return sharedOwnerFrame;
0N/A }
0N/A
0N/A /**
0N/A * Returns a SharedOwnerFrame's shutdown listener to dispose the SharedOwnerFrame
0N/A * if it has no more displayable children.
0N/A * @exception HeadlessException if GraphicsEnvironment.isHeadless()
0N/A * returns true.
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A */
0N/A static WindowListener getSharedOwnerFrameShutdownListener() throws HeadlessException {
0N/A Frame sharedOwnerFrame = getSharedOwnerFrame();
0N/A return (WindowListener)sharedOwnerFrame;
0N/A }
0N/A
0N/A /* Don't make these AppContext accessors public or protected --
0N/A * since AppContext is in sun.awt in 1.2, we shouldn't expose it
0N/A * even indirectly with a public API.
0N/A */
0N/A // REMIND(aim): phase out use of 4 methods below since they
0N/A // are just private covers for AWT methods (?)
0N/A
0N/A static Object appContextGet(Object key) {
0N/A return AppContext.getAppContext().get(key);
0N/A }
0N/A
0N/A static void appContextPut(Object key, Object value) {
0N/A AppContext.getAppContext().put(key, value);
0N/A }
0N/A
0N/A static void appContextRemove(Object key) {
0N/A AppContext.getAppContext().remove(key);
0N/A }
0N/A
0N/A
625N/A static Class<?> loadSystemClass(String className) throws ClassNotFoundException {
0N/A return Class.forName(className, true, Thread.currentThread().
0N/A getContextClassLoader());
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Convenience function for determining ComponentOrientation. Helps us
0N/A * avoid having Munge directives throughout the code.
0N/A */
0N/A static boolean isLeftToRight( Component c ) {
0N/A return c.getComponentOrientation().isLeftToRight();
0N/A }
0N/A private SwingUtilities() {
0N/A throw new Error("SwingUtilities is just a container for static methods");
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the Icon <code>icon</code> is an instance of
0N/A * ImageIcon, and the image it contains is the same as <code>image</code>.
0N/A */
0N/A static boolean doesIconReferenceImage(Icon icon, Image image) {
0N/A Image iconImage = (icon != null && (icon instanceof ImageIcon)) ?
0N/A ((ImageIcon)icon).getImage() : null;
0N/A return (iconImage == image);
0N/A }
0N/A
0N/A /**
0N/A * Returns index of the first occurrence of <code>mnemonic</code>
0N/A * within string <code>text</code>. Matching algorithm is not
0N/A * case-sensitive.
0N/A *
0N/A * @param text The text to search through, may be {@code null}
0N/A * @param mnemonic The mnemonic to find the character for.
0N/A * @return index into the string if exists, otherwise -1
0N/A */
0N/A static int findDisplayedMnemonicIndex(String text, int mnemonic) {
0N/A if (text == null || mnemonic == '\0') {
0N/A return -1;
0N/A }
0N/A
0N/A char uc = Character.toUpperCase((char)mnemonic);
0N/A char lc = Character.toLowerCase((char)mnemonic);
0N/A
0N/A int uci = text.indexOf(uc);
0N/A int lci = text.indexOf(lc);
0N/A
0N/A if (uci == -1) {
0N/A return lci;
0N/A } else if(lci == -1) {
0N/A return uci;
0N/A } else {
0N/A return (lci < uci) ? lci : uci;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Stores the position and size of
0N/A * the inner painting area of the specified component
0N/A * in <code>r</code> and returns <code>r</code>.
0N/A * The position and size specify the bounds of the component,
0N/A * adjusted so as not to include the border area (the insets).
0N/A * This method is useful for classes
0N/A * that implement painting code.
0N/A *
0N/A * @param c the JComponent in question; if {@code null}, this method returns {@code null}
0N/A * @param r the Rectangle instance to be modified;
0N/A * may be {@code null}
0N/A * @return {@code null} if the Component is {@code null};
0N/A * otherwise, returns the passed-in rectangle (if non-{@code null})
0N/A * or a new rectangle specifying position and size information
0N/A *
0N/A * @since 1.4
0N/A */
0N/A public static Rectangle calculateInnerArea(JComponent c, Rectangle r) {
0N/A if (c == null) {
0N/A return null;
0N/A }
0N/A Rectangle rect = r;
0N/A Insets insets = c.getInsets();
0N/A
0N/A if (rect == null) {
0N/A rect = new Rectangle();
0N/A }
0N/A
0N/A rect.x = insets.left;
0N/A rect.y = insets.top;
0N/A rect.width = c.getWidth() - insets.left - insets.right;
0N/A rect.height = c.getHeight() - insets.top - insets.bottom;
0N/A
0N/A return rect;
0N/A }
0N/A
0N/A static void updateRendererOrEditorUI(Object rendererOrEditor) {
0N/A if (rendererOrEditor == null) {
0N/A return;
0N/A }
0N/A
0N/A Component component = null;
0N/A
0N/A if (rendererOrEditor instanceof Component) {
0N/A component = (Component)rendererOrEditor;
0N/A }
0N/A if (rendererOrEditor instanceof DefaultCellEditor) {
0N/A component = ((DefaultCellEditor)rendererOrEditor).getComponent();
0N/A }
0N/A
0N/A if (component != null) {
0N/A SwingUtilities.updateComponentTreeUI(component);
0N/A }
0N/A }
1912N/A
1912N/A /**
2333N/A * Returns the first ancestor of the {@code component}
1912N/A * which is not an instance of {@link JLayer}.
2333N/A *
2333N/A * @param component {@code Component} to get
2333N/A * the first ancestor of, which is not a {@link JLayer} instance.
2333N/A *
2333N/A * @return the first ancestor of the {@code component}
2333N/A * which is not an instance of {@link JLayer}.
2333N/A * If such an ancestor can not be found, {@code null} is returned.
2333N/A *
1912N/A * @throws NullPointerException if {@code component} is {@code null}
2333N/A * @see JLayer
1912N/A *
1912N/A * @since 1.7
1912N/A */
2333N/A public static Container getUnwrappedParent(Component component) {
2333N/A Container parent = component.getParent();
2333N/A while(parent instanceof JLayer) {
2333N/A parent = parent.getParent();
2333N/A }
2333N/A return parent;
1912N/A }
1912N/A
1912N/A /**
1912N/A * Returns the first {@code JViewport}'s descendant
2333N/A * which is not an instance of {@code JLayer}.
2333N/A * If such a descendant can not be found, {@code null} is returned.
1912N/A *
1912N/A * If the {@code viewport}'s view component is not a {@code JLayer},
2333N/A * this method is equivalent to {@link JViewport#getView()}
2333N/A * otherwise {@link JLayer#getView()} will be recursively
2333N/A * called on all descending {@code JLayer}s.
2333N/A *
2333N/A * @param viewport {@code JViewport} to get the first descendant of,
2333N/A * which in not a {@code JLayer} instance.
1912N/A *
1912N/A * @return the first {@code JViewport}'s descendant
2333N/A * which is not an instance of {@code JLayer}.
2333N/A * If such a descendant can not be found, {@code null} is returned.
1912N/A *
1912N/A * @throws NullPointerException if {@code viewport} is {@code null}
1912N/A * @see JViewport#getView()
1912N/A * @see JLayer
2333N/A *
2333N/A * @since 1.7
1912N/A */
2333N/A public static Component getUnwrappedView(JViewport viewport) {
1912N/A Component view = viewport.getView();
1912N/A while (view instanceof JLayer) {
1912N/A view = ((JLayer)view).getView();
1912N/A }
1912N/A return view;
1912N/A }
1918N/A
1918N/A /**
1895N/A * Retrieves the validate root of a given container.
1895N/A *
1895N/A * If the container is contained within a {@code CellRendererPane}, this
1895N/A * method returns {@code null} due to the synthetic nature of the {@code
1895N/A * CellRendererPane}.
1895N/A * <p>
1895N/A * The component hierarchy must be displayable up to the toplevel component
1895N/A * (either a {@code Frame} or an {@code Applet} object.) Otherwise this
1895N/A * method returns {@code null}.
1895N/A * <p>
1895N/A * If the {@code visibleOnly} argument is {@code true}, the found validate
1895N/A * root and all its parents up to the toplevel component must also be
1895N/A * visible. Otherwise this method returns {@code null}.
1895N/A *
1895N/A * @return the validate root of the given container or null
1895N/A * @see java.awt.Component#isDisplayable()
1895N/A * @see java.awt.Component#isVisible()
1895N/A * @since 1.7
1895N/A */
1895N/A static Container getValidateRoot(Container c, boolean visibleOnly) {
1895N/A Container root = null;
1895N/A
1895N/A for (; c != null; c = c.getParent())
1895N/A {
1895N/A if (!c.isDisplayable() || c instanceof CellRendererPane) {
1895N/A return null;
1895N/A }
1895N/A if (c.isValidateRoot()) {
1895N/A root = c;
1895N/A break;
1895N/A }
1895N/A }
1895N/A
1895N/A if (root == null) {
1895N/A return null;
1895N/A }
1895N/A
1895N/A for (; c != null; c = c.getParent()) {
1895N/A if (!c.isDisplayable() || (visibleOnly && !c.isVisible())) {
1895N/A return null;
1895N/A }
1895N/A if (c instanceof Window || c instanceof Applet) {
1895N/A return root;
1895N/A }
1895N/A }
1895N/A
1895N/A return null;
1895N/A }
0N/A}