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.ant.freeform;
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.Iterator;
049: import java.util.List;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.api.project.ProjectManager;
052: import org.netbeans.modules.ant.freeform.spi.support.Util;
053: import org.netbeans.spi.project.CopyOperationImplementation;
054: import org.netbeans.spi.project.DeleteOperationImplementation;
055: import org.netbeans.spi.project.MoveOperationImplementation;
056: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
057: import org.openide.ErrorManager;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.w3c.dom.Element;
061:
062: /**
063: *
064: * @author Jan Lahoda
065: */
066: public class FreeformProjectOperations implements
067: DeleteOperationImplementation, CopyOperationImplementation,
068: MoveOperationImplementation {
069:
070: private FreeformProject project;
071:
072: public FreeformProjectOperations(FreeformProject project) {
073: this .project = project;
074: }
075:
076: private static void addFile(FileObject projectDirectory,
077: String fileName, List<FileObject> result) {
078: FileObject file = projectDirectory.getFileObject(fileName);
079:
080: if (file != null) {
081: result.add(file);
082: }
083: }
084:
085: public List<FileObject> getMetadataFiles() {
086: FileObject projectDirectory = project.getProjectDirectory();
087: List<FileObject> files = new ArrayList<FileObject>();
088:
089: addFile(projectDirectory, "nbproject", files); // NOI18N
090:
091: return files;
092: }
093:
094: public List<FileObject> getDataFiles() {
095: Element genldata = project.getPrimaryConfigurationData();
096: Element foldersEl = Util.findElement(genldata, "folders",
097: FreeformProjectType.NS_GENERAL); // NOI18N
098: List<Element> folders = foldersEl != null ? Util
099: .findSubElements(foldersEl) : Collections
100: .<Element> emptyList();
101: List<FileObject> result = new ArrayList<FileObject>();
102:
103: for (Element el : folders) {
104: if ("source-folder".equals(el.getLocalName())
105: && FreeformProjectType.NS_GENERAL.equals(el
106: .getNamespaceURI())) { // NOI18N
107: addFile(el, result);
108: }
109: }
110:
111: addFile(project.getProjectDirectory(), "build.xml", result); // NOI18N
112:
113: return result;
114: }
115:
116: private void addFile(Element folder, List<FileObject> result) {
117: Element location = Util.findElement(folder, "location",
118: FreeformProjectType.NS_GENERAL); // NOI18N
119:
120: if (location == null) {
121: return;
122: }
123:
124: PropertyEvaluator evaluator = project.evaluator();
125: String val = evaluator.evaluate(Util.findText(location));
126:
127: if (val == null) {
128: return;
129: }
130:
131: File f = project.helper().resolveFile(val);
132:
133: if (f == null) {
134: return;
135: }
136:
137: FileObject fo = FileUtil.toFileObject(f);
138:
139: if (fo != null
140: && FileUtil.isParentOf(project.getProjectDirectory(),
141: fo)) {
142: result.add(fo);
143: }
144: }
145:
146: public void notifyDeleting() throws IOException {
147: //TODO: invoke clean action if bound.
148: }
149:
150: public void notifyDeleted() throws IOException {
151: project.helper().notifyDeleted();
152: }
153:
154: public void notifyCopying() throws IOException {
155: }
156:
157: public void notifyCopied(Project original, File originalPath,
158: String nueName) throws IOException {
159: if (original != null) {
160: project.setName(nueName);
161: }
162: }
163:
164: public void notifyMoving() throws IOException {
165: }
166:
167: public void notifyMoved(Project original, File originalPath,
168: String nueName) throws IOException {
169: if (original != null) {
170: project.setName(nueName);
171: } else {
172: project.helper().notifyDeleted();
173: }
174: }
175:
176: }
|