This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 82469
Collapse All | Expand All

(-)apichanges.xml (-1 / +19 lines)
Lines 79-85 Link Here
79
    </apidefs>
79
    </apidefs>
80
80
81
    <!-- ACTUAL CHANGES BEGIN HERE: -->
81
    <!-- ACTUAL CHANGES BEGIN HERE: -->
82
82
    
83
    <change>
84
            <api name="general"/>
85
            <summary>Added methods for adding and removing libraries into/from library manager</summary>
86
            <version major="1" minor="14"/>
87
            <date day="11" month="8" year="2006"/>
88
            <author login="tzezula"/>
89
            <compatibility addition="yes"/>
90
            <description>
91
                <p>
92
                    Added method for adding and removing a new library into/from the library manager.
93
                    Added a factory class for creating Library (API object) form LibraryImplementation (SPI object).
94
                    Added a support methods for listing installed LibraryTypeProviders.
95
                </p>
96
            </description>
97
            <class package="org.netbeans.api.project.libraries" name="LibraryManager"/>
98
            <class package="org.netbeans.spi.project.libraries" name="LibraryFactory"/>
99
            <class package="org.netbeans.spi.project.libraries.support" name="LibrariesSupport"/>
100
        </change>    
83
    <changes>
101
    <changes>
84
102
85
        <change id="rel-vers-1">
103
        <change id="rel-vers-1">
(-)src/org/netbeans/api/project/libraries/Library.java (-1 / +10 lines)
Lines 26-31 Link Here
26
import java.util.List;
26
import java.util.List;
27
import java.util.MissingResourceException;
27
import java.util.MissingResourceException;
28
import java.util.ResourceBundle;
28
import java.util.ResourceBundle;
29
import org.netbeans.modules.project.libraries.LibraryAccessor;
29
import org.netbeans.spi.project.libraries.LibraryImplementation;
30
import org.netbeans.spi.project.libraries.LibraryImplementation;
30
import org.openide.ErrorManager;
31
import org.openide.ErrorManager;
31
import org.openide.util.NbBundle;
32
import org.openide.util.NbBundle;
Lines 56-62 Link Here
56
     * Creates new library instance
57
     * Creates new library instance
57
     *
58
     *
58
     */
59
     */
59
    Library (LibraryImplementation impl) {
60
    private Library (LibraryImplementation impl) {
60
        this.impl = impl;
61
        this.impl = impl;
61
        this.impl.addPropertyChangeListener (new PropertyChangeListener () {
62
        this.impl.addPropertyChangeListener (new PropertyChangeListener () {
62
            public void propertyChange(PropertyChangeEvent evt) {
63
            public void propertyChange(PropertyChangeEvent evt) {
Lines 203-208 Link Here
203
            // OK, not required to be there.
204
            // OK, not required to be there.
204
            return key;
205
            return key;
205
        }
206
        }
207
    }
208
    
209
    static {
210
        LibraryAccessor.DEFAULT = new LibraryAccessor () {
211
            public Library createLibrary (LibraryImplementation impl) {
212
                return new Library (impl);
213
            }
214
        };
206
    }
215
    }
207
216
208
} // end Library
217
} // end Library
(-)src/org/netbeans/api/project/libraries/LibraryManager.java (-1 / +57 lines)
Lines 30-36 Link Here
30
import java.beans.PropertyChangeSupport;
30
import java.beans.PropertyChangeSupport;
31
import java.beans.PropertyChangeListener;
31
import java.beans.PropertyChangeListener;
32
import java.beans.PropertyChangeEvent;
32
import java.beans.PropertyChangeEvent;
33
import java.io.IOException;
33
import java.util.*;
34
import java.util.*;
35
import org.netbeans.modules.project.libraries.WriteableLibraryProvider;
36
import org.netbeans.spi.project.libraries.LibraryFactory;
37
import org.netbeans.spi.project.libraries.support.LibrariesSupport;
34
38
35
// XXX make getLibraries return Set not array
39
// XXX make getLibraries return Set not array
36
40
Lines 104-110 Link Here
104
                this.currentStorages.add (storage);
108
                this.currentStorages.add (storage);
105
                LibraryImplementation[] impls = storage.getLibraries();
109
                LibraryImplementation[] impls = storage.getLibraries();
106
                for (int i=0; i<impls.length; i++) {                    
110
                for (int i=0; i<impls.length; i++) {                    
107
                    l.add(new Library (impls[i]));
111
                    l.add(LibraryFactory.createLibrary (impls[i]));
108
                }                
112
                }                
109
            }
113
            }
