下面列出了怎么用javax.swing.plaf.synth.SynthContext的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*
* <p>Overridden to cause this style to populate itself with data from
* UIDefaults, if necessary.</p>
*
* <p>In addition, NimbusStyle handles ColorTypes slightly differently from
* Synth.</p>
* <ul>
* <li>ColorType.BACKGROUND will equate to the color stored in UIDefaults
* named "background".</li>
* <li>ColorType.TEXT_BACKGROUND will equate to the color stored in
* UIDefaults named "textBackground".</li>
* <li>ColorType.FOREGROUND will equate to the color stored in UIDefaults
* named "textForeground".</li>
* <li>ColorType.TEXT_FOREGROUND will equate to the color stored in
* UIDefaults named "textForeground".</li>
* </ul>
*/
@Override protected Color getColorForState(SynthContext ctx, ColorType type) {
String key = null;
if (type == ColorType.BACKGROUND) {
key = "background";
} else if (type == ColorType.FOREGROUND) {
//map FOREGROUND as TEXT_FOREGROUND
key = "textForeground";
} else if (type == ColorType.TEXT_BACKGROUND) {
key = "textBackground";
} else if (type == ColorType.TEXT_FOREGROUND) {
key = "textForeground";
} else if (type == ColorType.FOCUS) {
key = "focus";
} else if (type != null) {
key = type.toString();
} else {
return DEFAULT_COLOR;
}
Color c = (Color) get(ctx, key);
//if all else fails, return a default color (which is a ColorUIResource)
if (c == null) c = DEFAULT_COLOR;
return c;
}
/**
* Scale a size based on the "JComponent.sizeVariant" client property of the
* component that is using this icon
*
* @param context The synthContext to get the component from
* @param size The size to scale
* @return The scaled size or original if "JComponent.sizeVariant" is not
* set
*/
private int scale(SynthContext context, int size) {
if (context == null || context.getComponent() == null){
return size;
}
// The key "JComponent.sizeVariant" is used to match Apple's LAF
String scaleKey = (String) context.getComponent().getClientProperty(
"JComponent.sizeVariant");
if (scaleKey != null) {
if (NimbusStyle.LARGE_KEY.equals(scaleKey)) {
size *= NimbusStyle.LARGE_SCALE;
} else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) {
size *= NimbusStyle.SMALL_SCALE;
} else if (NimbusStyle.MINI_KEY.equals(scaleKey)) {
// mini is not quite as small for icons as full mini is
// just too tiny
size *= NimbusStyle.MINI_SCALE + 0.07;
}
}
return size;
}
private static SynthContext getSynthContext () {
try {
JButton dummyButton = getDummyButton();
ButtonUI bui = dummyButton.getUI();
if (bui instanceof SynthUI) {
return ((SynthUI) bui).getContext(dummyButton);
} else {
throw new IllegalStateException ("I don't have a SynthButtonUI to play with"); //NOI18N
}
} catch (Exception e) {
functioning = Boolean.FALSE;
if (log) {
e.printStackTrace();
}
return null;
}
}
@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 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);
}
}
/**
* {@inheritDoc}
*
* <p>Overridden to cause this style to populate itself with data from
* UIDefaults, if necessary.</p>
*
* <p>In addition, NimbusStyle handles ColorTypes slightly differently from
* Synth.</p>
* <ul>
* <li>ColorType.BACKGROUND will equate to the color stored in UIDefaults
* named "background".</li>
* <li>ColorType.TEXT_BACKGROUND will equate to the color stored in
* UIDefaults named "textBackground".</li>
* <li>ColorType.FOREGROUND will equate to the color stored in UIDefaults
* named "textForeground".</li>
* <li>ColorType.TEXT_FOREGROUND will equate to the color stored in
* UIDefaults named "textForeground".</li>
* </ul>
*/
@Override protected Color getColorForState(SynthContext ctx, ColorType type) {
String key = null;
if (type == ColorType.BACKGROUND) {
key = "background";
} else if (type == ColorType.FOREGROUND) {
//map FOREGROUND as TEXT_FOREGROUND
key = "textForeground";
} else if (type == ColorType.TEXT_BACKGROUND) {
key = "textBackground";
} else if (type == ColorType.TEXT_FOREGROUND) {
key = "textForeground";
} else if (type == ColorType.FOCUS) {
key = "focus";
} else if (type != null) {
key = type.toString();
} else {
return DEFAULT_COLOR;
}
Color c = (Color) get(ctx, key);
//if all else fails, return a default color (which is a ColorUIResource)
if (c == null) c = DEFAULT_COLOR;
return c;
}
/**
* Paints the border of a text field.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintTextFieldBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
if (context.getComponent().getComponentOrientation().isLeftToRight()){
paintBorder(context, g, x, y, w, h, null);
} else {
AffineTransform transform = new AffineTransform();
transform.translate(x,y);
transform.scale(-1, 1);
transform.translate(-w,0);
paintBorder(context, g, 0, 0, w, h, transform);
}
}
/**
* {@inheritDoc}
*
* Overridden to cause this style to populate itself with data from
* UIDefaults, if necessary.
*/
@Override public void installDefaults(SynthContext ctx) {
validate();
//delegate to the superclass to install defaults such as background,
//foreground, font, and opaque onto the swing component.
super.installDefaults(ctx);
}
@Override
public void paintTabbedPaneContentBorder(SynthContext context, Graphics g, int x, int y, int w, int h) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(BG);
g.drawRoundRect(x, y, w, h, 6, 6);
}
/**
* Paints the background of a formatted text field.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintFormattedTextFieldBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
if (context.getComponent().getComponentOrientation().isLeftToRight()){
paintBackground(context, g, x, y, w, h, null);
} else {
AffineTransform transform = new AffineTransform();
transform.translate(x,y);
transform.scale(-1, 1);
transform.translate(-w,0);
paintBackground(context, g, 0, 0, w, h, transform);
}
}
/**
* Paints the background of an arrow button. Arrow buttons are created by
* some components, such as <code>JScrollBar</code>.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintArrowButtonBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
if (context.getComponent().getComponentOrientation().isLeftToRight()){
paintBackground(context, g, x, y, w, h, null);
} else {
AffineTransform transform = new AffineTransform();
transform.translate(x,y);
transform.scale(-1, 1);
transform.translate(-w,0);
paintBackground(context, g, 0, 0, w, h, transform);
}
}
/**
* {@inheritDoc}
*
* Overridden to cause this style to populate itself with data from
* UIDefaults, if necessary. If opacity is not specified in UI defaults,
* then it defaults to being non-opaque.
*/
@Override public boolean isOpaque(SynthContext ctx) {
// Force Table CellRenderers to be opaque
if ("Table.cellRenderer".equals(ctx.getComponent().getName())) {
return true;
}
Boolean opaque = (Boolean)get(ctx, "opaque");
return opaque == null ? false : opaque;
}
private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w,
int h, AffineTransform transform) {
Painter<Object> borderPainter = style.getBorderPainter(ctx);
if (borderPainter != null) {
paint(borderPainter, ctx, g, x, y, w, h,transform);
}
}
/**
* Returns the icon's height. This is a cover method for <code>
* getIconHeight(null)</code>.
*
* @param context the SynthContext describing the component/region, the
* style, and the state.
*
* @return an int specifying the fixed height of the icon.
*/
@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);
}
}
/**
* Paints the border of a formatted text field.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintFormattedTextFieldBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
if (context.getComponent().getComponentOrientation().isLeftToRight()){
paintBorder(context, g, x, y, w, h, null);
} else {
AffineTransform transform = new AffineTransform();
transform.translate(x,y);
transform.scale(-1, 1);
transform.translate(-w,0);
paintBorder(context, g, 0, 0, w, h, transform);
}
}
/**
* Paints the background of a combo box.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintComboBoxBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
if (context.getComponent().getComponentOrientation().isLeftToRight()){
paintBackground(context, g, x, y, w, h, null);
} else {
AffineTransform transform = new AffineTransform();
transform.translate(x,y);
transform.scale(-1, 1);
transform.translate(-w,0);
paintBackground(context, g, 0, 0, w, h, transform);
}
}
/**
* {@inheritDoc}
*
* <p>Overridden to cause this style to populate itself with data from
* UIDefaults, if necessary.</p>
*
* <p>Properties in UIDefaults may be specified in a chained manner. For
* example:
* <pre>
* background
* Button.opacity
* Button.Enabled.foreground
* Button.Enabled+Selected.background
* </pre>
*
* <p>In this example, suppose you were in the Enabled+Selected state and
* searched for "foreground". In this case, we first check for
* Button.Enabled+Selected.foreground, but no such color exists. We then
* fall back to the next valid state, in this case,
* Button.Enabled.foreground, and have a match. So we return it.</p>
*
* <p>Again, if we were in the state Enabled and looked for "background", we
* wouldn't find it in Button.Enabled, or in Button, but would at the top
* level in UIManager. So we return that value.</p>
*
* <p>One special note: the "key" passed to this method could be of the form
* "background" or "Button.background" where "Button" equals the prefix
* passed to the NimbusStyle constructor. In either case, it looks for
* "background".</p>
*
* @param ctx
* @param key must not be null
*/
@Override public Object get(SynthContext ctx, Object key) {
Values v = getValues(ctx);
// strip off the prefix, if there is one.
String fullKey = key.toString();
String partialKey = fullKey.substring(fullKey.indexOf(".") + 1);
Object obj = null;
int xstate = getExtendedState(ctx, v);
// check the cache
tmpKey.init(partialKey, xstate);
obj = v.cache.get(tmpKey);
boolean wasInCache = obj != null;
if (!wasInCache){
// Search exact matching states and then lesser matching states
RuntimeState s = null;
int[] lastIndex = new int[] {-1};
while (obj == null &&
(s = getNextState(v.states, lastIndex, xstate)) != null) {
obj = s.defaults.get(partialKey);
}
// Search Region Defaults
if (obj == null && v.defaults != null) {
obj = v.defaults.get(partialKey);
}
// return found object
// Search UIManager Defaults
if (obj == null) obj = UIManager.get(fullKey);
// Search Synth Defaults for InputMaps
if (obj == null && partialKey.equals("focusInputMap")) {
obj = super.get(ctx, fullKey);
}
// if all we got was a null, store this fact for later use
v.cache.put(new CacheKey(partialKey, xstate),
obj == null ? NULL : obj);
}
// return found object
return obj == NULL ? null : obj;
}
public void paintBorder(SynthContext context, Graphics g, int x, int y, int w, int h) {
((SeaGlassContext) context).getPainter().paintToolBarBorder(context, g, x, y, w, h, toolBar.getOrientation());
}
/**
* Paints the background of a button.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintButtonBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBackground(context, g, x, y, w, h, null);
}
/**
* Paints the background of the viewport.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintViewportBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBackground(context, g, x, y, w, h, null);
}
/**
* Paints the border of a scroll pane.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintScrollPaneBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBorder(context, g, x, y, w, h, null);
}
/**
* Paints the border of a slider. This implementation invokes the
* method of the same name without the orientation.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
* @param orientation One of <code>JSlider.HORIZONTAL</code> or
* <code>JSlider.VERTICAL</code>
* @since 1.6
*/
public void paintSliderBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h, int orientation) {
paintBorder(context, g, x, y, w, h, orientation);
}
/**
* Paints the border of the content area of a tool bar.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintToolBarContentBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBorder(context, g, x, y, w, h, null);
}
/**
* Paints the background of a split pane.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintSplitPaneBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBackground(context, g, x, y, w, h, null);
}
/**
* Paints the border of a scrollbar.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintScrollBarBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBorder(context, g, x, y, w, h, null);
}
/**
* Paints the border of a password field.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintPasswordFieldBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBorder(context, g, x, y, w, h, null);
}
/**
* Paints the background of a text pane.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintTextPaneBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBackground(context, g, x, y, w, h, null);
}
/**
* Paints the border of an internal frame title pane.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintInternalFrameTitlePaneBorder(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBorder(context, g, x, y, w, h, null);
}
/**
* Paints the background of the viewport.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
*/
public void paintViewportBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h) {
paintBackground(context, g, x, y, w, h, null);
}
/**
* Paints the background of the window containing the tool bar when it
* has been detached from its primary frame. This implementation invokes the
* method of the same name without the orientation.
*
* @param context SynthContext identifying the <code>JComponent</code> and
* <code>Region</code> to paint to
* @param g <code>Graphics</code> to paint to
* @param x X coordinate of the area to paint to
* @param y Y coordinate of the area to paint to
* @param w Width of the area to paint to
* @param h Height of the area to paint to
* @param orientation One of <code>JToolBar.HORIZONTAL</code> or
* <code>JToolBar.VERTICAL</code>
* @since 1.6
*/
public void paintToolBarDragWindowBackground(SynthContext context,
Graphics g, int x, int y,
int w, int h, int orientation) {
paintBackground(context, g, x, y, w, h, orientation);
}