KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.text.AttributeSet JavaDoc;
32 import javax.swing.text.SimpleAttributeSet JavaDoc;
33 import javax.swing.text.StyleConstants JavaDoc;
34 import org.netbeans.api.editor.mimelookup.MimePath;
35 import org.netbeans.api.editor.settings.AttributesUtilities;
36 import org.netbeans.api.editor.settings.EditorStyleConstants;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileSystem;
39 import org.openide.filesystems.Repository;
40 import org.openide.xml.XMLUtil;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44 import org.xml.sax.Attributes JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48
49 /**
50  * This class contains support static methods for loading / saving and
51  * translating coloring (fontsColors.xml) files. It calls XMLStorage utilities.
52  *
53  * @author Jan Jancura
54  */

55 final class ColoringStorage {
56
57     private static final Logger JavaDoc LOG = Logger.getLogger(ColoringStorage.class.getName());
58
59     // XXX: These constants are package private only for EditorSettingsImpl.init*() method.
60
// Once we get rid of filenames these will be removed.
61

62     /* package */ static final String JavaDoc ALL_LANGUAGES_FILE_NAME = "defaultColoring.xml"; // NOI18N
63
/* package */ static final String JavaDoc COLORING_FILE_NAME = "coloring.xml"; // NOI18N
64
/* package */ static final String JavaDoc HIGHLIGHTING_FILE_NAME = "editorColoring.xml"; // NOI18N
65

66     /** The name of the folder within a profile's folder containing module installed defaults. */
67     /* package */ static final String JavaDoc DEFAULTS_FOLDER = "Defaults"; //NOI18N
68

69     private static final String JavaDoc E_ROOT = "fontscolors"; //NOI18N
70
private static final String JavaDoc E_FONTCOLOR = "fontcolor"; //NOI18N
71
private static final String JavaDoc E_FONT = "font"; //NOI18N
72
private static final String JavaDoc A_NAME = "name"; //NOI18N
73
private static final String JavaDoc A_FOREGROUND = "foreColor"; //NOI18N
74
private static final String JavaDoc A_BACKGROUND = "bgColor"; //NOI18N
75
private static final String JavaDoc A_STRIKETHROUGH = "strikeThrough"; //NOI18N
76
private static final String JavaDoc A_WAVEUNDERLINE = "waveUnderlined"; //NOI18N
77
private static final String JavaDoc A_UNDERLINE = "underline"; //NOI18N
78
private static final String JavaDoc A_DEFAULT = "default"; //NOI18N
79
private static final String JavaDoc A_SIZE = "size"; //NOI18N
80
private static final String JavaDoc A_STYLE = "style"; //NOI18N
81
private static final String JavaDoc V_BOLD_ITALIC = "bold+italic"; //NOI18N
82
private static final String JavaDoc V_BOLD = "bold"; //NOI18N
83
private static final String JavaDoc V_ITALIC = "italic"; //NOI18N
84
private static final String JavaDoc V_PLAIN = "plain"; //NOI18N
85

86     private static final String JavaDoc PUBLIC_ID = "-//NetBeans//DTD Editor Fonts and Colors settings 1.1//EN"; //NOI18N
87
private static final String JavaDoc SYSTEM_ID = "http://www.netbeans.org/dtds/EditorFontsColors-1_1.dtd"; //NOI18N
88

89     private ColoringStorage() {
90     }
91     
92     // load ....................................................................
93

94     public static Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> loadColorings(
95         MimePath mimePath,
96         String JavaDoc profile,
97         boolean colorings, // true for colorings, false for highlightings
98
boolean defaults // read default values only
99
) {
100         String JavaDoc fileName = determineFileName(mimePath, colorings);
101         
102         // 1) load colorings
103
FileSystem fs = Repository.getDefault().getDefaultFileSystem();
104         FileObject fo = null;
105         
106         if (!defaults) {
107             fo = fs.findResource(Utils.getFileName(mimePath, profile, fileName));
108         }
109         
110         if (fo == null) {
111             fo = fs.findResource(Utils.getFileName(
112                 mimePath, profile, DEFAULTS_FOLDER + "/" + fileName)); //NOI18N
113
}
114         
115         if (fo == null) {
116             return null;
117         } else {
118             List JavaDoc l = (List JavaDoc) XMLStorage.load(fo, new ColoringsReader());
119             
120             // 2) translate names of categories
121
FileObject basicName = fs.findResource(
122                 Utils.getFileName(mimePath, EditorSettingsImpl.DEFAULT_PROFILE, DEFAULTS_FOLDER + "/" + fileName) //NOI18N
123
);
124             
125             Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> m = new HashMap JavaDoc<String JavaDoc, AttributeSet JavaDoc>();
126             Iterator JavaDoc it = l.iterator();
127             while (it.hasNext()) {
128                 SimpleAttributeSet JavaDoc as = (SimpleAttributeSet JavaDoc) it.next();
129                 String JavaDoc name = (String JavaDoc) as.getAttribute(StyleConstants.NameAttribute);
130                 String JavaDoc displayName = Utils.getLocalizedName(basicName, name, name);
131                 
132                 as.addAttribute(EditorStyleConstants.DisplayName, displayName);
133                 m.put(name, AttributesUtilities.createImmutable(as));
134             }
135             
136             return m;
137         }
138     }
139
140     private static class ColoringsReader extends XMLStorage.Handler {
141         private List JavaDoc<AttributeSet JavaDoc> colorings = new ArrayList JavaDoc<AttributeSet JavaDoc>();
142         
143         public Object JavaDoc getResult () {
144             return colorings;
145         }
146         
147         public void startElement (
148             String JavaDoc uri,
149             String JavaDoc localName,
150             String JavaDoc name,
151             Attributes JavaDoc attributes
152         ) throws SAXException JavaDoc {
153             try {
154                 if (name.equals(E_ROOT)) {
155                     // We don't read anythhing from the root element
156

157                 } else if (name.equals(E_FONTCOLOR)) {
158                     SimpleAttributeSet JavaDoc a = new SimpleAttributeSet JavaDoc();
159                     String JavaDoc value;
160                     
161                     a.addAttribute(StyleConstants.NameAttribute, attributes.getValue(A_NAME));
162                     
163                     value = attributes.getValue(A_BACKGROUND);
164                     if (value != null) {
165                         a.addAttribute(StyleConstants.Background, Utils.stringToColor(value));
166                     }
167                     
168                     value = attributes.getValue(A_FOREGROUND);
169                     if (value != null) {
170                         a.addAttribute(StyleConstants.Foreground, Utils.stringToColor(value));
171                     }
172
173                     value = attributes.getValue(A_UNDERLINE);
174                     if (value != null) {
175                         a.addAttribute(StyleConstants.Underline, Utils.stringToColor(value));
176                     }
177
178                     value = attributes.getValue(A_STRIKETHROUGH);
179                     if (value != null) {
180                         a.addAttribute(StyleConstants.StrikeThrough, Utils.stringToColor(value));
181                     }
182
183                     value = attributes.getValue(A_WAVEUNDERLINE);
184                     if (value != null) {
185                         a.addAttribute(EditorStyleConstants.WaveUnderlineColor, Utils.stringToColor(value));
186                     }
187                     
188                     value = attributes.getValue(A_DEFAULT);
189                     if (value != null) {
190                         a.addAttribute(EditorStyleConstants.Default, value);
191                     }
192                     
193                     colorings.add (a);
194                     
195                 } else if (name.equals(E_FONT)) {
196                     SimpleAttributeSet JavaDoc a = (SimpleAttributeSet JavaDoc) colorings.get(colorings.size() - 1);
197                     String JavaDoc value;
198                     
199                     value = attributes.getValue(A_NAME);
200                     if (value != null) {
201                         a.addAttribute(StyleConstants.FontFamily, value);
202                     }
203
204                     value = attributes.getValue(A_SIZE);
205                     if (value != null) {
206                         try {
207                             a.addAttribute(StyleConstants.FontSize, Integer.decode(value));
208                         } catch (NumberFormatException JavaDoc ex) {
209                             LOG.log(Level.WARNING, value + " is not a valid Integer; parsing " + //NOI18N
210
getProcessedFile().getPath(), ex);
211                         }
212                     }
213                     
214                     value = attributes.getValue(A_STYLE);
215                     if (value != null) {
216                         a.addAttribute(StyleConstants.Bold,
217                             Boolean.valueOf(value.indexOf(V_BOLD) >= 0)
218                         );
219                         a.addAttribute(
220                             StyleConstants.Italic,
221                             Boolean.valueOf(value.indexOf(V_ITALIC) >= 0)
222                         );
223                     }
224                 }
225             } catch (Exception JavaDoc ex) {
226                 LOG.log(Level.WARNING, "Can't parse colorings file " + getProcessedFile().getPath(), ex); //NOI18N
227
}
228         }
229         
230         public InputSource JavaDoc resolveEntity(String JavaDoc pubid, String JavaDoc sysid) {
231             return new InputSource JavaDoc(
232                 new java.io.ByteArrayInputStream JavaDoc(new byte [0])
233             );
234         }
235     } // End of ColoringsReader class
236

237     // delete ..........................................................
238

239     public static void deleteColorings(
240         MimePath mimePath,
241         String JavaDoc profile,
242         boolean colorings, // true for colorings, false for highlightings
243
boolean defaults // delete default values
244
) {
245         String JavaDoc fileName = determineFileName(mimePath, colorings);
246         if (defaults) {
247             fileName = DEFAULTS_FOLDER + "/" + fileName;
248         }
249
250         Utils.deleteFileObject(mimePath, profile, fileName);
251     }
252     
253     // save ..........................................................
254

255     public static void saveColorings(
256         MimePath mimePath,
257         String JavaDoc profile,
258         boolean colorings, // true for colorings, false for highlightings
259
boolean defaults, // save default values
260
Collection JavaDoc<AttributeSet JavaDoc> fontColors
261     ) {
262         String JavaDoc fileName = determineFileName(mimePath, colorings);
263         if (defaults) {
264             fileName = DEFAULTS_FOLDER + "/" + fileName;
265         }
266         
267         FileObject fo = Utils.createFileObject(mimePath, profile, fileName);
268         saveColorings(fo, fontColors);
269     }
270     
271     private static void saveColorings(FileObject fo, Collection JavaDoc<AttributeSet JavaDoc> colorings) {
272         Document JavaDoc doc = XMLUtil.createDocument(E_ROOT, null, PUBLIC_ID, SYSTEM_ID);
273         Node JavaDoc root = doc.getElementsByTagName(E_ROOT).item(0);
274         
275         for(AttributeSet JavaDoc category : colorings) {
276             Element JavaDoc fontColor = doc.createElement(E_FONTCOLOR);
277             root.appendChild(fontColor);
278             fontColor.setAttribute(A_NAME, (String JavaDoc) category.getAttribute(StyleConstants.NameAttribute));
279             
280             if (category.isDefined(StyleConstants.Foreground)) {
281                 fontColor.setAttribute(
282                     A_FOREGROUND,
283                     Utils.colorToString((Color JavaDoc) category.getAttribute(StyleConstants.Foreground))
284                 );
285             }
286             if (category.isDefined(StyleConstants.Background)) {
287                 fontColor.setAttribute(
288                     A_BACKGROUND,
289                     Utils.colorToString((Color JavaDoc) category.getAttribute(StyleConstants.Background))
290                 );
291             }
292             if (category.isDefined(StyleConstants.StrikeThrough)) {
293                 fontColor.setAttribute(
294                     A_STRIKETHROUGH,
295                     Utils.colorToString((Color JavaDoc) category.getAttribute(StyleConstants.StrikeThrough))
296                 );
297             }
298             if (category.isDefined(EditorStyleConstants.WaveUnderlineColor)) {
299                 fontColor.setAttribute(
300                     A_WAVEUNDERLINE,
301                     Utils.colorToString((Color JavaDoc) category.getAttribute(EditorStyleConstants.WaveUnderlineColor))
302                 );
303             }
304             if (category.isDefined(StyleConstants.Underline)) {
305                 fontColor.setAttribute(
306                     A_UNDERLINE,
307                     Utils.colorToString((Color JavaDoc) category.getAttribute(StyleConstants.Underline))
308                 );
309             }
310             if (category.isDefined(EditorStyleConstants.Default)) {
311                 fontColor.setAttribute(
312                     A_DEFAULT,
313                     (String JavaDoc) category.getAttribute(EditorStyleConstants.Default)
314                 );
315             }
316             
317             if ( category.isDefined(StyleConstants.FontFamily) ||
318                  category.isDefined(StyleConstants.FontSize) ||
319                  category.isDefined(StyleConstants.Bold) ||
320                  category.isDefined(StyleConstants.Italic)
321             ) {
322                 Element JavaDoc font = doc.createElement(E_FONT);
323                 fontColor.appendChild(font);
324                 
325                 if (category.isDefined(StyleConstants.FontFamily)) {
326                     font.setAttribute(
327                         A_NAME,
328                         (String JavaDoc) category.getAttribute(StyleConstants.FontFamily)
329                     );
330                 }
331                 if (category.isDefined(StyleConstants.FontSize)) {
332                     font.setAttribute(
333                         A_SIZE,
334                         ((Integer JavaDoc) category.getAttribute(StyleConstants.FontSize)).toString()
335                     );
336                 }
337                 if (category.isDefined(StyleConstants.Bold) ||
338                     category.isDefined(StyleConstants.Italic)
339                 ) {
340                     Boolean JavaDoc bold = Boolean.FALSE, italic = Boolean.FALSE;
341                     
342                     if (category.isDefined(StyleConstants.Bold)) {
343                         bold = (Boolean JavaDoc) category.getAttribute(StyleConstants.Bold);
344                     }
345                     if (category.isDefined(StyleConstants.Italic)) {
346                         italic = (Boolean JavaDoc) category.getAttribute(StyleConstants.Italic);
347                     }
348                     
349                     font.setAttribute(A_STYLE, bold.booleanValue() ?
350                         (italic.booleanValue() ? V_BOLD_ITALIC : V_BOLD) :
351                         (italic.booleanValue() ? V_ITALIC : V_PLAIN)
352                     );
353                 }
354             }
355         }
356
357         XMLStorage.save(fo, doc);
358     }
359
360     private static String JavaDoc determineFileName(MimePath mimePath, boolean colorings) {
361         String JavaDoc fileName;
362         
363         if (colorings) {
364             if (mimePath.size() == 0) {
365                 fileName = ALL_LANGUAGES_FILE_NAME;
366             } else {
367                 fileName = COLORING_FILE_NAME;
368             }
369         } else {
370             fileName = HIGHLIGHTING_FILE_NAME;
371         }
372         
373         return fileName;
374     }
375 }
376
Popular Tags