📄 swingutilities.java
字号:
/*
* @(#)SwingUtilities.java 1.80 99/04/22
*
* Copyright 1997-1999 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package javax.swing;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import java.lang.reflect.*;
import javax.accessibility.*;
import javax.swing.text.View;
/**
* A collection of utility methods for Swing.
*
* @version 1.80 04/22/99
* @author unknown
*/
public class SwingUtilities implements SwingConstants
{
// These states are system-wide, rather than AppContext wide.
private static boolean canAccessEventQueue = false;
private static boolean eventQueueTested = false;
/**
* Return true if <code>a</code> contains <code>b</code>
*/
public static final boolean isRectangleContainingRectangle(Rectangle a,Rectangle b) {
if (b.x >= a.x && (b.x + b.width) <= (a.x + a.width) &&
b.y >= a.y && (b.y + b.height) <= (a.y + a.height)) {
return true;
}
return false;
}
/**
* Return the rectangle (0,0,bounds.width,bounds.height) for the component <code>aComponent</code>
*/
public static Rectangle getLocalBounds(Component aComponent) {
Rectangle b = new Rectangle(aComponent.getBounds());
b.x = b.y = 0;
return b;
}
/**
* @return the first Window ancestor of c
*/
private static Window getWindowAncestor(Component c) {
for(Container p = c.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
/**
* Convert a <code>aPoint</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>aPoint</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>aPoint</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>aPoint</code>
* without any conversion.
*/
public static Point convertPoint(Component source,Point aPoint,Component destination) {
Point p;
if(source == null && destination == null)
return aPoint;
if(source == null) {
source = getWindowAncestor(destination);
if(source == null)
throw new Error("Source component not connected to component tree hierarchy");
}
p = new Point(aPoint);
convertPointToScreen(p,source);
if(destination == null) {
destination = getWindowAncestor(source);
if(destination == null)
throw new Error("Destination component not connected to component tree hierarchy");
}
convertPointFromScreen(p,destination);
return p;
}
/**
* Convert the point <code>(x,y)</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>(x,y)</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>(x,y)</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>(x,y)</code>
* without any conversion.
*/
public static Point convertPoint(Component source,int x, int y,Component destination) {
Point point = new Point(x,y);
return convertPoint(source,point,destination);
}
/**
* Convert the rectangle <code>aRectangle</code> in <code>source</code> coordinate system to
* <code>destination</code> coordinate system.
* If <code>source></code>is null,<code>aRectangle</code> is assumed to be in <code>destination</code>'s
* root component coordinate system.
* If <code>destination</code>is null, <code>aRectangle</code> will be converted to <code>source</code>'s
* root component coordinate system.
* If both <code>source</code> and <code>destination</code> are null, return <code>aRectangle</code>
* without any conversion.
*/
public static Rectangle convertRectangle(Component source,Rectangle aRectangle,Component destination) {
Point point = new Point(aRectangle.x,aRectangle.y);
point = convertPoint(source,point,destination);
return new Rectangle(point.x,point.y,aRectangle.width,aRectangle.height);
}
/**
* Convenience method for searching above <code>comp</code> in the
* component hierarchy and returns the first object of class <code>c</code> it
* finds. Can return null, if a class <code>c</code> cannot be found.
*/
public static Container getAncestorOfClass(Class c, Component comp) {
if(comp == null || c == null)
return null;
Container parent = comp.getParent();
while(parent != null && !(c.isInstance(parent)))
parent = parent.getParent();
return parent;
}
/**
* Convenience method for searching above <code>comp</code> in the
* component hierarchy and returns the first object of <code>name</code> it
* finds. Can return null, if <code>name</code> cannot be found.
*/
public static Container getAncestorNamed(String name, Component comp) {
if(comp == null || name == null)
return null;
Container parent = comp.getParent();
while(parent != null && !(name.equals(parent.getName())))
parent = parent.getParent();
return parent;
}
/**
* Returns the deepest visible descendent Component of <code>parent</code>
* that contains the location <code>x</code>, <code>y</code>.
* If <code>parent</code> does not contain the specified location,
* then <code>null</code> is returned. If <code>parent</code> is not a
* container, or none of <code>parent</code>'s visible descendents
* contain the specified location, <code>parent</code> is returned.
*
* @param parent the root component to begin the search
* @param x the x target location
* @param y the y target location
*/
public static Component getDeepestComponentAt(Component parent, int x, int y) {
if (!parent.contains(x, y)) {
return null;
}
if (parent instanceof Container) {
Component components[] = ((Container)parent).getComponents();
for (int i = 0 ; i < components.length ; i++) {
Component comp = components[i];
if (comp != null && comp.isVisible()) {
Point loc = comp.getLocation();
if (comp instanceof Container) {
comp = getDeepestComponentAt(comp, x - loc.x, y - loc.y);
} else {
comp = comp.getComponentAt(x - loc.x, y - loc.y);
}
if (comp != null && comp.isVisible()) {
return comp;
}
}
}
}
return parent;
}
/**
* Returns a MouseEvent similar to <code>sourceEvent</code> except that its x
* and y members have been converted to <code>destination</code>'s coordinate
* system. If <code>source</code> is null, <code>sourceEvent</code> x and y members
* are assumed to be into <code>destination<code>'s root component coordinate system.
* If <code>destination</code> is <code>null</code>, the
* returned MouseEvent will be in <code>source</code>'s coordinate system.
* <code>sourceEvent</code> will not be changed. A new event is returned.
* the <code>source</code> field of the returned event will be set
* to <code>destination</code> if destination is non null
* use the translateMouseEvent() method to translate a mouse event from
* one component to another without changing the source.
*/
public static MouseEvent convertMouseEvent(Component source,
MouseEvent sourceEvent,
Component destination) {
Point p = convertPoint(source,new Point(sourceEvent.getX(),
sourceEvent.getY()),
destination);
Component newSource;
if(destination != null)
newSource = destination;
else
newSource = source;
return new MouseEvent(newSource,
sourceEvent.getID(),
sourceEvent.getWhen(),
sourceEvent.getModifiers(),
p.x,p.y,
sourceEvent.getClickCount(),
sourceEvent.isPopupTrigger());
}
/**
* Convert a point from a component's coordinate system to
* screen coordinates.
*
* @param p a Point object (converted to the new coordinate system)
* @param c a Component object
*/
public static void convertPointToScreen(Point p,Component c) {
Rectangle b;
int x,y;
do {
if(c instanceof JComponent) {
x = ((JComponent)c).getX();
y = ((JComponent)c).getY();
} else if(c instanceof java.applet.Applet) {
Point pp = c.getLocationOnScreen();
x = pp.x;
y = pp.y;
} else {
b = c.getBounds();
x = b.x;
y = b.y;
}
p.x += x;
p.y += y;
if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
break;
c = c.getParent();
} while(c != null);
}
/**
* Convert a point from a screen coordinates to a component's
* coordinate system
*
* @param p a Point object (converted to the new coordinate system)
* @param c a Component object
*/
public static void convertPointFromScreen(Point p,Component c) {
Rectangle b;
int x,y;
do {
if(c instanceof JComponent) {
x = ((JComponent)c).getX();
y = ((JComponent)c).getY();
} else if(c instanceof java.applet.Applet) {
Point pp = c.getLocationOnScreen();
x = pp.x;
y = pp.y;
} else {
b = c.getBounds();
x = b.x;
y = b.y;
}
p.x -= x;
p.y -= y;
if(c instanceof java.awt.Window || c instanceof java.applet.Applet)
break;
c = c.getParent();
} while(c != null);
}
/** Return <code>aComponent</code>'s window **/
public static Window windowForComponent(Component aComponent) {
for (Container p = aComponent.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
return (Window)p;
}
}
return null;
}
/**
* Return <code>true</code> if a component <code>a</code> descends from a component <code>b</code>
*/
public static boolean isDescendingFrom(Component a,Component b) {
if(a == b)
return true;
for(Container p = a.getParent();p!=null;p=p.getParent())
if(p == b)
return true;
return false;
}
/**
* Convenience to calculate an intersection of two rectangles without allocating a new rectangle
* Return dest.
*/
public static Rectangle computeIntersection(int x,int y,int width,int height,Rectangle dest) {
int x1 = (x > dest.x) ? x : dest.x;
int x2 = ((x+width) < (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
int y1 = (y > dest.y) ? y : dest.y;
int y2 = ((y + height) < (dest.y + dest.height) ? (y+height) : (dest.y + dest.height));
dest.x = x1;
dest.y = y1;
dest.width = x2 - x1;
dest.height = y2 - y1;
// If rectangles don't intersect, return zero'd intersection.
if (dest.width < 0 || dest.height < 0) {
dest.x = dest.y = dest.width = dest.height = 0;
}
return dest;
}
/**
* Convenience to calculate the union of two rectangles without allocating a new rectangle
* Return dest
*/
public static Rectangle computeUnion(int x,int y,int width,int height,Rectangle dest) {
int x1 = (x < dest.x) ? x : dest.x;
int x2 = ((x+width) > (dest.x + dest.width)) ? (x+width) : (dest.x + dest.width);
int y1 = (y < dest.y) ? y : dest.y;
int y2 = ((y+height) > (dest.y + dest.height)) ? (y+height) : (dest.y + dest.height);
dest.x = x1;
dest.y = y1;
dest.width = (x2 - x1);
dest.height= (y2 - y1);
return dest;
}
/**
* Convenience returning an array of rect representing the regions within
* <code>rectA</code> that do not overlap with <code>rectB</code>. If the
* two Rects do not overlap, returns an empty array
*/
public static Rectangle[] computeDifference(Rectangle rectA,Rectangle rectB) {
if (rectB == null || !rectA.intersects(rectB) || isRectangleContainingRectangle(rectB,rectA)) {
return new Rectangle[0];
}
Rectangle t = new Rectangle();
Rectangle a=null,b=null,c=null,d=null;
Rectangle result[];
int rectCount = 0;
/* rectA contains rectB */
if (isRectangleContainingRectangle(rectA,rectB)) {
t.x = rectA.x; t.y = rectA.y; t.width = rectB.x - rectA.x; t.height = rectA.height;
if(t.width > 0 && t.height > 0) {
a = new Rectangle(t);
rectCount++;
}
t.x = rectB.x; t.y = rectA.y; t.width = rectB.width; t.height = rectB.y - rectA.y;
if(t.width > 0 && t.height > 0) {
b = new Rectangle(t);
rectCount++;
}
t.x = rectB.x; t.y = rectB.y + rectB.height; t.width = rectB.width;
t.height = rectA.y + rectA.height - (rectB.y + rectB.height);
if(t.width > 0 && t.height > 0) {
c = new Rectangle(t);
rectCount++;
}
t.x = rectB.x + rectB.width; t.y = rectA.y; t.width = rectA.x + rectA.width - (rectB.x + rectB.width);
t.height = rectA.height;
if(t.width > 0 && t.height > 0) {
d = new Rectangle(t);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -