FileDocCategorySizeDatePackage
DefaultSynthStyleFactory.javaAPI DocJava SE 6 API5713Tue Jun 10 00:26:54 BST 2008javax.swing.plaf.synth

DefaultSynthStyleFactory

public class DefaultSynthStyleFactory extends SynthStyleFactory
Factory used for obtaining styles. Supports associating a style based on the name of the component as returned by Component.getName(), and the Region associated with the JComponent. Lookup is done using regular expressions.
version
1.9, 11/17/05
author
Scott Violet

Fields Summary
public static final int
NAME
Used to indicate the lookup should be done based on Component name.
public static final int
REGION
Used to indicate the lookup should be done based on region.
private List
_styles
List containing set of StyleAssociations used in determining matching styles.
private BakedArrayList
_tmpList
Used during lookup.
private Map
_resolvedStyles
Maps from a List (BakedArrayList to be precise) to the merged style.
private SynthStyle
_defaultStyle
Used if there are no styles matching a widget.
Constructors Summary
DefaultSynthStyleFactory()



     
        _tmpList = new BakedArrayList(5);
        _styles = new ArrayList<StyleAssociation>();
        _resolvedStyles = new HashMap();
    
Methods Summary
public synchronized voidaddStyle(sun.swing.plaf.synth.DefaultSynthStyle style, java.lang.String path, int type)

        if (path == null) {
            // Make an empty path match all.
            path = ".*";
        }
        if (type == NAME) {
            _styles.add(StyleAssociation.createStyleAssociation(
                            path, style, type));
        }
        else if (type == REGION) {
            _styles.add(StyleAssociation.createStyleAssociation(
                            path.toLowerCase(), style, type));
        }
    
private voidcacheStyle(java.util.List styles, javax.swing.plaf.synth.SynthStyle style)
Caches the specified style.

        BakedArrayList cachedStyles = new BakedArrayList(styles);

        _resolvedStyles.put(cachedStyles, style);
    
private javax.swing.plaf.synth.SynthStylegetCachedStyle(java.util.List styles)
Returns the cached style from the passed in arguments.

        if (styles.size() == 0) {
            return null;
        }
        return (SynthStyle)_resolvedStyles.get(styles);
    
private javax.swing.plaf.synth.SynthStylegetDefaultStyle()
Returns the style to use if there are no matching styles.

        if (_defaultStyle == null) {
            _defaultStyle = new DefaultSynthStyle();
            ((DefaultSynthStyle)_defaultStyle).setFont(
                new FontUIResource(Font.DIALOG, Font.PLAIN,12));
        }
        return _defaultStyle;
    
private voidgetMatchingStyles(java.util.List matches, javax.swing.JComponent c, javax.swing.plaf.synth.Region id)
Fetches any styles that match the passed into arguments into matches.

        String idName = id.getLowerCaseName();
        String cName = c.getName();

        if (cName == null) {
            cName = "";
        }
        for (int counter = _styles.size() - 1; counter >= 0; counter--){
            StyleAssociation sa = _styles.get(counter);
            String path;

            if (sa.getID() == NAME) {
                path = cName;
            }
            else {
                path = idName;
            }

            if (sa.matches(path) && matches.indexOf(sa.getStyle()) == -1) {
                matches.add(sa.getStyle());
            }
        }
    
public synchronized javax.swing.plaf.synth.SynthStylegetStyle(javax.swing.JComponent c, javax.swing.plaf.synth.Region id)
Returns the style for the specified Component.

param
c Component asking for
param
id ID of the Component

        BakedArrayList matches = _tmpList;

        matches.clear();
        getMatchingStyles(matches, c, id);

        if (matches.size() == 0) {
            return getDefaultStyle();
        }
        // Use a cached Style if possible, otherwise create a new one.
        matches.cacheHashCode();
        SynthStyle style = getCachedStyle(matches);

        if (style == null) {
            style = mergeStyles(matches);

            if (style != null) {
                cacheStyle(matches, style);
            }
        }
        return style;
    
private javax.swing.plaf.synth.SynthStylemergeStyles(java.util.List styles)
Creates a single Style from the passed in styles. The passed in List is reverse sorted, that is the most recently added style found to match will be first.

        int size = styles.size();

        if (size == 0) {
            return null;
        }
        else if (size == 1) {
            return (SynthStyle)((DefaultSynthStyle)styles.get(0)).clone();
        }
        // NOTE: merging is done backwards as DefaultSynthStyleFactory reverses
        // order, that is, the most specific style is first.
        DefaultSynthStyle style = (DefaultSynthStyle)styles.get(size - 1);

        style = (DefaultSynthStyle)style.clone();
        for (int counter = size - 2; counter >= 0; counter--) {
            style = ((DefaultSynthStyle)styles.get(counter)).addTo(style);
        }
        return style;