KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seplatform > libraries > J2SELibraryTypeProvider


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 package org.netbeans.modules.java.j2seplatform.libraries;
20
21 import org.netbeans.spi.project.libraries.LibraryTypeProvider;
22 import org.netbeans.spi.project.libraries.LibraryImplementation;
23 import org.netbeans.spi.project.libraries.support.LibrariesSupport;
24 import org.netbeans.spi.project.support.ant.EditableProperties;
25 import org.netbeans.spi.project.support.ant.PropertyUtils;
26 import org.netbeans.api.project.ProjectManager;
27 import org.openide.ErrorManager;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.filesystems.URLMapper;
30 import org.openide.filesystems.FileObject;
31
32 import java.beans.Customizer JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.File JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.net.URI JavaDoc;
39 import java.net.URISyntaxException JavaDoc;
40 import org.openide.util.Lookup;
41 import org.openide.util.NbBundle;
42
43 /**
44  *
45  * <lu>
46  * <li><code>parserdb</code> volume type can be used to associate prebuild parserDB
47  * with the library. The resouce in such a case must specify path to .jsc file.</li>
48  * </lu>
49  */

50 public final class J2SELibraryTypeProvider implements LibraryTypeProvider {
51
52     private J2SELibraryTypeProvider () {
53     }
54
55     private static final String JavaDoc LIB_PREFIX = "libs.";
56     public static final String JavaDoc LIBRARY_TYPE = "j2se"; //NOI18N
57
public static final String JavaDoc VOLUME_TYPE_CLASSPATH = "classpath"; //NOI18N
58
public static final String JavaDoc VOLUME_TYPE_SRC = "src"; //NOI18N
59
public static final String JavaDoc VOLUME_TYPE_JAVADOC = "javadoc"; //NOI18N
60
public static final String JavaDoc[] VOLUME_TYPES = new String JavaDoc[] {
61         VOLUME_TYPE_CLASSPATH,
62         VOLUME_TYPE_SRC,
63         VOLUME_TYPE_JAVADOC,
64     };
65
66     public String JavaDoc getLibraryType() {
67         return LIBRARY_TYPE;
68     }
69     
70     public String JavaDoc getDisplayName () {
71         return NbBundle.getMessage (J2SELibraryTypeProvider.class,"TXT_J2SELibraryType");
72     }
73
74     public String JavaDoc[] getSupportedVolumeTypes () {
75         return VOLUME_TYPES;
76     }
77
78     public LibraryImplementation createLibrary() {
79         return LibrariesSupport.createLibraryImplementation(LIBRARY_TYPE,VOLUME_TYPES);
80     }
81
82
83     public void libraryCreated(final LibraryImplementation libraryImpl) {
84         assert libraryImpl != null;
85         ProjectManager.mutex().postWriteRequest(
86                 new Runnable JavaDoc () {
87                     public void run () {
88                         try {
89                             EditableProperties props = PropertyUtils.getGlobalProperties();
90                             boolean save = addLibraryIntoBuild(libraryImpl,props);
91                             if (save) {
92                                 PropertyUtils.putGlobalProperties (props);
93                             }
94                         } catch (IOException JavaDoc ioe) {
95                             ErrorManager.getDefault().notify (ioe);
96                         }
97                     }
98                 }
99         );
100     }
101
102     public void libraryDeleted(final LibraryImplementation libraryImpl) {
103         assert libraryImpl != null;
104         ProjectManager.mutex().postWriteRequest(new Runnable JavaDoc () {
105                 public void run() {
106                     try {
107                         EditableProperties props = PropertyUtils.getGlobalProperties();
108                         for (int i=0; i < VOLUME_TYPES.length; i++) {
109                             String JavaDoc property = LIB_PREFIX + libraryImpl.getName() + '.' + VOLUME_TYPES[i]; //NOI18N
110
props.remove(property);
111                         }
112                         PropertyUtils.putGlobalProperties(props);
113                     } catch (IOException JavaDoc ioe) {
114                         ErrorManager.getDefault().notify (ioe);
115                     }
116                 }
117             });
118     }
119
120     public Customizer JavaDoc getCustomizer(String JavaDoc volumeType) {
121         if (VOLUME_TYPES[0].equals(volumeType)||
122             VOLUME_TYPES[1].equals(volumeType)||
123             VOLUME_TYPES[2].equals(volumeType)) {
124             return new J2SEVolumeCustomizer (volumeType);
125         }
126         else {
127             return null;
128         }
129     }
130     
131
132     public Lookup getLookup() {
133         return Lookup.EMPTY;
134     }
135
136     public static LibraryTypeProvider create () {
137         return new J2SELibraryTypeProvider();
138     }
139
140     private static boolean addLibraryIntoBuild(LibraryImplementation impl, EditableProperties props) {
141         boolean modified = false;
142         for (int i=0; i<VOLUME_TYPES.length; i++) {
143             String JavaDoc propName = LIB_PREFIX + impl.getName() + '.' + VOLUME_TYPES[i]; //NOI18N
144
List JavaDoc roots = impl.getContent (VOLUME_TYPES[i]);
145             if (roots == null) {
146                 //Non valid library, but try to recover
147
continue;
148             }
149             StringBuffer JavaDoc propValue = new StringBuffer JavaDoc();
150             boolean first = true;
151             for (Iterator JavaDoc rootsIt=roots.iterator(); rootsIt.hasNext();) {
152                 URL JavaDoc url = (URL JavaDoc) rootsIt.next();
153                 if ("jar".equals(url.getProtocol())) {
154                     url = FileUtil.getArchiveFile (url);
155                     // XXX check whether this is really the root
156
}
157                 File JavaDoc f = null;
158                 FileObject fo = URLMapper.findFileObject(url);
159                 if (fo != null) {
160                     f = FileUtil.toFile(fo);
161                 }
162                 else if ("file".equals(url.getProtocol())) { //NOI18N
163
//If the file does not exist (eg library from cleaned project)
164
// and it is a file protocol URL, add it.
165
URI JavaDoc uri = URI.create (url.toExternalForm());
166                     if (uri != null) {
167                         f = new File JavaDoc (uri);
168                     }
169                 }
170                 if (f != null) {
171                     if (!first) {
172                         propValue.append(File.pathSeparatorChar);
173                     }
174                     first = false;
175                     f = FileUtil.normalizeFile(f);
176                     propValue.append (f.getAbsolutePath());
177                 }
178                 else {
179                     ErrorManager.getDefault().log ("J2SELibraryTypeProvider: Can not resolve URL: "+url);
180                 }
181             }
182             String JavaDoc oldValue = props.getProperty (propName);
183             String JavaDoc newValue = propValue.toString();
184             if (!newValue.equals(oldValue)) {
185                     props.setProperty (propName, newValue);
186                     modified = true;
187             }
188         }
189         return modified;
190     }
191     
192 }
193
Popular Tags