1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.awt.Color ; 23 import java.awt.Font ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.MissingResourceException ; 33 import java.util.ResourceBundle ; 34 import java.util.StringTokenizer ; 35 import java.util.WeakHashMap ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 import javax.swing.KeyStroke ; 39 import javax.swing.text.AttributeSet ; 40 import javax.swing.text.StyleConstants ; 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 57 public class Utils { 58 59 private static final Logger LOG = Logger.getLogger(Utils.class.getName()); 60 61 private static final Map <Color , String > colorToName = new HashMap <Color , String >(); 62 private static final Map <String , Color > nameToColor = new HashMap <String , Color >(); 63 private static final Map <String , Integer > nameToFontStyle = new HashMap <String , Integer >(); 64 private static final Map <Integer , String > fontStyleToName = new HashMap <Integer , String >(); 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 (Font.PLAIN)); 94 fontStyleToName.put (new Integer (Font.PLAIN), "plain"); 95 nameToFontStyle.put ("bold", new Integer (Font.BOLD)); 96 fontStyleToName.put (new Integer (Font.BOLD), "bold"); 97 nameToFontStyle.put ("italic", new Integer (Font.ITALIC)); 98 fontStyleToName.put (new Integer (Font.ITALIC), "italic"); 99 nameToFontStyle.put ("bold+italic", new Integer (Font.BOLD + Font.ITALIC)); 100 fontStyleToName.put (new Integer (Font.BOLD + Font.ITALIC), "bold+italic"); 101 } 102 103 static String colorToString (Color color) { 104 if (colorToName.containsKey (color)) 105 return (String ) colorToName.get (color); 106 return Integer.toHexString (color.getRGB ()); 107 } 108 109 static Color stringToColor (String color) throws Exception { 110 if (nameToColor.containsKey (color)) 111 return (Color ) nameToColor.get (color); 112 try { 113 return new Color ((int) Long.parseLong (color, 16)); 114 } catch (NumberFormatException ex) { 115 throw new Exception (); 116 } 117 } 118 119 static String keyStrokesToString (Collection <KeyStroke > keys) { 120 StringBuffer sb = new StringBuffer (); 121 122 Iterator <KeyStroke > it = keys.iterator(); 123 if (it.hasNext ()) { 124 sb.append(Utilities.keyToString(it.next())); 125 while (it.hasNext()) { 127 sb.append ('$'). 128 append(Utilities.keyToString(it.next())); 129 } 131 } 132 133 return sb.toString (); 134 } 135 136 static KeyStroke [] stringToKeyStrokes (String key) { 137 StringTokenizer st = new StringTokenizer (key, "$"); 138 List <KeyStroke > result = new ArrayList <KeyStroke >(); 139 key = null; 140 while (st.hasMoreTokens ()) { 141 String ks = st.nextToken ().trim (); 142 KeyStroke keyStroke = Utilities.stringToKey (ks); 143 if (keyStroke == null) { 145 LOG.fine("no key stroke for:" + key); 146 continue; 147 } 148 result.add(keyStroke); 153 } 154 return result.toArray(new KeyStroke [result.size ()]); 155 } 156 157 static FileObject getFileObject(MimePath mimePath, String profile, String fileNameExt) { 158 String name = getFileName(mimePath, profile, fileNameExt); 159 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 160 return fs.findResource(name); 161 } 162 163 166 static FileObject createFileObject(MimePath mimePath, String profile, String fileName) { 167 String 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 ex) { 176 LOG.log(Level.WARNING, "Can't create editor settings file or folder: " + name, ex); return null; 178 } 179 } 180 181 184 static void deleteFileObject(MimePath mimePath, String profile, String fileName) { 185 String 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 ex) { 192 LOG.log(Level.WARNING, "Can't delete editor settings file " + fo.getPath(), ex); } 194 } 195 } 196 197 static String getFileName(MimePath mimePath, String profile, String fileName) { 198 StringBuilder sb = new StringBuilder ("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 next) throws IOException { 216 FileObject fo1 = fo.getFileObject (next); 217 if (fo1 == null) 218 return fo.createFolder (next); 219 return fo1; 220 } 221 222 static String getLocalizedName(FileObject fo, String key, String defaultValue) { 223 assert key != null : "The key can't be null"; 225 Object [] bundleInfo = findResourceBundle(fo); 226 if (bundleInfo[1] != null) { 227 try { 228 return ((ResourceBundle ) bundleInfo[1]).getString(key); 229 } catch (MissingResourceException ex) { 230 LOG.log(Level.WARNING, "The bundle '" + bundleInfo[0] + "' is missing key '" + key + "'.", ex); } 232 } 233 234 return defaultValue; 235 } 236 237 private static final WeakHashMap <FileObject, Object []> bundleInfos = new WeakHashMap <FileObject, Object []>(); 238 private static Object [] findResourceBundle(FileObject fo) { 239 assert fo != null : "FileObject can't be null"; 241 synchronized (bundleInfos) { 242 Object [] bundleInfo = bundleInfos.get(fo); 243 if (bundleInfo == null) { 244 String bundleName = null; 245 Object attrValue = fo.getAttribute("SystemFileSystem.localizingBundle"); if (attrValue instanceof String ) { 247 bundleName = (String ) attrValue; 248 } 249 250 if (bundleName != null) { 251 try { 252 bundleInfo = new Object [] { bundleName, NbBundle.getBundle(bundleName) }; 253 } catch (MissingResourceException ex) { 254 LOG.log(Level.WARNING, "Can't find resource bundle for " + fo.getPath(), ex); } 256 } else { 257 } 259 260 if (bundleInfo == null) { 261 bundleInfo = new Object [] { bundleName, null }; 262 } 263 264 bundleInfos.put(fo, bundleInfo); 265 } 266 267 return bundleInfo; 268 } 269 } 270 271 274 public static MimePath mimeTypes2mimePath(String [] 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 288 public static Map <String , AttributeSet > immutize(Map <String , AttributeSet > map) { 289 Map <String , AttributeSet > immutizedMap = new HashMap <String , AttributeSet >(); 290 291 for(String name : map.keySet()) { 292 AttributeSet attribs = map.get(name); 293 immutizedMap.put(name, AttributesUtilities.createImmutable(attribs)); 294 } 295 296 return Collections.unmodifiableMap(immutizedMap); 297 } 298 299 public static Map <String , AttributeSet > immutize(Collection <AttributeSet > set) { 300 Map <String , AttributeSet > immutizedMap = new HashMap <String , AttributeSet >(); 301 302 for(AttributeSet as : set) { 303 Object nameObject = as.getAttribute(StyleConstants.NameAttribute); 304 if (nameObject instanceof String ) { 305 immutizedMap.put((String ) nameObject, as); 306 } else { 307 LOG.warning("Ignoring AttributeSet with invalid StyleConstants.NameAttribute. AttributeSet: " + as); } 309 } 310 311 return Collections.unmodifiableMap(immutizedMap); 312 } 313 314 } 315 | Popular Tags |