001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ruby.railsprojects;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Arrays;
048: import java.util.LinkedHashSet;
049: import java.util.List;
050: import java.util.Set;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectInformation;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.spi.project.CopyOperationImplementation;
055: import org.netbeans.spi.project.DeleteOperationImplementation;
056: import org.netbeans.spi.project.MoveOperationImplementation;
057: import org.netbeans.modules.ruby.spi.project.support.rake.RakeProjectHelper;
058: import org.netbeans.modules.ruby.spi.project.support.rake.EditableProperties;
059: import org.netbeans.modules.ruby.spi.project.support.rake.PropertyUtils;
060: import org.openide.filesystems.FileObject;
061: import org.openide.util.NbBundle;
062:
063: /**
064: * @author Jan Lahoda
065: */
066: public class RailsProjectOperations implements
067: DeleteOperationImplementation, CopyOperationImplementation,
068: MoveOperationImplementation {
069:
070: private RailsProject project;
071:
072: public RailsProjectOperations(RailsProject project) {
073: this .project = project;
074: }
075:
076: private static void addFile(FileObject projectDirectory,
077: String fileName, Set<FileObject> result) {
078: FileObject file = projectDirectory.getFileObject(fileName);
079: if (file != null) {
080: result.add(file);
081: }
082: }
083:
084: public List<FileObject> getMetadataFiles() {
085: FileObject projectDirectory = project.getProjectDirectory();
086: Set<FileObject> files = new LinkedHashSet<FileObject>();
087:
088: addFile(projectDirectory, "nbproject", files); // NOI18N
089: addFile(projectDirectory, "build.xml", files); // NOI18N
090: addFile(projectDirectory, "xml-resources", files); //NOI18N
091: addFile(projectDirectory, "catalog.xml", files); //NOI18N
092:
093: return new ArrayList<FileObject>(files);
094: }
095:
096: public List<FileObject> getDataFiles() {
097: Set<FileObject> files = new LinkedHashSet<FileObject>();
098: files
099: .addAll(Arrays.asList(project.getSourceRoots()
100: .getRoots()));
101: files.addAll(Arrays.asList(project.getTestSourceRoots()
102: .getRoots()));
103: // additional dirs
104: addFile(project.getProjectDirectory(), "app", files); // NOI18N
105: addFile(project.getProjectDirectory(), "tmp", files); // NOI18N
106: addFile(project.getProjectDirectory(), "test", files); // NOI18N
107: // additional files
108: addFile(project.getProjectDirectory(), "README", files); // NOI18N
109: addFile(project.getProjectDirectory(), "Rakefile", files); // NOI18N
110: return new ArrayList<FileObject>(files);
111: }
112:
113: public void notifyDeleting() throws IOException {
114: project.notifyDeleting();
115: // RailsActionProvider ap = project.getLookup().lookup(RailsActionProvider.class);
116: //
117: // assert ap != null;
118: //
119: // TODO: Clean
120: // Properties p = new Properties();
121: // String[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, Lookup.EMPTY, p);
122: // FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
123: //
124: // assert targetNames != null;
125: // assert targetNames.length > 0;
126: //
127: // ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
128: }
129:
130: public void notifyDeleted() throws IOException {
131: project.getRakeProjectHelper().notifyDeleted();
132: }
133:
134: public void notifyCopying() {
135: //nothing.
136: }
137:
138: public void notifyCopied(Project original, File originalPath,
139: String nueName) {
140: if (original == null) {
141: //do nothing for the original project.
142: return;
143: }
144:
145: fixDistJarProperty(nueName);
146: project.getReferenceHelper().fixReferences(originalPath);
147:
148: project.setName(nueName);
149: }
150:
151: public void notifyMoving() throws IOException {
152: if (!this .project.getUpdateHelper().requestSave()) {
153: throw new IOException(NbBundle.getMessage(
154: RailsProjectOperations.class,
155: "MSG_OldProjectMetadata"));
156: }
157: notifyDeleting();
158: }
159:
160: public void notifyMoved(Project original, File originalPath,
161: String nueName) {
162: if (original == null) {
163: project.getRakeProjectHelper().notifyDeleted();
164: return;
165: }
166:
167: fixDistJarProperty(nueName);
168: project.setName(nueName);
169: project.getReferenceHelper().fixReferences(originalPath);
170: }
171:
172: private static boolean isParent(File folder, File fo) {
173: if (folder.equals(fo))
174: return false;
175:
176: while (fo != null) {
177: if (fo.equals(folder))
178: return true;
179:
180: fo = fo.getParentFile();
181: }
182:
183: return false;
184: }
185:
186: private void fixDistJarProperty(final String newName) {
187: ProjectManager.mutex().writeAccess(new Runnable() {
188: public void run() {
189: ProjectInformation pi = project.getLookup().lookup(
190: ProjectInformation.class);
191: String oldDistJar = pi == null ? null : "${dist.dir}/"
192: + PropertyUtils.getUsablePropertyName(pi
193: .getDisplayName()) + ".jar"; //NOI18N
194: EditableProperties ep = project
195: .getUpdateHelper()
196: .getProperties(
197: RakeProjectHelper.PROJECT_PROPERTIES_PATH);
198: String propValue = ep.getProperty("dist.jar"); //NOI18N
199: if (oldDistJar != null && oldDistJar.equals(propValue)) {
200: ep.put("dist.jar", "${dist.dir}/"
201: + PropertyUtils
202: .getUsablePropertyName(newName)
203: + ".jar"); //NOI18N
204: project.getUpdateHelper().putProperties(
205: RakeProjectHelper.PROJECT_PROPERTIES_PATH,
206: ep);
207: }
208: }
209: });
210: }
211: }
|