📄 baseline.java
字号:
/*
* Copyright (C) 2005-2006 Sun Microsystems, Inc. All rights reserved. Use is
* subject to license terms.
*/
package org.jdesktop.layout;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.swing.border.*;
import javax.swing.plaf.metal.MetalLookAndFeel;
/**
* Convenience class that can be used to determine the baseline of a
* particular component. The static method <code>getBaseline</code> uses the
* following algorithm to determine the baseline:
* <ol>
* <li>If the component has a <code>getBaseline(JComponent,int,int)</code>
* method, invoke it.
* <li>If there is a <code>UIManager</code> property of the name
* <code>Baseline.instance</code>, forward the call to that Baseline.
* <li>Otherwise use the built in support.
* </ol>
* <p>
* In addition to determining the baseline, this class also allows for
* determining how the baseline changes as the size of the component changes.
* The method getBaselineResizeBehavior can be used for this. This will return
* one of BRB_OTHER, BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT or
* BRB_CENTER_OFFSET. The following algorithm is used in determining the
* baseline resize behavior.
* <ol>
* <li>If the Component defines the method
* getBaselineResizeBehaviorInt, the return value from that method is used.
* <li>If running on 1.6, the Component method getBaselineResizeBehavior is
* invoked and the return value converted to one of the constants defined
* by this class.
* <li>If the component is one of the known Swing components,the baseline resize
* behavior is calculated and returned.
* <li>Otherwise, BRB_OTHER is returned.
* </ol>
* <p>
* This class is primarily useful for JREs prior to 1.6. In 1.6 API for this
* was added directly to Component. When run on 1.6 or newer, this class calls
* into the appropriate Component methods.
*
* @version $Revision: 1.1 $
*/
public class Baseline {
static final int BRB_NONE = 0;
/**
* Baseline resize behavior constant. Indicates as the size of the component
* changes the baseline remains a fixed distance from the top of the
* component.
*/
public static final int BRB_CONSTANT_ASCENT = 1;
/**
* Baseline resize behavior constant. Indicates as the size of the component
* changes the baseline remains a fixed distance from the bottom of the
* component.
*/
public static final int BRB_CONSTANT_DESCENT = 2;
/**
* Baseline resize behavior constant. Indicates as the size of the component
* changes the baseline remains a fixed distance from the center of the
* component.
*/
public static final int BRB_CENTER_OFFSET = 3;
/**
* Baseline resize behavior constant. Indicates as the size of the component
* changes the baseline can not be determined using one of the other
* constants.
*/
public static final int BRB_OTHER = 4;
//
// Used by button and label baseline code, cached to avoid excessive
// garbage.
//
private static final Rectangle viewRect = new Rectangle();
private static final Rectangle textRect = new Rectangle();
private static final Rectangle iconRect = new Rectangle();
//
// These come from TitleBorder. NOTE that these are NOT final in
// TitledBorder
//
private static final int EDGE_SPACING = 2;
private static final int TEXT_SPACING = 2;
private static final Insets EMPTY_INSETS = new Insets(0, 0, 0, 0);
// Prototype label for calculating baseline of tables.
private static JLabel TABLE_LABEL;
// Prototype label for calculating baseline of lists.
private static JLabel LIST_LABEL;
// Prototype label for calculating baseline of trees.
private static JLabel TREE_LABEL;
// Corresponds to com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
private static Class CLASSIC_WINDOWS;
// Whether or not we've tried to load WindowsClassicLookAndFeel.
private static boolean checkedForClassic;
// Corresponds to com.sun.java.swing.plaf.windows.WindowsLookAndFeel
private static Class WINDOWS_CLASS;
// Whether we've tried to load WindowsLookAndFeel
private static boolean checkedForWindows;
// Whether or not we are running in a sandbox. This is used to determine
// how we should decide if we're using ocean.
private static boolean inSandbox;
// If in the sandbox, this is set after we've determine if using ocean.
private static boolean checkedForOcean;
// Whether or not using ocean. This is only used if inSandbox.
private static boolean usingOcean;
// Map<Class,Method>
private static final Map BASELINE_MAP = Collections.
synchronizedMap(new HashMap(1));
// Map<Class,Method> Method is getBaselineResizeBehaviorAsInt
private static final Map BRB_I_MAP = Collections.
synchronizedMap(new HashMap(1));
private static final Method COMPONENT_BASELINE_METHOD;
private static final Method COMPONENT_BRB_METHOD;
private static final Object ENUM_BRB_CENTER_OFFSET;
private static final Object ENUM_BRB_CONSTANT_ASCENT;
private static final Object ENUM_BRB_CONSTANT_DESCENT;
private static final Object ENUM_BRB_OTHER;
// Temporary JList: used to determine baseline resize behavior for
// comboboxs.
private static JList brbList;
// Temporary ListCellRenderer: used to determine baseline resize behavior
// for comboboxs.
private static ListCellRenderer brbListCellRenderer;
static {
Method componentBaselineMethod = null;
Method componentBRBMethod = null;
Method componentBRBIMethod = null;
Object brbCenterOffset = null;
Object brbConstantAscent = null;
Object brbConstantDescent = null;
Object brbOther = null;
try {
componentBaselineMethod = Component.class.getMethod(
"getBaseline", new Class[] { int.class, int.class});
componentBRBMethod = Component.class.getMethod(
"getBaselineResizeBehavior", new Class[] { });
Class brbClass = Class.forName("java.awt.Component$BaselineResizeBehavior");
brbCenterOffset = getFieldValue(brbClass, "CENTER_OFFSET");
brbConstantAscent = getFieldValue(brbClass, "CONSTANT_ASCENT");
brbConstantDescent = getFieldValue(brbClass, "CONSTANT_DESCENT");
brbOther = getFieldValue(brbClass, "OTHER");
} catch (NoSuchMethodException nsme) {
} catch (ClassNotFoundException cnfe) {
} catch (NoSuchFieldException nsfe) {
} catch (IllegalAccessException iae) {
}
if (componentBaselineMethod == null ||
componentBRBMethod == null ||
brbCenterOffset == null ||
brbConstantDescent == null ||
brbConstantAscent == null ||
brbOther == null) {
componentBaselineMethod = componentBRBMethod = null;
brbCenterOffset = brbConstantAscent = brbConstantDescent =
brbOther = null;
}
COMPONENT_BASELINE_METHOD = componentBaselineMethod;
COMPONENT_BRB_METHOD = componentBRBMethod;
ENUM_BRB_CENTER_OFFSET = brbCenterOffset;
ENUM_BRB_CONSTANT_ASCENT = brbConstantAscent;
ENUM_BRB_CONSTANT_DESCENT = brbConstantDescent;
ENUM_BRB_OTHER = brbOther;
}
private static Object getFieldValue(Class type, String name) throws IllegalAccessException, NoSuchFieldException {
return type.getField(name).get(null);
}
static int getBaselineResizeBehavior(Component c) {
if (c instanceof JComponent) {
return getBaselineResizeBehavior((JComponent)c);
}
return BRB_OTHER;
}
/**
* Returns a constant indicating how the baseline varies with the size
* of the component.
*
* @param c the JComponent to get the baseline resize behavior for
* @return one of BRB_CONSTANT_ASCENT, BRB_CONSTANT_DESCENT,
* BRB_CENTER_OFFSET or BRB_OTHER
*/
public static int getBaselineResizeBehavior(JComponent c) {
Method brbIMethod = getBRBIMethod(c);
if (brbIMethod != null) {
return invokeBRBIMethod(brbIMethod, c);
}
if (COMPONENT_BRB_METHOD != null) {
return getBaselineResizeBehaviorUsingMustang(c);
}
String uid = c.getUIClassID();
if (uid == "ButtonUI" || uid == "CheckBoxUI" ||
uid == "RadioButtonUI" || uid == "ToggleButtonUI") {
return getButtonBaselineResizeBehavior((AbstractButton)c);
}
else if (uid == "ComboBoxUI") {
return getComboBoxBaselineResizeBehavior((JComboBox)c);
}
else if (uid == "TextAreaUI") {
return getTextAreaBaselineResizeBehavior((JTextArea)c);
}
else if (uid == "TextFieldUI" ||
uid == "FormattedTextFieldUI" ||
uid == "PasswordFieldUI") {
return getSingleLineTextBaselineResizeBehavior((JTextField)c);
}
else if (uid == "LabelUI") {
return getLabelBaselineResizeBehavior((JLabel)c);
}
else if (uid == "ListUI") {
return getListBaselineResizeBehavior((JList)c);
}
else if (uid == "PanelUI") {
return getPanelBaselineResizeBehavior((JPanel)c);
}
else if (uid == "ProgressBarUI") {
return getProgressBarBaselineResizeBehavior((JProgressBar)c);
}
else if (uid == "SliderUI") {
return getSliderBaselineResizeBehavior((JSlider)c);
}
else if (uid == "SpinnerUI") {
return getSpinnerBaselineResizeBehavior((JSpinner)c);
}
else if (uid == "ScrollPaneUI") {
return getScrollPaneBaselineBaselineResizeBehavior((JScrollPane)c);
}
else if (uid == "TabbedPaneUI") {
return getTabbedPaneBaselineResizeBehavior((JTabbedPane)c);
}
else if (uid == "TableUI") {
return getTableBaselineResizeBehavior((JTable)c);
}
else if (uid == "TreeUI") {
return getTreeBaselineResizeBehavior((JTree)c);
}
return BRB_OTHER;
}
private static int getBaselineResizeBehaviorUsingMustang(JComponent c) {
try {
Object result = COMPONENT_BRB_METHOD.invoke(c, null);
if (result == ENUM_BRB_CENTER_OFFSET) {
return BRB_CENTER_OFFSET;
} else if (result == ENUM_BRB_CONSTANT_ASCENT) {
return BRB_CONSTANT_ASCENT;
} else if (result == ENUM_BRB_CONSTANT_DESCENT) {
return BRB_CONSTANT_DESCENT;
}
} catch (IllegalAccessException iae) {
assert false;
} catch (IllegalArgumentException iae2) {
assert false;
} catch (InvocationTargetException ite) {
assert false;
}
return BRB_OTHER;
}
private static Method getBRBIMethod(Component component) {
Class klass = component.getClass();
while (klass != null) {
if (BRB_I_MAP.containsKey(klass)) {
Method method = (Method)BRB_I_MAP.get(klass);
return method;
}
klass = klass.getSuperclass();
}
klass = component.getClass();
Method[] methods = klass.getMethods();
for (int i = methods.length - 1; i >= 0; i--) {
Method method = methods[i];
if ("getBaselineResizeBehaviorInt".equals(method.getName())) {
Class[] params = method.getParameterTypes();
if (params.length == 0) {
BRB_I_MAP.put(klass, method);
return method;
}
}
}
BRB_I_MAP.put(klass, null);
return null;
}
private static int invokeBRBIMethod(Method method, Component c) {
int brb = BRB_OTHER;
try {
brb = ((Integer)method.invoke(c, null)).intValue();
} catch (IllegalAccessException iae) {
} catch (IllegalArgumentException iae2) {
} catch (InvocationTargetException ite2) {
}
return brb;
}
private static int getTreeBaselineResizeBehavior(JTree tree) {
return BRB_CONSTANT_ASCENT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -