1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import org.apache.tools.ant.module.api.support.ActionUtils; 29 import org.netbeans.api.java.project.JavaProjectConstants; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectUtils; 32 import org.netbeans.api.project.SourceGroup; 33 import org.netbeans.api.project.Sources; 34 import org.netbeans.modules.apisupport.project.NbModuleProject; 35 import org.netbeans.modules.apisupport.project.suite.SuiteProject; 36 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils; 37 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo; 38 import org.netbeans.spi.project.ActionProvider; 39 import org.netbeans.spi.project.CopyOperationImplementation; 40 import org.netbeans.spi.project.DeleteOperationImplementation; 41 import org.netbeans.spi.project.MoveOperationImplementation; 42 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 43 import org.openide.DialogDisplayer; 44 import org.openide.NotifyDescriptor; 45 import org.openide.filesystems.FileObject; 46 import org.openide.util.NbBundle; 47 48 51 public final class ModuleOperations implements DeleteOperationImplementation, 52 MoveOperationImplementation, CopyOperationImplementation { 53 54 private static final Map <String , SuiteProject> TEMPORARY_CACHE = new HashMap (); 55 56 private final NbModuleProject project; 57 private final FileObject projectDir; 58 59 public ModuleOperations(final NbModuleProject project) { 60 this.project = project; 61 this.projectDir = project.getProjectDirectory(); 62 } 63 64 public void notifyDeleting() throws IOException { 65 notifyDeleting(false); 66 } 67 68 private void notifyDeleting(boolean temporary) throws IOException { 69 FileObject buildXML = projectDir.getFileObject(GeneratedFilesHelper.BUILD_XML_PATH); 70 ActionUtils.runTarget(buildXML, new String [] { ActionProvider.COMMAND_CLEAN }, null).waitFinished(); 71 72 SuiteProject suite = SuiteUtils.findSuite(project); 73 if (suite != null) { 74 if (temporary) { 75 SuiteUtils.removeModuleFromSuite(project); 76 } else { 77 SuiteUtils.removeModuleFromSuiteWithDependencies(project); 81 } 82 } 83 84 project.notifyDeleting(); 85 } 86 87 public void notifyDeleted() throws IOException { 88 project.getHelper().notifyDeleted(); 89 } 90 91 public void notifyMoving() throws IOException { 92 SuiteProject suite = SuiteUtils.findSuite(project); 93 if (suite != null) { 94 TEMPORARY_CACHE.put(project.getCodeNameBase(), suite); 95 } 96 notifyDeleting(true); 97 } 98 99 public void notifyMoved(Project original, File originalPath, String nueName) throws IOException { 100 if (original == null) { project.getHelper().notifyDeleted(); 102 } else { SuiteProject suite = (SuiteProject) TEMPORARY_CACHE.remove(project.getCodeNameBase()); 104 if (suite != null) { 105 SuiteUtils.addModule(suite, project); 106 } 107 boolean isRename = original.getProjectDirectory().getParent().equals( 108 project.getProjectDirectory().getParent()); 109 if (isRename) { 110 setDisplayName(nueName); 111 } 112 } 113 } 114 115 public void notifyCopying() throws IOException { 116 SuiteProject suite = SuiteUtils.findSuite(project); 117 if (suite != null) { 118 TEMPORARY_CACHE.put(project.getCodeNameBase(), suite); 123 SuiteUtils.removeModuleFromSuite(project); 124 } 125 } 126 127 public void notifyCopied(Project original, File originalPath, String nueName) throws IOException { 128 if (original == null) { SuiteProject suite = (SuiteProject) TEMPORARY_CACHE.remove(project.getCodeNameBase()); 130 if (suite != null) { 131 SuiteUtils.addModule(suite, (NbModuleProject) project); 134 } 135 } else { 136 adjustDisplayName(); 138 } 139 } 140 141 public List <FileObject> getMetadataFiles() { 142 List <FileObject> files = new ArrayList (); 143 addFile(GeneratedFilesHelper.BUILD_XML_PATH, files); 144 addFile("manifest.mf", files); addFile("nbproject", files); addFile(".cvsignore", files); return files; 148 } 149 150 public List <FileObject> getDataFiles() { 151 List <FileObject> files = new ArrayList (); 152 153 Sources srcs = ProjectUtils.getSources(project); 154 SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 155 for (int i = 0; i < grps.length; i++) { 156 FileObject srcRoot = grps[i].getRootFolder(); 157 if (srcRoot.getPath().endsWith("test/unit/src")) { addFile("test", files); } else { 160 files.add(srcRoot); 161 } 162 } 163 164 return files; 165 } 166 167 private void addFile(String fileName, List <FileObject> result) { 168 FileObject file = projectDir.getFileObject(fileName); 169 if (file != null) { 170 result.add(file); 171 } 172 } 173 174 private void adjustDisplayName() throws IOException { 175 setDisplayName(ProjectUtils.getInformation(project).getDisplayName() + " (0)"); } 178 179 private void setDisplayName(String nueName) throws IOException { 180 LocalizedBundleInfo.Provider lbiProvider = 181 (LocalizedBundleInfo.Provider) project.getLookup().lookup(LocalizedBundleInfo.Provider.class); 182 if (lbiProvider != null) { 183 LocalizedBundleInfo info = lbiProvider.getLocalizedBundleInfo(); 184 if (info != null) { 185 info.setDisplayName(nueName); info.store(); 188 } 189 } 190 } 191 192 static boolean canRun(final NbModuleProject project, final boolean emitWarningToUser) { 193 boolean result = true; 194 String testUserDir = project.evaluator().getProperty("test.user.dir"); FileObject testUserDirFO = project.getHelper().resolveFileObject(testUserDir); 196 if (testUserDirFO != null && testUserDirFO.isFolder()) { 197 FileObject lock = testUserDirFO.getFileObject("lock"); if (lock != null && lock.isData()) { 199 if (emitWarningToUser) { 200 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 201 NbBundle.getMessage(ModuleOperations.class, "ERR_ModuleIsBeingRun"))); 202 } 203 result = false; 204 } 205 } 206 return result; 207 } 208 209 } 210 | Popular Tags |