KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > project > ArchiveProjectOperations


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.archive.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.List JavaDoc;
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 /**
39  *
40  * @author Ludovic Champenois
41  */

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 JavaDoc fileName, List JavaDoc<FileObject> result) {
51         FileObject file = projectDirectory.getFileObject(fileName);
52         
53         if (file != null) {
54             result.add(file);
55         }
56     }
57     
58     public List JavaDoc<FileObject> getMetadataFiles() {
59         FileObject projectDirectory = project.getProjectDirectory();
60         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
61         
62         addFile(projectDirectory, "nbproject", files); // NOI18N
63
addFile(projectDirectory, "build.xml", files); // NOI18N
64
addFile(projectDirectory, "dist", files); // NOI18N
65

66         return files;
67     }
68     
69     private static final String JavaDoc TMPPROJ_LIT = "tmpproj"; // NOI18N
70
private static final String JavaDoc SUBARCHIVES_LIT = "subarchives"; // NOI18N
71

72     public List JavaDoc<FileObject> getDataFiles() {
73         FileObject projectDirectory = project.getProjectDirectory();
74         List JavaDoc/*<FileObject>*/ files = new ArrayList JavaDoc();
75         addFile(projectDirectory, TMPPROJ_LIT, files);
76         addFile(projectDirectory, SUBARCHIVES_LIT, files);
77         addFile(projectDirectory, "setup",files); // NOI18N
78

79         return files;
80     }
81     
82     public void notifyDeleting() throws IOException JavaDoc {
83         
84     }
85     
86     public void notifyDeleted() throws IOException JavaDoc {
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 JavaDoc originalPath, final String JavaDoc nueName) {
98         if (original == null) {
99             //do nothing for the original project.
100
return ;
101         }
102         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
103             public void run() {
104                 try {
105                     copyFilesToNewLocation(nueName, original);
106                 } catch (IOException JavaDoc ex) {
107                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + ex);
108                 }
109             }
110             
111         });
112     }
113     
114     private void copyFilesToNewLocation(final String JavaDoc nueName, final Project original) throws IOException JavaDoc {
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         // copy the tmpproject to the new location
123
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 JavaDoc 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 JavaDoc {
139         
140     }
141     
142     public void notifyMoved(final Project original, final File JavaDoc originalPath, final String JavaDoc newName) {
143         if (original == null) {
144             project.getAntProjectHelper().notifyDeleted();
145             return ;
146         }
147         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc 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 JavaDoc ex) {
173                                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
174                             }
175                         }
176                     }
177                 } catch (IOException JavaDoc ex) {
178                     // this is actually pretty bad...
179
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
180                 }
181                 
182             }
183         });
184     }
185     
186     private static void doCopy(FileObject from, FileObject toParent) throws IOException JavaDoc {
187         if (null != from) {
188             if (from.isFolder()) {
189                 //FileObject copy = toParent.getF
190
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