110
            for (Iterator it = removed.iterator(); it.hasNext();) {
114
            for (Iterator it = removed.iterator(); it.hasNext();) {
Lines 116-121 Link Here
116
            this.cache = l;
120
            this.cache = l;
117
        }
121
        }
118
        return (Library[]) this.cache.toArray(new Library[this.cache.size()]);
122
        return (Library[]) this.cache.toArray(new Library[this.cache.size()]);
123
    }
124
    
125
    
126
    /**
127
     * Installs a new library into the library manager.
128
     * <div class="nonnormative">
129
     * <p>
130
     * A typical usage would be:
131
     * </p>
132
     * LibraryManager libraryManager = LibraryManager.getDefault();
133
     * LibraryImplementation libImpl = LibrariesSupport.getLibraryTypeProvider("j2se").createLibrary();        
134
     * libImpl.setName("FooLibTest");
135
     * libImpl.setContent ("classpath",listOfResources);
136
     * libraryManager.addLibrary(LibraryFactory.createLibrary(libImpl));
137
     * </div>
138
     * @param library to be installed, the library has to be created
139
     * with registered {@link org.netbeans.spi.project.libraries.LibraryTypeProvider}.
140
     * @throws IOException when the library cannot be stored
141
     * @throws IllegalArgumentException if the library is not recognized by any 
142
     * {@link org.netbeans.spi.project.libraries.LibraryTypeProvider} or the library
143
     * of the same name already exists.
144
     * @since org.netbeans.modules.project.libraries/1 1.14
145
     */
146
    public void addLibrary (final Library library) throws IOException, IllegalArgumentException {
147
        assert library != null;
148
        if (LibrariesSupport.getLibraryTypeProvider(library.getType()) == null) {
149
            throw new IllegalArgumentException ("Trying to add a library of unknown type: " + library.getType()); //NOI18N
150
        }
151
        String newLibraryName = library.getName();
152
        if ( newLibraryName == null || getLibrary(newLibraryName)!= null) {
153
            throw new IllegalArgumentException ("Library hasn't name or the name is already used: " + newLibraryName); //NOI18N
154
        }
155
        final Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template (WriteableLibraryProvider.class));
156
        final Collection/*<WriteableLibraryProvider>*/ providers = result.allInstances();
157
        assert providers.size() == 1;        
158
        ((WriteableLibraryProvider)providers.iterator().next()).addLibrary(library.getLibraryImplementation());
159
    }
160
    
161
    /**
162
     * Removes installed library 
163
     * @param library to be removed. 
164
     * @throws IOException when library cannot be deleted.
165
     * @throws IllegalArgumentException when library is not installed in a writeable 
166
     * {@link org.netbeans.spi.project.libraries.LibraryProvider}
167
     * @since org.netbeans.modules.project.libraries/1 1.14
168
     */
169
    public void removeLibrary (final Library library) throws IOException, IllegalArgumentException {
170
        assert library != null;
171
        final Lookup.Result result = Lookup.getDefault().lookup(new Lookup.Template (WriteableLibraryProvider.class));
172
        final Collection/*<WriteableLibraryProvider>*/ providers = result.allInstances();
173
        assert providers.size() == 1;
174
        ((WriteableLibraryProvider)providers.iterator().next()).removeLibrary(library.getLibraryImplementation());
119
    }
175
    }
