KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > RailsProjectOperations


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.ruby.railsprojects;
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.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectInformation;
30 import org.netbeans.api.project.ProjectManager;
31 import org.netbeans.spi.project.ActionProvider;
32 import org.netbeans.spi.project.CopyOperationImplementation;
33 import org.netbeans.spi.project.DeleteOperationImplementation;
34 import org.netbeans.spi.project.MoveOperationImplementation;
35 import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
36 import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties;
37 import org.netbeans.modules.ruby.spi.project.support.rake.GeneratedFilesHelper;
38 import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
39 import org.openide.filesystems.FileObject;
40 import org.openide.util.Lookup;
41 import org.openide.util.NbBundle;
42
43 /**
44  *
45  * @author Jan Lahoda
46  */

47 public class RailsProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
48     private RailsProject project;
49     
50     public RailsProjectOperations(RailsProject 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         
57         if (file != null) {
58             result.add(file);
59         }
60     }
61     
62     public List JavaDoc<FileObject> getMetadataFiles() {
63         FileObject projectDirectory = project.getProjectDirectory();
64         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
65         
66         addFile(projectDirectory, "nbproject", files); // NOI18N
67
addFile(projectDirectory, "build.xml", files); // NOI18N
68
addFile(projectDirectory, "xml-resources", files); //NOI18N
69
addFile(projectDirectory, "catalog.xml", files); //NOI18N
70

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