下面列出了怎么用javax.swing.plaf.synth.SynthStyle的API类实例代码及写法,或者点击链接到github查看源代码。
public Font getFont() {
SynthStyle style = getSynthStyle (aRegion);
if (Boolean.TRUE.equals(functioning)) {
try {
Font result = (Font) synthStyle_getFontForState.invoke (style,
new Object [] {
getSynthContext ()
});
if (result == null) {
result = (Font) fallback;
}
return result;
} catch (Exception e) {
functioning = Boolean.FALSE;
if (log) {
e.printStackTrace();
}
}
}
//This will only happen once, after which functioning will be false
return null;
}
public Color getColor () {
SynthStyle style = getSynthStyle (aRegion);
if (Boolean.TRUE.equals(functioning)) {
try {
Color result = (Color) synthStyle_getColorForState.invoke (style,
new Object [] {
getSynthContext (),
aColorType
});
if (result == null) {
result = (Color) fallback;
}
if (darken) {
result = result.darker();
}
return result;
} catch (Exception e) {
functioning = Boolean.FALSE;
if (log) {
e.printStackTrace();
}
}
}
//This will only happen once, after which functioning will be false
return null;
}
/**
* Called by UIManager when this look and feel is installed.
*/
@Override
public void initialize() {
super.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
SynthStyle style = getSeaGlassStyle(c, r);
if (!(style instanceof SeaGlassStyle)) {
style = new SeaGlassStyleWrapper(style);
}
return style;
}
});
}
/**
* A convience method that will reset the Style of StyleContext if
* necessary.
*
* @return newStyle
*/
public static SynthStyle updateSeaglassStyle(SynthContext context, SeaglassUI ui) {
SynthStyle newStyle = getStyle(context.getComponent(), context.getRegion());
// TODO rossi 04.07.2011 this code is now private in the Synth L&F
// SynthStyle oldStyle = context.getStyle();
//
// if (newStyle != oldStyle) {
// if (oldStyle != null) {
// oldStyle.uninstallDefaults(context);
// }
// context.setStyle(newStyle);
// newStyle.installDefaults(context, ui);
// }
return newStyle;
}
/**
* A convience method that will reset the Style of StyleContext if
* necessary.
*
* @param context the SynthContext corresponding to the current state.
* @param ui the UI delegate.
*
* @return the new, updated style.
*/
public static SynthStyle updateStyle(SeaGlassContext context, SeaglassUI ui) {
SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(), context.getRegion());
SynthStyle oldStyle = context.getStyle();
if (newStyle != oldStyle) {
if (oldStyle != null) {
oldStyle.uninstallDefaults(context);
}
context.setStyle(newStyle);
if (newStyle instanceof SeaGlassStyle) {
((SeaGlassStyle) newStyle).installDefaults(context, ui);
}
}
return newStyle;
}
private void updateStyle(JTextComponent comp) {
SeaGlassContext context = getContext(comp, ENABLED);
SynthStyle oldStyle = style;
style = SeaGlassLookAndFeel.updateStyle(context, this);
if (style != oldStyle) {
SeaGlassTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
if (oldStyle != null) {
uninstallKeyboardActions();
installKeyboardActions();
}
}
context.dispose();
}
private void updateStyle(JComponent c) {
SeaGlassContext context = getContext(c, ENABLED);
// Note: JViewport is special cased as it does not allow for
// a border to be set. JViewport.setBorder is overriden to throw
// an IllegalArgumentException. Refer to SynthScrollPaneUI for
// details of this.
SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(), context.getRegion());
SynthStyle oldStyle = context.getStyle();
if (newStyle != oldStyle) {
if (oldStyle != null) {
oldStyle.uninstallDefaults(context);
}
context.setStyle(newStyle);
newStyle.installDefaults(context);
}
this.style = newStyle;
context.dispose();
}
/**
* @see javax.swing.plaf.basic.BasicButtonUI#getMinimumSize(javax.swing.JComponent)
*/
public Dimension getMinimumSize(JComponent c) {
if (c.getComponentCount() > 0 && c.getLayout() != null) {
return null;
}
AbstractButton b = (AbstractButton) c;
SeaGlassContext ss = getContext(c);
final SynthStyle style2 = ss.getStyle();
Dimension size = style2.getGraphicsUtils(ss).getMinimumSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b),
b.getHorizontalAlignment(), b.getVerticalAlignment(),
b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
b.getIconTextGap(), b.getDisplayedMnemonicIndex());
ss.dispose();
return size;
}
/**
* @see javax.swing.plaf.basic.BasicButtonUI#getPreferredSize(javax.swing.JComponent)
*/
public Dimension getPreferredSize(JComponent c) {
if (c.getComponentCount() > 0 && c.getLayout() != null) {
return null;
}
AbstractButton b = (AbstractButton) c;
SeaGlassContext ss = getContext(c);
SynthStyle style2 = ss.getStyle();
SynthGraphicsUtils graphicsUtils = style2.getGraphicsUtils(ss);
Dimension size = graphicsUtils.getPreferredSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b),
b.getHorizontalAlignment(),
b.getVerticalAlignment(), b.getHorizontalTextPosition(),
b.getVerticalTextPosition(), b.getIconTextGap(),
b.getDisplayedMnemonicIndex());
ss.dispose();
// Make height odd.
size.height &= ~1;
return size;
}
/**
* @see javax.swing.plaf.basic.BasicButtonUI#getMaximumSize(javax.swing.JComponent)
*/
public Dimension getMaximumSize(JComponent c) {
if (c.getComponentCount() > 0 && c.getLayout() != null) {
return null;
}
AbstractButton b = (AbstractButton) c;
SeaGlassContext ss = getContext(c);
final SynthStyle style2 = ss.getStyle();
Dimension size = style2.getGraphicsUtils(ss).getMaximumSize(ss, style2.getFont(ss), b.getText(), getSizingIcon(b),
b.getHorizontalAlignment(), b.getVerticalAlignment(),
b.getHorizontalTextPosition(), b.getVerticalTextPosition(),
b.getIconTextGap(), b.getDisplayedMnemonicIndex());
ss.dispose();
return size;
}
private void updateStyle(JToolBar c) {
SeaGlassContext context = getContext(c, Region.TOOL_BAR_CONTENT, null, ENABLED);
contentStyle = SeaGlassLookAndFeel.updateStyle(context, this);
context.getComponent().setOpaque(false);
context.dispose();
context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, null, ENABLED);
context.getComponent().setOpaque(false);
dragWindowStyle = SeaGlassLookAndFeel.updateStyle(context, this);
context.dispose();
context = getContext(c, ENABLED);
context.getComponent().setOpaque(false);
SynthStyle oldStyle = style;
style = SeaGlassLookAndFeel.updateStyle(context, this);
if (oldStyle != style) {
handleIcon = style.getIcon(context, "ToolBar.handleIcon");
if (oldStyle != null) {
uninstallKeyboardActions();
installKeyboardActions();
}
}
context.dispose();
}
private void updateStyle(JComponent c) {
SeaGlassContext context = getContext(c, ENABLED);
Window window = SwingUtilities.getWindowAncestor(popupMenu);
if (PlatformUtils.isMac() && window != null) {
WindowUtils.makeWindowNonOpaque(window);
}
SeaGlassStyle oldStyle = style;
SynthStyle s = SeaGlassLookAndFeel.updateStyle(context, this);
if (s instanceof SeaGlassStyle) {
style = (SeaGlassStyle) s;
if (style != oldStyle) {
if (oldStyle != null) {
uninstallKeyboardActions();
installKeyboardActions();
}
}
}
context.dispose();
}
private void updateStyle(JScrollPane c) {
SeaGlassContext context = getContext(c, ENABLED);
SynthStyle oldStyle = style;
style = SeaGlassLookAndFeel.updateStyle(context, this);
if (style != oldStyle) {
Border vpBorder = scrollpane.getViewportBorder();
if ((vpBorder == null) || (vpBorder instanceof UIResource)) {
scrollpane.setViewportBorder(new ViewportBorder(context));
}
if (oldStyle != null) {
uninstallKeyboardActions(c);
installKeyboardActions(c);
}
}
context.dispose();
}
private void updateStyle(JComponent c) {
SeaGlassContext context = getContext(c, ENABLED);
SynthStyle oldStyle = style;
style = SeaGlassLookAndFeel.updateStyle(context, this);
if (style != oldStyle) {
Icon frameIcon = frame.getFrameIcon();
if (frameIcon == null || frameIcon instanceof UIResource) {
frame.setFrameIcon(context.getStyle().getIcon(context, "InternalFrame.icon"));
}
if (oldStyle != null) {
uninstallKeyboardActions();
installKeyboardActions();
}
}
context.dispose();
}
public void propertyChange(PropertyChangeEvent evt) {
SynthStyle oldStyle = style;
JInternalFrame f = (JInternalFrame) evt.getSource();
String prop = evt.getPropertyName();
if (SeaGlassLookAndFeel.shouldUpdateStyle(evt)) {
updateStyle(f);
}
if (style == oldStyle && (prop == JInternalFrame.IS_MAXIMUM_PROPERTY || prop == JInternalFrame.IS_SELECTED_PROPERTY)) {
// Border (and other defaults) may need to change
SeaGlassContext context = getContext(f, ENABLED);
style.uninstallDefaults(context);
((SeaGlassStyle) style).installDefaults(context, this);
}
}
/**
* Create a SynthContext for the component, subregion, and state.
*
* @param c the component.
* @param subregion the subregion.
* @param state the state.
*
* @return the newly created SynthContext.
*/
private SeaGlassContext getContext(JComponent c, Region subregion, int state) {
SynthStyle style = null;
Class klass = SeaGlassContext.class;
if (subregion == Region.TABBED_PANE_TAB) {
style = tabStyle;
} else if (subregion == Region.TABBED_PANE_TAB_AREA) {
style = tabAreaStyle;
} else if (subregion == Region.TABBED_PANE_CONTENT) {
style = tabContentStyle;
} else if (subregion == SeaGlassRegion.TABBED_PANE_TAB_CLOSE_BUTTON) {
style = tabCloseStyle;
}
return SeaGlassContext.getContext(klass, c, subregion, style, state);
}
private void updateStyle(JComboBox comboBox) {
SeaGlassStyle oldStyle = style;
SeaGlassContext context = getContext(comboBox, ENABLED);
SynthStyle s = SeaGlassLookAndFeel.updateStyle(context, this);
if (s instanceof SeaGlassStyle) {
style = (SeaGlassStyle) s;
if (style != oldStyle) {
popupInsets = (Insets) style.get(context, "ComboBox.popupInsets");
useListColors = style.getBoolean(context, "ComboBox.rendererUseListColors", true);
buttonWhenNotEditable = style.getBoolean(context, "ComboBox.buttonWhenNotEditable", false);
pressedWhenPopupVisible = style.getBoolean(context, "ComboBox.pressedWhenPopupVisible", false);
if (oldStyle != null) {
uninstallKeyboardActions();
installKeyboardActions();
}
forceOpaque = style.getBoolean(context, "ComboBox.forceOpaque", false);
}
}
context.dispose();
}
/**
* Creates a SeaGlassContext with the specified values. This is meant for
* subclasses and custom UI implementors. You very rarely need to construct
* a SeaGlassContext, though some methods will take one.
*
* @param component JComponent
* @param region Identifies the portion of the JComponent
* @param style Style associated with the component
* @param state State of the component as defined in SynthConstants.
*/
public SeaGlassContext(JComponent component, Region region, SynthStyle style, int state) {
super(component, region, style, state);
if (component == fakeComponent) {
this.component = null;
this.region = null;
this.style = null;
return;
}
if (component == null || region == null || style == null) {
throw new NullPointerException("You must supply a non-null component, region and style");
}
reset(component, region, style, state);
}
public static void paint(SynthContext context, SynthContext accContext, Graphics g, Icon checkIcon, Icon arrowIcon,
String acceleratorDelimiter, int defaultTextIconGap, String propertyPrefix) {
JMenuItem mi = (JMenuItem) context.getComponent();
SynthStyle style = context.getStyle();
g.setFont(style.getFont(context));
Rectangle viewRect = new Rectangle(0, 0, mi.getWidth(), mi.getHeight());
boolean leftToRight = SeaGlassLookAndFeel.isLeftToRight(mi);
applyInsets(viewRect, mi.getInsets(), leftToRight);
SeaGlassMenuItemLayoutHelper lh = new SeaGlassMenuItemLayoutHelper(context, accContext, mi, checkIcon, arrowIcon, viewRect,
defaultTextIconGap, acceleratorDelimiter, leftToRight, MenuItemLayoutHelper.useCheckAndArrow(mi), propertyPrefix);
MenuItemLayoutHelper.LayoutResult lr = lh.layoutMenuItem();
paintMenuItem(g, lh, lr);
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/**
* <p>Locate the style associated with the given region, and component.
* This is called from NimbusLookAndFeel in the SynthStyleFactory
* implementation.</p>
*
* <p>Lookup occurs as follows:<br/>
* Check the map of styles <code>m</code>. If the map contains no styles at
* all, then simply return the defaultStyle. If the map contains styles,
* then iterate over all of the styles for the Region <code>r</code> looking
* for the best match, based on prefix. If a match was made, then return
* that SynthStyle. Otherwise, return the defaultStyle.</p>
*
* @param comp The component associated with this region. For example, if
* the Region is Region.Button then the component will be a JButton.
* If the Region is a subregion, such as ScrollBarThumb, then the
* associated component will be the component that subregion belongs
* to, such as JScrollBar. The JComponent may be named. It may not be
* null.
* @param r The region we are looking for a style for. May not be null.
*/
SynthStyle getStyle(JComponent comp, Region r) {
//validate method arguments
if (comp == null || r == null) {
throw new IllegalArgumentException(
"Neither comp nor r may be null");
}
//if there are no lazy styles registered for the region r, then return
//the default style
List<LazyStyle> styles = m.get(r);
if (styles == null || styles.size() == 0) {
return defaultStyle;
}
//Look for the best SynthStyle for this component/region pair.
LazyStyle foundStyle = null;
for (LazyStyle s : styles) {
if (s.matches(comp)) {
//replace the foundStyle if foundStyle is null, or
//if the new style "s" is more specific (ie, its path was
//longer), or if the foundStyle was "simple" and the new style
//was not (ie: the foundStyle was for something like Button and
//the new style was for something like "MyButton", hence, being
//more specific.) In all cases, favor the most specific style
//found.
if (foundStyle == null ||
(foundStyle.parts.length < s.parts.length) ||
(foundStyle.parts.length == s.parts.length
&& foundStyle.simple && !s.simple)) {
foundStyle = s;
}
}
}
//return the style, if found, or the default style if not found
return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp, r);
}
/**
* Gets the style. Creates it if necessary.
* @return the style
*/
SynthStyle getStyle(JComponent c, Region r) {
// if the component has overrides, it gets its own unique style
// instead of the shared style.
if (c.getClientProperty("Nimbus.Overrides") != null) {
Map<Region, SynthStyle> map = overridesCache.get(c);
SynthStyle s = null;
if (map == null) {
map = new HashMap<Region, SynthStyle>();
overridesCache.put(c, map);
} else {
s = map.get(r);
}
if (s == null) {
s = new NimbusStyle(prefix, c);
map.put(r, s);
}
return s;
}
// lazily create the style if necessary
if (style == null)
style = new NimbusStyle(prefix, null);
// return the style
return style;
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/** Called by UIManager when this look and feel is installed. */
@Override public void initialize() {
super.initialize();
defaults.initialize();
// create synth style factory
setStyleFactory(new SynthStyleFactory() {
@Override
public SynthStyle getStyle(JComponent c, Region r) {
return defaults.getStyle(c, r);
}
});
}
/**
* <p>Locate the style associated with the given region, and component.
* This is called from NimbusLookAndFeel in the SynthStyleFactory
* implementation.</p>
*
* <p>Lookup occurs as follows:<br/>
* Check the map of styles <code>m</code>. If the map contains no styles at
* all, then simply return the defaultStyle. If the map contains styles,
* then iterate over all of the styles for the Region <code>r</code> looking
* for the best match, based on prefix. If a match was made, then return
* that SynthStyle. Otherwise, return the defaultStyle.</p>
*
* @param comp The component associated with this region. For example, if
* the Region is Region.Button then the component will be a JButton.
* If the Region is a subregion, such as ScrollBarThumb, then the
* associated component will be the component that subregion belongs
* to, such as JScrollBar. The JComponent may be named. It may not be
* null.
* @param r The region we are looking for a style for. May not be null.
*/
SynthStyle getStyle(JComponent comp, Region r) {
//validate method arguments
if (comp == null || r == null) {
throw new IllegalArgumentException(
"Neither comp nor r may be null");
}
//if there are no lazy styles registered for the region r, then return
//the default style
List<LazyStyle> styles = m.get(r);
if (styles == null || styles.size() == 0) {
return defaultStyle;
}
//Look for the best SynthStyle for this component/region pair.
LazyStyle foundStyle = null;
for (LazyStyle s : styles) {
if (s.matches(comp)) {
//replace the foundStyle if foundStyle is null, or
//if the new style "s" is more specific (ie, its path was
//longer), or if the foundStyle was "simple" and the new style
//was not (ie: the foundStyle was for something like Button and
//the new style was for something like "MyButton", hence, being
//more specific.) In all cases, favor the most specific style
//found.
if (foundStyle == null ||
(foundStyle.parts.length < s.parts.length) ||
(foundStyle.parts.length == s.parts.length
&& foundStyle.simple && !s.simple)) {
foundStyle = s;
}
}
}
//return the style, if found, or the default style if not found
return foundStyle == null ? defaultStyle : foundStyle.getStyle(comp, r);
}
/**
* Gets the style. Creates it if necessary.
* @return the style
*/
SynthStyle getStyle(JComponent c, Region r) {
// if the component has overrides, it gets its own unique style
// instead of the shared style.
if (c.getClientProperty("Nimbus.Overrides") != null) {
Map<Region, SynthStyle> map = overridesCache.get(c);
SynthStyle s = null;
if (map == null) {
map = new HashMap<Region, SynthStyle>();
overridesCache.put(c, map);
} else {
s = map.get(r);
}
if (s == null) {
s = new NimbusStyle(prefix, c);
map.put(r, s);
}
return s;
}
// lazily create the style if necessary
if (style == null)
style = new NimbusStyle(prefix, null);
// return the style
return style;
}
private static void patchOptionPaneIcons(final UIDefaults defaults) {
if (UIUtil.isUnderGTKLookAndFeel() && defaults.get(ourOptionPaneIconKeys[0]) == null) {
// GTK+ L&F keeps icons hidden in style
final SynthStyle style = SynthLookAndFeel.getStyle(new JOptionPane(""), Region.DESKTOP_ICON);
if (style != null) {
for (final String key : ourOptionPaneIconKeys) {
final Object icon = style.get(null, key);
if (icon != null) defaults.put(key, icon);
}
}
}
}
/** Paint bumps to specific Graphics. */
@Override
public void paint (Graphics g) {
Icon icon = UIManager.getIcon("ToolBar.handleIcon");
Region region = Region.TOOL_BAR;
SynthStyleFactory sf = SynthLookAndFeel.getStyleFactory();
SynthStyle style = sf.getStyle(toolbar, region);
SynthContext context = new SynthContext(toolbar, region, style, SynthConstants.DEFAULT);
SynthGraphicsUtils sgu = context.getStyle().getGraphicsUtils(context);
sgu.paintText(context, g, null, icon, SwingConstants.LEADING, SwingConstants.LEADING, 0, 0, 0, -1, 0);
}