120
176
121
    /**
177
    /**
(-)src/org/netbeans/modules/project/libraries/LibraryAccessor.java (+45 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.netbeans.modules.project.libraries;
21
22
import org.netbeans.api.project.libraries.Library;
23
import org.netbeans.spi.project.libraries.LibraryImplementation;
24
import org.openide.util.Exceptions;
25
26
/**
27
 *
28
 * @author Tomas Zezula
29
 */
30
public abstract class LibraryAccessor {
31
    
32
    public static LibraryAccessor DEFAULT;
33
    
34
    // force loading of Library class. That will set DEFAULT variable.
35
    static {
36
        try {
37
            Object o = Class.forName("org.netbeans.api.project.libraries.Library",true,LibraryAccessor.class.getClassLoader());
38
        } catch (ClassNotFoundException cnf) {
39
            Exceptions.printStackTrace(cnf);
40
        }
41
    }
42
    
43
    public abstract Library createLibrary (LibraryImplementation libraryImplementation);
44
    
45
}
(-)src/org/netbeans/spi/project/libraries/LibraryFactory.java (+50 lines)
Added Link Here
1
/*
2
 * The contents of this file are subject to the terms of the Common Development
3
 * and Distribution License (the License). You may not use this file except in
4
 * compliance with the License.
5
 *
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7
 * or http://www.netbeans.org/cddl.txt.
8
 *
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
11
 * If applicable, add the following below the CDDL Header, with the fields
12
 * enclosed by brackets [] replaced by your own identifying information:
13
 * "Portions Copyrighted [year] [name of copyright owner]"
14
 *
15
 * The Original Software is NetBeans. The Initial Developer of the Original
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17
 * Microsystems, Inc. All Rights Reserved.
18
 */
19
20
package org.netbeans.spi.project.libraries;
21
22
import org.netbeans.api.project.libraries.Library;
23
import org.netbeans.modules.project.libraries.LibraryAccessor;
24
25
/**
26
 * A factory class to create {@link Library} instances.
27
 * You are not permitted to create them directly; instead you implement
28
 * {@link LibraryImplementation} and use this factory.
29
 * See also {@link org.netbeans.spi.project.libraries.support.LibrariesSupport}
30
 * for easier ways to create {@link LibraryImplementation}.
31
 * @since org.netbeans.modules.project.libraries/1 1.14
32
 * @author Tomas Zezula
33
 */
34
public class LibraryFactory {
35
    
36
    private LibraryFactory() {
37
    }
38
    
39
    
40
    /**
41
     * Creates Library for LibraryImplementation
42
     * @param libraryImplementation the library SPI object
43
     * @return Library API instance
44
     */
45
    public static Library createLibrary (LibraryImplementation libraryImplementation) {
46
        assert libraryImplementation != null;
47
        return LibraryAccessor.DEFAULT.createLibrary(libraryImplementation);
48
    }
49
    
50
}
(-)src/org/netbeans/spi/project/libraries/support/LibrariesSupport.java (+23 lines)
Lines 18-25 Link Here
18
 */
18
 */
19
package org.netbeans.spi.project.libraries.support;
19
package org.netbeans.spi.project.libraries.support;
20
20
21
import org.netbeans.modules.project.libraries.LibraryTypeRegistry;
21
import org.netbeans.spi.project.libraries.LibraryImplementation;
22
import org.netbeans.spi.project.libraries.LibraryImplementation;
22
import org.netbeans.modules.project.libraries.DefaultLibraryImplementation;
23
import org.netbeans.modules.project.libraries.DefaultLibraryImplementation;
24
import org.netbeans.spi.project.libraries.LibraryTypeProvider;
23
25
24
/**
26
/**
25
 * SPI Support class.
27
 * SPI Support class.
Lines 39-43 Link Here
39
     */
41
     */
