KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > project > libraries > Library


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.api.project.libraries;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.MissingResourceException JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import org.netbeans.modules.project.libraries.LibraryAccessor;
31 import org.netbeans.spi.project.libraries.LibraryImplementation;
32 import org.openide.ErrorManager;
33 import org.openide.util.NbBundle;
34
35 /**
36  * Library models typed bundle of typed volumes.
37  * <p>
38  * Library volumes are typed and query-able by their type. The type is
39  * represented by type string. Strictly speaking volumes are
40  * named rather then typed but the name express their type.
41  * The volume is a list of resoruces.
42  * <p>
43  * For more details see <a HREF="package-summary.html">libraries overview</a>.
44  * @author Petr Kuzel, Tomas Zezula
45  */

46 public final class Library {
47     
48     public static final String JavaDoc PROP_NAME = "name"; //NOI18N
49
public static final String JavaDoc PROP_DESCRIPTION = "description"; //NOI18N
50
public static final String JavaDoc PROP_CONTENT = "content"; //NOI18N
51

52     // delegating peer
53
private LibraryImplementation impl;
54
55     private List JavaDoc<PropertyChangeListener JavaDoc> listeners;
56
57     /**
58      * Creates new library instance
59      *
60      */

61     private Library (LibraryImplementation impl) {
62         this.impl = impl;
63         this.impl.addPropertyChangeListener (new PropertyChangeListener JavaDoc () {
64             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
65                 String JavaDoc propName = evt.getPropertyName();
66                 Library.this.fireChange (propName,evt.getOldValue(),evt.getNewValue());
67             }
68         });
69     } // end create
70

71     /**
72      * Access typed but raw library data.
73      * <p>
74      * The contents are defined by SPI providers and identified
75      * by the <a HREF="package-summary.html#volumeType">volume types</a>. For example the j2se library supports the following
76      * volume types: classpath - the library classpath roots, src - the library sources, javadoc - the library javadoc.
77      * Your module must have contract with a particular provider's module to be able to query it effectively.
78      * </p>
79      *
80      * @param volumeType which resources to return.
81      * @return path of URLs of given type (possibly empty but never <code>null</code>)
82      */

83     public List JavaDoc<URL JavaDoc> getContent(final String JavaDoc volumeType) {
84         return this.impl.getContent (volumeType);
85     } // end getContent
86

87
88
89     /**
90      * Get library binding name. The name identifies library
91      * in scope of one libraries storage.
92      * <p>
93      *
94      * @return String with library name
95      */

96     public String JavaDoc getName() {
97         return impl.getName();
98     } // end getName
99

100
101     /**
102      * Returns description of the library.
103      * The description provides more detailed information about the library.
104      * @return String the description or null if the description is not available
105      */

106     public String JavaDoc getDescription () {
107         return this.getLocalizedString(this.impl.getLocalizingBundle(),this.impl.getDescription());
108     }
109
110
111     /**
112      * Returns the display name of the library.
113      * The display name is either equal to the name or
114      * is a localized version of the name.
115      * @return String the display name, never returns null.
116      */

117     public String JavaDoc getDisplayName () {
118         return this.getLocalizedString(this.impl.getLocalizingBundle(),this.impl.getName());
119     }
120
121
122     /**
123      * Gets the type of library. The library type identifies
124      * the provider which has created the library and implies
125      * the volues contained in it.
126      * @return String (e.g. j2se for J2SE library)
127      */

128     public String JavaDoc getType () {
129         return this.impl.getType();
130     }
131
132
133     // delegated identity
134
public boolean equals(Object JavaDoc obj) {
135         if (obj == this) return true;
136         if (obj instanceof Library) {
137             Library peer = (Library) obj;
138             return peer.impl.equals(impl);
139         }
140         return false;
141     }
142
143     // delegated identity
144
public int hashCode() {
145         return impl.hashCode();
146     }
147
148     /**
149      * Adds PropertyChangeListener
150      * @param listener
151      */

152     public synchronized void addPropertyChangeListener (PropertyChangeListener JavaDoc listener) {
153         if (this.listeners == null)
154             this.listeners = new ArrayList JavaDoc<PropertyChangeListener JavaDoc>();
155         this.listeners.add (listener);
156     }
157
158     /**
159      * Removes PropertyChangeListener
160      * @param listener
161      */

162     public synchronized void removePropertyChangeListener (PropertyChangeListener JavaDoc listener) {
163         if (this.listeners == null)
164             return;
165         this.listeners.remove (listener);
166     }
167
168
169     LibraryImplementation getLibraryImplementation () {
170         return this.impl;
171     }
172
173     private void fireChange (String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
174         List JavaDoc<PropertyChangeListener JavaDoc> ls;
175         synchronized (this) {
176             if (this.listeners == null)
177                 return;
178             ls = new ArrayList JavaDoc<PropertyChangeListener JavaDoc>(listeners);
179         }
180         PropertyChangeEvent JavaDoc event = new PropertyChangeEvent JavaDoc (this, propertyName, oldValue, newValue);
181         for (PropertyChangeListener JavaDoc l : ls) {
182             l.propertyChange(event);
183         }
184     }
185
186
187     private String JavaDoc getLocalizedString (String JavaDoc bundleName, String JavaDoc key) {
188         if (key == null) {
189             return null;
190         }
191         if (bundleName == null) {
192             return key;
193         }
194         ResourceBundle JavaDoc bundle;
195         try {
196             bundle = NbBundle.getBundle(bundleName);
197         } catch (MissingResourceException JavaDoc mre) {
198             // Bogus bundle.
199
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mre);
200             return key;
201         }
202         try {
203             return bundle.getString(key);
204         } catch (MissingResourceException JavaDoc mre) {
205             // OK, not required to be there.
206
return key;
207         }
208     }
209     
210     static {
211         LibraryAccessor.DEFAULT = new LibraryAccessor () {
212             public Library createLibrary (LibraryImplementation impl) {
213                 return new Library (impl);
214             }
215         };
216     }
217
218 } // end Library
219

220
Popular Tags