KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > ModuleOperations


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.apisupport.project.ui;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.apache.tools.ant.module.api.support.ActionUtils;
29 import org.netbeans.api.java.project.JavaProjectConstants;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectUtils;
32 import org.netbeans.api.project.SourceGroup;
33 import org.netbeans.api.project.Sources;
34 import org.netbeans.modules.apisupport.project.NbModuleProject;
35 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
36 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
37 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
38 import org.netbeans.spi.project.ActionProvider;
39 import org.netbeans.spi.project.CopyOperationImplementation;
40 import org.netbeans.spi.project.DeleteOperationImplementation;
41 import org.netbeans.spi.project.MoveOperationImplementation;
42 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
43 import org.openide.DialogDisplayer;
44 import org.openide.NotifyDescriptor;
45 import org.openide.filesystems.FileObject;
46 import org.openide.util.NbBundle;
47
48 /**
49  * @author Martin Krauskopf
50  */

51 public final class ModuleOperations implements DeleteOperationImplementation,
52         MoveOperationImplementation, CopyOperationImplementation {
53     
54     private static final Map JavaDoc<String JavaDoc, SuiteProject> TEMPORARY_CACHE = new HashMap JavaDoc();
55     
56     private final NbModuleProject project;
57     private final FileObject projectDir;
58     
59     public ModuleOperations(final NbModuleProject project) {
60         this.project = project;
61         this.projectDir = project.getProjectDirectory();
62     }
63     
64     public void notifyDeleting() throws IOException JavaDoc {
65         notifyDeleting(false);
66     }
67     
68     private void notifyDeleting(boolean temporary) throws IOException JavaDoc {
69         FileObject buildXML = projectDir.getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
70         ActionUtils.runTarget(buildXML, new String JavaDoc[] { ActionProvider.COMMAND_CLEAN }, null).waitFinished();
71         
72         SuiteProject suite = SuiteUtils.findSuite(project);
73         if (suite != null) {
74             if (temporary) {
75                 SuiteUtils.removeModuleFromSuite(project);
76             } else {
77                 // XXX we should ask the user in the same way as when we removing the
78
// module in suite logical view. But it is not possible with the
79
// current Project API. (maybe by some wrapper in the ModuleActions)
80
SuiteUtils.removeModuleFromSuiteWithDependencies(project);
81             }
82         }
83         
84         project.notifyDeleting();
85     }
86     
87     public void notifyDeleted() throws IOException JavaDoc {
88         project.getHelper().notifyDeleted();
89     }
90     
91     public void notifyMoving() throws IOException JavaDoc {
92         SuiteProject suite = SuiteUtils.findSuite(project);
93         if (suite != null) {
94             TEMPORARY_CACHE.put(project.getCodeNameBase(), suite);
95         }
96         notifyDeleting(true);
97     }
98     
99     public void notifyMoved(Project original, File JavaDoc originalPath, String JavaDoc nueName) throws IOException JavaDoc {
100         if (original == null) { // called on the original project
101
project.getHelper().notifyDeleted();
102         } else { // called on the new project
103
SuiteProject suite = (SuiteProject) TEMPORARY_CACHE.remove(project.getCodeNameBase());
104             if (suite != null) {
105                 SuiteUtils.addModule(suite, project);
106             }
107             boolean isRename = original.getProjectDirectory().getParent().equals(
108                     project.getProjectDirectory().getParent());
109             if (isRename) {
110                 setDisplayName(nueName);
111             }
112         }
113     }
114     
115     public void notifyCopying() throws IOException JavaDoc {
116         SuiteProject suite = SuiteUtils.findSuite(project);
117         if (suite != null) {
118             // Let's remove the project from its suite. Since we want to be a
119
// copy a standalone module for now. And since we cannot control
120
// the phase between a new project is physically copied and when it
121
// is opened we have to use this workaround.
122
TEMPORARY_CACHE.put(project.getCodeNameBase(), suite);
123             SuiteUtils.removeModuleFromSuite(project);
124         }
125     }
126     
127     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) throws IOException JavaDoc {
128         if (original == null) { // called on the original project
129
SuiteProject suite = (SuiteProject) TEMPORARY_CACHE.remove(project.getCodeNameBase());
130             if (suite != null) {
131                 // Let's readd the original suite component to its suite. Look
132
// into notifyCopying() commens for more details.
133
SuiteUtils.addModule(suite, (NbModuleProject) project);
134             }
135         } else {
136             // Adjust display name so the copy can be recognized from the original.
137
adjustDisplayName();
138         }
139     }
140     
141     public List JavaDoc<FileObject> getMetadataFiles() {
142         List JavaDoc<FileObject> files = new ArrayList JavaDoc();
143         addFile(GeneratedFilesHelper.BUILD_XML_PATH, files);
144         addFile("manifest.mf", files); // NOI18N
145
addFile("nbproject", files); // NOI18N
146
addFile(".cvsignore", files); // NOI18N
147
return files;
148     }
149     
150     public List JavaDoc<FileObject> getDataFiles() {
151         List JavaDoc<FileObject> files = new ArrayList JavaDoc();
152         
153         Sources srcs = ProjectUtils.getSources(project);
154         SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
155         for (int i = 0; i < grps.length; i++) {
156             FileObject srcRoot = grps[i].getRootFolder();
157             if (srcRoot.getPath().endsWith("test/unit/src")) { // NOI18N
158
addFile("test", files); // NOI18N
159
} else {
160                 files.add(srcRoot);
161             }
162         }
163         
164         return files;
165     }
166     
167     private void addFile(String JavaDoc fileName, List JavaDoc<FileObject> result) {
168         FileObject file = projectDir.getFileObject(fileName);
169         if (file != null) {
170             result.add(file);
171         }
172     }
173     
174     private void adjustDisplayName() throws IOException JavaDoc {
175         // XXX what if the user makes two copies from one module?
176
setDisplayName(ProjectUtils.getInformation(project).getDisplayName() + " (0)"); // NOI18N
177
}
178     
179     private void setDisplayName(String JavaDoc nueName) throws IOException JavaDoc {
180         LocalizedBundleInfo.Provider lbiProvider =
181                 (LocalizedBundleInfo.Provider) project.getLookup().lookup(LocalizedBundleInfo.Provider.class);
182         if (lbiProvider != null) {
183             LocalizedBundleInfo info = lbiProvider.getLocalizedBundleInfo();
184             if (info != null) {
185                 // XXX what if the user makes two copies from one module?
186
info.setDisplayName(nueName); // NOI18N
187
info.store();
188             }
189         }
190     }
191     
192     static boolean canRun(final NbModuleProject project, final boolean emitWarningToUser) {
193         boolean result = true;
194         String JavaDoc testUserDir = project.evaluator().getProperty("test.user.dir"); // NOI18N
195
FileObject testUserDirFO = project.getHelper().resolveFileObject(testUserDir);
196         if (testUserDirFO != null && testUserDirFO.isFolder()) {
197             FileObject lock = testUserDirFO.getFileObject("lock"); // NOI18N
198
if (lock != null && lock.isData()) {
199                 if (emitWarningToUser) {
200                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
201                             NbBundle.getMessage(ModuleOperations.class, "ERR_ModuleIsBeingRun")));
202                 }
203                 result = false;
204             }
205         }
206         return result;
207     }
208     
209 }
210
Popular Tags