KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.WeakReference JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.WeakHashMap JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.text.AttributeSet JavaDoc;
30 import javax.swing.text.StyleConstants JavaDoc;
31 import org.netbeans.api.editor.mimelookup.MimePath;
32 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory;
33
34 /**
35  *
36  * @author Jan Jancura
37  */

38 public final class FontColorSettingsImpl extends FontColorSettingsFactory {
39
40     private static final Logger JavaDoc LOG = Logger.getLogger(FontColorSettingsImpl.class.getName());
41
42     private static final Map JavaDoc<MimePath, WeakReference JavaDoc<FontColorSettingsImpl>> INSTANCES =
43         new WeakHashMap JavaDoc<MimePath, WeakReference JavaDoc<FontColorSettingsImpl>>();
44     
45     public static synchronized FontColorSettingsImpl get(MimePath mimePath) {
46         WeakReference JavaDoc<FontColorSettingsImpl> reference = INSTANCES.get(mimePath);
47         FontColorSettingsImpl result = reference == null ? null : reference.get();
48         
49         if (result == null) {
50             result = new FontColorSettingsImpl(mimePath);
51             INSTANCES.put(mimePath, new WeakReference JavaDoc<FontColorSettingsImpl>(result));
52         }
53         
54         return result;
55     }
56     
57     private final MimePath mimePath;
58     
59     /**
60      * Construction prohibited for API clients.
61      */

62     private FontColorSettingsImpl(MimePath mimePath) {
63         this.mimePath = mimePath;
64     }
65     
66     public MimePath getMimePath() {
67         return mimePath;
68     }
69     
70     //-----------------------------------------------------------------------
71
// FontColorSettingsFactory implementation
72
//-----------------------------------------------------------------------
73

74     /**
75      * Gets all token font and colors for given profile or null, if
76      * profile does not exists.
77      *
78      * @param profile the name of profile
79      *
80      * @return AttributeSet describing the font and colors or null, if
81      * profile does not exists
82      */

83     public Collection JavaDoc<AttributeSet JavaDoc> getAllFontColors (String JavaDoc profile) {
84         
85         // 1) resolve profile name
86
profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile);
87         
88         // 2) read colorings
89
Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> m = getColorings(profile);
90         return Collections.unmodifiableCollection(m.values());
91     }
92     
93     /**
94      * Gets default values for all font & colors for given profile, or null
95      * if profile does not exist or if it does not have any defaults.
96      *
97      * @param profile the name of profile
98      *
99      * @return AttributeSet describing the font and colors or null, if
100      * profile does not exists
101      */

102     public Collection JavaDoc<AttributeSet JavaDoc> getAllFontColorDefaults(String JavaDoc profile) {
103         
104     // 1) resolve profile name
105
profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile);
106         
107         // 2) read defaults
108
Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> m = getDefaultColorings(profile);
109         return Collections.unmodifiableCollection(m.values());
110     }
111     
112     /**
113      * Sets all token font and colors for given profile.
114      *
115      * @param profile the name of profile
116      * @param fontColors new colorings
117      */

118     public void setAllFontColors (
119         String JavaDoc profile,
120         Collection JavaDoc<AttributeSet JavaDoc> fontColors
121     ) {
122         // 1) resolve profile name
123
LOG.fine("set " + profile);
124         
125         boolean specialProfile = profile.startsWith("test"); //NOI18N
126
profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile);
127         
128         if (fontColors == null) {
129             // 2) remove coloring / revert to defaults
130
ColoringStorage.deleteColorings(mimePath, profile, true, false); //NOI18N
131
colorings.remove (profile);
132         } else {
133             Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> map = Utils.immutize(fontColors);
134             
135             // 2) save coloring
136
if (!specialProfile) {
137                 ColoringStorage.saveColorings(mimePath, profile, true, false, map.values()); //NOI18N
138
}
139             
140             // 3) cache coloring
141
colorings.put(profile, map);
142         }
143         
144         EditorSettingsImpl.getInstance().notifyTokenFontColorChange(mimePath, profile);
145     }
146     
147     /**
148      * Sets default values for all token font and colors for given scheme.
149      *
150      * @param profile the name of profile
151      * @param fontColors new colorings
152      */

153     public void setAllFontColorsDefaults (
154         String JavaDoc profile,
155         Collection JavaDoc<AttributeSet JavaDoc> fontColors
156     ) {
157         // 1) resolve profile name
158
LOG.fine("set defaults " + profile);
159
160         boolean specialProfile = profile.startsWith("test"); //NOI18N
161
profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile);
162         
163         if (fontColors == null) {
164             // 2) remove coloring / revert to defaults
165
ColoringStorage.deleteColorings(mimePath, profile, true, true); //NOI18N
166
defaults.remove (profile);
167         } else {
168             Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> map = Utils.immutize(fontColors);
169             
170             // 2) save coloring
171
if (!specialProfile) {
172                 ColoringStorage.saveColorings(mimePath, profile, true, true, map.values()); //NOI18N
173
}
174             
175             // 3) cache coloring
176
defaults.put(profile, map);
177         }
178         
179         EditorSettingsImpl.getInstance().notifyTokenFontColorChange(mimePath, profile);
180     }
181     
182     //-----------------------------------------------------------------------
183
// private implementation
184
//-----------------------------------------------------------------------
185

186     private final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AttributeSet JavaDoc>> colorings = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AttributeSet JavaDoc>>();
187     
188     /* package */ Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> getColorings (String JavaDoc profile) {
189     if (!colorings.containsKey (profile)) {
190             boolean specialProfile = profile.startsWith("test"); //NOI18N
191

192             Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> m = ColoringStorage.loadColorings (
193                 mimePath,
194                 specialProfile ? EditorSettingsImpl.DEFAULT_PROFILE : profile,
195                 true,
196                 false
197             );
198             if (m == null)
199                 m = ColoringStorage.loadColorings (
200                     mimePath,
201                     EditorSettingsImpl.DEFAULT_PROFILE,
202                     true,
203                     false
204                 );
205             colorings.put(profile, m);
206     }
207         
208         Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> c = colorings.get(profile);
209         
210     return c == null ? Collections.<String JavaDoc, AttributeSet JavaDoc>emptyMap() : c;
211     }
212     
213     private final Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AttributeSet JavaDoc>> defaults = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, AttributeSet JavaDoc>>();
214     
215     /* package */ Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> getDefaultColorings (String JavaDoc profile) {
216     if (!defaults.containsKey (profile)) {
217             Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> m = ColoringStorage.loadColorings
218                 (mimePath, profile, true, true);
219             defaults.put (profile, m);
220     }
221         
222         Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> c = defaults.get(profile);
223         
224     return c == null ? Collections.<String JavaDoc, AttributeSet JavaDoc>emptyMap() : c;
225     }
226
227 }
228
Popular Tags