1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.lang.ref.WeakReference ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.Map ; 27 import java.util.WeakHashMap ; 28 import java.util.logging.Logger ; 29 import javax.swing.text.AttributeSet ; 30 import javax.swing.text.StyleConstants ; 31 import org.netbeans.api.editor.mimelookup.MimePath; 32 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory; 33 34 38 public final class FontColorSettingsImpl extends FontColorSettingsFactory { 39 40 private static final Logger LOG = Logger.getLogger(FontColorSettingsImpl.class.getName()); 41 42 private static final Map <MimePath, WeakReference <FontColorSettingsImpl>> INSTANCES = 43 new WeakHashMap <MimePath, WeakReference <FontColorSettingsImpl>>(); 44 45 public static synchronized FontColorSettingsImpl get(MimePath mimePath) { 46 WeakReference <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 <FontColorSettingsImpl>(result)); 52 } 53 54 return result; 55 } 56 57 private final MimePath mimePath; 58 59 62 private FontColorSettingsImpl(MimePath mimePath) { 63 this.mimePath = mimePath; 64 } 65 66 public MimePath getMimePath() { 67 return mimePath; 68 } 69 70 74 83 public Collection <AttributeSet > getAllFontColors (String profile) { 84 85 profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile); 87 88 Map <String , AttributeSet > m = getColorings(profile); 90 return Collections.unmodifiableCollection(m.values()); 91 } 92 93 102 public Collection <AttributeSet > getAllFontColorDefaults(String profile) { 103 104 profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile); 106 107 Map <String , AttributeSet > m = getDefaultColorings(profile); 109 return Collections.unmodifiableCollection(m.values()); 110 } 111 112 118 public void setAllFontColors ( 119 String profile, 120 Collection <AttributeSet > fontColors 121 ) { 122 LOG.fine("set " + profile); 124 125 boolean specialProfile = profile.startsWith("test"); profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile); 127 128 if (fontColors == null) { 129 ColoringStorage.deleteColorings(mimePath, profile, true, false); colorings.remove (profile); 132 } else { 133 Map <String , AttributeSet > map = Utils.immutize(fontColors); 134 135 if (!specialProfile) { 137 ColoringStorage.saveColorings(mimePath, profile, true, false, map.values()); } 139 140 colorings.put(profile, map); 142 } 143 144 EditorSettingsImpl.getInstance().notifyTokenFontColorChange(mimePath, profile); 145 } 146 147 153 public void setAllFontColorsDefaults ( 154 String profile, 155 Collection <AttributeSet > fontColors 156 ) { 157 LOG.fine("set defaults " + profile); 159 160 boolean specialProfile = profile.startsWith("test"); profile = EditorSettingsImpl.getInstance().getInternalFontColorProfile(profile); 162 163 if (fontColors == null) { 164 ColoringStorage.deleteColorings(mimePath, profile, true, true); defaults.remove (profile); 167 } else { 168 Map <String , AttributeSet > map = Utils.immutize(fontColors); 169 170 if (!specialProfile) { 172 ColoringStorage.saveColorings(mimePath, profile, true, true, map.values()); } 174 175 defaults.put(profile, map); 177 } 178 179 EditorSettingsImpl.getInstance().notifyTokenFontColorChange(mimePath, profile); 180 } 181 182 186 private final Map <String , Map <String , AttributeSet >> colorings = new HashMap <String , Map <String , AttributeSet >>(); 187 188 Map <String , AttributeSet > getColorings (String profile) { 189 if (!colorings.containsKey (profile)) { 190 boolean specialProfile = profile.startsWith("test"); 192 Map <String , AttributeSet > 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 <String , AttributeSet > c = colorings.get(profile); 209 210 return c == null ? Collections.<String , AttributeSet >emptyMap() : c; 211 } 212 213 private final Map <String , Map <String , AttributeSet >> defaults = new HashMap <String , Map <String , AttributeSet >>(); 214 215 Map <String , AttributeSet > getDefaultColorings (String profile) { 216 if (!defaults.containsKey (profile)) { 217 Map <String , AttributeSet > m = ColoringStorage.loadColorings 218 (mimePath, profile, true, true); 219 defaults.put (profile, m); 220 } 221 222 Map <String , AttributeSet > c = defaults.get(profile); 223 224 return c == null ? Collections.<String , AttributeSet >emptyMap() : c; 225 } 226 227 } 228 | Popular Tags |