~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/archiveproject/src/org/netbeans/modules/j2ee/archive/project/ArchiveProjectOperations.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.archive.project;
 
43
 
 
44
import java.io.File;
 
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;
 
59
 
 
60
/**
 
61
 *
 
62
 * @author Ludovic Champenois
 
63
 */
 
64
public class ArchiveProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
 
65
    
 
66
    private ArchiveProject project;
 
67
    
 
68
    public ArchiveProjectOperations(ArchiveProject project) {
 
69
        this.project = project;
 
70
    }
 
71
    
 
72
    private static void addFile(FileObject projectDirectory, String fileName, List<FileObject> result) {
 
73
        FileObject file = projectDirectory.getFileObject(fileName);
 
74
        
 
75
        if (file != null) {
 
76
            result.add(file);
 
77
        }
 
78
    }
 
79
    
 
80
    public List<FileObject> getMetadataFiles() {
 
81
        FileObject projectDirectory = project.getProjectDirectory();
 
82
        List<FileObject> files = new ArrayList<FileObject>();
 
83
        
 
84
        addFile(projectDirectory, "nbproject", files); // NOI18N
 
85
        addFile(projectDirectory, "build.xml", files); // NOI18N
 
86
        addFile(projectDirectory, "dist", files); // NOI18N
 
87
        
 
88
        return files;
 
89
    }
 
90
    
 
91
    private static final String TMPPROJ_LIT = "tmpproj";            // NOI18N
 
92
    private static final String SUBARCHIVES_LIT = "subarchives";    // NOI18N
 
93
    
 
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
 
100
        
 
101
        return files;
 
102
    }
 
103
    
 
104
    public void notifyDeleting() throws IOException {
 
105
        
 
106
    }
 
107
    
 
108
    public void notifyDeleted() throws IOException {
 
109
        if (project.getProjectDirectory().getFileObject(TMPPROJ_LIT) == null) {
 
110
            project.getProjectDirectory().delete();
 
111
        }
 
112
        project.getAntProjectHelper().notifyDeleted();
 
113
    }
 
114
    
 
115
    public void notifyCopying() {
 
116
        
 
117
    }
 
118
    
 
119
    public void notifyCopied(final Project original, final File originalPath, final String nueName) {
 
120
        if (original == null) {
 
121
            //do nothing for the original project.
 
122
            return ;
 
123
        }
 
124
        ProjectManager.mutex().writeAccess(new Runnable() {
 
125
            public void run() {
 
126
                try {
 
127
                    copyFilesToNewLocation(nueName, original);
 
128
                } catch (IOException ex) {
 
129
                    ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + ex);
 
130
                }
 
131
            }
 
132
            
 
133
        });
 
134
    }
 
135
    
 
136
    private void copyFilesToNewLocation(final String nueName, final Project original) throws IOException {
 
137
        project.setName(nueName);
 
138
        
 
139
        AntProjectHelper helper = project.getAntProjectHelper();
 
140
        EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
 
141
        
 
142
        helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
 
143
        
 
144
        // copy the tmpproject to the new location
 
145
        if (!original.getProjectDirectory().equals(project.getProjectDirectory())) {
 
146
            doCopy(original.getProjectDirectory().getFileObject(TMPPROJ_LIT),project.getProjectDirectory());
 
147
            
 
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();
 
154
                    doCopy(t,dest);
 
155
                }
 
156
            }
 
157
        }
 
158
    }
 
159
    
 
160
    public void notifyMoving() throws IOException {
 
161
        
 
162
    }
 
163
    
 
164
    public void notifyMoved(final Project original, final File originalPath, final String newName) {
 
165
        if (original == null) {
 
166
            project.getAntProjectHelper().notifyDeleted();
 
167
            return ;
 
168
        }
 
169
        ProjectManager.mutex().writeAccess(new Runnable() {
 
170
            public void run() {
 
171
                try {
 
172
                    copyFilesToNewLocation(newName, original);
 
173
                    FileObject subdir = original.getProjectDirectory().getFileObject(SUBARCHIVES_LIT);
 
174
                    if (!original.getProjectDirectory().equals(project.getProjectDirectory())) {                        
 
175
                        try {
 
176
                            if (null != subdir) {
 
177
                                subdir.delete();
 
178
                            }
 
179
                        } catch (IOException ex) {
 
180
                            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
 
181
                        }
 
182
                        FileObject target = original.getProjectDirectory().getFileObject(TMPPROJ_LIT);
 
183
                        if (null != target) {
 
184
                            try {
 
185
                                target.delete();
 
186
                            } catch (IOException ex) {
 
187
                                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
 
188
                            }
 
189
                        }
 
190
                        target = original.getProjectDirectory();
 
191
                        if (null != target) {
 
192
                            try {
 
193
                                target.delete();
 
194
                            } catch (IOException ex) {
 
195
                                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
 
196
                            }
 
197
                        }
 
198
                    }
 
199
                } catch (IOException ex) {
 
200
                    // this is actually pretty bad...
 
201
                    ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
 
202
                }
 
203
                
 
204
            }
 
205
        });
 
206
    }
 
207
    
 
208
    private static void doCopy(FileObject from, FileObject toParent) throws IOException {
 
209
        if (null != from) {
 
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);
 
216
                }
 
217
            } else {
 
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());
 
222
                }
 
223
            }
 
224
        }
 
225
    }
 
226
    
 
227
}