1 19 20 package org.netbeans.modules.j2ee.clientproject; 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.j2ee.clientproject.classpath.AppClientProjectClassPathExtender; 31 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties; 32 import org.netbeans.modules.j2ee.dd.api.client.AppClient; 33 import org.netbeans.spi.project.ActionProvider; 34 import org.netbeans.spi.project.CopyOperationImplementation; 35 import org.netbeans.spi.project.DeleteOperationImplementation; 36 import org.netbeans.spi.project.MoveOperationImplementation; 37 import org.netbeans.spi.project.support.ant.AntProjectHelper; 38 import org.netbeans.spi.project.support.ant.EditableProperties; 39 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 40 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 41 import org.netbeans.spi.project.support.ant.PropertyUtils; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.Lookup; 46 import org.openide.util.NbBundle; 47 import org.openide.util.lookup.Lookups; 48 49 53 public class AppClientProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation { 54 55 private final AppClientProject project; 56 57 public AppClientProjectOperations(AppClientProject project) { 58 this.project = project; 59 } 60 61 private static void addFile(FileObject projectDirectory, String fileName, List <FileObject> result) { 62 FileObject file = projectDirectory.getFileObject(fileName); 63 64 if (file != null) { 65 result.add(file); 66 } 67 } 68 69 public List <FileObject> getMetadataFiles() { 70 FileObject projectDirectory = project.getProjectDirectory(); 71 List <FileObject> files = new ArrayList <FileObject>(); 72 73 addFile(projectDirectory, "nbproject", files); addFile(projectDirectory, "build.xml", files); addFile(projectDirectory, "catalog.xml", files); 77 return files; 78 } 79 80 public List <FileObject> getDataFiles() { 81 List <FileObject> files = new ArrayList <FileObject>(); 82 83 FileObject metaInf = project.getCarModule().getMetaInf(); 84 if (metaInf != null) 85 files.add(metaInf); 86 87 SourceRoots src = project.getSourceRoots(); 88 FileObject[] srcRoots = src.getRoots(); 89 90 for (int cntr = 0; cntr < srcRoots.length; cntr++) { 91 files.add(srcRoots[cntr]); 92 } 93 94 PropertyEvaluator evaluator = project.evaluator(); 95 String prop = evaluator.getProperty(AppClientProjectProperties.SOURCE_ROOT); 96 if (prop != null) { 97 FileObject projectDirectory = project.getProjectDirectory(); 98 FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop); 99 if (projectDirectory != srcDir && !files.contains(srcDir)) 100 files.add(srcDir); 101 } 102 103 SourceRoots test = project.getTestSourceRoots(); 104 FileObject[] testRoots = test.getRoots(); 105 106 for (int cntr = 0; cntr < testRoots.length; cntr++) { 107 files.add(testRoots[cntr]); 108 } 109 110 File resourceDir = project.getCarModule().getEnterpriseResourceDirectory(); 111 if (resourceDir != null) { 112 FileObject resourceFO = FileUtil.toFileObject(resourceDir); 113 if (resourceFO != null) 114 files.add(resourceFO); 115 } 116 117 return files; 118 } 119 120 public void notifyDeleting() throws IOException { 121 AppClientActionProvider ap = (AppClientActionProvider) project.getLookup().lookup(AppClientActionProvider.class); 122 123 assert ap != null; 124 125 Lookup context = Lookups.fixed(new Object [0]); 126 Properties p = new Properties (); 127 String [] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p); 128 FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH); 129 130 assert targetNames != null; 131 assert targetNames.length > 0; 132 133 ActionUtils.runTarget(buildXML, targetNames, p).waitFinished(); 134 135 AppClientProjectClassPathExtender extender = (AppClientProjectClassPathExtender)project.getLookup().lookup(AppClientProjectClassPathExtender.class); 136 extender.notifyDeleting(); 137 } 138 139 public void notifyDeleted() throws IOException { 140 project.getAntProjectHelper().notifyDeleted(); 141 } 142 143 public void notifyCopying() { 144 } 146 147 public void notifyCopied(Project original, File originalPath, String nueName) { 148 if (original == null) { 149 return ; 151 } 152 153 project.getReferenceHelper().fixReferences(originalPath); 154 fixOtherReferences(originalPath); 155 156 project.setName(nueName); 157 } 158 159 public void notifyMoving() throws IOException { 160 if (!this.project.getUpdateHelper().requestSave()) { 161 throw new IOException (NbBundle.getMessage(AppClientProjectOperations.class, 162 "MSG_OldProjectMetadata")); } 164 notifyDeleting(); 165 } 166 167 public void notifyMoved(Project original, File originalPath, final String newName) { 168 if (original == null) { 169 project.getAntProjectHelper().notifyDeleted(); 170 return ; 171 } 172 173 final String oldProjectName = project.getName(); 174 175 project.setName(newName); 176 project.getReferenceHelper().fixReferences(originalPath); 177 fixOtherReferences(originalPath); 178 179 ProjectManager.mutex().writeAccess(new Runnable () { 180 public void run() { 181 AntProjectHelper helper = project.getAntProjectHelper(); 182 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 183 184 String jarName = (String ) projectProps.get(AppClientProjectProperties.JAR_NAME); 185 String oldName = jarName.substring(0, jarName.length() - 4); 186 if (jarName.endsWith(".jar") && oldName.equals(oldProjectName)) projectProps.put(AppClientProjectProperties.JAR_NAME, newName + ".jar"); 189 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps); 190 AppClient appClient = Utils.getAppClient(project); 191 appClient.setDisplayName(newName); 192 FileObject ddFO = project.getCarModule().getDeploymentDescriptor(); 193 if (ddFO != null) { 194 try { 195 appClient.write(ddFO); 196 } catch (IOException ioe) { 197 ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe); 198 } 199 } 200 } 201 }); 202 } 203 204 private void fixOtherReferences(final File originalPath) { 205 final String property = AppClientProjectProperties.META_INF; 206 final File projectDir = FileUtil.toFile(project.getProjectDirectory()); 207 208 ProjectManager.mutex().writeAccess(new Runnable () { 209 public void run() { 210 EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 211 String path = props.getProperty(property); 212 if (path == null) { 213 return; 214 } 215 216 if (path.startsWith(originalPath.getAbsolutePath())) { 217 String relative = PropertyUtils.relativizeFile(originalPath, new File (path)); 218 String fixedPath = new File (projectDir, relative).getAbsolutePath(); 219 props.setProperty(property, fixedPath); 220 project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); 221 } 222 } 223 }); 224 } 225 } 226 | Popular Tags |