1 19 package org.netbeans.modules.xml.catalog.settings; 20 21 import java.io.*; 22 import java.util.*; 23 import java.beans.*; 24 import org.netbeans.modules.xml.catalog.lib.IteratorIterator; 25 26 import org.openide.*; 27 import org.openide.util.HelpCtx; 28 import org.openide.util.io.NbMarshalledObject; 29 30 import org.netbeans.modules.xml.catalog.spi.*; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.Repository; 34 import org.openide.loaders.DataFolder; 35 import org.openide.loaders.DataObject; 36 import org.openide.loaders.DataObjectNotFoundException; 37 import org.openide.loaders.FolderLookup; 38 import org.openide.util.Lookup; 39 import org.openide.util.LookupListener; 40 import org.openide.util.LookupEvent; 41 import org.openide.util.lookup.Lookups; 42 43 44 77 public final class CatalogSettings implements Externalizable { 78 79 80 private static final long serialVersionUID = 7895789034L; 81 82 public static final int VERSION_1 = 1; 84 85 public static final String PROP_MOUNTED_CATALOGS = "catalogs"; 87 private static final String REGISTRATIONS = "Plugins/XML/UserCatalogs"; 89 90 95 public static final String PROP_PRJ_INSTANCE = "cat-prj-in"; 96 97 private static Lookup userCatalogLookup; 99 100 private List mountedCatalogs = new ArrayList(5); 102 103 private PropertyChangeSupport listeners = null; 104 105 private final CatalogListener catalogListener = new CL(); 106 107 private static ErrorManager err = null; 108 109 private static Lookup.Result result = null; 111 112 private static CatalogSettings instance = null; 114 115 119 public CatalogSettings() { 120 init(); 121 } 122 123 124 127 private void init() { 128 listeners = new PropertyChangeSupport(this); 129 } 130 131 132 136 public static synchronized CatalogSettings getDefault() { 137 if (result == null) { 138 result = Lookup.getDefault().lookup(new Lookup.Template(CatalogSettings.class)); 139 140 result.addLookupListener(new LookupListener() { 142 public void resultChanged(LookupEvent e) { 143 CatalogSettings oldSettings = instance; 144 synchronized (CatalogSettings.class) { 145 instance = (CatalogSettings) Lookup.getDefault().lookup(CatalogSettings.class); 146 } 147 if (oldSettings != null) { 148 oldSettings.firePropertyChange(PROP_PRJ_INSTANCE, oldSettings, instance); 149 } 150 } 151 }); 152 153 instance = (CatalogSettings) result.allInstances().iterator().next(); 155 } 156 return instance; 157 } 158 159 163 public final void addCatalog(CatalogReader provider) { 164 synchronized (this) { 165 if (provider == null) 166 throw new IllegalArgumentException ("null provider not permited"); if (mountedCatalogs.contains(provider) == false) { 168 mountedCatalogs.add(provider); 169 } 170 } 171 firePropertyChange(PROP_MOUNTED_CATALOGS, null, null); 172 173 try { 175 provider.addCatalogListener(catalogListener); 176 } catch (UnsupportedOperationException ex) { 177 } 181 } 182 183 186 public final void removeCatalog(CatalogReader provider) { 187 synchronized (this) { 188 mountedCatalogs.remove(provider); 189 } 190 firePropertyChange(PROP_MOUNTED_CATALOGS, null, null); 191 192 try { 194 provider.removeCatalogListener(catalogListener); 195 } catch (UnsupportedOperationException ex) { 196 } 198 199 } 200 201 206 public final boolean isRemovable(CatalogReader provider) { 207 return mountedCatalogs.contains(provider); 208 } 209 210 219 public final synchronized Iterator getCatalogs(Class [] providerClasses) { 220 221 IteratorIterator it = new IteratorIterator(); 223 it.add(mountedCatalogs.iterator()); 224 225 Lookup.Template template = new Lookup.Template(CatalogReader.class); 226 Lookup.Result result = getUserCatalogsLookup().lookup(template); 227 it.add(result.allInstances().iterator()); 228 229 if (providerClasses == null) 230 return it; 231 232 ArrayList list = new ArrayList(); 233 234 while (it.hasNext()) { 235 Object next = it.next(); 236 boolean add = true; 238 for (int i=0; i<providerClasses.length; i++) { 239 if (!providerClasses[i].isAssignableFrom(next.getClass())) { 240 add = false; 241 break; 242 } 243 } 244 if (add) list.add(next); 246 } 247 return list.iterator(); 248 } 249 250 253 private static Lookup getUserCatalogsLookup() { 254 if (userCatalogLookup == null) { 255 FileSystem sfs = Repository.getDefault().getDefaultFileSystem(); 256 FileObject folder = sfs.findResource(REGISTRATIONS); 257 if (folder == null) return Lookups.fixed(new Object [0]); 259 try { 260 DataFolder dataFolder = (DataFolder) DataObject.find(folder); 261 userCatalogLookup = new FolderLookup(dataFolder).getLookup(); 262 } catch (DataObjectNotFoundException ex) { 263 throw new IllegalStateException ("There must be DataFolder for " + folder + "!"); 264 } 265 } 266 return userCatalogLookup; 267 } 268 269 271 272 public void addPropertyChangeListener(PropertyChangeListener l){ 273 listeners.addPropertyChangeListener(l); 274 } 275 276 public void removePropertyChangeListener(PropertyChangeListener l) { 277 listeners.removePropertyChangeListener(l); 278 } 279 280 private void firePropertyChange(String name, Object oldValue, Object newValue) { 281 listeners.firePropertyChange(name, oldValue, newValue); 282 } 283 284 286 287 290 public synchronized void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 291 293 if ( Util.THIS.isLoggable() ) Util.THIS.debug("CatalogSettings.readExternal()"); 295 int version = in.readInt(); 297 299 if (version != VERSION_1) throw new StreamCorruptedException("Unsupported catalog externalization protocol version (" + version + ")."); 301 int persistentCount = in.readInt(); 303 for (int i = 0; i<persistentCount; i++) { 304 305 String catalogClass = (String ) in.readObject(); NbMarshalledObject marshaled = (NbMarshalledObject) in.readObject(); try { 308 Object unmarshaled = marshaled.get(); 309 if (mountedCatalogs.contains(unmarshaled) == false) { 310 mountedCatalogs.add(unmarshaled); 311 } 312 } catch (ClassNotFoundException ex) { 313 emgr().annotate(ex, Util.THIS.getString("EXC_deserialization_failed", catalogClass)); 315 emgr().notify(ErrorManager.INFORMATIONAL, ex); 316 } catch (IOException ex) { 317 emgr().annotate(ex, Util.THIS.getString("EXC_deserialization_failed", catalogClass)); 319 emgr().notify(ErrorManager.INFORMATIONAL, ex); 320 } catch (RuntimeException ex) { 321 emgr().annotate(ex, Util.THIS.getString("EXC_deserialization_failed", catalogClass)); 323 emgr().notify(ErrorManager.INFORMATIONAL, ex); 324 } 325 } 326 } 327 328 331 public synchronized void writeExternal(ObjectOutput out) throws IOException { 332 334 if ( Util.THIS.isLoggable() ) Util.THIS.debug("CatalogSettings.writeExternal()"); 336 out.writeInt(VERSION_1); 338 int persistentCount = 0; 339 340 Iterator it = mountedCatalogs.iterator(); 341 342 while (it.hasNext()) { 343 Object next = it.next(); 344 if (next instanceof Serializable) { 345 persistentCount++; 346 } 347 } 348 349 it = mountedCatalogs.iterator(); 350 351 out.writeInt(persistentCount); 353 while (it.hasNext()) { 354 Object next = it.next(); 355 if (next instanceof Serializable) { 356 try { 357 NbMarshalledObject marshaled = new NbMarshalledObject(next); 358 out.writeObject(next.getClass().getName()); out.writeObject(marshaled); } catch (IOException ex) { 361 emgr().annotate(ex, Util.THIS.getString("EXC_serialization_failed", next.getClass())); 363 emgr().notify(ErrorManager.INFORMATIONAL, ex); 364 } catch (RuntimeException ex) { 365 emgr().annotate(ex, Util.THIS.getString("EXC_serialization_failed", next.getClass())); 367 emgr().notify(ErrorManager.INFORMATIONAL, ex); 368 } 369 } 370 } 371 } 372 373 374 private ErrorManager emgr() { 375 return ErrorManager.getDefault(); 376 } 377 378 379 382 public String toString() { 383 Lookup.Template template = new Lookup.Template(CatalogReader.class); 384 Lookup.Result result = getUserCatalogsLookup().lookup(template); 385 return "CatalogSettings[ global-scope: " + result.allInstances() + 386 ", project-scope: " + mountedCatalogs + " ]"; 387 } 388 389 390 394 private class CL implements CatalogListener { 395 396 397 public void notifyRemoved(String publicID) { 398 } 399 400 401 public void notifyNew(String publicID) { 402 } 403 404 405 public void notifyUpdate(String publicID) { 406 } 407 408 411 public void notifyInvalidate() { 412 firePropertyChange("settings changed!", null, CatalogSettings.this); 413 } 414 } 415 416 } 417 | Popular Tags |