1 19 20 package org.netbeans.modules.languages.features; 21 22 import java.awt.Color ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Map ; 31 import javax.swing.text.AttributeSet ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.SimpleAttributeSet ; 34 import javax.swing.text.StyleConstants ; 35 import org.netbeans.api.editor.settings.EditorStyleConstants; 36 import org.netbeans.api.languages.ASTPath; 37 import org.netbeans.api.languages.Context; 38 import org.netbeans.api.languages.SyntaxContext; 39 import org.netbeans.modules.editor.settings.storage.api.EditorSettings; 40 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory; 41 import org.netbeans.modules.languages.Feature; 42 import org.netbeans.modules.languages.Language; 43 import org.netbeans.modules.languages.Language.TokenType; 44 45 46 50 public class ColorsManager { 51 52 public static final String COLOR = "COLOR"; 53 54 static List <AttributeSet > getColors (Language l, ASTPath path, Document doc) { 55 List <AttributeSet > result = new ArrayList <AttributeSet > (); 56 Context context = SyntaxContext.create (doc, path); 57 List <Feature> fs = l.getFeatures (COLOR, path); 58 Iterator <Feature> it = fs.iterator (); 59 while (it.hasNext ()) { 60 Feature f = it.next (); 61 if (!f.getBoolean ("condition", context, true)) continue; 62 result.add (createColoring (f)); 63 } 64 return result; 65 } 66 67 public static void initColorings (Language l) { 68 FontColorSettingsFactory fcsf = EditorSettings.getDefault (). 69 getFontColorSettings (new String [] {l.getMimeType ()}); 70 Map <String ,AttributeSet > colorsMap = new HashMap <String ,AttributeSet > (); 71 Iterator <Language> it = l.getImportedLanguages ().iterator (); 72 while (it.hasNext ()) 73 addColors (colorsMap, it.next ()); 74 if (l.getMimeType().equals("text/html2")) 75 System.out.println(""); 76 addColors (colorsMap, l); 77 fcsf.setAllFontColorsDefaults ("NetBeans", colorsMap.values ()); 78 fcsf.setAllFontColors ("NetBeans", colorsMap.values ()); 79 } 80 81 private static void addColors (Map <String ,AttributeSet > colorsMap, Language l) { 82 Map <String ,AttributeSet > defaultsMap = getDefaultColors (); 83 List <Feature> list = l.getFeatures ("COLOR"); 84 Iterator <Feature> it = list.iterator (); 85 while (it.hasNext ()) { 86 Feature f = it.next (); 87 AttributeSet as = createColoring (f); 88 colorsMap.put ( 89 (String ) as.getAttribute (StyleConstants.NameAttribute), 90 as 91 ); 92 } 93 94 Iterator <TokenType> it2 = l.getTokenTypes ().iterator (); 95 while (it2.hasNext ()) { 96 TokenType token = it2.next (); 97 String type = token.getType (); 98 if (colorsMap.containsKey (type)) continue; 99 SimpleAttributeSet sas = new SimpleAttributeSet (); 100 sas.addAttribute (StyleConstants.NameAttribute, type); 101 sas.addAttribute (EditorStyleConstants.DisplayName, type); 102 String def = type; 103 int i = def.lastIndexOf ('_'); 104 if (i > 0) def = def.substring (i + 1); 105 if (defaultsMap.containsKey (def)) 106 sas.addAttribute (EditorStyleConstants.Default, def); 107 colorsMap.put (type, sas); 108 } 109 } 110 111 private static List <AttributeSet > getColors (Language l) { 112 List <Feature> list = l.getFeatures ("COLORS"); 113 List <AttributeSet > result = new ArrayList <AttributeSet > (); 114 Iterator <Feature> it = list.iterator (); 115 while (it.hasNext ()) { 116 Feature f = it.next (); 117 result.add (createColoring (f)); 118 } 119 return result; 120 } 121 122 private static AttributeSet createColoring (Feature f) { 123 String colorName = (String ) f.getValue ("color_name"); 124 if (colorName == null) 125 colorName = f.getSelector ().getAsString (); 126 return createColoring ( 127 colorName, 128 (String ) f.getValue ("default_coloring"), 129 (String ) f.getValue ("foreground_color"), 130 (String ) f.getValue ("background_color"), 131 (String ) f.getValue ("underline_color"), 132 (String ) f.getValue ("wave_underline_color"), 133 (String ) f.getValue ("strike_through_color"), 134 (String ) f.getValue ("font_name"), 135 (String ) f.getValue ("font_type") 136 ); 137 } 138 139 private static AttributeSet createColoring ( 140 String colorName, 141 String defaultColor, 142 String foreground, 143 String background, 144 String underline, 145 String waveunderline, 146 String strikethrough, 147 String fontName, 148 String fontType 149 ) { 150 SimpleAttributeSet coloring = new SimpleAttributeSet (); 151 coloring.addAttribute (StyleConstants.NameAttribute, colorName); 152 if (defaultColor != null) 153 coloring.addAttribute (EditorStyleConstants.Default, defaultColor); 154 if (foreground != null) 155 coloring.addAttribute (StyleConstants.Foreground, readColor (foreground)); 156 if (background != null) 157 coloring.addAttribute (StyleConstants.Background, readColor (background)); 158 if (strikethrough != null) 159 coloring.addAttribute (StyleConstants.StrikeThrough, readColor (strikethrough)); 160 if (underline != null) 161 coloring.addAttribute (StyleConstants.Underline, readColor (underline)); 162 if (waveunderline != null) 163 coloring.addAttribute (EditorStyleConstants.WaveUnderlineColor, readColor (waveunderline)); 164 if (fontName != null) 165 coloring.addAttribute (StyleConstants.FontFamily, fontName); 166 if (fontType != null) { 167 if (fontType.toLowerCase ().indexOf ("bold") >= 0) 168 coloring.addAttribute (StyleConstants.Bold, Boolean.TRUE); 169 if (fontType.toLowerCase ().indexOf ("italic") >= 0) 170 coloring.addAttribute (StyleConstants.Italic, Boolean.TRUE); 171 } 172 return coloring; 173 } 174 175 private static Map <String ,Color > colors = new HashMap <String ,Color > (); 176 static { 177 colors.put ("black", Color.black); 178 colors.put ("blue", Color.blue); 179 colors.put ("cyan", Color.cyan); 180 colors.put ("darkGray", Color.darkGray); 181 colors.put ("gray", Color.gray); 182 colors.put ("green", Color.green); 183 colors.put ("lightGray", Color.lightGray); 184 colors.put ("magenta", Color.magenta); 185 colors.put ("orange", Color.orange); 186 colors.put ("pink", Color.pink); 187 colors.put ("red", Color.red); 188 colors.put ("white", Color.white); 189 colors.put ("yellow", Color.yellow); 190 } 191 192 static Color readColor (String color) { 193 if (color == null) return null; 194 Color result = (Color ) colors.get (color); 195 if (result == null) 196 result = Color.decode (color); 197 return result; 198 } 199 200 241 private static void addColor ( 242 String tokenType, 243 SimpleAttributeSet sas, 244 Map <String ,AttributeSet > colorsMap, 245 Map <String ,AttributeSet > defaultsMap 246 ) { 247 if (sas == null) 248 sas = new SimpleAttributeSet (); 249 else 250 sas = new SimpleAttributeSet (sas); 251 String colorName = (String ) sas.getAttribute (StyleConstants.NameAttribute); 252 if (colorName == null) 253 colorName = tokenType; 254 sas.addAttribute (StyleConstants.NameAttribute, colorName); 255 sas.addAttribute (EditorStyleConstants.DisplayName, colorName); 256 if (!sas.isDefined (EditorStyleConstants.Default)) { 257 String def = colorName; 258 int i = def.lastIndexOf ('_'); 259 if (i > 0) def = def.substring (i + 1); 260 if (defaultsMap.containsKey (def)) 261 sas.addAttribute (EditorStyleConstants.Default, def); 262 } 263 colorsMap.put (colorName, sas); 264 } 265 266 private static Map <String ,AttributeSet > getDefaultColors () { 267 Collection <AttributeSet > defaults = EditorSettings.getDefault (). 268 getDefaultFontColorDefaults ("NetBeans"); 269 Map <String ,AttributeSet > defaultsMap = new HashMap <String ,AttributeSet > (); 270 Iterator <AttributeSet > it = defaults.iterator (); while (it.hasNext ()) { 272 AttributeSet as = it.next (); 273 defaultsMap.put ( 274 (String ) as.getAttribute (StyleConstants.NameAttribute), 275 as 276 ); 277 } 278 return defaultsMap; 279 } 280 281 private static Map getCurrentColors (Language l) { 282 FontColorSettingsFactory fcsf = EditorSettings.getDefault (). 284 getFontColorSettings (new String [] {l.getMimeType ()}); 285 Collection <AttributeSet > colors = fcsf.getAllFontColors ("NetBeans"); 286 Map <String ,AttributeSet > colorsMap = new HashMap <String ,AttributeSet > (); 287 Iterator <AttributeSet > it = colors.iterator (); 288 while (it.hasNext ()) { 289 AttributeSet as = it.next (); 290 colorsMap.put ( 291 (String ) as.getAttribute (StyleConstants.NameAttribute), 292 as 293 ); 294 } 295 return colorsMap; 296 } 297 } 298 | Popular Tags |