1 19 20 package org.netbeans.modules.j2ee.archive.project; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import org.netbeans.api.project.Project; 28 import org.netbeans.api.project.ProjectManager; 29 import org.netbeans.spi.project.CopyOperationImplementation; 30 import org.netbeans.spi.project.DeleteOperationImplementation; 31 import org.netbeans.spi.project.MoveOperationImplementation; 32 import org.netbeans.spi.project.support.ant.AntProjectHelper; 33 import org.netbeans.spi.project.support.ant.EditableProperties; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 38 42 public class ArchiveProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation { 43 44 private ArchiveProject project; 45 46 public ArchiveProjectOperations(ArchiveProject project) { 47 this.project = project; 48 } 49 50 private static void addFile(FileObject projectDirectory, String fileName, List <FileObject> result) { 51 FileObject file = projectDirectory.getFileObject(fileName); 52 53 if (file != null) { 54 result.add(file); 55 } 56 } 57 58 public List <FileObject> getMetadataFiles() { 59 FileObject projectDirectory = project.getProjectDirectory(); 60 List <FileObject> files = new ArrayList <FileObject>(); 61 62 addFile(projectDirectory, "nbproject", files); addFile(projectDirectory, "build.xml", files); addFile(projectDirectory, "dist", files); 66 return files; 67 } 68 69 private static final String TMPPROJ_LIT = "tmpproj"; private static final String SUBARCHIVES_LIT = "subarchives"; 72 public List <FileObject> getDataFiles() { 73 FileObject projectDirectory = project.getProjectDirectory(); 74 List files = new ArrayList (); 75 addFile(projectDirectory, TMPPROJ_LIT, files); 76 addFile(projectDirectory, SUBARCHIVES_LIT, files); 77 addFile(projectDirectory, "setup",files); 79 return files; 80 } 81 82 public void notifyDeleting() throws IOException { 83 84 } 85 86 public void notifyDeleted() throws IOException { 87 if (project.getProjectDirectory().getFileObject(TMPPROJ_LIT) == null) { 88 project.getProjectDirectory().delete(); 89 } 90 project.getAntProjectHelper().notifyDeleted(); 91 } 92 93 public void notifyCopying() { 94 95 } 96 97 public void notifyCopied(final Project original, final File originalPath, final String nueName) { 98 if (original == null) { 99 return ; 101 } 102 ProjectManager.mutex().writeAccess(new Runnable () { 103 public void run() { 104 try { 105 copyFilesToNewLocation(nueName, original); 106 } catch (IOException ex) { 107 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + ex); 108 } 109 } 110 111 }); 112 } 113 114 private void copyFilesToNewLocation(final String nueName, final Project original) throws IOException { 115 project.setName(nueName); 116 117 AntProjectHelper helper = project.getAntProjectHelper(); 118 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 119 120 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps); 121 122 if (!original.getProjectDirectory().equals(project.getProjectDirectory())) { 124 doCopy(original.getProjectDirectory().getFileObject(TMPPROJ_LIT),project.getProjectDirectory()); 125 126 FileObject subdir = original.getProjectDirectory().getFileObject(SUBARCHIVES_LIT); 127 if (null != subdir) { 128 FileObject dest = FileUtil.createFolder(project.getProjectDirectory(),SUBARCHIVES_LIT); 129 Enumeration subarchives = subdir.getFolders(false); 130 while (subarchives.hasMoreElements()) { 131 FileObject t = (FileObject) subarchives.nextElement(); 132 doCopy(t,dest); 133 } 134 } 135 } 136 } 137 138 public void notifyMoving() throws IOException { 139 140 } 141 142 public void notifyMoved(final Project original, final File originalPath, final String newName) { 143 if (original == null) { 144 project.getAntProjectHelper().notifyDeleted(); 145 return ; 146 } 147 ProjectManager.mutex().writeAccess(new Runnable () { 148 public void run() { 149 try { 150 copyFilesToNewLocation(newName, original); 151 FileObject subdir = original.getProjectDirectory().getFileObject(SUBARCHIVES_LIT); 152 if (!original.getProjectDirectory().equals(project.getProjectDirectory())) { 153 try { 154 if (null != subdir) { 155 subdir.delete(); 156 } 157 } catch (IOException ex) { 158 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 159 } 160 FileObject target = original.getProjectDirectory().getFileObject(TMPPROJ_LIT); 161 if (null != target) { 162 try { 163 target.delete(); 164 } catch (IOException ex) { 165 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 166 } 167 } 168 target = original.getProjectDirectory(); 169 if (null != target) { 170 try { 171 target.delete(); 172 } catch (IOException ex) { 173 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 174 } 175 } 176 } 177 } catch (IOException ex) { 178 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 180 } 181 182 } 183 }); 184 } 185 186 private static void doCopy(FileObject from, FileObject toParent) throws IOException { 187 if (null != from) { 188 if (from.isFolder()) { 189 FileObject copy = FileUtil.createFolder(toParent,from.getNameExt()); 191 FileObject[] kids = from.getChildren(); 192 for (int i = 0; i < kids.length; i++) { 193 doCopy(kids[i], copy); 194 } 195 } else { 196 assert from.isData(); 197 FileObject target = toParent.getFileObject(from.getName(),from.getExt()); 198 if (null == target) { 199 FileUtil.copyFile(from, toParent, from.getName(), from.getExt()); 200 } 201 } 202 } 203 } 204 205 } 206 | Popular Tags |