KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebProjectOperations


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import org.apache.tools.ant.module.api.support.ActionUtils;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.modules.web.project.classpath.WebProjectClassPathExtender;
31 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
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.netbeans.spi.project.support.ant.PropertyEvaluator;
40 import org.netbeans.spi.project.support.ant.PropertyUtils;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.Lookups;
45
46 /**
47  *
48  * @author Jan Lahoda
49  */

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

74         return files;
75     }
76     
77     public List JavaDoc/*<FileObject>*/ getDataFiles() {
78         List JavaDoc/*<FileObject>*/ files = new ArrayList JavaDoc();
79         
80         FileObject docRoot = project.getAPIWebModule().getDocumentBase();
81         if (docRoot != null)
82             files.add(docRoot);
83         
84         FileObject confDir = project.getWebModule().getConfDir();
85         if (confDir != null)
86             files.add(confDir);
87         
88         File JavaDoc resourceDir = project.getWebModule().getEnterpriseResourceDirectory();
89         if (resourceDir != null) {
90             FileObject resourceFO = FileUtil.toFileObject(resourceDir);
91             if (resourceFO != null)
92                 files.add(resourceFO);
93         }
94         
95         SourceRoots src = project.getSourceRoots();
96         FileObject[] srcRoots = src.getRoots();
97
98         for (int cntr = 0; cntr < srcRoots.length; cntr++) {
99             files.add(srcRoots[cntr]);
100         }
101
102         PropertyEvaluator evaluator = project.evaluator();
103         String JavaDoc prop = evaluator.getProperty(WebProjectProperties.SOURCE_ROOT);
104         if (prop != null) {
105             FileObject projectDirectory = project.getProjectDirectory();
106             FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop);
107             if (projectDirectory != srcDir && !files.contains(srcDir))
108                 files.add(srcDir);
109         }
110         
111         SourceRoots test = project.getTestSourceRoots();
112         FileObject[] testRoots = test.getRoots();
113         
114         for (int cntr = 0; cntr < testRoots.length; cntr++) {
115             files.add(testRoots[cntr]);
116         }
117
118         return files;
119     }
120     
121     public void notifyDeleting() throws IOException JavaDoc {
122         WebActionProvider ap = (WebActionProvider) project.getLookup().lookup(WebActionProvider.class);
123         
124         assert ap != null;
125         
126         Lookup context = Lookups.fixed(new Object JavaDoc[0]);
127         Properties JavaDoc p = new Properties JavaDoc();
128         String JavaDoc[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p);
129         FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
130         
131         assert targetNames != null;
132         assert targetNames.length > 0;
133         
134         ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
135         
136         WebProjectClassPathExtender extender = (WebProjectClassPathExtender)project.getLookup().lookup(WebProjectClassPathExtender.class);
137         extender.notifyDeleting();
138     }
139     
140     public void notifyDeleted() throws IOException JavaDoc {
141         project.getAntProjectHelper().notifyDeleted();
142     }
143     
144     public void notifyCopying() {
145         //nothing.
146
}
147     
148     public void notifyCopied(Project original, File JavaDoc originalPath, final String JavaDoc newName) {
149         if (original == null) {
150             //nothing for the original project
151
return ;
152         }
153         
154     final String JavaDoc oldProjectName = project.getName();
155         
156         project.getReferenceHelper().fixReferences(originalPath);
157         
158         project.setName(newName);
159         
160         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
161             public void run() {
162         AntProjectHelper helper = project.getAntProjectHelper();
163         EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
164
165         String JavaDoc warName = (String JavaDoc) projectProps.get(WebProjectProperties.WAR_NAME);
166         String JavaDoc warEarName = (String JavaDoc) projectProps.get(WebProjectProperties.WAR_EAR_NAME);
167         String JavaDoc oldName = warName.substring(0, warName.length() - 4);
168         if (warName.endsWith(".war") && oldName.equals(oldProjectName)) //NOI18N
169
projectProps.put(WebProjectProperties.WAR_NAME, PropertyUtils.getUsablePropertyName(newName) + ".war"); //NOI18N
170
if (warEarName.endsWith(".war") && oldName.equals(oldProjectName)) //NOI18N
171
projectProps.put(WebProjectProperties.WAR_EAR_NAME, PropertyUtils.getUsablePropertyName(newName) + ".war"); //NOI18N
172

173                 ProjectWebModule wm = (ProjectWebModule) project.getLookup ().lookup(ProjectWebModule.class);
174                 if (wm != null) //should not be null
175
wm.setContextPath("/" + newName);
176                 
177         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
178             }
179         });
180     }
181     
182     public void notifyMoving() throws IOException JavaDoc {
183         notifyDeleting();
184     }
185     
186     public void notifyMoved(Project original, File JavaDoc originalPath, final String JavaDoc newName) {
187         if (original == null) {
188             project.getAntProjectHelper().notifyDeleted();
189             return ;
190         }
191     
192     final String JavaDoc oldProjectName = project.getName();
193     
194         project.setName(newName);
195         project.getReferenceHelper().fixReferences(originalPath);
196
197         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
198             public void run() {
199         AntProjectHelper helper = project.getAntProjectHelper();
200         EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
201         EditableProperties privateProps = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
202
203         String JavaDoc warName = (String JavaDoc) projectProps.get(WebProjectProperties.WAR_NAME);
204         String JavaDoc warEarName = (String JavaDoc) projectProps.get(WebProjectProperties.WAR_EAR_NAME);
205         String JavaDoc oldName = warName.substring(0, warName.length() - 4);
206         if (warName.endsWith(".war") && oldName.equals(oldProjectName)) //NOI18N
207
projectProps.put(WebProjectProperties.WAR_NAME, newName + ".war"); //NOI18N
208
if (warEarName.endsWith(".war") && oldName.equals(oldProjectName)) //NOI18N
209
projectProps.put(WebProjectProperties.WAR_EAR_NAME, newName + ".war"); //NOI18N
210

211         ProjectWebModule wm = (ProjectWebModule) project.getLookup().lookup(ProjectWebModule.class);
212         String JavaDoc serverId = privateProps.getProperty(WebProjectProperties.J2EE_SERVER_INSTANCE);
213         String JavaDoc oldCP = wm.getContextPath(serverId);
214         if (oldCP != null && oldName.equals(oldCP.substring(1)))
215             wm.setContextPath(serverId, "/" + newName); //NOI18N
216

217         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
218             }
219         });
220     }
221
222 }
223
Popular Tags