2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.j2ee.archive.project;
45
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.Enumeration;
48
import java.util.List;
49
import org.netbeans.api.project.Project;
50
import org.netbeans.api.project.ProjectManager;
51
import org.netbeans.spi.project.CopyOperationImplementation;
52
import org.netbeans.spi.project.DeleteOperationImplementation;
53
import org.netbeans.spi.project.MoveOperationImplementation;
54
import org.netbeans.spi.project.support.ant.AntProjectHelper;
55
import org.netbeans.spi.project.support.ant.EditableProperties;
56
import org.openide.ErrorManager;
57
import org.openide.filesystems.FileObject;
58
import org.openide.filesystems.FileUtil;
62
* @author Ludovic Champenois
64
public class ArchiveProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
66
private ArchiveProject project;
68
public ArchiveProjectOperations(ArchiveProject project) {
69
this.project = project;
72
private static void addFile(FileObject projectDirectory, String fileName, List<FileObject> result) {
73
FileObject file = projectDirectory.getFileObject(fileName);
80
public List<FileObject> getMetadataFiles() {
81
FileObject projectDirectory = project.getProjectDirectory();
82
List<FileObject> files = new ArrayList<FileObject>();
84
addFile(projectDirectory, "nbproject", files); // NOI18N
85
addFile(projectDirectory, "build.xml", files); // NOI18N
86
addFile(projectDirectory, "dist", files); // NOI18N
91
private static final String TMPPROJ_LIT = "tmpproj"; // NOI18N
92
private static final String SUBARCHIVES_LIT = "subarchives"; // NOI18N
94
public List<FileObject> getDataFiles() {
95
FileObject projectDirectory = project.getProjectDirectory();
96
List/*<FileObject>*/ files = new ArrayList();
97
addFile(projectDirectory, TMPPROJ_LIT, files);
98
addFile(projectDirectory, SUBARCHIVES_LIT, files);
99
addFile(projectDirectory, "setup",files); // NOI18N
104
public void notifyDeleting() throws IOException {
108
public void notifyDeleted() throws IOException {
109
if (project.getProjectDirectory().getFileObject(TMPPROJ_LIT) == null) {
110
project.getProjectDirectory().delete();
112
project.getAntProjectHelper().notifyDeleted();
115
public void notifyCopying() {
119
public void notifyCopied(final Project original, final File originalPath, final String nueName) {
120
if (original == null) {
121
//do nothing for the original project.
124
ProjectManager.mutex().writeAccess(new Runnable() {
127
copyFilesToNewLocation(nueName, original);
128
} catch (IOException ex) {
129
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + ex);
136
private void copyFilesToNewLocation(final String nueName, final Project original) throws IOException {
137
project.setName(nueName);
139
AntProjectHelper helper = project.getAntProjectHelper();
140
EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
142
helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
144
// copy the tmpproject to the new location
145
if (!original.getProjectDirectory().equals(project.getProjectDirectory())) {
146
doCopy(original.getProjectDirectory().getFileObject(TMPPROJ_LIT),project.getProjectDirectory());
148
FileObject subdir = original.getProjectDirectory().getFileObject(SUBARCHIVES_LIT);
149
if (null != subdir) {
150
FileObject dest = FileUtil.createFolder(project.getProjectDirectory(),SUBARCHIVES_LIT);
151
Enumeration subarchives = subdir.getFolders(false);
152
while (subarchives.hasMoreElements()) {
153
FileObject t = (FileObject) subarchives.nextElement();
160
public void notifyMoving() throws IOException {
164
public void notifyMoved(final Project original, final File originalPath, final String newName) {
165
if (original == null) {
166
project.getAntProjectHelper().notifyDeleted();
169
ProjectManager.mutex().writeAccess(new Runnable() {
172
copyFilesToNewLocation(newName, original);
173
FileObject subdir = original.getProjectDirectory().getFileObject(SUBARCHIVES_LIT);
174
if (!original.getProjectDirectory().equals(project.getProjectDirectory())) {
176
if (null != subdir) {
179
} catch (IOException ex) {
180
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
182
FileObject target = original.getProjectDirectory().getFileObject(TMPPROJ_LIT);
183
if (null != target) {
186
} catch (IOException ex) {
187
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
190
target = original.getProjectDirectory();
191
if (null != target) {
194
} catch (IOException ex) {
195
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
199
} catch (IOException ex) {
200
// this is actually pretty bad...
201
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
208
private static void doCopy(FileObject from, FileObject toParent) throws IOException {
210
if (from.isFolder()) {
211
//FileObject copy = toParent.getF
212
FileObject copy = FileUtil.createFolder(toParent,from.getNameExt());
213
FileObject[] kids = from.getChildren();
214
for (int i = 0; i < kids.length; i++) {
215
doCopy(kids[i], copy);
218
assert from.isData();
219
FileObject target = toParent.getFileObject(from.getName(),from.getExt());
220
if (null == target) {
221
FileUtil.copyFile(from, toParent, from.getName(), from.getExt());