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-2006 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.j2ee.earproject;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.List;
049: import java.util.Properties;
050: import org.apache.tools.ant.module.api.support.ActionUtils;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
054: import org.netbeans.spi.project.ActionProvider;
055: import org.netbeans.spi.project.CopyOperationImplementation;
056: import org.netbeans.spi.project.DeleteOperationImplementation;
057: import org.netbeans.spi.project.MoveOperationImplementation;
058: import org.netbeans.spi.project.support.ant.AntProjectHelper;
059: import org.netbeans.spi.project.support.ant.EditableProperties;
060: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.Lookup;
063: import org.openide.util.lookup.Lookups;
064:
065: /**
066: * @author Jan Lahoda
067: */
068: public class EarProjectOperations implements
069: DeleteOperationImplementation, CopyOperationImplementation,
070: MoveOperationImplementation {
071:
072: private final EarProject project;
073:
074: public EarProjectOperations(EarProject project) {
075: this .project = project;
076: }
077:
078: private static void addFile(FileObject projectDirectory,
079: String fileName, List<FileObject> result) {
080: FileObject file = projectDirectory.getFileObject(fileName);
081: if (file != null) {
082: result.add(file);
083: }
084: }
085:
086: public List<FileObject> getMetadataFiles() {
087: FileObject projectDirectory = project.getProjectDirectory();
088: List<FileObject> files = new ArrayList<FileObject>();
089:
090: addFile(projectDirectory, "nbproject", files); // NOI18N
091: addFile(projectDirectory, "build.xml", files); // NOI18N
092: addFile(projectDirectory, "src", files); // NOI18N
093: addFile(projectDirectory, ".cvsignore", files); // NOI18N
094:
095: return files;
096: }
097:
098: public List<FileObject> getDataFiles() {
099: return Collections.emptyList();
100: }
101:
102: public void notifyDeleting() throws IOException {
103: EarActionProvider ap = project.getLookup().lookup(
104: EarActionProvider.class);
105:
106: assert ap != null;
107:
108: Lookup context = Lookups.fixed(new Object[0]);
109: Properties p = new Properties();
110: String[] targetNames = ap.getTargetNames(
111: ActionProvider.COMMAND_CLEAN, context, p);
112: FileObject buildXML = project.getProjectDirectory()
113: .getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
114:
115: assert targetNames != null;
116: assert targetNames.length > 0;
117:
118: ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
119: }
120:
121: public void notifyDeleted() throws IOException {
122: project.getAntProjectHelper().notifyDeleted();
123: }
124:
125: public void notifyCopying() {
126: //nothing.
127: }
128:
129: public void notifyCopied(Project original, File originalPath,
130: String nueName) {
131: if (original == null) {
132: //do nothing for the original project.
133: return;
134: }
135:
136: project.setName(nueName);
137: }
138:
139: public void notifyMoving() throws IOException {
140: notifyDeleting();
141: }
142:
143: public void notifyMoved(Project original, File originalPath,
144: final String newName) {
145: if (original == null) {
146: project.getAntProjectHelper().notifyDeleted();
147: return;
148: }
149:
150: final String oldProjectName = project.getName();
151:
152: project.setName(newName);
153:
154: ProjectManager.mutex().writeAccess(new Runnable() {
155: public void run() {
156: AntProjectHelper helper = project.getAntProjectHelper();
157: EditableProperties projectProps = helper
158: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
159:
160: String earName = projectProps
161: .get(EarProjectProperties.JAR_NAME);
162: String oldName = earName.substring(0,
163: earName.length() - 4);
164: if (earName.endsWith(".ear")
165: && oldName.equals(oldProjectName)) { //NOI18N
166: projectProps.put(EarProjectProperties.JAR_NAME,
167: newName + ".ear"); //NOI18N
168: }
169:
170: helper.putProperties(
171: AntProjectHelper.PROJECT_PROPERTIES_PATH,
172: projectProps);
173: }
174: });
175:
176: }
177:
178: }
|