KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > CompositeFCS


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.editor.settings.storage;
21
22 import java.awt.Color JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.swing.UIManager JavaDoc;
28 import javax.swing.text.AttributeSet JavaDoc;
29 import javax.swing.text.SimpleAttributeSet JavaDoc;
30 import javax.swing.text.StyleConstants JavaDoc;
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 /**
38  *
39  * @author Vita Stejskal
40  */

41 public final class CompositeFCS extends FontColorSettings {
42
43     // A few words about the default coloring. It's special, it always contains
44
// foreground, background ant font related attributes. If they hadn't been
45
// supplied by a user the default coloring will use system defaults. Therefore
46
// this coloring should not be merged with any other colorings when folowing
47
// the chain of coloring delegates.
48

49     /** The name of the default coloring. */
50     private static final String JavaDoc DEFAULT = "default"; //NOI18N
51

52     private static final int DEFAULT_FONT_SIZE = UIManager.get("customFontSize") != null ? //NOI18N
53
((Integer JavaDoc) UIManager.get("customFontSize")).intValue() : //NOI18N
54
UIManager.getFont("TextField.font").getSize(); //NOI18N
55

56     private static final AttributeSet JavaDoc HARDCODED_DEFAULT_COLORING = AttributesUtilities.createImmutable(
57         StyleConstants.NameAttribute, DEFAULT,
58         StyleConstants.Foreground, Color.black,
59         StyleConstants.Background, Color.white,
60         StyleConstants.FontFamily, "Monospaced", //NOI18N
61
StyleConstants.FontSize, DEFAULT_FONT_SIZE < 12 ? 12 : DEFAULT_FONT_SIZE
62     );
63   
64     // Special instance to mark 'no attributes' for a token
65
private static final AttributeSet JavaDoc NULL = new SimpleAttributeSet JavaDoc();
66     
67     private final FontColorSettingsImpl [] allFcsi;
68     /* package */ final String JavaDoc profile;
69     private final Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> tokensCache = new HashMap JavaDoc<String JavaDoc, AttributeSet JavaDoc>();
70     
71     /** Creates a new instance of CompositeFCS */
72     public CompositeFCS(MimePath [] allPaths, String JavaDoc profile) {
73         super();
74         
75         assert allPaths != null : "The parameter allPaths should not be null"; //NOI18N
76
assert allPaths.length > 0 : "The parameter allPaths should always contain at least MimePath.EMPTY"; //NOI18N
77
assert profile != null : "The parameter profile should not be null"; //NOI18N
78

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     /**
88      * Gets the coloring for a highlight. Highlights are used for highlighting
89      * important things in editor such as a caret row, text selection, marking
90      * text found by the last search peration, etc. They are not bound to any
91      * tokens and therefore are mime type independent.
92      */

93     public AttributeSet JavaDoc getFontColors(String JavaDoc highlightName) {
94         assert highlightName != null : "The parameter highlightName must not be null."; //NOI18N
95

96         if (highlightName.equals(DEFAULT)) {
97             return getTokenFontColors(DEFAULT);
98         }
99         
100         AttributeSet JavaDoc attribs = null;
101         Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> 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 // dumpAttribs(attribs, highlightName, false);
110
return attribs;
111     }
112
113     public AttributeSet JavaDoc getTokenFontColors(String JavaDoc tokenName) {
114         assert tokenName != null : "The parameter tokenName must not be null."; //NOI18N
115

116         synchronized (tokensCache) {
117             AttributeSet JavaDoc attribs = tokensCache.get(tokenName);
118
119             if (attribs == null) {
120                 attribs = findColoringForToken(tokenName);
121 // dumpAttribs(attribs, tokenName, true);
122
tokensCache.put(tokenName, attribs);
123 // } else {
124
// System.out.println("Using cached value for token '" + tokenName + "' CompoundFCS.this = " + this);
125
}
126             
127             return attribs == NULL ? null : attribs;
128         }
129     }
130     
131     //-----------------------------------------------------------------------
132
// private implementation
133
//-----------------------------------------------------------------------
134

135     private AttributeSet JavaDoc findColoringForToken(String JavaDoc tokenName) {
136         ArrayList JavaDoc<AttributeSet JavaDoc> colorings = new ArrayList JavaDoc<AttributeSet JavaDoc>();
137         String JavaDoc 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 JavaDoc[colorings.size()]));
149         } else {
150             return NULL;
151         }
152     }
153     
154     private String JavaDoc processLayer(FontColorSettingsImpl fcsi, String JavaDoc name, ArrayList JavaDoc<AttributeSet JavaDoc> colorings) {
155         // Try colorings first
156
AttributeSet JavaDoc as = fcsi.getColorings(profile).get(name);
157         if (as == null) {
158             // If not found, try the layer's default colorings
159
as = fcsi.getDefaultColorings(profile).get(name);
160         }
161
162         // If we found a coloring then process it
163
if (as != null) {
164             colorings.add(as);
165
166             String JavaDoc nameOfColoring = (String JavaDoc) as.getAttribute(StyleConstants.NameAttribute);
167             String JavaDoc nameOfDelegate = (String JavaDoc) as.getAttribute(EditorStyleConstants.Default);
168             if (nameOfDelegate != null && !nameOfDelegate.equals(DEFAULT)) {
169                 if (!nameOfDelegate.equals(nameOfColoring)) {
170                     // Find delegate on the same layer
171
nameOfDelegate = processLayer(fcsi, nameOfDelegate, colorings);
172                 }
173             } else {
174                 // Use the coloring's name as the default name of a delegate
175
nameOfDelegate = nameOfColoring;
176             }
177
178             name = nameOfDelegate;
179         }
180
181         // Return updated name - either the name of the coloring or the name of
182
// the coloring's delegate
183
return name;
184     }
185     
186     private void dumpAttribs(AttributeSet JavaDoc attribs, String JavaDoc name, boolean tokenColoring) {
187 // if (!allFcsi[0].getMimePath().getPath().equals("text/x-java")) { //NOI18N
188
// return;
189
// }
190

191         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
192         sb.append("Attribs for base mime path '"); //NOI18N
193
sb.append(allFcsi[0].getMimePath().getPath());
194         sb.append("' and "); //NOI18N
195
if (tokenColoring) {
196             sb.append("token '"); //NOI18N
197
} else {
198             sb.append("highlight '"); //NOI18N
199
}
200         sb.append(name);
201         sb.append("' = {"); //NOI18N
202

203         Enumeration JavaDoc<?> keys = attribs.getAttributeNames();
204         while (keys.hasMoreElements()) {
205             Object JavaDoc key = keys.nextElement();
206             Object JavaDoc value = attribs.getAttribute(key);
207
208             sb.append("'" + key + "' = '" + value + "'"); //NOI18N
209
if (keys.hasMoreElements()) {
210                 sb.append(", "); //NOI18N
211
}
212         }
213
214         sb.append("} CompoundFCS.this = "); //NOI18N
215
sb.append(this.toString());
216         
217         System.out.println(sb.toString());
218     }
219 }
220
Popular Tags