KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarProjectOperations


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.earproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Properties JavaDoc;
28 import org.apache.tools.ant.module.api.support.ActionUtils;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
32 import org.netbeans.spi.project.ActionProvider;
33 import org.netbeans.spi.project.CopyOperationImplementation;
34 import org.netbeans.spi.project.DeleteOperationImplementation;
35 import org.netbeans.spi.project.MoveOperationImplementation;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
39 import org.openide.filesystems.FileObject;
40 import org.openide.util.Lookup;
41 import org.openide.util.lookup.Lookups;
42
43 /**
44  * @author Jan Lahoda
45  */

46 public class EarProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
47     
48     private final EarProject project;
49     
50     public EarProjectOperations(EarProject project) {
51         this.project = project;
52     }
53     
54     private static void addFile(FileObject projectDirectory, String JavaDoc fileName, List JavaDoc<FileObject> result) {
55         FileObject file = projectDirectory.getFileObject(fileName);
56         if (file != null) {
57             result.add(file);
58         }
59     }
60     
61     public List JavaDoc/*<FileObject>*/ getMetadataFiles() {
62         FileObject projectDirectory = project.getProjectDirectory();
63         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
64         
65         addFile(projectDirectory, "nbproject", files); // NOI18N
66
addFile(projectDirectory, "build.xml", files); // NOI18N
67
addFile(projectDirectory, "src", files); // NOI18N
68
addFile(projectDirectory, ".cvsignore", files); // NOI18N
69

70         return files;
71     }
72     
73     public List JavaDoc<FileObject> getDataFiles() {
74         return Collections.emptyList();
75     }
76     
77     public void notifyDeleting() throws IOException JavaDoc {
78         EarActionProvider ap = (EarActionProvider) project.getLookup().lookup(EarActionProvider.class);
79         
80         assert ap != null;
81         
82         Lookup context = Lookups.fixed(new Object JavaDoc[0]);
83         Properties JavaDoc p = new Properties JavaDoc();
84         String JavaDoc[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p);
85         FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
86         
87         assert targetNames != null;
88         assert targetNames.length > 0;
89         
90         ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
91     }
92     
93     public void notifyDeleted() throws IOException JavaDoc {
94         project.getAntProjectHelper().notifyDeleted();
95     }
96    
97     public void notifyCopying() {
98         //nothing.
99
}
100     
101     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) {
102         if (original == null) {
103             //do nothing for the original project.
104
return ;
105         }
106         
107         project.setName(nueName);
108     }
109     
110     public void notifyMoving() throws IOException JavaDoc {
111         notifyDeleting();
112     }
113     
114     public void notifyMoved(Project original, File JavaDoc originalPath, final String JavaDoc newName) {
115         if (original == null) {
116             project.getAntProjectHelper().notifyDeleted();
117             return ;
118         }
119     
120     final String JavaDoc oldProjectName = project.getName();
121         
122         project.setName(newName);
123     
124         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
125             public void run() {
126         AntProjectHelper helper = project.getAntProjectHelper();
127         EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
128
129         String JavaDoc earName = (String JavaDoc) projectProps.get(EarProjectProperties.JAR_NAME);
130         String JavaDoc oldName = earName.substring(0, earName.length() - 4);
131         if (earName.endsWith(".ear") && oldName.equals(oldProjectName)) //NOI18N
132
projectProps.put(EarProjectProperties.JAR_NAME, newName + ".ear"); //NOI18N
133

134         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
135             }
136         });
137
138     }
139         
140 }
141
Popular Tags