KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > ColorsManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.languages.features;
21
22 import java.awt.Color JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.swing.text.AttributeSet JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.SimpleAttributeSet JavaDoc;
34 import javax.swing.text.StyleConstants JavaDoc;
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 /**
47  *
48  * @author Jan Jancura
49  */

50 public class ColorsManager {
51
52     public static final String JavaDoc COLOR = "COLOR";
53     
54     static List JavaDoc<AttributeSet JavaDoc> getColors (Language l, ASTPath path, Document JavaDoc doc) {
55         List JavaDoc<AttributeSet JavaDoc> result = new ArrayList JavaDoc<AttributeSet JavaDoc> ();
56         Context context = SyntaxContext.create (doc, path);
57         List JavaDoc<Feature> fs = l.getFeatures (COLOR, path);
58         Iterator JavaDoc<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 JavaDoc[] {l.getMimeType ()});
70         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> colorsMap = new HashMap JavaDoc<String JavaDoc,AttributeSet JavaDoc> ();
71         Iterator JavaDoc<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 JavaDoc<String JavaDoc,AttributeSet JavaDoc> colorsMap, Language l) {
82         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> defaultsMap = getDefaultColors ();
83         List JavaDoc<Feature> list = l.getFeatures ("COLOR");
84         Iterator JavaDoc<Feature> it = list.iterator ();
85         while (it.hasNext ()) {
86             Feature f = it.next ();
87             AttributeSet JavaDoc as = createColoring (f);
88             colorsMap.put (
89                 (String JavaDoc) as.getAttribute (StyleConstants.NameAttribute),
90                 as
91             );
92         }
93         
94         Iterator JavaDoc<TokenType> it2 = l.getTokenTypes ().iterator ();
95         while (it2.hasNext ()) {
96             TokenType token = it2.next ();
97             String JavaDoc type = token.getType ();
98             if (colorsMap.containsKey (type)) continue;
99             SimpleAttributeSet JavaDoc sas = new SimpleAttributeSet JavaDoc ();
100             sas.addAttribute (StyleConstants.NameAttribute, type);
101             sas.addAttribute (EditorStyleConstants.DisplayName, type);
102             String JavaDoc 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 JavaDoc<AttributeSet JavaDoc> getColors (Language l) {
112         List JavaDoc<Feature> list = l.getFeatures ("COLORS");
113         List JavaDoc<AttributeSet JavaDoc> result = new ArrayList JavaDoc<AttributeSet JavaDoc> ();
114         Iterator JavaDoc<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 JavaDoc createColoring (Feature f) {
123         String JavaDoc colorName = (String JavaDoc) f.getValue ("color_name");
124         if (colorName == null)
125             colorName = f.getSelector ().getAsString ();
126         return createColoring (
127             colorName,
128             (String JavaDoc) f.getValue ("default_coloring"),
129             (String JavaDoc) f.getValue ("foreground_color"),
130             (String JavaDoc) f.getValue ("background_color"),
131             (String JavaDoc) f.getValue ("underline_color"),
132             (String JavaDoc) f.getValue ("wave_underline_color"),
133             (String JavaDoc) f.getValue ("strike_through_color"),
134             (String JavaDoc) f.getValue ("font_name"),
135             (String JavaDoc) f.getValue ("font_type")
136         );
137     }
138     
139     private static AttributeSet JavaDoc createColoring (
140         String JavaDoc colorName,
141         String JavaDoc defaultColor,
142         String JavaDoc foreground,
143         String JavaDoc background,
144         String JavaDoc underline,
145         String JavaDoc waveunderline,
146         String JavaDoc strikethrough,
147         String JavaDoc fontName,
148         String JavaDoc fontType
149     ) {
150         SimpleAttributeSet JavaDoc coloring = new SimpleAttributeSet JavaDoc ();
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 JavaDoc<String JavaDoc,Color JavaDoc> colors = new HashMap JavaDoc<String JavaDoc,Color JavaDoc> ();
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 JavaDoc readColor (String JavaDoc color) {
193         if (color == null) return null;
194         Color JavaDoc result = (Color JavaDoc) colors.get (color);
195         if (result == null)
196             result = Color.decode (color);
197         return result;
198     }
199     
200 // public static Map<String,AttributeSet> getColorMap (Language l) {
201
// Map defaultsMap = getDefaultColors ();
202
// Map<String,AttributeSet> colorsMap = getCurrentColors (l);
203
// Iterator<TokenType> it = l.getTokenTypes ().iterator ();
204
// while (it.hasNext ()) {
205
// TokenType token = it.next ();
206
// List<SimpleAttributeSet> colors = (List<SimpleAttributeSet>) getFeature
207
// (Language.COLOR, token.getType ());
208
// if (colors != null)
209
// for (Iterator<SimpleAttributeSet> it2 = colors.iterator (); it2.hasNext ();) {
210
// SimpleAttributeSet as = it2.next();
211
// String id = (String) as.getAttribute ("color_name"); // NOI18N
212
// if (id == null)
213
// id = token.getType ();
214
// addColor (id, as, colorsMap, defaultsMap);
215
// }
216
// else
217
// addColor (token.getType (), null, colorsMap, defaultsMap);
218
// }
219
//
220
// //List<AttributeSet> colors = getColors (l);
221
// Map m = (Map) features.get (Language.COLOR);
222
// if (m == null)
223
// return Collections.<String,AttributeSet>emptyMap ();
224
// Iterator<String> it2 = m.keySet ().iterator ();
225
// while (it2.hasNext ()) {
226
// String type = it2.next ();
227
// if (colorsMap.containsKey (type))
228
// continue;
229
// Object obj = m.get (type);
230
// if (obj != null) {
231
// for (Iterator iter = ((List)obj).iterator(); iter.hasNext(); ) {
232
// SimpleAttributeSet as = (SimpleAttributeSet) iter.next();
233
// addColor (type, as, colorsMap, defaultsMap);
234
// }
235
// }
236
// }
237
// addColor ("error", null, colorsMap, defaultsMap);
238
// return colorsMap;
239
// }
240

241     private static void addColor (
242         String JavaDoc tokenType,
243         SimpleAttributeSet JavaDoc sas,
244         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> colorsMap,
245         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> defaultsMap
246     ) {
247         if (sas == null)
248             sas = new SimpleAttributeSet JavaDoc ();
249         else
250             sas = new SimpleAttributeSet JavaDoc (sas);
251         String JavaDoc colorName = (String JavaDoc) 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 JavaDoc 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 JavaDoc<String JavaDoc,AttributeSet JavaDoc> getDefaultColors () {
267         Collection JavaDoc<AttributeSet JavaDoc> defaults = EditorSettings.getDefault ().
268             getDefaultFontColorDefaults ("NetBeans");
269         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> defaultsMap = new HashMap JavaDoc<String JavaDoc,AttributeSet JavaDoc> ();
270         Iterator JavaDoc<AttributeSet JavaDoc> it = defaults.iterator (); // check if IDE Defaults module is installed
271
while (it.hasNext ()) {
272             AttributeSet JavaDoc as = it.next ();
273             defaultsMap.put (
274                 (String JavaDoc) as.getAttribute (StyleConstants.NameAttribute),
275                 as
276             );
277         }
278         return defaultsMap;
279     }
280     
281     private static Map JavaDoc getCurrentColors (Language l) {
282         // current colors
283
FontColorSettingsFactory fcsf = EditorSettings.getDefault ().
284             getFontColorSettings (new String JavaDoc[] {l.getMimeType ()});
285         Collection JavaDoc<AttributeSet JavaDoc> colors = fcsf.getAllFontColors ("NetBeans");
286         Map JavaDoc<String JavaDoc,AttributeSet JavaDoc> colorsMap = new HashMap JavaDoc<String JavaDoc,AttributeSet JavaDoc> ();
287         Iterator JavaDoc<AttributeSet JavaDoc> it = colors.iterator ();
288         while (it.hasNext ()) {
289             AttributeSet JavaDoc as = it.next ();
290             colorsMap.put (
291                 (String JavaDoc) as.getAttribute (StyleConstants.NameAttribute),
292                 as
293             );
294         }
295         return colorsMap;
296     }
297 }
298
Popular Tags