1 7 package javax.swing.plaf.synth; 8 9 import javax.swing.*; 10 import javax.swing.plaf.FontUIResource ; 11 import java.awt.Font ; 12 import java.util.*; 13 import java.util.regex.*; 14 import sun.swing.plaf.synth.*; 15 import sun.swing.BakedArrayList; 16 17 26 class DefaultSynthStyleFactory extends SynthStyleFactory { 27 30 public static final int NAME = 0; 31 34 public static final int REGION = 1; 35 36 40 private List<StyleAssociation> _styles; 41 44 private BakedArrayList _tmpList; 45 46 49 private Map _resolvedStyles; 50 51 54 private SynthStyle _defaultStyle; 55 56 57 DefaultSynthStyleFactory() { 58 _tmpList = new BakedArrayList(5); 59 _styles = new ArrayList<StyleAssociation>(); 60 _resolvedStyles = new HashMap(); 61 } 62 63 public synchronized void addStyle(DefaultSynthStyle style, 64 String path, int type) throws PatternSyntaxException { 65 if (path == null) { 66 path = ".*"; 68 } 69 if (type == NAME) { 70 _styles.add(StyleAssociation.createStyleAssociation( 71 path, style, type)); 72 } 73 else if (type == REGION) { 74 _styles.add(StyleAssociation.createStyleAssociation( 75 path.toLowerCase(), style, type)); 76 } 77 } 78 79 85 public synchronized SynthStyle getStyle(JComponent c, Region id) { 86 BakedArrayList matches = _tmpList; 87 88 matches.clear(); 89 getMatchingStyles(matches, c, id); 90 91 if (matches.size() == 0) { 92 return getDefaultStyle(); 93 } 94 matches.cacheHashCode(); 96 SynthStyle style = getCachedStyle(matches); 97 98 if (style == null) { 99 style = mergeStyles(matches); 100 101 if (style != null) { 102 cacheStyle(matches, style); 103 } 104 } 105 return style; 106 } 107 108 111 private SynthStyle getDefaultStyle() { 112 if (_defaultStyle == null) { 113 _defaultStyle = new DefaultSynthStyle(); 114 ((DefaultSynthStyle)_defaultStyle).setFont( 115 new FontUIResource ("Dialog", Font.PLAIN,12)); 116 } 117 return _defaultStyle; 118 } 119 120 124 private void getMatchingStyles(java.util.List matches, JComponent c, 125 Region id) { 126 String idName = id.getLowerCaseName(); 127 String cName = c.getName(); 128 129 if (cName == null) { 130 cName = ""; 131 } 132 for (int counter = _styles.size() - 1; counter >= 0; counter--){ 133 StyleAssociation sa = _styles.get(counter); 134 String path; 135 136 if (sa.getID() == NAME) { 137 path = cName; 138 } 139 else { 140 path = idName; 141 } 142 143 if (sa.matches(path) && matches.indexOf(sa.getStyle()) == -1) { 144 matches.add(sa.getStyle()); 145 } 146 } 147 } 148 149 152 private void cacheStyle(java.util.List styles, SynthStyle style) { 153 BakedArrayList cachedStyles = new BakedArrayList(styles); 154 155 _resolvedStyles.put(cachedStyles, style); 156 } 157 158 161 private SynthStyle getCachedStyle(java.util.List styles) { 162 if (styles.size() == 0) { 163 return null; 164 } 165 return (SynthStyle )_resolvedStyles.get(styles); 166 } 167 168 173 private SynthStyle mergeStyles(java.util.List styles) { 174 int size = styles.size(); 175 176 if (size == 0) { 177 return null; 178 } 179 else if (size == 1) { 180 return (SynthStyle )((DefaultSynthStyle)styles.get(0)).clone(); 181 } 182 DefaultSynthStyle style = (DefaultSynthStyle)styles.get(size - 1); 185 186 style = (DefaultSynthStyle)style.clone(); 187 for (int counter = size - 2; counter >= 0; counter--) { 188 style = ((DefaultSynthStyle)styles.get(counter)).addTo(style); 189 } 190 return style; 191 } 192 } 193 | Popular Tags |