KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > AppClientProjectOperations


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.clientproject;
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.j2ee.clientproject.classpath.AppClientProjectClassPathExtender;
31 import org.netbeans.modules.j2ee.clientproject.ui.customizer.AppClientProjectProperties;
32 import org.netbeans.modules.j2ee.dd.api.client.AppClient;
33 import org.netbeans.spi.project.ActionProvider;
34 import org.netbeans.spi.project.CopyOperationImplementation;
35 import org.netbeans.spi.project.DeleteOperationImplementation;
36 import org.netbeans.spi.project.MoveOperationImplementation;
37 import org.netbeans.spi.project.support.ant.AntProjectHelper;
38 import org.netbeans.spi.project.support.ant.EditableProperties;
39 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.netbeans.spi.project.support.ant.PropertyUtils;
42 import org.openide.ErrorManager;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.util.Lookup;
46 import org.openide.util.NbBundle;
47 import org.openide.util.lookup.Lookups;
48
49 /**
50  *
51  * @author Jan Lahoda
52  */

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

77         return files;
78     }
79     
80     public List JavaDoc<FileObject> getDataFiles() {
81         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
82         
83         FileObject metaInf = project.getCarModule().getMetaInf();
84         if (metaInf != null)
85             files.add(metaInf);
86         
87         SourceRoots src = project.getSourceRoots();
88         FileObject[] srcRoots = src.getRoots();
89         
90         for (int cntr = 0; cntr < srcRoots.length; cntr++) {
91             files.add(srcRoots[cntr]);
92         }
93         
94         PropertyEvaluator evaluator = project.evaluator();
95         String JavaDoc prop = evaluator.getProperty(AppClientProjectProperties.SOURCE_ROOT);
96         if (prop != null) {
97             FileObject projectDirectory = project.getProjectDirectory();
98             FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop);
99             if (projectDirectory != srcDir && !files.contains(srcDir))
100                 files.add(srcDir);
101         }
102         
103         SourceRoots test = project.getTestSourceRoots();
104         FileObject[] testRoots = test.getRoots();
105         
106         for (int cntr = 0; cntr < testRoots.length; cntr++) {
107             files.add(testRoots[cntr]);
108         }
109         
110         File JavaDoc resourceDir = project.getCarModule().getEnterpriseResourceDirectory();
111         if (resourceDir != null) {
112             FileObject resourceFO = FileUtil.toFileObject(resourceDir);
113             if (resourceFO != null)
114                 files.add(resourceFO);
115         }
116         
117         return files;
118     }
119     
120     public void notifyDeleting() throws IOException JavaDoc {
121         AppClientActionProvider ap = (AppClientActionProvider) project.getLookup().lookup(AppClientActionProvider.class);
122         
123         assert ap != null;
124         
125         Lookup context = Lookups.fixed(new Object JavaDoc[0]);
126         Properties JavaDoc p = new Properties JavaDoc();
127         String JavaDoc[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p);
128         FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
129         
130         assert targetNames != null;
131         assert targetNames.length > 0;
132         
133         ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
134         
135         AppClientProjectClassPathExtender extender = (AppClientProjectClassPathExtender)project.getLookup().lookup(AppClientProjectClassPathExtender.class);
136         extender.notifyDeleting();
137     }
138     
139     public void notifyDeleted() throws IOException JavaDoc {
140         project.getAntProjectHelper().notifyDeleted();
141     }
142     
143     public void notifyCopying() {
144         //nothing.
145
}
146     
147     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) {
148         if (original == null) {
149             //do nothing for the original project.
150
return ;
151         }
152         
153         project.getReferenceHelper().fixReferences(originalPath);
154         fixOtherReferences(originalPath);
155         
156         project.setName(nueName);
157     }
158     
159     public void notifyMoving() throws IOException JavaDoc {
160         if (!this.project.getUpdateHelper().requestSave()) {
161             throw new IOException JavaDoc(NbBundle.getMessage(AppClientProjectOperations.class,
162                     "MSG_OldProjectMetadata")); // NOI18N
163
}
164         notifyDeleting();
165     }
166     
167     public void notifyMoved(Project original, File JavaDoc originalPath, final String JavaDoc newName) {
168         if (original == null) {
169             project.getAntProjectHelper().notifyDeleted();
170             return ;
171         }
172         
173         final String JavaDoc oldProjectName = project.getName();
174         
175         project.setName(newName);
176         project.getReferenceHelper().fixReferences(originalPath);
177         fixOtherReferences(originalPath);
178         
179         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
180             public void run() {
181                 AntProjectHelper helper = project.getAntProjectHelper();
182                 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
183                 
184                 String JavaDoc jarName = (String JavaDoc) projectProps.get(AppClientProjectProperties.JAR_NAME);
185                 String JavaDoc oldName = jarName.substring(0, jarName.length() - 4);
186                 if (jarName.endsWith(".jar") && oldName.equals(oldProjectName)) //NOI18N
187
projectProps.put(AppClientProjectProperties.JAR_NAME, newName + ".jar"); //NOI18N
188

189                 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
190                 AppClient appClient = Utils.getAppClient(project);
191                 appClient.setDisplayName(newName);
192                 FileObject ddFO = project.getCarModule().getDeploymentDescriptor();
193                 if (ddFO != null) {
194                     try {
195                         appClient.write(ddFO);
196                     } catch (IOException JavaDoc ioe) {
197                         ErrorManager.getDefault().notify(ErrorManager.WARNING, ioe);
198                     }
199                 }
200             }
201         });
202     }
203     
204     private void fixOtherReferences(final File JavaDoc originalPath) {
205         final String JavaDoc property = AppClientProjectProperties.META_INF;
206         final File JavaDoc projectDir = FileUtil.toFile(project.getProjectDirectory());
207         
208         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
209             public void run() {
210                 EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
211                 String JavaDoc path = props.getProperty(property);
212                 if (path == null) {
213                     return;
214                 }
215                 
216                 if (path.startsWith(originalPath.getAbsolutePath())) {
217                     String JavaDoc relative = PropertyUtils.relativizeFile(originalPath, new File JavaDoc(path));
218                     String JavaDoc fixedPath = new File JavaDoc(projectDir, relative).getAbsolutePath();
219                     props.setProperty(property, fixedPath);
220                     project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
221                 }
222             }
223         });
224     }
225 }
226
Popular Tags