1 19 package org.netbeans.spi.editor.hints; 20 21 import java.beans.XMLDecoder ; 22 import java.beans.XMLEncoder ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import org.netbeans.modules.editor.hints.options.ProvidersListAccessor; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.FileLock; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.filesystems.Repository; 38 import org.openide.loaders.DataFolder; 39 import org.openide.loaders.FolderLookup; 40 import org.openide.util.Lookup.Template; 41 42 46 public final class ProvidersList { 47 48 static { 49 ProvidersListAccessor.INSTANCE = new ProvidersListAccessor() { 50 public Collection <String > getInstalledProvidersList() { 51 return ProvidersList.getInstalledProvidersList(); 52 } 53 public void setProviderEnabled(String providerKey, boolean enabled) { 54 ProvidersList.setProviderEnabled(providerKey, enabled); 55 } 56 public void setSeverity(String providerKey, String errorKey, Severity severity) { 57 ProvidersList.setSeverity(providerKey, errorKey, severity); 58 } 59 public ProviderDescription getProviderDescription(String providerKey) { 60 return ProvidersList.getProviderDescription(providerKey); 61 } 62 public List <ProviderDescription> getDescriptions() { 63 return ProvidersList.getDescriptions(); 64 } 65 }; 66 } 67 68 private static final String PROVIDERS_LIST_FOLDER = "EditorHints/"; private static final String CONFIG_FILE_NAME = "editorhintsconfig.xml"; 71 private static List <ProviderDescription> descriptions = null; 72 private static Map <String , Boolean > key2Enabled = null; 73 private static Map <String , ProviderDescription> key2Description = null; 74 private static Map <String , Map <String , Severity>> key2Error2Severity = null; 75 76 private static boolean initialized = false; 77 78 79 private ProvidersList() { 80 } 81 82 static synchronized Collection <String > getInstalledProvidersList() { 83 if (!initialized) { 84 init(); 85 initialized = true; 86 } 87 88 return key2Description.keySet(); 89 } 90 91 private static void init() { 92 key2Description = new HashMap <String , ProviderDescription>(); 93 descriptions = new ArrayList <ProviderDescription>(); 94 FileObject listFolder = Repository.getDefault().getDefaultFileSystem().findResource(PROVIDERS_LIST_FOLDER); 95 FolderLookup flookup = new FolderLookup(DataFolder.findContainer(listFolder)); 96 Collection <? extends ProviderDescription> looked = flookup.getLookup().lookupAll(ProviderDescription.class); 97 98 for (ProviderDescription desc : looked) { 99 key2Description.put(desc.getKey(), desc); 100 descriptions.add(desc); 101 } 102 103 FileObject configFile = Repository.getDefault().getDefaultFileSystem().findResource(CONFIG_FILE_NAME); 104 105 if (configFile != null) { 106 InputStream ins = null; 107 108 try { 109 ins = configFile.getInputStream(); 110 111 XMLDecoder decoder = new XMLDecoder (ins); 112 113 key2Error2Severity = (Map <String , Map <String , Severity>>) decoder.readObject(); 114 key2Enabled = (Map <String , Boolean >) decoder.readObject(); 115 116 decoder.close(); 117 } catch (IOException e) { 118 ErrorManager.getDefault().notify(e); 119 } finally { 120 if (ins != null) { 121 try { 122 ins.close(); 123 } catch (IOException e) { 124 ErrorManager.getDefault().notify(e); 125 } 126 } 127 } 128 } else { 129 key2Error2Severity = new HashMap <String , Map <String , Severity>>(); 130 key2Enabled = new HashMap <String , Boolean >(); 131 } 132 } 133 134 private static void save() { 135 FileLock lock = null; 136 OutputStream out = null; 137 138 try { 139 FileObject configFile = Repository.getDefault().getDefaultFileSystem().findResource(CONFIG_FILE_NAME); 140 141 if (configFile == null) { 142 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 143 144 configFile = FileUtil.createData(root, CONFIG_FILE_NAME); 145 } 146 147 lock = configFile.lock(); 148 out = configFile.getOutputStream(lock); 149 150 XMLEncoder encoder = new XMLEncoder (out); 151 152 encoder.writeObject(key2Error2Severity); 153 encoder.writeObject(key2Enabled); 154 155 encoder.close(); 156 } catch (IOException e) { 157 ErrorManager.getDefault().notify(e); 158 } finally { 159 if (out != null) { 160 try { 161 out.close(); 162 } catch (IOException e) { 163 ErrorManager.getDefault().notify(e); 164 } 165 } 166 167 if (lock != null) { 168 lock.releaseLock(); 169 } 170 } 171 } 172 173 178 public static boolean isProviderEnabled(String providerKey) { 179 if (!initialized) { 180 init(); 181 initialized = true; 182 } 183 184 if (!key2Description.containsKey(providerKey)) { 185 throw new IllegalArgumentException ("Unknown provider key: " + providerKey); } 187 188 Boolean enabled = (Boolean ) key2Enabled.get(providerKey); 189 190 if (enabled == null) { 191 ProviderDescription desc = getProviderDescription(providerKey); 192 193 return desc.getDefaultState(); 194 } 195 196 return enabled.booleanValue(); 197 } 198 199 205 public static synchronized Severity getErrorSeverity(String providerKey, String errorKey) { 206 if (!initialized) { 207 init(); 208 initialized = true; 209 } 210 211 if (!key2Description.containsKey(providerKey)) { 212 throw new IllegalArgumentException ("Unknown provider key: " + providerKey); } 214 215 Map <String , Severity> error2Severity = key2Error2Severity.get(providerKey); 216 217 if (error2Severity == null) { 218 key2Error2Severity.put(providerKey, error2Severity = new HashMap <String , Severity>()); 219 } 220 221 Severity severity = error2Severity.get(errorKey); 222 223 if (severity == null) { 224 ProviderDescription desc = getProviderDescription(providerKey); 225 226 if (desc.getSupportedErrorKeys().contains(errorKey)) { 227 return desc.getErrorDefaultSeverity(errorKey); 228 } else { 229 throw new IllegalArgumentException ("Unknown error key: " + errorKey + " for provider: " + providerKey); } 231 } 232 233 return severity; 234 } 235 236 238 static synchronized void setProviderEnabled(String providerKey, boolean enabled) { 239 if (!initialized) { 240 init(); 241 initialized = true; 242 } 243 244 if (!key2Description.containsKey(providerKey)) { 245 throw new IllegalArgumentException ("Unknown provider key: " + providerKey); } 247 248 key2Enabled.put(providerKey, Boolean.valueOf(enabled)); 249 250 save(); 251 } 252 253 255 static synchronized void setSeverity(String providerKey, String errorKey, Severity severity) { 256 if (!initialized) { 257 init(); 258 initialized = true; 259 } 260 261 if (!getProviderDescription(providerKey).getSupportedErrorKeys().contains(errorKey)) { 262 throw new IllegalArgumentException ("Unknown error key: " + errorKey + " for provider: " + providerKey); } 264 265 Map <String , Severity> error2Severity = key2Error2Severity.get(providerKey); 266 267 if (error2Severity == null) { 268 key2Error2Severity.put(providerKey, error2Severity = new HashMap <String , Severity>()); 269 } 270 271 error2Severity.put(errorKey, severity); 272 273 save(); 274 } 275 276 278 static synchronized ProviderDescription getProviderDescription(String providerKey) { 279 if (!initialized) { 280 init(); 281 initialized = true; 282 } 283 284 if (!key2Description.containsKey(providerKey)) { 285 throw new IllegalArgumentException ("Unknown provider key: " + providerKey); } 287 288 return (ProviderDescription) key2Description.get(providerKey); 289 } 290 291 293 static synchronized List <ProviderDescription> getDescriptions() { 294 if (!initialized) { 295 init(); 296 initialized = true; 297 } 298 299 return descriptions; 300 } 301 302 } 333 | Popular Tags |