40
    public static LibraryImplementation createLibraryImplementation (String libraryType, String[] volumeTypes) {
42
    public static LibraryImplementation createLibraryImplementation (String libraryType, String[] volumeTypes) {
41
        return new DefaultLibraryImplementation (libraryType, volumeTypes);
43
        return new DefaultLibraryImplementation (libraryType, volumeTypes);
44
    }
45
    
46
    /**
47
     * Returns registered {@link LibraryTypeProvider} for given library type. This method 
48
     * is mostly used by {@link org.netbeans.spi.project.libraries.LibraryProvider} implementators.
49
     * @param libraryType  the type of library for which the provider should be returned.
50
     * @return {@link LibraryTypeProvider} for given library type or null, if none is registered.
51
     * @since org.netbeans.modules.project.libraries/1 1.14
52
     */
53
    public static LibraryTypeProvider getLibraryTypeProvider (String libraryType) {
54
        return LibraryTypeRegistry.getDefault().getLibraryTypeProvider(libraryType);
55
    }
56
    
57
    /**
58
     * Returns all registered {@link LibraryTypeProvider}s. This method 
59
     * is mostly used by {@link org.netbeans.spi.project.libraries.LibraryProvider} implementators.
60
     * @return an array of {@link LibraryTypeProvider}, never returns null.
61
     * @since org.netbeans.modules.project.libraries/1 1.14
62
     */
63
    public static LibraryTypeProvider[] getLibraryTypeProviders () {
64
        return LibraryTypeRegistry.getDefault().getLibraryTypeProviders();
42
    }
65
    }
43
}
66
}
(-)test/unit/src/org/netbeans/api/project/libraries/LibraryManagerTest.java (-15 / +123 lines)
Lines 22-27 Link Here
22
import java.beans.PropertyChangeEvent;
22
import java.beans.PropertyChangeEvent;
23
import java.beans.PropertyChangeListener;
23
import java.beans.PropertyChangeListener;
24
import java.beans.PropertyChangeSupport;
24
import java.beans.PropertyChangeSupport;
25
import java.io.IOException;
25
import java.net.MalformedURLException;
26
import java.net.MalformedURLException;
26
import java.net.URL;
27
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.ArrayList;
Lines 32-41 Link Here
32
import java.util.List;
33
import java.util.List;
33
import java.util.Map;
34
import java.util.Map;
34
import java.util.Set;
35
import java.util.Set;
36
import java.util.StringTokenizer;
35
import org.netbeans.api.project.TestUtil;
37
import org.netbeans.api.project.TestUtil;
36
import org.netbeans.junit.NbTestCase;
38
import org.netbeans.junit.NbTestCase;
39
import org.netbeans.modules.project.libraries.WriteableLibraryProvider;
40
import org.netbeans.spi.project.libraries.LibraryFactory;
37
import org.netbeans.spi.project.libraries.LibraryImplementation;
41
import org.netbeans.spi.project.libraries.LibraryImplementation;
38
import org.netbeans.spi.project.libraries.LibraryProvider;
42
import org.netbeans.spi.project.libraries.LibraryTypeProvider;
43
import org.netbeans.spi.project.libraries.support.LibrariesSupport;
44
import org.openide.filesystems.FileObject;
45
import org.openide.filesystems.Repository;
46
import org.openide.loaders.DataFolder;
47
import org.openide.loaders.InstanceDataObject;
48
import org.openide.util.lookup.Lookups;
39
import org.openide.util.lookup.Lookups;
49
import org.openide.util.lookup.Lookups;
40
/**
50
/**
41
 *
51
 *
Lines 45-50 Link Here
45
    
55
    
46
    private TestLibraryProvider lp;
56
    private TestLibraryProvider lp;
47
    
57
    
58
    private static final String LIBRARY_TYPE = "j2se";  //NOI18N
59
    private static final String[] VOLUME_TYPES = new String[] {
60
        "bin",
61
        "src",
62
        "doc"
63
    };
64
    
48
    /** Creates a new instance of LibraryManagerTest */
65
    /** Creates a new instance of LibraryManagerTest */
