? libraries.diff Index: apichanges.xml =================================================================== RCS file: /cvs/projects/libraries/apichanges.xml,v retrieving revision 1.4 diff -u -r1.4 apichanges.xml --- apichanges.xml 30 Jun 2006 21:27:07 -0000 1.4 +++ apichanges.xml 11 Aug 2006 09:49:55 -0000 @@ -79,7 +79,25 @@ - + + + + Added methods for adding and removing libraries into/from library manager + + + + + +

+ Added method for adding and removing a new library into/from the library manager. + Added a factory class for creating Library (API object) form LibraryImplementation (SPI object). + Added a support methods for listing installed LibraryTypeProviders. +

+
+ + + +
Index: src/org/netbeans/api/project/libraries/Library.java =================================================================== RCS file: /cvs/projects/libraries/src/org/netbeans/api/project/libraries/Library.java,v retrieving revision 1.7 diff -u -r1.7 Library.java --- src/org/netbeans/api/project/libraries/Library.java 30 Jun 2006 21:27:08 -0000 1.7 +++ src/org/netbeans/api/project/libraries/Library.java 11 Aug 2006 09:49:57 -0000 @@ -26,6 +26,7 @@ import java.util.List; import java.util.MissingResourceException; import java.util.ResourceBundle; +import org.netbeans.modules.project.libraries.LibraryAccessor; import org.netbeans.spi.project.libraries.LibraryImplementation; import org.openide.ErrorManager; import org.openide.util.NbBundle; @@ -56,7 +57,7 @@ * Creates new library instance * */ - Library (LibraryImplementation impl) { + private Library (LibraryImplementation impl) { this.impl = impl; this.impl.addPropertyChangeListener (new PropertyChangeListener () { public void propertyChange(PropertyChangeEvent evt) { @@ -203,6 +204,14 @@ // OK, not required to be there. return key; } + } + + static { + LibraryAccessor.DEFAULT = new LibraryAccessor () { + public Library createLibrary (LibraryImplementation impl) { + return new Library (impl); + } + }; } } // end Library Index: src/org/netbeans/api/project/libraries/LibraryManager.java =================================================================== RCS file: /cvs/projects/libraries/src/org/netbeans/api/project/libraries/LibraryManager.java,v retrieving revision 1.5 diff -u -r1.5 LibraryManager.java --- src/org/netbeans/api/project/libraries/LibraryManager.java 30 Jun 2006 21:27:08 -0000 1.5 +++ src/org/netbeans/api/project/libraries/LibraryManager.java 11 Aug 2006 09:49:58 -0000 @@ -30,7 +30,11 @@ import java.beans.PropertyChangeSupport; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeEvent; +import java.io.IOException; import java.util.*; +import org.netbeans.modules.project.libraries.WriteableLibraryProvider; +import org.netbeans.spi.project.libraries.LibraryFactory; +import org.netbeans.spi.project.libraries.support.LibrariesSupport; // XXX make getLibraries return Set not array @@ -104,7 +108,7 @@ this.currentStorages.add (storage); LibraryImplementation[] impls = storage.getLibraries(); for (int i=0; i + *

+ * A typical usage would be: + *

+ * LibraryManager libraryManager = LibraryManager.getDefault(); + * LibraryImplementation libImpl = LibrariesSupport.getLibraryTypeProvider("j2se").createLibrary(); + * libImpl.setName("FooLibTest"); + * libImpl.setContent ("classpath",listOfResources); + * libraryManager.addLibrary(LibraryFactory.createLibrary(libImpl)); + * + * @param library to be installed, the library has to be created + * with registered {@link org.netbeans.spi.project.libraries.LibraryTypeProvider}. + * @throws IOException when the library cannot be stored + * @throws IllegalArgumentException if the library is not recognized by any + * {@link org.netbeans.spi.project.libraries.LibraryTypeProvider} or the library + * of the same name already exists. + * @since org.netbeans.modules.project.libraries/1 1.14 + */ + public void addLibrary (final Library library) throws IOException, IllegalArgumentException { + assert library != null; + if (LibrariesSupport.getLibraryTypeProvider(library.getType()) == null) { + throw new IllegalArgumentException ("Trying to add a library of unknown type: " + library.getType()); //NOI18N + } + String newLibraryName = library.getName(); + if ( newLibraryName == null || getLibrary(newLibraryName)!= null) { + throw new IllegalArgumentException ("Library hasn't name or the name is already used: " + newLibraryName); //NOI18N + } + final Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template (WriteableLibraryProvider.class)); + final Collection/**/ providers = result.allInstances(); + assert providers.size() == 1; + ((WriteableLibraryProvider)providers.iterator().next()).addLibrary(library.getLibraryImplementation()); + } + + /** + * Removes installed library + * @param library to be removed. + * @throws IOException when library cannot be deleted. + * @throws IllegalArgumentException when library is not installed in a writeable + * {@link org.netbeans.spi.project.libraries.LibraryProvider} + * @since org.netbeans.modules.project.libraries/1 1.14 + */ + public void removeLibrary (final Library library) throws IOException, IllegalArgumentException { + assert library != null; + final Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template (WriteableLibraryProvider.class)); + final Collection/**/ providers = result.allInstances(); + assert providers.size() == 1; + ((WriteableLibraryProvider)providers.iterator().next()).removeLibrary(library.getLibraryImplementation()); } /** Index: src/org/netbeans/modules/project/libraries/LibraryAccessor.java =================================================================== RCS file: src/org/netbeans/modules/project/libraries/LibraryAccessor.java diff -N src/org/netbeans/modules/project/libraries/LibraryAccessor.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/modules/project/libraries/LibraryAccessor.java 11 Aug 2006 09:49:59 -0000 @@ -0,0 +1,45 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.modules.project.libraries; + +import org.netbeans.api.project.libraries.Library; +import org.netbeans.spi.project.libraries.LibraryImplementation; +import org.openide.util.Exceptions; + +/** + * + * @author Tomas Zezula + */ +public abstract class LibraryAccessor { + + public static LibraryAccessor DEFAULT; + + // force loading of Library class. That will set DEFAULT variable. + static { + try { + Object o = Class.forName("org.netbeans.api.project.libraries.Library",true,LibraryAccessor.class.getClassLoader()); + } catch (ClassNotFoundException cnf) { + Exceptions.printStackTrace(cnf); + } + } + + public abstract Library createLibrary (LibraryImplementation libraryImplementation); + +} Index: src/org/netbeans/spi/project/libraries/LibraryFactory.java =================================================================== RCS file: src/org/netbeans/spi/project/libraries/LibraryFactory.java diff -N src/org/netbeans/spi/project/libraries/LibraryFactory.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/netbeans/spi/project/libraries/LibraryFactory.java 11 Aug 2006 09:49:59 -0000 @@ -0,0 +1,50 @@ +/* + * The contents of this file are subject to the terms of the Common Development + * and Distribution License (the License). You may not use this file except in + * compliance with the License. + * + * You can obtain a copy of the License at http://www.netbeans.org/cddl.html + * or http://www.netbeans.org/cddl.txt. + * + * When distributing Covered Code, include this CDDL Header Notice in each file + * and include the License file at http://www.netbeans.org/cddl.txt. + * If applicable, add the following below the CDDL Header, with the fields + * enclosed by brackets [] replaced by your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.netbeans.spi.project.libraries; + +import org.netbeans.api.project.libraries.Library; +import org.netbeans.modules.project.libraries.LibraryAccessor; + +/** + * A factory class to create {@link Library} instances. + * You are not permitted to create them directly; instead you implement + * {@link LibraryImplementation} and use this factory. + * See also {@link org.netbeans.spi.project.libraries.support.LibrariesSupport} + * for easier ways to create {@link LibraryImplementation}. + * @since org.netbeans.modules.project.libraries/1 1.14 + * @author Tomas Zezula + */ +public class LibraryFactory { + + private LibraryFactory() { + } + + + /** + * Creates Library for LibraryImplementation + * @param libraryImplementation the library SPI object + * @return Library API instance + */ + public static Library createLibrary (LibraryImplementation libraryImplementation) { + assert libraryImplementation != null; + return LibraryAccessor.DEFAULT.createLibrary(libraryImplementation); + } + +} Index: src/org/netbeans/spi/project/libraries/support/LibrariesSupport.java =================================================================== RCS file: /cvs/projects/libraries/src/org/netbeans/spi/project/libraries/support/LibrariesSupport.java,v retrieving revision 1.3 diff -u -r1.3 LibrariesSupport.java --- src/org/netbeans/spi/project/libraries/support/LibrariesSupport.java 30 Jun 2006 21:27:12 -0000 1.3 +++ src/org/netbeans/spi/project/libraries/support/LibrariesSupport.java 11 Aug 2006 09:50:00 -0000 @@ -18,8 +18,10 @@ */ package org.netbeans.spi.project.libraries.support; +import org.netbeans.modules.project.libraries.LibraryTypeRegistry; import org.netbeans.spi.project.libraries.LibraryImplementation; import org.netbeans.modules.project.libraries.DefaultLibraryImplementation; +import org.netbeans.spi.project.libraries.LibraryTypeProvider; /** * SPI Support class. @@ -39,5 +41,26 @@ */ public static LibraryImplementation createLibraryImplementation (String libraryType, String[] volumeTypes) { return new DefaultLibraryImplementation (libraryType, volumeTypes); + } + + /** + * Returns registered {@link LibraryTypeProvider} for given library type. This method + * is mostly used by {@link org.netbeans.spi.project.libraries.LibraryProvider} implementators. + * @param libraryType the type of library for which the provider should be returned. + * @return {@link LibraryTypeProvider} for given library type or null, if none is registered. + * @since org.netbeans.modules.project.libraries/1 1.14 + */ + public static LibraryTypeProvider getLibraryTypeProvider (String libraryType) { + return LibraryTypeRegistry.getDefault().getLibraryTypeProvider(libraryType); + } + + /** + * Returns all registered {@link LibraryTypeProvider}s. This method + * is mostly used by {@link org.netbeans.spi.project.libraries.LibraryProvider} implementators. + * @return an array of {@link LibraryTypeProvider}, never returns null. + * @since org.netbeans.modules.project.libraries/1 1.14 + */ + public static LibraryTypeProvider[] getLibraryTypeProviders () { + return LibraryTypeRegistry.getDefault().getLibraryTypeProviders(); } } Index: test/unit/src/org/netbeans/api/project/libraries/LibraryManagerTest.java =================================================================== RCS file: /cvs/projects/libraries/test/unit/src/org/netbeans/api/project/libraries/LibraryManagerTest.java,v retrieving revision 1.4 diff -u -r1.4 LibraryManagerTest.java --- test/unit/src/org/netbeans/api/project/libraries/LibraryManagerTest.java 30 Jun 2006 21:27:12 -0000 1.4 +++ test/unit/src/org/netbeans/api/project/libraries/LibraryManagerTest.java 11 Aug 2006 09:50:01 -0000 @@ -22,6 +22,7 @@ import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; +import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -32,10 +33,19 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.StringTokenizer; import org.netbeans.api.project.TestUtil; import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.project.libraries.WriteableLibraryProvider; +import org.netbeans.spi.project.libraries.LibraryFactory; import org.netbeans.spi.project.libraries.LibraryImplementation; -import org.netbeans.spi.project.libraries.LibraryProvider; +import org.netbeans.spi.project.libraries.LibraryTypeProvider; +import org.netbeans.spi.project.libraries.support.LibrariesSupport; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.Repository; +import org.openide.loaders.DataFolder; +import org.openide.loaders.InstanceDataObject; +import org.openide.util.lookup.Lookups; import org.openide.util.lookup.Lookups; /** * @@ -45,6 +55,13 @@ private TestLibraryProvider lp; + private static final String LIBRARY_TYPE = "j2se"; //NOI18N + private static final String[] VOLUME_TYPES = new String[] { + "bin", + "src", + "doc" + }; + /** Creates a new instance of LibraryManagerTest */ public LibraryManagerTest (String testName) { super (testName); @@ -53,7 +70,27 @@ protected void setUp() throws Exception { super.setUp(); lp = new TestLibraryProvider (); - TestUtil.setLookup (Lookups.fixed(new Object[] {lp})); + TestUtil.setLookup (Lookups.fixed(new Object[] { + lp + })); + registerLibraryTypeProvider(); + } + + + private static void registerLibraryTypeProvider () throws Exception { + StringTokenizer tk = new StringTokenizer("org-netbeans-api-project-libraries/LibraryTypeProviders","/"); + FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); + while (tk.hasMoreElements()) { + String pathElement = tk.nextToken(); + FileObject tmp = root.getFileObject(pathElement); + if (tmp == null) { + tmp = root.createFolder(pathElement); + } + root = tmp; + } + if (root.getChildren().length == 0) { + InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class); + } } public void testGetLibraries () throws Exception { @@ -93,6 +130,30 @@ assertNull ("Nonexisting library", lib); } + public void testAddRemoveLibrary () throws Exception { + final LibraryImplementation[] impls = createTestLibs(); + lp.setLibraries(impls); + final LibraryManager lm = LibraryManager.getDefault(); + Library[] libs = lm.getLibraries(); + assertEquals ("Libraries count", 2, libs.length); + assertLibsEquals (libs, impls); + final LibraryTypeProvider provider = LibrariesSupport.getLibraryTypeProvider(LIBRARY_TYPE); + assertNotNull (provider); + final LibraryImplementation newLibImplementation = provider.createLibrary(); + newLibImplementation.setName("NewLib"); + final Library newLibrary = LibraryFactory.createLibrary(newLibImplementation); + lm.addLibrary(newLibrary); + libs = lm.getLibraries(); + assertEquals ("Libraries count", 3, libs.length); + List newLibs = new ArrayList (Arrays.asList(impls)); + newLibs.add (newLibImplementation); + assertLibsEquals (libs, (LibraryImplementation[])newLibs.toArray(new LibraryImplementation[newLibs.size()])); + lm.removeLibrary(newLibrary); + libs = lm.getLibraries(); + assertEquals("Libraries count",2,libs.length); + assertLibsEquals (libs, impls); + } + static LibraryImplementation[] createTestLibs () throws MalformedURLException { LibraryImplementation[] impls = new LibraryImplementation[] { new TestLibraryImplementation (), @@ -129,14 +190,14 @@ } - static class TestLibraryProvider implements LibraryProvider { + static class TestLibraryProvider implements WriteableLibraryProvider { private PropertyChangeSupport support; - private LibraryImplementation[] libraries; + private List libraries; public TestLibraryProvider () { this.support = new PropertyChangeSupport (this); - this.libraries = new LibraryImplementation[0]; + this.libraries = new ArrayList(); } public void removePropertyChangeListener(PropertyChangeListener listener) { @@ -148,11 +209,27 @@ } public LibraryImplementation[] getLibraries() { - return this.libraries; - } + return (LibraryImplementation[]) this.libraries.toArray(new LibraryImplementation[libraries.size()]); + } public void setLibraries (LibraryImplementation[] libraries) { - this.libraries = libraries; + this.libraries = new ArrayList(Arrays.asList(libraries)); + this.support.firePropertyChange(PROP_LIBRARIES,null,null); + } + + public void addLibrary(LibraryImplementation library) throws IOException { + this.libraries.add (library); + this.support.firePropertyChange(PROP_LIBRARIES,null,null); + } + + public void removeLibrary(LibraryImplementation library) throws IOException { + this.libraries.remove (library); + this.support.firePropertyChange(PROP_LIBRARIES,null,null); + } + + public void updateLibrary(LibraryImplementation oldLibrary, LibraryImplementation newLibrary) throws IOException { + this.libraries.remove(oldLibrary); + this.libraries.add (newLibrary); this.support.firePropertyChange(PROP_LIBRARIES,null,null); } @@ -175,6 +252,41 @@ } } + + public static class TestLibraryTypeProvider implements LibraryTypeProvider { + + + public String getDisplayName() { + return LIBRARY_TYPE; + } + + public String getLibraryType() { + return LIBRARY_TYPE; + } + + public String[] getSupportedVolumeTypes() { + return VOLUME_TYPES; + } + + public LibraryImplementation createLibrary() { + return LibrariesSupport.createLibraryImplementation(LIBRARY_TYPE, VOLUME_TYPES); + } + + public void libraryDeleted(LibraryImplementation library) { + } + + public void libraryCreated(LibraryImplementation library) { + } + + public java.beans.Customizer getCustomizer(String volumeType) { + return null; + } + + public org.openide.util.Lookup getLookup() { + return null; + } + } + static class TestLibraryImplementation implements LibraryImplementation { private static final Set supportedTypes; @@ -184,12 +296,8 @@ private Map contents; private PropertyChangeSupport support; - static { - Set st = new HashSet (); - st.add ("bin"); - st.add ("src"); - st.add ("doc"); - supportedTypes = Collections.unmodifiableSet(st); + static { + supportedTypes = Collections.unmodifiableSet(new HashSet(Arrays.asList(VOLUME_TYPES))); } public TestLibraryImplementation () { @@ -198,7 +306,7 @@ } public String getType() { - return "TestLibraryType"; + return LIBRARY_TYPE; } public String getName() {