001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.settings.storage.fontscolors;
043:
044: import java.io.IOException;
045: import org.netbeans.modules.editor.settings.storage.*;
046: import java.lang.ref.WeakReference;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.HashMap;
050: import java.util.Map;
051: import java.util.WeakHashMap;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import javax.swing.text.AttributeSet;
055: import org.netbeans.api.editor.mimelookup.MimePath;
056: import org.netbeans.modules.editor.settings.storage.api.EditorSettingsStorage;
057: import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory;
058:
059: /**
060: *
061: * @author Jan Jancura, Vita Stejskal
062: */
063: public final class FontColorSettingsImpl extends
064: FontColorSettingsFactory {
065:
066: private static final Logger LOG = Logger
067: .getLogger(FontColorSettingsImpl.class.getName());
068:
069: private static final Map<MimePath, WeakReference<FontColorSettingsImpl>> INSTANCES = new WeakHashMap<MimePath, WeakReference<FontColorSettingsImpl>>();
070:
071: public static synchronized FontColorSettingsImpl get(
072: MimePath mimePath) {
073: WeakReference<FontColorSettingsImpl> reference = INSTANCES
074: .get(mimePath);
075: FontColorSettingsImpl result = reference == null ? null
076: : reference.get();
077:
078: if (result == null) {
079: result = new FontColorSettingsImpl(mimePath);
080: INSTANCES.put(mimePath,
081: new WeakReference<FontColorSettingsImpl>(result));
082: }
083:
084: return result;
085: }
086:
087: private final MimePath mimePath;
088:
089: /**
090: * Construction prohibited for API clients.
091: */
092: private FontColorSettingsImpl(MimePath mimePath) {
093: this .mimePath = mimePath;
094: }
095:
096: public MimePath getMimePath() {
097: return mimePath;
098: }
099:
100: /**
101: * Translates profile's display name to its Id. If the profile's display name
102: * can't be translated this method will simply return the profile's display name
103: * without translation.
104: */
105: public String getInternalFontColorProfile(String profile) {
106: ProfilesTracker tracker = ProfilesTracker.get(
107: ColoringStorage.ID, EditorSettingsImpl.EDITORS_FOLDER);
108: ProfilesTracker.ProfileDescription pd = tracker
109: .getProfileByDisplayName(profile);
110: return pd == null ? profile : pd.getId();
111: }
112:
113: //-----------------------------------------------------------------------
114: // FontColorSettingsFactory implementation
115: //-----------------------------------------------------------------------
116:
117: /**
118: * Gets all token font and colors for given profile or null, if
119: * profile does not exists.
120: *
121: * @param profile the name of profile
122: *
123: * @return AttributeSet describing the font and colors or null, if
124: * profile does not exists
125: */
126: public Collection<AttributeSet> getAllFontColors(String profile) {
127: profile = getInternalFontColorProfile(profile);
128: Map<String, AttributeSet> m = getColorings(profile);
129: return Collections.unmodifiableCollection(m.values());
130: }
131:
132: /**
133: * Gets default values for all font & colors for given profile, or null
134: * if profile does not exist or if it does not have any defaults.
135: *
136: * @param profile the name of profile
137: *
138: * @return AttributeSet describing the font and colors or null, if
139: * profile does not exists
140: */
141: public Collection<AttributeSet> getAllFontColorDefaults(
142: String profile) {
143: profile = getInternalFontColorProfile(profile);
144: return getDefaultColorings(profile).values();
145: }
146:
147: /**
148: * Sets all token font and colors for given profile.
149: *
150: * @param profile the name of profile
151: * @param fontColors new colorings
152: */
153: public void setAllFontColors(String profile,
154: Collection<AttributeSet> fontColors) {
155: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
156: .<String, AttributeSet> get(ColoringStorage.ID);
157: boolean specialProfile = profile.startsWith("test"); //NOI18N
158: profile = getInternalFontColorProfile(profile);
159:
160: if (fontColors == null) {
161: try {
162: ess.delete(mimePath, profile, false);
163: } catch (IOException ioe) {
164: LOG.log(Level.WARNING, null, ioe);
165: }
166: colorings.remove(profile);
167: } else {
168: Map<String, AttributeSet> map = Utils.immutize(fontColors);
169:
170: if (!specialProfile) {
171: try {
172: ess.save(mimePath, profile, false, map);
173: } catch (IOException ioe) {
174: LOG.log(Level.WARNING, null, ioe);
175: }
176: }
177:
178: colorings.put(profile, map);
179: }
180:
181: EditorSettingsImpl.getInstance().notifyTokenFontColorChange(
182: mimePath, profile);
183: }
184:
185: /**
186: * Sets default values for all token font and colors for given scheme.
187: *
188: * @param profile the name of profile
189: * @param fontColors new colorings
190: */
191: public void setAllFontColorsDefaults(String profile,
192: Collection<AttributeSet> fontColors) {
193: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
194: .<String, AttributeSet> get(ColoringStorage.ID);
195: boolean specialProfile = profile.startsWith("test"); //NOI18N
196: profile = getInternalFontColorProfile(profile);
197:
198: try {
199: if (fontColors == null) {
200: ess.delete(mimePath, profile, true);
201: } else {
202: ess.save(mimePath, profile, true, Utils
203: .immutize(fontColors));
204: }
205: } catch (IOException ioe) {
206: LOG.log(Level.WARNING, null, ioe);
207: }
208:
209: EditorSettingsImpl.getInstance().notifyTokenFontColorChange(
210: mimePath, profile);
211: }
212:
213: //-----------------------------------------------------------------------
214: // private implementation
215: //-----------------------------------------------------------------------
216:
217: private final Map<String, Map<String, AttributeSet>> colorings = new HashMap<String, Map<String, AttributeSet>>();
218:
219: /* package */Map<String, AttributeSet> getColorings(String profile) {
220: if (!colorings.containsKey(profile)) {
221: boolean specialProfile = profile.startsWith("test"); //NOI18N
222: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
223: .<String, AttributeSet> get(ColoringStorage.ID);
224:
225: Map<String, AttributeSet> profileColorings = null;
226: try {
227: profileColorings = ess
228: .load(
229: mimePath,
230: specialProfile ? EditorSettingsImpl.DEFAULT_PROFILE
231: : profile, false);
232: } catch (IOException ioe) {
233: LOG.log(Level.WARNING, null, ioe);
234: }
235:
236: Map<String, AttributeSet> defaultProfileColorings = null;
237: if (!specialProfile
238: && !EditorSettingsImpl.DEFAULT_PROFILE
239: .equals(profile)) {
240: try {
241: defaultProfileColorings = ess.load(mimePath,
242: EditorSettingsImpl.DEFAULT_PROFILE, false);
243: } catch (IOException ioe) {
244: LOG.log(Level.WARNING, null, ioe);
245: }
246: }
247:
248: // Add colorings from the default profile that do not exist in
249: // the profileColorings. They are normally the same, but when
250: // imported from previous version some colorings can be missing.
251: // See #119709
252: Map<String, AttributeSet> m = new HashMap<String, AttributeSet>();
253: if (defaultProfileColorings != null) {
254: m.putAll(defaultProfileColorings);
255: }
256: if (profileColorings != null) {
257: m.putAll(profileColorings);
258: }
259: profileColorings = Collections.unmodifiableMap(m);
260:
261: colorings.put(profile, profileColorings);
262: }
263:
264: Map<String, AttributeSet> c = colorings.get(profile);
265: return c == null ? Collections
266: .<String, AttributeSet> emptyMap() : c;
267: }
268:
269: /* package */Map<String, AttributeSet> getDefaultColorings(
270: String profile) {
271: EditorSettingsStorage<String, AttributeSet> ess = EditorSettingsStorage
272: .<String, AttributeSet> get(ColoringStorage.ID);
273: try {
274: return ess.load(mimePath, profile, true);
275: } catch (IOException ioe) {
276: LOG.log(Level.WARNING, null, ioe);
277: return Collections.<String, AttributeSet> emptyMap();
278: }
279: }
280:
281: }
|