1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.modules.apisupport.project.layers.LayerUtils; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.modules.SpecificationVersion; 39 40 56 public final class CreatedModifiedFiles { 57 58 63 public interface Operation { 64 65 66 void run() throws IOException ; 67 68 75 String [] getModifiedPaths(); 76 77 83 String [] getCreatedPaths(); 84 85 90 String [] getInvalidPaths(); 91 92 103 104 } 105 106 private final SortedSet <String > createdPaths = new TreeSet (); 107 private final SortedSet <String > modifiedPaths = new TreeSet (); 108 private final SortedSet <String > invalidPaths = new TreeSet (); 109 110 111 private final Project project; 112 private final List <CreatedModifiedFiles.Operation> operations = new ArrayList (); 113 114 private LayerUtils.LayerHandle layerHandle; 117 LayerUtils.LayerHandle getLayerHandle() { 118 if (layerHandle == null) { 119 layerHandle = LayerUtils.layerForProject(project); 120 } 121 return layerHandle; 122 } 123 124 128 public CreatedModifiedFiles(Project project) { 129 this.project = project; 130 } 131 132 140 public void add(Operation operation) { 141 operations.add(operation); 142 createdPaths.addAll(Arrays.asList(operation.getCreatedPaths())); 144 modifiedPaths.addAll(Arrays.asList(operation.getModifiedPaths())); 145 invalidPaths.addAll(Arrays.asList(operation.getInvalidPaths())); 146 } 147 148 152 public void run() throws IOException { 153 boolean oldAutosave = false; 154 if (layerHandle != null) { 155 oldAutosave = layerHandle.isAutosave(); 156 layerHandle.setAutosave(false); 157 } 158 try { 159 for (Iterator it = operations.iterator(); it.hasNext(); ) { 160 Operation op = (Operation) it.next(); 161 op.run(); 162 } 163 if (layerHandle != null) { 164 layerHandle.save(); 166 } 167 } finally { 168 if (layerHandle != null) { 169 layerHandle.setAutosave(oldAutosave); 170 } 171 } 172 } 175 176 public String [] getCreatedPaths() { 177 if (createdPaths == null) { 178 return new String [0]; 179 } else { 180 String [] s = new String [createdPaths.size()]; 181 return (String []) createdPaths.toArray(s); 182 } 183 } 184 185 public String [] getModifiedPaths() { 186 if (modifiedPaths == null) { 187 return new String [0]; 188 } else { 189 String [] s = new String [modifiedPaths.size()]; 190 return (String []) modifiedPaths.toArray(s); 191 } 192 } 193 194 public String [] getInvalidPaths() { 195 if (invalidPaths == null) { 196 return new String [0]; 197 } else { 198 String [] s = new String [invalidPaths.size()]; 199 return (String []) invalidPaths.toArray(s); 200 } 201 } 202 203 210 public Operation createFile(String path, URL content) { 211 return CreatedModifiedFilesFactory.createFile(project, path, content); 212 } 213 214 229 public Operation createFileWithSubstitutions(String path, 230 URL content, Map <String ,String > tokens) { 231 return CreatedModifiedFilesFactory.createFileWithSubstitutions(project, path, content, tokens); 232 } 233 234 239 public Operation bundleKey(String bundlePath, String key, String value) { 240 return CreatedModifiedFilesFactory.bundleKey(project, key, value, bundlePath); 241 } 242 243 248 public Operation bundleKeyDefaultBundle(String key, String value) { 249 return CreatedModifiedFilesFactory.bundleKeyDefaultBundle(project, key, value); 250 } 251 252 267 public Operation addLoaderSection(String dataLoaderClass, String installBefore) { 268 return CreatedModifiedFilesFactory.addLoaderSection(project, dataLoaderClass, installBefore); 269 } 270 271 283 public Operation addLookupRegistration(String interfaceClass, String implClass, boolean inTests) { 284 return CreatedModifiedFilesFactory.addLookupRegistration( 285 project, interfaceClass, implClass, inTests); 286 } 287 288 302 public Operation addModuleDependency(String codeNameBase, String 303 releaseVersion, SpecificationVersion version, boolean useInCompiler) { 304 return CreatedModifiedFilesFactory.addModuleDependency(project, codeNameBase, 305 releaseVersion, version, useInCompiler); 306 } 307 308 314 public CreatedModifiedFiles.Operation addModuleDependency(String codeNameBase) { 315 return addModuleDependency(codeNameBase, null, null, true); 316 } 317 318 345 public Operation createLayerEntry( 346 String layerPath, 347 URL content, 348 Map <String ,String > substitutionTokens, 349 String localizedDisplayName, 350 Map <String ,Object > fileAttributes) { 351 return CreatedModifiedFilesFactory.createLayerEntry(this, project, layerPath, 352 content, substitutionTokens, 353 localizedDisplayName, fileAttributes); 354 } 355 356 362 public Operation manifestModification(String section, Map <String ,String > attributes) { 363 return CreatedModifiedFilesFactory.manifestModification(project, section, attributes); 364 } 365 366 373 public Operation propertiesModification(String propertyPath, 374 Map <String ,String > properties) { 375 return CreatedModifiedFilesFactory.propertiesModification(project, propertyPath, properties); 376 } 377 378 388 public CreatedModifiedFiles.Operation createLayerAttribute(final String parentPath, 389 final String attrName, final Object attrValue) { 390 return layerModifications(new LayerOperation() { 391 public void run(FileSystem layer) throws IOException { 392 FileObject f = layer.findResource(parentPath); 393 if (f == null) { 394 403 throw new IOException (parentPath); 404 } 405 f.setAttribute(attrName, attrValue); 406 } 407 }, Collections.EMPTY_SET); 408 } 409 410 419 public Operation orderLayerEntry(final String layerPath, final String precedingItemName, 420 final String followingItemName) { 421 return layerModifications(new LayerOperation() { 422 public void run(FileSystem layer) throws IOException { 423 FileObject f = FileUtil.createFolder(layer.getRoot(), layerPath); 424 f.setAttribute(precedingItemName + '/' + followingItemName, Boolean.TRUE); 425 } 426 }, Collections.EMPTY_SET); 427 } 428 429 440 public Operation layerModifications(final LayerOperation op, final Set <String > externalFiles) { 441 return CreatedModifiedFilesFactory.layerModifications(project, op, externalFiles, this); 442 } 443 444 448 public interface LayerOperation { 449 450 455 void run(FileSystem layer) throws IOException ; 456 457 } 458 459 } 460 | Popular Tags |