下面列出了怎么用javax.swing.plaf.UIResource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected void configureScrollBarColors() {
UIDefaults uidefaults = XToolkit.getUIDefaults();
Color bg = scrollbar.getBackground();
if (bg == null || bg instanceof UIResource) {
scrollbar.setBackground(uidefaults.getColor("ScrollBar.background"));
}
Color fg = scrollbar.getForeground();
if (fg == null || fg instanceof UIResource) {
scrollbar.setForeground(uidefaults.getColor("ScrollBar.foreground"));
}
thumbHighlightColor = uidefaults.getColor("ScrollBar.thumbHighlight");
thumbLightShadowColor = uidefaults.getColor("ScrollBar.thumbShadow");
thumbDarkShadowColor = uidefaults.getColor("ScrollBar.thumbDarkShadow");
thumbColor = uidefaults.getColor("ScrollBar.thumb");
trackColor = uidefaults.getColor("ScrollBar.track");
trackHighlightColor = uidefaults.getColor("ScrollBar.trackHighlight");
}
@Override
public Object createValue( UIDefaults table ) {
Font defaultFont = UIManager.getFont( "defaultFont" );
if( lastDefaultFont != defaultFont ) {
lastDefaultFont = defaultFont;
if( scaleFactor != 1 ) {
// scale font
int newFontSize = Math.round( defaultFont.getSize() * scaleFactor );
font = new FontUIResource( defaultFont.deriveFont( (float) newFontSize ) );
} else {
// make sure that font is a UIResource for LaF switching
font = (defaultFont instanceof UIResource)
? defaultFont
: new FontUIResource( defaultFont );
}
}
return font;
}
/**
* Sets the list properties that have not been explicitly overridden to {@code null}. A property is considered
* overridden if its current value is not a {@code UIResource}.
*
* @see #installDefaults
* @see #uninstallUI
* @see CellRendererPane
*/
protected void uninstallDefaults() {
LookAndFeel.uninstallBorder(list);
if (list.getFont() instanceof UIResource) {
list.setFont(null);
}
if (list.getForeground() instanceof UIResource) {
list.setForeground(null);
}
if (list.getBackground() instanceof UIResource) {
list.setBackground(null);
}
if (list.getSelectionBackground() instanceof UIResource) {
list.setSelectionBackground(null);
}
if (list.getSelectionForeground() instanceof UIResource) {
list.setSelectionForeground(null);
}
if (list.getCellRenderer() instanceof UIResource) {
list.setCellRenderer(null);
}
if (list.getTransferHandler() instanceof UIResource) {
list.setTransferHandler(null);
}
}
private JButton createArrow(final int direction) {
int buttonPad = UIManager.getInt("Spinner.buttonPad");
Insets insets = new Insets(0, buttonPad, 0, buttonPad);
JButton button = ArrowButton.createUpDownArrow(spinner,
getArrowIcon(direction),
getArrowInactiveIcon(direction), direction,
false, true, insets);
Border buttonBorder = UIManager.getBorder("Spinner.arrowButtonBorder");
if (buttonBorder instanceof UIResource) {
// Wrap the border to avoid having the UIResource be replaced by
// the ButtonUI. This is the opposite of using BorderUIResource.
button.setBorder(new CompoundBorder(buttonBorder, null));
} else {
button.setBorder(buttonBorder);
}
button.setInheritsPopupMenu(true);
return button;
}
protected void paintButtonBackground(final Graphics g, final JComponent c) {
Graphics2D g2 = (Graphics2D) g;
AbstractButton b = (AbstractButton) c;
if (shouldDrawBackground(b)) {
int arc = getArc(c);
int width = c.getWidth();
int height = c.getHeight();
Insets margin = b.getMargin();
if (margin instanceof UIResource) margin = new Insets(0, 0, 0, 0);
if (ButtonConstants.isBorderlessVariant(c)) {
paintBorderlessBackground(b, g2, arc, width, height, margin);
} else {
paintDefaultBackground(b, g2, arc, width, height);
}
}
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
g2d.setColor(getNonSelectedTabTitleColor());
if (tabPane.getSelectedIndex() == tabIndex) {
boolean pressed = isPressedAt(tabIndex);
boolean enabled = tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex);
Color textColor = getSelectedTabTitleColor(enabled, pressed);
Color shadowColor = getSelectedTabTitleShadowColor(enabled);
AquaUtils.paintDropShadowText(g2d, tabPane, font, metrics, textRect.x, textRect.y, 0, 1, textColor, shadowColor, title);
return;
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
private Font fontWithRule(final Font font, final FontSizeRule rule) {
if (font == null) return null;
float size = font.getSize2D();
float newSize = rule.adjustFontSize(size);
if (newSize == size) return font;
if (newSize <= 0) {
LOGGER.warning("Font " + font + " would be invisible after applying " + rule + ". Font won't be changed!");
return font;
}
Font withRule = font.deriveFont(newSize);
if (font instanceof UIResource
&& !(withRule instanceof UIResource)) {
withRule = new DarkFontUIResource(withRule);
}
return withRule;
}
static void swapSelectionColors(final String prefix, final JList c, final Object value) {
if (!isComponentValid(c)) return;
final Color bg = c.getSelectionBackground();
final Color fg = c.getSelectionForeground();
if (!(bg instanceof UIResource) || !(fg instanceof UIResource)) return;
if (Boolean.FALSE.equals(value)) {
setSelectionColors(c, "List.selectionInactiveForeground", "List.selectionInactiveBackground");
return;
}
if (Boolean.TRUE.equals(value)) {
setSelectionColors(c, "List.selectionForeground", "List.selectionBackground");
return;
}
}
@Override
public int getIconWidth(SynthContext context) {
if (context == null) {
return width;
}
JComponent c = context.getComponent();
if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
//we only do the -1 hack for UIResource borders, assuming
//that the border is probably going to be our border
if (c.getBorder() instanceof UIResource) {
return c.getWidth() - 1;
} else {
return c.getWidth();
}
} else {
return scale(context, width);
}
}
@Override
public int getIconHeight(SynthContext context) {
if (context == null) {
return height;
}
JComponent c = context.getComponent();
if (c instanceof JToolBar){
JToolBar toolbar = (JToolBar)c;
if (toolbar.getOrientation() == JToolBar.HORIZONTAL) {
//we only do the -1 hack for UIResource borders, assuming
//that the border is probably going to be our border
if (toolbar.getBorder() instanceof UIResource) {
return c.getHeight() - 1;
} else {
return c.getHeight();
}
} else {
return scale(context, width);
}
} else {
return scale(context, height);
}
}
private DerivedColor getDerivedColor(String uin, String parentUin,
float hOffset, float sOffset,
float bOffset, int aOffset,
boolean uiResource) {
DerivedColor color;
if (uiResource) {
color = new DerivedColor.UIResource(parentUin,
hOffset, sOffset, bOffset, aOffset);
} else {
color = new DerivedColor(parentUin, hOffset, sOffset,
bOffset, aOffset);
}
if (derivedColors.containsKey(color)) {
return derivedColors.get(color);
} else {
derivedColors.put(color, color);
color.rederiveColor(); /// move to ARP.decodeColor() ?
colorTree.addColor(uin, color);
return color;
}
}
static void fillRect(final Graphics g, final Component c, final Color color,
final int x, final int y, final int w, final int h) {
if (!(g instanceof Graphics2D)) {
return;
}
final Graphics2D cg = (Graphics2D) g.create();
try {
if (color instanceof UIResource && isWindowTextured(c)
&& color.equals(SystemColor.window)) {
cg.setComposite(AlphaComposite.Src);
cg.setColor(resetAlpha(color));
} else {
cg.setColor(color);
}
cg.fillRect(x, y, w, h);
} finally {
cg.dispose();
}
}
@Override
protected void installDefaults( JRootPane c ) {
super.installDefaults( c );
// Update background color of JFrame or JDialog parent to avoid bad border
// on HiDPI screens when switching from light to dark Laf.
// The background of JFrame is initialized in JFrame.frameInit() and
// the background of JDialog in JDialog.dialogInit(),
// but it was not updated when switching Laf.
Container parent = c.getParent();
if( parent instanceof JFrame || parent instanceof JDialog ) {
Color background = parent.getBackground();
if( background == null || background instanceof UIResource )
parent.setBackground( UIManager.getColor( "control" ) );
}
// enable dark window appearance on macOS when running in JetBrains Runtime
if( SystemInfo.IS_JETBRAINS_JVM && SystemInfo.IS_MAC_OS_10_14_MOJAVE ) {
LookAndFeel laf = UIManager.getLookAndFeel();
boolean isDark = laf instanceof FlatLaf && ((FlatLaf)laf).isDark();
c.putClientProperty( "jetbrains.awt.windowDarkAppearance", isDark );
}
}
@Override
protected void paintText( Graphics g, AbstractButton b, Rectangle textRect, String text ) {
if( isHelpButton( b ) )
return;
if( defaultBoldText && isDefaultButton( b ) && b.getFont() instanceof UIResource ) {
Font boldFont = g.getFont().deriveFont( Font.BOLD );
g.setFont( boldFont );
int boldWidth = b.getFontMetrics( boldFont ).stringWidth( text );
if( boldWidth > textRect.width ) {
textRect.x -= (boldWidth - textRect.width) / 2;
textRect.width = boldWidth;
}
}
paintText( g, b, textRect, text, getForeground( b ) );
}
/**
* Uninstalls any state that this style installed on
* the <code>JComponent</code> from <code>context</code>.
* <p>
* Styles should NOT depend upon this being called, in certain cases
* it may never be called.
*
* @param context SynthContext identifying component to install properties
* to.
*/
public void uninstallDefaults(SynthContext context) {
if (!context.isSubregion()) {
// NOTE: because getForeground, getBackground and getFont will look
// at the parent Container, if we set them to null it may
// mean we they return a non-null and non-UIResource value
// preventing install from correctly settings its colors/font. For
// this reason we do not uninstall the fg/bg/font.
JComponent c = context.getComponent();
Border border = c.getBorder();
if (border instanceof UIResource) {
c.setBorder(null);
}
}
}
protected void paintTitle(final Graphics2D g2d, final Font font, final FontMetrics metrics, final Rectangle textRect, final int tabIndex, final String title) {
final View v = getTextViewForTab(tabIndex);
if (v != null) {
v.paint(g2d, textRect);
return;
}
if (title == null) return;
final Color color = tabPane.getForegroundAt(tabIndex);
if (color instanceof UIResource) {
g2d.setColor(getNonSelectedTabTitleColor());
if (tabPane.getSelectedIndex() == tabIndex) {
boolean pressed = isPressedAt(tabIndex);
boolean enabled = tabPane.isEnabled() && tabPane.isEnabledAt(tabIndex);
Color textColor = getSelectedTabTitleColor(enabled, pressed);
Color shadowColor = getSelectedTabTitleShadowColor(enabled);
AquaUtils.paintDropShadowText(g2d, tabPane, font, metrics, textRect.x, textRect.y, 0, 1, textColor, shadowColor, title);
return;
}
} else {
g2d.setColor(color);
}
g2d.setFont(font);
SwingUtilities2.drawString(tabPane, g2d, title, textRect.x, textRect.y + metrics.getAscent());
}
@Override
public int getIconWidth(SynthContext context) {
if (context == null) {
return width;
}
JComponent c = context.getComponent();
if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
//we only do the -1 hack for UIResource borders, assuming
//that the border is probably going to be our border
if (c.getBorder() instanceof UIResource) {
return c.getWidth() - 1;
} else {
return c.getWidth();
}
} else {
return scale(context, width);
}
}
static void swapSelectionColors(final String prefix, final JList c, final Object value) {
if (!isComponentValid(c)) return;
final Color bg = c.getSelectionBackground();
final Color fg = c.getSelectionForeground();
if (!(bg instanceof UIResource) || !(fg instanceof UIResource)) return;
if (Boolean.FALSE.equals(value)) {
setSelectionColors(c, "List.selectionInactiveForeground", "List.selectionInactiveBackground");
return;
}
if (Boolean.TRUE.equals(value)) {
setSelectionColors(c, "List.selectionForeground", "List.selectionBackground");
return;
}
}
/**
* {@inheritDoc}
*/
@Override
protected void uninstallDefaults() {
SynthContext context = getContext(tree, ENABLED);
style.uninstallDefaults(context);
context.dispose();
style = null;
context = getContext(tree, Region.TREE_CELL, ENABLED);
cellStyle.uninstallDefaults(context);
context.dispose();
cellStyle = null;
if (tree.getTransferHandler() instanceof UIResource) {
tree.setTransferHandler(null);
}
}
static void swapSelectionColors(final String prefix, final JTable c, final Object value) {
if (!isComponentValid(c)) return;
final Color bg = c.getSelectionBackground();
final Color fg = c.getSelectionForeground();
if (!(bg instanceof UIResource) || !(fg instanceof UIResource)) return;
if (Boolean.FALSE.equals(value)) {
setSelectionColors(c, "Table.selectionInactiveForeground", "Table.selectionInactiveBackground");
return;
}
if (Boolean.TRUE.equals(value)) {
setSelectionColors(c, "Table.selectionForeground", "Table.selectionBackground");
return;
}
}
static void fillRect(final Graphics g, final Component c, final Color color,
final int x, final int y, final int w, final int h) {
if (!(g instanceof Graphics2D)) {
return;
}
final Graphics2D cg = (Graphics2D) g.create();
try {
if (color instanceof UIResource && isWindowTextured(c)
&& color.equals(SystemColor.window)) {
cg.setComposite(AlphaComposite.Src);
cg.setColor(resetAlpha(color));
} else {
cg.setColor(color);
}
cg.fillRect(x, y, w, h);
} finally {
cg.dispose();
}
}
@Override
public Insets getBorderInsets( Component c, Insets insets ) {
if( FlatButtonUI.isToolBarButton( c ) ) {
// In toolbars, use button margin only if explicitly set.
// Otherwise use toolbar margin specified in UI defaults.
Insets margin = (c instanceof AbstractButton)
? ((AbstractButton)c).getMargin()
: null;
FlatUIUtils.setInsets( insets, UIScale.scale( FlatUIUtils.addInsets( toolbarSpacingInsets,
(margin != null && !(margin instanceof UIResource)) ? margin : toolbarMargin ) ) );
} else {
insets = super.getBorderInsets( c, insets );
// use smaller left and right insets for icon-only or single-character buttons (so that they are square)
if( FlatButtonUI.isIconOnlyOrSingleCharacterButton( c ) && ((AbstractButton)c).getMargin() instanceof UIResource )
insets.left = insets.right = Math.min( insets.top, insets.bottom );
}
return insets;
}
public static void installForeground(final Component component, final Color color) {
if (component == null || color == null) return;
Color c = component.getForeground();
if (c == null || c instanceof UIResource) {
component.setForeground(color);
}
}
@Override
protected Color getForeground(final AbstractButton button) {
if (button.isSelected()
&& !ToggleButtonConstants.isSlider(button)
&& !ButtonConstants.isBorderlessVariant(button)
&& button.getForeground() instanceof UIResource) {
return selectedForeground;
}
return super.getForeground(button);
}
/**
* Convenience method to change the UI ActionMap for <code>component</code>
* to <code>uiActionMap</code>. If <code>uiActionMap</code> is {@code null},
* this removes any previously installed UI ActionMap.
*
* @since 1.3
*/
public static void replaceUIActionMap(JComponent component,
ActionMap uiActionMap) {
ActionMap map = component.getActionMap((uiActionMap != null));
while (map != null) {
ActionMap parent = map.getParent();
if (parent == null || (parent instanceof UIResource)) {
map.setParent(uiActionMap);
return;
}
map = parent;
}
}
protected Color getTabForeground(final int index, final boolean isSelected) {
if (tabPane.isEnabled() && tabPane.isEnabledAt(index)) {
Color fg = tabPane.getForegroundAt(index);
if (isSelected && (fg instanceof UIResource)) {
Color selectedFG = selectedForeground;
if (selectedFG != null) {
fg = selectedFG;
}
}
return fg;
} else {
return disabledForeground;
}
}
private void configureRenderer(SynthContext context) {
TreeCellRenderer renderer = tree.getCellRenderer();
if (renderer instanceof DefaultTreeCellRenderer) {
DefaultTreeCellRenderer r = (DefaultTreeCellRenderer)renderer;
SynthStyle style = context.getStyle();
context.setComponentState(ENABLED | SELECTED);
Color color = r.getTextSelectionColor();
if (color == null || (color instanceof UIResource)) {
r.setTextSelectionColor(style.getColor(
context, ColorType.TEXT_FOREGROUND));
}
color = r.getBackgroundSelectionColor();
if (color == null || (color instanceof UIResource)) {
r.setBackgroundSelectionColor(style.getColor(
context, ColorType.TEXT_BACKGROUND));
}
context.setComponentState(ENABLED);
color = r.getTextNonSelectionColor();
if (color == null || color instanceof UIResource) {
r.setTextNonSelectionColor(style.getColorForState(
context, ColorType.TEXT_FOREGROUND));
}
color = r.getBackgroundNonSelectionColor();
if (color == null || color instanceof UIResource) {
r.setBackgroundNonSelectionColor(style.getColorForState(
context, ColorType.TEXT_BACKGROUND));
}
}
}
/**
* Update is file list.
*/
protected void updateIsFileList() {
boolean b = PropertyUtil.getBooleanProperty(list, "List.isFileList");
if (b != isFileList) {
isFileList = b;
Font oldFont = list.getFont();
if (oldFont == null || oldFont instanceof UIResource) {
Font newFont = UIManager.getFont(b ? "FileChooser.listFont" : "List.font");
if (newFont != null && newFont != oldFont) {
list.setFont(newFont);
}
}
}
}
/**
* Initializes list properties such as font, foreground, and background, and adds the CellRendererPane. The font,
* foreground, and background properties are only set if their current value is either null or a UIResource, other
* properties are set if the current value is null.
*
* @see #uninstallDefaults
* @see #installUI
* @see CellRendererPane
*/
protected void installDefaults() {
list.setLayout(null);
LookAndFeel.installBorder(list, "List.border");
LookAndFeel.installColorsAndFont(list, "List.background", "List.foreground", "List.font");
LookAndFeel.installProperty(list, PropertyKey.OPAQUE, Boolean.TRUE);
if (list.getCellRenderer() == null) {
@SuppressWarnings("unchecked")
ListCellRenderer<Object> tmp = (ListCellRenderer<Object>) (UIManager.get("List.cellRenderer"));
list.setCellRenderer(tmp);
}
Color sbg = list.getSelectionBackground();
if (sbg == null || sbg instanceof UIResource) {
list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
}
Color sfg = list.getSelectionForeground();
if (sfg == null || sfg instanceof UIResource) {
list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
}
Long l = (Long) UIManager.get("List.timeFactor");
timeFactor = (l != null) ? l : 1000L;
updateIsFileList();
}
@Override
public void focusGained(final FocusEvent e) {
Color bg = table.getSelectionBackground();
if (bg instanceof UIResource) {
table.setSelectionBackground(selectionBackground);
}
table.repaint();
}