1 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 ; 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.util.List ; 36 import java.util.Iterator ; 37 import java.net.URL ; 38 import java.net.URI ; 39 import java.net.URISyntaxException ; 40 import org.openide.util.Lookup; 41 import org.openide.util.NbBundle; 42 43 50 public final class J2SELibraryTypeProvider implements LibraryTypeProvider { 51 52 private J2SELibraryTypeProvider () { 53 } 54 55 private static final String LIB_PREFIX = "libs."; 56 public static final String LIBRARY_TYPE = "j2se"; public static final String VOLUME_TYPE_CLASSPATH = "classpath"; public static final String VOLUME_TYPE_SRC = "src"; public static final String VOLUME_TYPE_JAVADOC = "javadoc"; public static final String [] VOLUME_TYPES = new String [] { 61 VOLUME_TYPE_CLASSPATH, 62 VOLUME_TYPE_SRC, 63 VOLUME_TYPE_JAVADOC, 64 }; 65 66 public String getLibraryType() { 67 return LIBRARY_TYPE; 68 } 69 70 public String getDisplayName () { 71 return NbBundle.getMessage (J2SELibraryTypeProvider.class,"TXT_J2SELibraryType"); 72 } 73 74 public String [] 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 () { 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 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 () { 105 public void run() { 106 try { 107 EditableProperties props = PropertyUtils.getGlobalProperties(); 108 for (int i=0; i < VOLUME_TYPES.length; i++) { 109 String property = LIB_PREFIX + libraryImpl.getName() + '.' + VOLUME_TYPES[i]; props.remove(property); 111 } 112 PropertyUtils.putGlobalProperties(props); 113 } catch (IOException ioe) { 114 ErrorManager.getDefault().notify (ioe); 115 } 116 } 117 }); 118 } 119 120 public Customizer getCustomizer(String 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 propName = LIB_PREFIX + impl.getName() + '.' + VOLUME_TYPES[i]; List roots = impl.getContent (VOLUME_TYPES[i]); 145 if (roots == null) { 146 continue; 148 } 149 StringBuffer propValue = new StringBuffer (); 150 boolean first = true; 151 for (Iterator rootsIt=roots.iterator(); rootsIt.hasNext();) { 152 URL url = (URL ) rootsIt.next(); 153 if ("jar".equals(url.getProtocol())) { 154 url = FileUtil.getArchiveFile (url); 155 } 157 File 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())) { URI uri = URI.create (url.toExternalForm()); 166 if (uri != null) { 167 f = new File (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 oldValue = props.getProperty (propName); 183 String 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 |