KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEProjectOperations


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.java.j2seproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays 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.ProjectInformation;
31 import org.netbeans.api.project.ProjectManager;
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.PropertyUtils;
40 import org.openide.filesystems.FileObject;
41 import org.openide.util.Lookup;
42 import org.openide.util.NbBundle;
43
44 /**
45  *
46  * @author Jan Lahoda
47  */

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

73         return files;
74     }
75     
76     public List JavaDoc<FileObject> getDataFiles() {
77         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
78         files.addAll(Arrays.asList(project.getSourceRoots().getRoots()));
79         files.addAll(Arrays.asList(project.getTestSourceRoots().getRoots()));
80         addFile(project.getProjectDirectory(), "manifest.mf", files); // NOI18N
81
return files;
82     }
83     
84     public void notifyDeleting() throws IOException JavaDoc {
85         J2SEActionProvider ap = project.getLookup().lookup(J2SEActionProvider.class);
86         
87         assert ap != null;
88         
89         Properties JavaDoc p = new Properties JavaDoc();
90         String JavaDoc[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, Lookup.EMPTY, p);
91         FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
92         
93         assert targetNames != null;
94         assert targetNames.length > 0;
95         
96         ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
97     }
98     
99     public void notifyDeleted() throws IOException JavaDoc {
100         project.getAntProjectHelper().notifyDeleted();
101     }
102     
103     public void notifyCopying() {
104         //nothing.
105
}
106     
107     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) {
108         if (original == null) {
109             //do nothing for the original project.
110
return ;
111         }
112         
113         fixDistJarProperty (nueName);
114         project.getReferenceHelper().fixReferences(originalPath);
115         
116         project.setName(nueName);
117     }
118     
119     public void notifyMoving() throws IOException JavaDoc {
120         if (!this.project.getUpdateHelper().requestSave()) {
121             throw new IOException JavaDoc (NbBundle.getMessage(J2SEProjectOperations.class,
122                 "MSG_OldProjectMetadata"));
123         }
124         notifyDeleting();
125     }
126     
127     public void notifyMoved(Project original, File JavaDoc originalPath, String JavaDoc nueName) {
128         if (original == null) {
129             project.getAntProjectHelper().notifyDeleted();
130             return ;
131         }
132         
133         fixDistJarProperty (nueName);
134         project.setName(nueName);
135     project.getReferenceHelper().fixReferences(originalPath);
136     }
137     
138     private static boolean isParent(File JavaDoc folder, File JavaDoc fo) {
139         if (folder.equals(fo))
140             return false;
141         
142         while (fo != null) {
143             if (fo.equals(folder))
144                 return true;
145             
146             fo = fo.getParentFile();
147         }
148         
149         return false;
150     }
151     
152     private void fixDistJarProperty (final String JavaDoc newName) {
153         ProjectManager.mutex().writeAccess(new Runnable JavaDoc () {
154             public void run () {
155                 ProjectInformation pi = (ProjectInformation) project.getLookup().lookup(ProjectInformation.class);
156                 String JavaDoc oldDistJar = pi == null ? null : "${dist.dir}/"+PropertyUtils.getUsablePropertyName(pi.getDisplayName())+".jar"; //NOI18N
157
EditableProperties ep = project.getUpdateHelper().getProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH);
158                 String JavaDoc propValue = ep.getProperty("dist.jar"); //NOI18N
159
if (oldDistJar != null && oldDistJar.equals (propValue)) {
160                     ep.put ("dist.jar","${dist.dir}/"+PropertyUtils.getUsablePropertyName(newName)+".jar"); //NOI18N
161
project.getUpdateHelper().putProperties (AntProjectHelper.PROJECT_PROPERTIES_PATH,ep);
162                 }
163             }
164         });
165     }
166     
167 }
168
Popular Tags