KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Font JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.MissingResourceException JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34 import java.util.StringTokenizer JavaDoc;
35 import java.util.WeakHashMap JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38 import javax.swing.KeyStroke JavaDoc;
39 import javax.swing.text.AttributeSet JavaDoc;
40 import javax.swing.text.StyleConstants JavaDoc;
41 import org.netbeans.api.editor.mimelookup.MimePath;
42 import org.netbeans.api.editor.settings.AttributesUtilities;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileSystem;
45 import org.openide.filesystems.FileUtil;
46 import org.openide.filesystems.Repository;
47 import org.openide.util.NbBundle;
48 import org.openide.util.Utilities;
49
50
51 /**
52  * This class contains support static methods for loading / saving and
53  * translating coloring (fontsColors.xml) files. It calls XMLStorage utilities.
54  *
55  * @author Jan Jancura
56  */

57 public class Utils {
58     
59     private static final Logger JavaDoc LOG = Logger.getLogger(Utils.class.getName());
60     
61     private static final Map JavaDoc<Color JavaDoc, String JavaDoc> colorToName = new HashMap JavaDoc<Color JavaDoc, String JavaDoc>();
62     private static final Map JavaDoc<String JavaDoc, Color JavaDoc> nameToColor = new HashMap JavaDoc<String JavaDoc, Color JavaDoc>();
63     private static final Map JavaDoc<String JavaDoc, Integer JavaDoc> nameToFontStyle = new HashMap JavaDoc<String JavaDoc, Integer JavaDoc>();
64     private static final Map JavaDoc<Integer JavaDoc, String JavaDoc> fontStyleToName = new HashMap JavaDoc<Integer JavaDoc, String JavaDoc>();
65     static {
66         colorToName.put (Color.black, "black");
67         nameToColor.put ("black", Color.black);
68         colorToName.put (Color.blue, "blue");
69         nameToColor.put ("blue", Color.blue);
70         colorToName.put (Color.cyan, "cyan");
71         nameToColor.put ("cyan", Color.cyan);
72         colorToName.put (Color.darkGray, "darkGray");
73         nameToColor.put ("darkGray", Color.darkGray);
74         colorToName.put (Color.gray, "gray");
75         nameToColor.put ("gray", Color.gray);
76         colorToName.put (Color.green, "green");
77         nameToColor.put ("green", Color.green);
78         colorToName.put (Color.lightGray, "lightGray");
79         nameToColor.put ("lightGray", Color.lightGray);
80         colorToName.put (Color.magenta, "magenta");
81         nameToColor.put ("magenta", Color.magenta);
82         colorToName.put (Color.orange, "orange");
83         nameToColor.put ("orange", Color.orange);
84         colorToName.put (Color.pink, "pink");
85         nameToColor.put ("pink", Color.pink);
86         colorToName.put (Color.red, "red");
87         nameToColor.put ("red", Color.red);
88         colorToName.put (Color.white, "white");
89         nameToColor.put ("white", Color.white);
90         colorToName.put (Color.yellow, "yellow");
91         nameToColor.put ("yellow", Color.yellow);
92         
93         nameToFontStyle.put ("plain", new Integer JavaDoc (Font.PLAIN));
94         fontStyleToName.put (new Integer JavaDoc (Font.PLAIN), "plain");
95         nameToFontStyle.put ("bold", new Integer JavaDoc (Font.BOLD));
96         fontStyleToName.put (new Integer JavaDoc (Font.BOLD), "bold");
97         nameToFontStyle.put ("italic", new Integer JavaDoc (Font.ITALIC));
98         fontStyleToName.put (new Integer JavaDoc (Font.ITALIC), "italic");
99         nameToFontStyle.put ("bold+italic", new Integer JavaDoc (Font.BOLD + Font.ITALIC));
100         fontStyleToName.put (new Integer JavaDoc (Font.BOLD + Font.ITALIC), "bold+italic");
101     }
102     
103     static String JavaDoc colorToString (Color JavaDoc color) {
104     if (colorToName.containsKey (color))
105         return (String JavaDoc) colorToName.get (color);
106     return Integer.toHexString (color.getRGB ());
107     }
108     
109     static Color JavaDoc stringToColor (String JavaDoc color) throws Exception JavaDoc {
110     if (nameToColor.containsKey (color))
111         return (Color JavaDoc) nameToColor.get (color);
112         try {
113             return new Color JavaDoc ((int) Long.parseLong (color, 16));
114         } catch (NumberFormatException JavaDoc ex) {
115             throw new Exception JavaDoc ();
116         }
117     }
118     
119     static String JavaDoc keyStrokesToString (Collection JavaDoc<KeyStroke JavaDoc> keys) {
120         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
121         
122         Iterator JavaDoc<KeyStroke JavaDoc> it = keys.iterator();
123         if (it.hasNext ()) {
124             sb.append(Utilities.keyToString(it.next()));
125             //S ystem.out.println("2 " + keys [0] + ">" + Utilities.keyToString (keys [0]));
126
while (it.hasNext()) {
127                 sb.append ('$').
128                     append(Utilities.keyToString(it.next()));
129                 //S ystem.out.println("2 " + keys [i] + ">" + Utilities.keyToString (keys [i]));
130
}
131         }
132         
133         return sb.toString ();
134     }
135     
136     static KeyStroke JavaDoc[] stringToKeyStrokes (String JavaDoc key) {
137         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (key, "$");
138         List JavaDoc<KeyStroke JavaDoc> result = new ArrayList JavaDoc<KeyStroke JavaDoc>();
139         key = null;
140         while (st.hasMoreTokens ()) {
141             String JavaDoc ks = st.nextToken ().trim ();
142             KeyStroke JavaDoc keyStroke = Utilities.stringToKey (ks);
143             //S ystem.out.println("1 " + ks + ">" + keyStroke);
144
if (keyStroke == null) {
145                 LOG.fine("no key stroke for:" + key);
146                 continue;
147             }
148 // if (key == null)
149
// key = Utilities.keyToString (keyStroke);
150
// else
151
// key += "$" + Utilities.keyToString (keyStroke);
152
result.add(keyStroke);
153         }
154         return result.toArray(new KeyStroke JavaDoc[result.size ()]);
155     }
156     
157     static FileObject getFileObject(MimePath mimePath, String JavaDoc profile, String JavaDoc fileNameExt) {
158         String JavaDoc name = getFileName(mimePath, profile, fileNameExt);
159         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
160         return fs.findResource(name);
161     }
162     
163     /**
164      * Crates FileObject for given mimeTypes and profile.
165      */

166     static FileObject createFileObject(MimePath mimePath, String JavaDoc profile, String JavaDoc fileName) {
167         String JavaDoc name = getFileName(mimePath, profile, fileName);
168         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
169         try {
170             if (fileName == null) {
171                 return FileUtil.createFolder(fs.getRoot(), name);
172             } else {
173                 return FileUtil.createData(fs.getRoot(), name);
174             }
175         } catch (IOException JavaDoc ex) {
176             LOG.log(Level.WARNING, "Can't create editor settings file or folder: " + name, ex); //NOI18N
177
return null;
178         }
179     }
180     
181     /**
182      * Crates FileObject for given mimeTypes and profile.
183      */

184     static void deleteFileObject(MimePath mimePath, String JavaDoc profile, String JavaDoc fileName) {
185         String JavaDoc name = getFileName(mimePath, profile, fileName);
186         FileSystem fs = Repository.getDefault().getDefaultFileSystem();
187         FileObject fo = fs.findResource(name);
188         if (fo != null) {
189             try {
190                 fo.delete();
191             } catch (IOException JavaDoc ex) {
192                 LOG.log(Level.WARNING, "Can't delete editor settings file " + fo.getPath(), ex); //NOI18N
193
}
194         }
195     }
196     
197     static String JavaDoc getFileName(MimePath mimePath, String JavaDoc profile, String JavaDoc fileName) {
198         StringBuilder JavaDoc sb = new StringBuilder JavaDoc("Editors");
199         
200         if (mimePath.size() > 0) {
201             sb.append('/').append(mimePath.getPath());
202         }
203         
204         if (profile != null) {
205             sb.append('/').append(profile);
206         }
207         
208         if (fileName != null) {
209             sb.append('/').append(fileName);
210         }
211         
212         return sb.toString();
213     }
214
215     private static FileObject createFile (FileObject fo, String JavaDoc next) throws IOException JavaDoc {
216         FileObject fo1 = fo.getFileObject (next);
217         if (fo1 == null)
218             return fo.createFolder (next);
219         return fo1;
220     }
221     
222     static String JavaDoc getLocalizedName(FileObject fo, String JavaDoc key, String JavaDoc defaultValue) {
223         assert key != null : "The key can't be null"; //NOI18N
224

225         Object JavaDoc [] bundleInfo = findResourceBundle(fo);
226         if (bundleInfo[1] != null) {
227             try {
228                 return ((ResourceBundle JavaDoc) bundleInfo[1]).getString(key);
229             } catch (MissingResourceException JavaDoc ex) {
230                 LOG.log(Level.WARNING, "The bundle '" + bundleInfo[0] + "' is missing key '" + key + "'.", ex); //NOI18N
231
}
232         }
233         
234         return defaultValue;
235     }
236
237     private static final WeakHashMap JavaDoc<FileObject, Object JavaDoc []> bundleInfos = new WeakHashMap JavaDoc<FileObject, Object JavaDoc []>();
238     private static Object JavaDoc [] findResourceBundle(FileObject fo) {
239         assert fo != null : "FileObject can't be null"; //NOI18N
240

241         synchronized (bundleInfos) {
242             Object JavaDoc [] bundleInfo = bundleInfos.get(fo);
243             if (bundleInfo == null) {
244                 String JavaDoc bundleName = null;
245                 Object JavaDoc attrValue = fo.getAttribute("SystemFileSystem.localizingBundle"); //NOI18N
246
if (attrValue instanceof String JavaDoc) {
247                     bundleName = (String JavaDoc) attrValue;
248                 }
249
250                 if (bundleName != null) {
251                     try {
252                         bundleInfo = new Object JavaDoc [] { bundleName, NbBundle.getBundle(bundleName) };
253                     } catch (MissingResourceException JavaDoc ex) {
254                         LOG.log(Level.WARNING, "Can't find resource bundle for " + fo.getPath(), ex); //NOI18N
255
}
256                 } else {
257                     //[PENDING][HACK] LOG.log(Level.WARNING, "The file " + fo.getPath() + " does not specify its resource bundle.", new Throwable("@@@")); //NOI18N
258
}
259
260                 if (bundleInfo == null) {
261                    bundleInfo = new Object JavaDoc [] { bundleName, null };
262                 }
263
264                 bundleInfos.put(fo, bundleInfo);
265             }
266
267             return bundleInfo;
268         }
269     }
270     
271     /**
272      * Converts an array of mime types to a <code>MimePath</code> instance.
273      */

274     public static MimePath mimeTypes2mimePath(String JavaDoc[] mimeTypes) {
275         MimePath mimePath = MimePath.EMPTY;
276         
277         for (int i = 0; i < mimeTypes.length; i++) {
278             mimePath = MimePath.get(mimePath, mimeTypes[i]);
279         }
280         
281         return mimePath;
282     }
283
284     /**
285      * Creates unmodifiable copy of the original map converting <code>AttributeSet</code>s
286      * to their immutable versions.
287      */

288     public static Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> immutize(Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> map) {
289         Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> immutizedMap = new HashMap JavaDoc<String JavaDoc, AttributeSet JavaDoc>();
290         
291         for(String JavaDoc name : map.keySet()) {
292             AttributeSet JavaDoc attribs = map.get(name);
293             immutizedMap.put(name, AttributesUtilities.createImmutable(attribs));
294         }
295         
296         return Collections.unmodifiableMap(immutizedMap);
297     }
298     
299     public static Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> immutize(Collection JavaDoc<AttributeSet JavaDoc> set) {
300         Map JavaDoc<String JavaDoc, AttributeSet JavaDoc> immutizedMap = new HashMap JavaDoc<String JavaDoc, AttributeSet JavaDoc>();
301     
302         for(AttributeSet JavaDoc as : set) {
303             Object JavaDoc nameObject = as.getAttribute(StyleConstants.NameAttribute);
304             if (nameObject instanceof String JavaDoc) {
305                 immutizedMap.put((String JavaDoc) nameObject, as);
306             } else {
307                 LOG.warning("Ignoring AttributeSet with invalid StyleConstants.NameAttribute. AttributeSet: " + as); //NOI18N
308
}
309         }
310             
311         return Collections.unmodifiableMap(immutizedMap);
312     }
313     
314 }
315
Popular Tags