📄 swingutilities.java
字号:
else {
return false;
}
}
return eventDispatchThreadClass.isInstance(currentThread);
}
/*
* --- Accessibility Support ---
*
*/
/**
* Get the index of this object in its accessible parent.
*
* @return -1 of this object does not have an accessible parent.
* Otherwise, the index of the child in its accessible parent.
*/
public static int getAccessibleIndexInParent(Component c) {
int index = -1;
Container parent = c.getParent();
if (parent != null && parent instanceof Accessible) {
Component ca[] = parent.getComponents();
for (int i = 0; i < ca.length; i++) {
if (ca[i] instanceof Accessible) {
index++;
}
if (c.equals(ca[i])) {
return index;
}
}
}
return -1;
}
/**
* Returns the Accessible child contained at the local coordinate
* Point, if one exists.
*
* @return the Accessible at the specified location, if it exists
*/
public static Accessible getAccessibleAt(Component c, Point p) {
if (c instanceof Accessible) {
Accessible a = (Accessible) c;
if (a != null) {
AccessibleContext ac = a.getAccessibleContext();
if (ac != null) {
AccessibleComponent acmp;
Point location;
int nchildren = ac.getAccessibleChildrenCount();
for (int i=0; i < nchildren; i++) {
a = ac.getAccessibleChild(i);
if ((a != null)) {
ac = a.getAccessibleContext();
if (ac != null) {
acmp = ac.getAccessibleComponent();
if ((acmp != null) && (acmp.isShowing())) {
location = acmp.getLocation();
Point np = new Point(p.x-location.x,
p.y-location.y);
if (acmp.contains(np)){
return a;
}
}
}
}
}
}
}
return (Accessible) c;
} else {
Component ret = c;
if (!c.contains(p.x,p.y)) {
ret = null;
} else if (c instanceof Container) {
Container cnt = (Container) c;
int ncomponents = cnt.getComponentCount();
for (int i=0; i < ncomponents; i++) {
Component comp = cnt.getComponent(i);
if ((comp != null) && comp.isShowing()) {
Point location = comp.getLocation();
if (comp.contains(p.x-location.x,p.y-location.y)) {
ret = comp;
}
}
}
}
if (ret instanceof Accessible) {
return (Accessible) ret;
}
}
return null;
}
/**
* Get the state of this object.
*
* @return an instance of AccessibleStateSet containing the current state
* set of the object
* @see AccessibleState
*/
public static AccessibleStateSet getAccessibleStateSet(Component c) {
AccessibleStateSet states = new AccessibleStateSet();
if (c.isEnabled()) {
states.add(AccessibleState.ENABLED);
}
if (c.isFocusTraversable()) {
states.add(AccessibleState.FOCUSABLE);
}
if (c.isVisible()) {
states.add(AccessibleState.VISIBLE);
}
if (c.isShowing()) {
states.add(AccessibleState.SHOWING);
}
// [[[FIXME: WDW - for JDK1.2 this code can be replaced with
// c.hasFocus()]]]
for (Container p = c.getParent(); p != null; p = p.getParent()) {
if (p instanceof Window) {
if (((Window)p).getFocusOwner() == c) {
states.add(AccessibleState.FOCUSED);
}
}
}
if (c instanceof Accessible) {
AccessibleContext ac = ((Accessible) c).getAccessibleContext();
if (ac != null) {
Accessible ap = ac.getAccessibleParent();
if (ap != null) {
AccessibleContext pac = ap.getAccessibleContext();
if (pac != null) {
AccessibleSelection as = pac.getAccessibleSelection();
if (as != null) {
states.add(AccessibleState.SELECTABLE);
int i = ac.getAccessibleIndexInParent();
if (i >= 0) {
if (as.isAccessibleChildSelected(i)) {
states.add(AccessibleState.SELECTED);
}
}
}
}
}
}
}
if (c instanceof JComponent) {
if (((JComponent) c).isOpaque()) {
states.add(AccessibleState.OPAQUE);
}
}
return states;
}
/**
* Returns the number of accessible children in the object. If all
* of the children of this object implement Accessible, than this
* method should return the number of children of this object.
*
* @return the number of accessible children in the object.
*/
public static int getAccessibleChildrenCount(Component c) {
int count = 0;
if (c instanceof Container) {
Component[] children = ((Container) c).getComponents();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Accessible) {
count++;
}
}
}
return count;
}
/**
* Return the nth Accessible child of the object.
*
* @param i zero-based index of child
* @return the nth Accessible child of the object
*/
public static Accessible getAccessibleChild(Component c, int i) {
if (c instanceof Container) {
Component[] children = ((Container) c).getComponents();
int count = 0;
for (int j = 0; j < children.length; j++) {
if (children[j] instanceof Accessible) {
if (count == i) {
return (Accessible) children[j];
} else {
count++;
}
}
}
}
return null;
}
/**
* Return the child component which has focus, if any. The HotJava
* SecurityManager forbids applet access to getFocusOwner(), so if the
* component is an applet, we check whether a JComponent has focus.
* Non-Swing components in an applet on HotJava are out-of-luck,
* unfortunately.
*/
public static Component findFocusOwner(Component c) {
if (c instanceof Window) {
return ((Window)c).getFocusOwner();
}
if (c instanceof JComponent && ((JComponent)c).hasFocus()) {
return c;
}
if (c instanceof Container) {
int n = ((Container)c).countComponents();
for (int i = 0; i < n; i++) {
Component focusOwner =
findFocusOwner(((Container)c).getComponent(i));
if (focusOwner != null) {
return focusOwner;
}
}
return null;
} else {
return null; // Component doesn't have hasFocus().
}
}
/**
* If c is a JRootPane descendant return its JRootPane ancestor.
* If c is a RootPaneContainer then return its JRootPane.
* @return the JRootPane for Component c or null.
*/
public static JRootPane getRootPane(Component c) {
if (c instanceof RootPaneContainer) {
return ((RootPaneContainer)c).getRootPane();
}
for( ; c != null; c = c.getParent()) {
if (c instanceof JRootPane) {
return (JRootPane)c;
}
}
return null;
}
/**
* Returns the root component for the current component tree.
* @return the first ancestor of c that's a Window or the last Applet ancestor
*/
public static Component getRoot(Component c) {
Component applet = null;
for(Component p = c; p != null; p = p.getParent()) {
if (p instanceof Window) {
return p;
}
if (p instanceof Applet) {
applet = p;
}
}
return applet;
}
// Don't use String, as it's not guaranteed to be unique in a Hashtable.
private static final Object sharedOwnerFrameKey =
new StringBuffer("SwingUtilities.sharedOwnerFrame");
/**
* Returns a toolkit-private, shared, invisible Frame
* to be the owner for JDialogs and JWindows created with
* null owners.
*/
static Frame getSharedOwnerFrame() {
Frame sharedOwnerFrame =
(Frame)SwingUtilities.appContextGet(sharedOwnerFrameKey);
if (sharedOwnerFrame == null) {
sharedOwnerFrame = new Frame() {
public void show() {
// This frame can never be shown
}
public synchronized void dispose() {
try {
getToolkit().getSystemEventQueue();
super.dispose();
} catch (Exception e) {
// untrusted code not allowed to dispose
}
}
};
SwingUtilities.appContextPut(sharedOwnerFrameKey,
sharedOwnerFrame);
}
return sharedOwnerFrame;
}
// The following static var and methods are a temporary
// workaround for a Solaris bug where disposing modal dialogs
// can sometimes cause a segmentation violation. Once this
// AWT bug is fixed and available in browsers, we can remove
// this workaround.
private static final Object dialogsKey =
new StringBuffer("SwingUtilities.dialogs");
static JDialog getRecycledModalDialog(Frame frame, String title) {
Vector dialogs = (Vector)SwingUtilities.appContextGet(dialogsKey);
if (dialogs == null) {
dialogs = new Vector();
SwingUtilities.appContextPut(dialogsKey, dialogs);
}
JDialog dialog = null;
synchronized(dialogs) {
for(int i = 0; i < dialogs.size(); i++) {
dialog = (JDialog)dialogs.elementAt(i);
if (dialog.getParent() == frame) {
//System.out.println("Found available dialog: "+dialog);
dialogs.removeElement(dialog);
dialog.setTitle(title);
return dialog;
}
}
dialog = new JDialog(frame, title, true);
//System.out.println("Created new dialog: "+dialog);
}
return dialog;
}
static void recycleModalDialog(JDialog dialog) {
Vector dialogs = (Vector)SwingUtilities.appContextGet(dialogsKey);
synchronized(dialogs) {
dialog.getContentPane().removeAll();
dialogs.addElement(dialog);
}
}
/* Don't make these AppContext accessors public or protected --
* since AppContext is in sun.awt in 1.2, we shouldn't expose it
* even indirectly with a public API.
*/
static Hashtable appContextTable = new Hashtable(2);
static Object appContextGet(Object key) {
return appContextTable.get(key);
}
static void appContextPut(Object key, Object value) {
appContextTable.put(key, value);
}
static void appContextRemove(Object key) {
appContextTable.remove(key);
}
static Class loadSystemClass(String className) throws ClassNotFoundException {
return Class.forName(className);
}
final static void doPrivileged(final Runnable doRun) {
doRun.run(); // ... "a doo run run".
}
/*
* Convenience function for determining ComponentOrientation. Helps us
* avoid having Munge directives throughout the code.
*/
static boolean isLeftToRight( Component c ) {
return true;
}
private SwingUtilities() {
throw new Error("SwingUtilities is just a container for static methods");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -