1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.awt.Color ; 23 import java.util.ArrayList ; 24 import java.util.Enumeration ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import javax.swing.UIManager ; 28 import javax.swing.text.AttributeSet ; 29 import javax.swing.text.SimpleAttributeSet ; 30 import javax.swing.text.StyleConstants ; 31 import org.netbeans.api.editor.mimelookup.MimePath; 32 import org.netbeans.api.editor.settings.AttributesUtilities; 33 import org.netbeans.api.editor.settings.EditorStyleConstants; 34 import org.netbeans.api.editor.settings.FontColorSettings; 35 import org.netbeans.modules.editor.settings.storage.api.EditorSettings; 36 37 41 public final class CompositeFCS extends FontColorSettings { 42 43 49 50 private static final String DEFAULT = "default"; 52 private static final int DEFAULT_FONT_SIZE = UIManager.get("customFontSize") != null ? ((Integer ) UIManager.get("customFontSize")).intValue() : UIManager.getFont("TextField.font").getSize(); 56 private static final AttributeSet HARDCODED_DEFAULT_COLORING = AttributesUtilities.createImmutable( 57 StyleConstants.NameAttribute, DEFAULT, 58 StyleConstants.Foreground, Color.black, 59 StyleConstants.Background, Color.white, 60 StyleConstants.FontFamily, "Monospaced", StyleConstants.FontSize, DEFAULT_FONT_SIZE < 12 ? 12 : DEFAULT_FONT_SIZE 62 ); 63 64 private static final AttributeSet NULL = new SimpleAttributeSet (); 66 67 private final FontColorSettingsImpl [] allFcsi; 68 final String profile; 69 private final Map <String , AttributeSet > tokensCache = new HashMap <String , AttributeSet >(); 70 71 72 public CompositeFCS(MimePath [] allPaths, String profile) { 73 super(); 74 75 assert allPaths != null : "The parameter allPaths should not be null"; assert allPaths.length > 0 : "The parameter allPaths should always contain at least MimePath.EMPTY"; assert profile != null : "The parameter profile should not be null"; 79 this.allFcsi = new FontColorSettingsImpl [allPaths.length]; 80 for(int i = 0; i < allPaths.length; i++) { 81 allFcsi[i] = FontColorSettingsImpl.get(allPaths[i]); 82 } 83 84 this.profile = profile; 85 } 86 87 93 public AttributeSet getFontColors(String highlightName) { 94 assert highlightName != null : "The parameter highlightName must not be null."; 96 if (highlightName.equals(DEFAULT)) { 97 return getTokenFontColors(DEFAULT); 98 } 99 100 AttributeSet attribs = null; 101 Map <String , AttributeSet > coloringsMap = EditorSettings.getDefault().getHighlightings(profile); 102 if (coloringsMap != null) { 103 attribs = coloringsMap.get(highlightName); 104 if (attribs == null) { 105 attribs = NULL; 106 } 107 } 108 109 return attribs; 111 } 112 113 public AttributeSet getTokenFontColors(String tokenName) { 114 assert tokenName != null : "The parameter tokenName must not be null."; 116 synchronized (tokensCache) { 117 AttributeSet attribs = tokensCache.get(tokenName); 118 119 if (attribs == null) { 120 attribs = findColoringForToken(tokenName); 121 tokensCache.put(tokenName, attribs); 123 } 126 127 return attribs == NULL ? null : attribs; 128 } 129 } 130 131 135 private AttributeSet findColoringForToken(String tokenName) { 136 ArrayList <AttributeSet > colorings = new ArrayList <AttributeSet >(); 137 String name = tokenName; 138 139 for(FontColorSettingsImpl fcsi : allFcsi) { 140 name = processLayer(fcsi, name, colorings); 141 } 142 143 if (tokenName.equals(DEFAULT)) { 144 colorings.add(HARDCODED_DEFAULT_COLORING); 145 } 146 147 if (colorings.size() > 0) { 148 return AttributesUtilities.createImmutable(colorings.toArray(new AttributeSet [colorings.size()])); 149 } else { 150 return NULL; 151 } 152 } 153 154 private String processLayer(FontColorSettingsImpl fcsi, String name, ArrayList <AttributeSet > colorings) { 155 AttributeSet as = fcsi.getColorings(profile).get(name); 157 if (as == null) { 158 as = fcsi.getDefaultColorings(profile).get(name); 160 } 161 162 if (as != null) { 164 colorings.add(as); 165 166 String nameOfColoring = (String ) as.getAttribute(StyleConstants.NameAttribute); 167 String nameOfDelegate = (String ) as.getAttribute(EditorStyleConstants.Default); 168 if (nameOfDelegate != null && !nameOfDelegate.equals(DEFAULT)) { 169 if (!nameOfDelegate.equals(nameOfColoring)) { 170 nameOfDelegate = processLayer(fcsi, nameOfDelegate, colorings); 172 } 173 } else { 174 nameOfDelegate = nameOfColoring; 176 } 177 178 name = nameOfDelegate; 179 } 180 181 return name; 184 } 185 186 private void dumpAttribs(AttributeSet attribs, String name, boolean tokenColoring) { 187 191 StringBuilder sb = new StringBuilder (); 192 sb.append("Attribs for base mime path '"); sb.append(allFcsi[0].getMimePath().getPath()); 194 sb.append("' and "); if (tokenColoring) { 196 sb.append("token '"); } else { 198 sb.append("highlight '"); } 200 sb.append(name); 201 sb.append("' = {"); 203 Enumeration <?> keys = attribs.getAttributeNames(); 204 while (keys.hasMoreElements()) { 205 Object key = keys.nextElement(); 206 Object value = attribs.getAttribute(key); 207 208 sb.append("'" + key + "' = '" + value + "'"); if (keys.hasMoreElements()) { 210 sb.append(", "); } 212 } 213 214 sb.append("} CompoundFCS.this = "); sb.append(this.toString()); 216 217 System.out.println(sb.toString()); 218 } 219 } 220 | Popular Tags |