1 19 20 package org.netbeans.modules.web.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Properties ; 27 import org.apache.tools.ant.module.api.support.ActionUtils; 28 import org.netbeans.api.project.Project; 29 import org.netbeans.api.project.ProjectManager; 30 import org.netbeans.modules.web.project.classpath.WebProjectClassPathExtender; 31 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 32 import org.netbeans.spi.project.ActionProvider; 33 import org.netbeans.spi.project.CopyOperationImplementation; 34 import org.netbeans.spi.project.DeleteOperationImplementation; 35 import org.netbeans.spi.project.MoveOperationImplementation; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.EditableProperties; 38 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 39 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 40 import org.netbeans.spi.project.support.ant.PropertyUtils; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.util.Lookup; 44 import org.openide.util.lookup.Lookups; 45 46 50 public class WebProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation { 51 52 private WebProject project; 53 54 public WebProjectOperations(WebProject project) { 55 this.project = project; 56 } 57 58 private static void addFile(FileObject projectDirectory, String fileName, List result) { 59 FileObject file = projectDirectory.getFileObject(fileName); 60 61 if (file != null) { 62 result.add(file); 63 } 64 } 65 66 public List getMetadataFiles() { 67 FileObject projectDirectory = project.getProjectDirectory(); 68 List files = new ArrayList (); 69 70 addFile(projectDirectory, "nbproject", files); addFile(projectDirectory, "build.xml", files); addFile(projectDirectory, "catalog.xml", files); 74 return files; 75 } 76 77 public List getDataFiles() { 78 List files = new ArrayList (); 79 80 FileObject docRoot = project.getAPIWebModule().getDocumentBase(); 81 if (docRoot != null) 82 files.add(docRoot); 83 84 FileObject confDir = project.getWebModule().getConfDir(); 85 if (confDir != null) 86 files.add(confDir); 87 88 File resourceDir = project.getWebModule().getEnterpriseResourceDirectory(); 89 if (resourceDir != null) { 90 FileObject resourceFO = FileUtil.toFileObject(resourceDir); 91 if (resourceFO != null) 92 files.add(resourceFO); 93 } 94 95 SourceRoots src = project.getSourceRoots(); 96 FileObject[] srcRoots = src.getRoots(); 97 98 for (int cntr = 0; cntr < srcRoots.length; cntr++) { 99 files.add(srcRoots[cntr]); 100 } 101 102 PropertyEvaluator evaluator = project.evaluator(); 103 String prop = evaluator.getProperty(WebProjectProperties.SOURCE_ROOT); 104 if (prop != null) { 105 FileObject projectDirectory = project.getProjectDirectory(); 106 FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop); 107 if (projectDirectory != srcDir && !files.contains(srcDir)) 108 files.add(srcDir); 109 } 110 111 SourceRoots test = project.getTestSourceRoots(); 112 FileObject[] testRoots = test.getRoots(); 113 114 for (int cntr = 0; cntr < testRoots.length; cntr++) { 115 files.add(testRoots[cntr]); 116 } 117 118 return files; 119 } 120 121 public void notifyDeleting() throws IOException { 122 WebActionProvider ap = (WebActionProvider) project.getLookup().lookup(WebActionProvider.class); 123 124 assert ap != null; 125 126 Lookup context = Lookups.fixed(new Object [0]); 127 Properties p = new Properties (); 128 String [] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p); 129 FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH); 130 131 assert targetNames != null; 132 assert targetNames.length > 0; 133 134 ActionUtils.runTarget(buildXML, targetNames, p).waitFinished(); 135 136 WebProjectClassPathExtender extender = (WebProjectClassPathExtender)project.getLookup().lookup(WebProjectClassPathExtender.class); 137 extender.notifyDeleting(); 138 } 139 140 public void notifyDeleted() throws IOException { 141 project.getAntProjectHelper().notifyDeleted(); 142 } 143 144 public void notifyCopying() { 145 } 147 148 public void notifyCopied(Project original, File originalPath, final String newName) { 149 if (original == null) { 150 return ; 152 } 153 154 final String oldProjectName = project.getName(); 155 156 project.getReferenceHelper().fixReferences(originalPath); 157 158 project.setName(newName); 159 160 ProjectManager.mutex().writeAccess(new Runnable () { 161 public void run() { 162 AntProjectHelper helper = project.getAntProjectHelper(); 163 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 164 165 String warName = (String ) projectProps.get(WebProjectProperties.WAR_NAME); 166 String warEarName = (String ) projectProps.get(WebProjectProperties.WAR_EAR_NAME); 167 String oldName = warName.substring(0, warName.length() - 4); 168 if (warName.endsWith(".war") && oldName.equals(oldProjectName)) projectProps.put(WebProjectProperties.WAR_NAME, PropertyUtils.getUsablePropertyName(newName) + ".war"); if (warEarName.endsWith(".war") && oldName.equals(oldProjectName)) projectProps.put(WebProjectProperties.WAR_EAR_NAME, PropertyUtils.getUsablePropertyName(newName) + ".war"); 173 ProjectWebModule wm = (ProjectWebModule) project.getLookup ().lookup(ProjectWebModule.class); 174 if (wm != null) wm.setContextPath("/" + newName); 176 177 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps); 178 } 179 }); 180 } 181 182 public void notifyMoving() throws IOException { 183 notifyDeleting(); 184 } 185 186 public void notifyMoved(Project original, File originalPath, final String newName) { 187 if (original == null) { 188 project.getAntProjectHelper().notifyDeleted(); 189 return ; 190 } 191 192 final String oldProjectName = project.getName(); 193 194 project.setName(newName); 195 project.getReferenceHelper().fixReferences(originalPath); 196 197 ProjectManager.mutex().writeAccess(new Runnable () { 198 public void run() { 199 AntProjectHelper helper = project.getAntProjectHelper(); 200 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 201 EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 202 203 String warName = (String ) projectProps.get(WebProjectProperties.WAR_NAME); 204 String warEarName = (String ) projectProps.get(WebProjectProperties.WAR_EAR_NAME); 205 String oldName = warName.substring(0, warName.length() - 4); 206 if (warName.endsWith(".war") && oldName.equals(oldProjectName)) projectProps.put(WebProjectProperties.WAR_NAME, newName + ".war"); if (warEarName.endsWith(".war") && oldName.equals(oldProjectName)) projectProps.put(WebProjectProperties.WAR_EAR_NAME, newName + ".war"); 211 ProjectWebModule wm = (ProjectWebModule) project.getLookup().lookup(ProjectWebModule.class); 212 String serverId = privateProps.getProperty(WebProjectProperties.J2EE_SERVER_INSTANCE); 213 String oldCP = wm.getContextPath(serverId); 214 if (oldCP != null && oldName.equals(oldCP.substring(1))) 215 wm.setContextPath(serverId, "/" + newName); 217 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps); 218 } 219 }); 220 } 221 222 } 223 | Popular Tags |