1 19 20 package org.netbeans.api.project.libraries; 21 22 23 import org.netbeans.api.project.libraries.Library; 24 import org.netbeans.spi.project.libraries.LibraryImplementation; 25 import org.netbeans.spi.project.libraries.LibraryProvider; 26 import org.openide.util.Lookup; 27 import org.openide.util.LookupListener; 28 import org.openide.util.LookupEvent; 29 30 import java.beans.PropertyChangeSupport ; 31 import java.beans.PropertyChangeListener ; 32 import java.beans.PropertyChangeEvent ; 33 import java.io.IOException ; 34 import java.util.*; 35 import org.netbeans.modules.project.libraries.WritableLibraryProvider; 36 import org.netbeans.spi.project.libraries.LibraryFactory; 37 import org.netbeans.spi.project.libraries.support.LibrariesSupport; 38 39 41 46 public final class LibraryManager { 47 48 public static final String PROP_LIBRARIES = "libraries"; 50 private static LibraryManager instance; 51 52 private Lookup.Result<LibraryProvider> result; 53 private Collection<LibraryProvider> currentStorages = new ArrayList<LibraryProvider>(); 54 private PropertyChangeListener plistener; 55 private PropertyChangeSupport listeners; 56 private Collection<Library> cache; 57 58 59 private LibraryManager () { 60 this.listeners = new PropertyChangeSupport (this); 61 } 62 63 68 public Library getLibrary(String name) { 69 assert name != null; 70 Library[] libs = this.getLibraries(); 71 for (int i = 0; i < libs.length; i++) { 72 if (name.equals(libs[i].getName())) { 73 return libs[i]; 74 } 75 } 76 return null; 77 } 78 79 84 public synchronized Library[] getLibraries() { 85 if (this.cache == null) { 86 if (this.result == null) { 87 plistener = new PropertyChangeListener () { 88 public void propertyChange(PropertyChangeEvent evt) { 89 resetCache (); 90 } 91 }; 92 result = Lookup.getDefault().lookupResult(LibraryProvider.class); 93 result.addLookupListener (new LookupListener() { 94 public void resultChanged(LookupEvent ev) { 95 resetCache (); 96 } 97 }); 98 } 99 List<Library> l = new ArrayList<Library>(); 100 Collection<? extends LibraryProvider> instances = result.allInstances(); 101 Collection<LibraryProvider> added = new HashSet<LibraryProvider>(instances); 102 added.removeAll (currentStorages); 103 Collection<LibraryProvider> removed = new HashSet<LibraryProvider>(currentStorages); 104 removed.removeAll (instances); 105 currentStorages.clear(); 106 for (LibraryProvider storage : instances) { 107 this.currentStorages.add (storage); 108 for (LibraryImplementation impl : storage.getLibraries()) { 109 l.add(LibraryFactory.createLibrary(impl)); 110 } 111 } 112 for (LibraryProvider p : removed) { 113 p.removePropertyChangeListener(this.plistener); 114 } 115 for (LibraryProvider p : added) { 116 p.addPropertyChangeListener(this.plistener); 117 } 118 this.cache = l; 119 } 120 return this.cache.toArray(new Library[this.cache.size()]); 121 } 122 123 124 144 public void addLibrary (final Library library) throws IOException , IllegalArgumentException { 145 assert library != null; 146 if (LibrariesSupport.getLibraryTypeProvider(library.getType()) == null) { 147 throw new IllegalArgumentException ("Trying to add a library of unknown type: " + library.getType()); } 149 String newLibraryName = library.getName(); 150 if ( newLibraryName == null || getLibrary(newLibraryName)!= null) { 151 throw new IllegalArgumentException ("Library hasn't name or the name is already used: " + newLibraryName); } 153 final Collection<? extends WritableLibraryProvider> providers = Lookup.getDefault().lookupAll(WritableLibraryProvider.class); 154 assert providers.size() == 1; 155 providers.iterator().next().addLibrary(library.getLibraryImplementation()); 156 } 157 158 166 public void removeLibrary (final Library library) throws IOException , IllegalArgumentException { 167 assert library != null; 168 final Collection<? extends WritableLibraryProvider> providers = Lookup.getDefault().lookupAll(WritableLibraryProvider.class); 169 assert providers.size() == 1; 170 providers.iterator().next().removeLibrary(library.getLibraryImplementation()); 171 } 172 173 178 public synchronized void addPropertyChangeListener (PropertyChangeListener listener) { 179 assert listener != null; 180 this.listeners.addPropertyChangeListener (listener); 181 } 182 183 187 public void removePropertyChangeListener (PropertyChangeListener listener) { 188 assert listener != null; 189 this.listeners.removePropertyChangeListener (listener); 190 } 191 192 193 private synchronized void resetCache () { 194 this.cache = null; 195 this.listeners.firePropertyChange(PROP_LIBRARIES, null, null); 196 } 197 198 199 203 public static synchronized LibraryManager getDefault () { 204 if (instance == null) { 205 instance = new LibraryManager(); 206 } 207 return instance; 208 } 209 210 211 212 } 214 | Popular Tags |