49
    public LibraryManagerTest (String testName) {
66
    public LibraryManagerTest (String testName) {
50
        super (testName);
67
        super (testName);
Lines 53-59 Link Here
53
    protected void setUp() throws Exception {
70
    protected void setUp() throws Exception {
54
        super.setUp();
71
        super.setUp();
55
        lp = new TestLibraryProvider ();
72
        lp = new TestLibraryProvider ();
56
        TestUtil.setLookup (Lookups.fixed(new Object[] {lp}));
73
        TestUtil.setLookup (Lookups.fixed(new Object[] {
74
            lp
75
        }));
76
        registerLibraryTypeProvider();
77
    }
78
    
79
    
80
    private static void registerLibraryTypeProvider () throws Exception {
81
        StringTokenizer tk = new StringTokenizer("org-netbeans-api-project-libraries/LibraryTypeProviders","/");
82
        FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
83
        while (tk.hasMoreElements()) {
84
            String pathElement = tk.nextToken();
85
            FileObject tmp = root.getFileObject(pathElement);
86
            if (tmp == null) {
87
                tmp = root.createFolder(pathElement);
88
            }
89
            root = tmp;
90
        }
91
        if (root.getChildren().length == 0) {
92
            InstanceDataObject.create (DataFolder.findFolder(root),"TestLibraryTypeProvider",TestLibraryTypeProvider.class);
93
        }
57
    }
94
    }
58
    
95
    
59
    public void testGetLibraries () throws Exception {        
96
    public void testGetLibraries () throws Exception {        
Lines 93-98 Link Here
93
        assertNull ("Nonexisting library", lib);
130
        assertNull ("Nonexisting library", lib);
94
    }
131
    }
95
    
132
    
133
    public void testAddRemoveLibrary () throws Exception {
134
        final LibraryImplementation[] impls = createTestLibs();
135
        lp.setLibraries(impls);
136
        final LibraryManager lm = LibraryManager.getDefault();
137
        Library[] libs = lm.getLibraries();
138
        assertEquals ("Libraries count", 2, libs.length);
139
        assertLibsEquals (libs, impls);
140
        final LibraryTypeProvider provider = LibrariesSupport.getLibraryTypeProvider(LIBRARY_TYPE);
141
        assertNotNull (provider);
142
        final LibraryImplementation newLibImplementation = provider.createLibrary();
143
        newLibImplementation.setName("NewLib");
144
        final Library newLibrary = LibraryFactory.createLibrary(newLibImplementation);
145
        lm.addLibrary(newLibrary);
146
        libs = lm.getLibraries();
147
        assertEquals ("Libraries count", 3, libs.length);
148
        List newLibs = new ArrayList (Arrays.asList(impls));
149
        newLibs.add (newLibImplementation);
150
        assertLibsEquals (libs, (LibraryImplementation[])newLibs.toArray(new LibraryImplementation[newLibs.size()]));
151
        lm.removeLibrary(newLibrary);
152
        libs = lm.getLibraries();
153
        assertEquals("Libraries count",2,libs.length);
154
        assertLibsEquals (libs, impls);
155
    }
156
    
96
    static LibraryImplementation[] createTestLibs () throws MalformedURLException {
157
    static LibraryImplementation[] createTestLibs () throws MalformedURLException {
97
        LibraryImplementation[] impls = new LibraryImplementation[] {
158
        LibraryImplementation[] impls = new LibraryImplementation[] {
98
            new TestLibraryImplementation (),
159
            new TestLibraryImplementation (),
Lines 129-142 Link Here
129
    }
190
    }
130
    
191
    
131
    
192
    
132
    static class TestLibraryProvider implements LibraryProvider  {
193
    static class TestLibraryProvider implements WriteableLibraryProvider  {
133
        
194
        
134
        private PropertyChangeSupport support;
195
        private PropertyChangeSupport support;
135
        private LibraryImplementation[] libraries;
196
        private List libraries;
136
        
197
        
137
        public TestLibraryProvider () {
198
        public TestLibraryProvider () {
138
            this.support = new PropertyChangeSupport (this);
199
            this.support = new PropertyChangeSupport (this);
139
            this.libraries = new LibraryImplementation[0];
200
            this.libraries = new ArrayList();
140
        }
201
        }
141
        
202
        
142
        public void removePropertyChangeListener(PropertyChangeListener listener) {
203
        public void removePropertyChangeListener(PropertyChangeListener listener) {
Lines 148-158 Link Here
148
        }
209
        }
149
210
150
        public LibraryImplementation[] getLibraries() {
211
        public LibraryImplementation[] getLibraries() {
151
            return this.libraries;
212
            return (LibraryImplementation[]) this.libraries.toArray(new LibraryImplementation[libraries.size()]);
152
        }
213
        }                
153
        
214
        
154
        public void setLibraries (LibraryImplementation[] libraries) {
215
        public void setLibraries (LibraryImplementation[] libraries) {
155
            this.libraries = libraries;
216
            this.libraries = new ArrayList(Arrays.asList(libraries));
217
            this.support.firePropertyChange(PROP_LIBRARIES,null,null);
218
        }
219
220
        public void addLibrary(LibraryImplementation library) throws IOException {
221
            this.libraries.add (library);
222
            this.support.firePropertyChange(PROP_LIBRARIES,null,null);
223
        }
224
225
        public void removeLibrary(LibraryImplementation library) throws IOException {
226
            this.libraries.remove (library);
227
            this.support.firePropertyChange(PROP_LIBRARIES,null,null);
228
        }
229
230
        public void updateLibrary(LibraryImplementation oldLibrary, LibraryImplementation newLibrary) throws IOException {
231
            this.libraries.remove(oldLibrary);
232
            this.libraries.add (newLibrary);
156
            this.support.firePropertyChange(PROP_LIBRARIES,null,null);
233
            this.support.firePropertyChange(PROP_LIBRARIES,null,null);
157
        }
234
        }
158
        
235
        
Lines 175-180 Link Here
175
        }
252
        }
176
    }
253
    }
177
    
254
    
255
    
256
    public static class TestLibraryTypeProvider implements LibraryTypeProvider {
257
            
258
259
        public String getDisplayName() {
260
            return LIBRARY_TYPE;
261
        }
262
263
        public String getLibraryType() {
264
            return LIBRARY_TYPE;
265
        }
266
267
        public String[] getSupportedVolumeTypes() {
268
            return VOLUME_TYPES;
269
        }
270
271
        public LibraryImplementation createLibrary() {
272
            return LibrariesSupport.createLibraryImplementation(LIBRARY_TYPE, VOLUME_TYPES);
273
        }
274
275
        public void libraryDeleted(LibraryImplementation library) {
276
        }
277
278
        public void libraryCreated(LibraryImplementation library) {
279
        }
280
281
        public java.beans.Customizer getCustomizer(String volumeType) {
282
            return null;
283
        }
284
285
        public org.openide.util.Lookup getLookup() {
286
            return null;
287
        }
288
    }
289
    
178
    static class TestLibraryImplementation implements LibraryImplementation {
290
    static class TestLibraryImplementation implements LibraryImplementation {
179
        
291
        
180
        private static final Set supportedTypes;
292
        private static final Set supportedTypes;
Lines 184-195 Link Here
184
        private Map contents;
296
        private Map contents;
185
        private PropertyChangeSupport support;
297
        private PropertyChangeSupport support;
186
        
298
        
187
        static {
299
        static {            
188
            Set st = new HashSet ();
300
            supportedTypes = Collections.unmodifiableSet(new HashSet(Arrays.asList(VOLUME_TYPES)));
189
            st.add ("bin");
190
            st.add ("src");
191
            st.add ("doc");
192
            supportedTypes = Collections.unmodifiableSet(st);
193
        }
301
        }
194
        
302
        
195
        public TestLibraryImplementation () {
303
        public TestLibraryImplementation () {
Lines 198-204 Link Here
198
        }
306
        }
199
        
307
        
200
        public String getType() {
308
        public String getType() {
201
            return "TestLibraryType";
309
            return LIBRARY_TYPE;
202
        }
310
        }
203
        
311
        
204
        public String getName() {
312
        public String getName() {

Return to bug 82469