KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformProjectOperations


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.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.modules.ant.freeform.spi.support.Util;
31 import org.netbeans.spi.project.CopyOperationImplementation;
32 import org.netbeans.spi.project.DeleteOperationImplementation;
33 import org.netbeans.spi.project.MoveOperationImplementation;
34 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.w3c.dom.Element JavaDoc;
39
40 /**
41  *
42  * @author Jan Lahoda
43  */

44 public class FreeformProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
45     
46     private FreeformProject project;
47     
48     public FreeformProjectOperations(FreeformProject project) {
49         this.project = project;
50     }
51     
52     private static void addFile(FileObject projectDirectory, String JavaDoc fileName, List JavaDoc<FileObject> result) {
53         FileObject file = projectDirectory.getFileObject(fileName);
54         
55         if (file != null) {
56             result.add(file);
57         }
58     }
59     
60     public List JavaDoc<FileObject> getMetadataFiles() {
61         FileObject projectDirectory = project.getProjectDirectory();
62         List JavaDoc<FileObject> files = new ArrayList JavaDoc<FileObject>();
63         
64         addFile(projectDirectory, "nbproject", files); // NOI18N
65

66         return files;
67     }
68     
69     public List JavaDoc<FileObject> getDataFiles() {
70         Element JavaDoc genldata = project.getPrimaryConfigurationData();
71         Element JavaDoc foldersEl = Util.findElement(genldata, "folders", FreeformProjectType.NS_GENERAL); // NOI18N
72
List JavaDoc<Element JavaDoc> folders = foldersEl != null ? Util.findSubElements(foldersEl) : Collections.<Element JavaDoc>emptyList();
73         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
74
75         for (Element JavaDoc el : folders) {
76             if ("source-folder".equals(el.getLocalName()) && FreeformProjectType.NS_GENERAL.equals(el.getNamespaceURI())) { // NOI18N
77
addFile(el, result);
78             }
79         }
80         
81         addFile(project.getProjectDirectory(), "build.xml", result); // NOI18N
82

83         return result;
84     }
85     
86     private void addFile(Element JavaDoc folder, List JavaDoc<FileObject> result) {
87         Element JavaDoc location = Util.findElement(folder, "location", FreeformProjectType.NS_GENERAL); // NOI18N
88

89         if (location == null) {
90             return ;
91         }
92         
93         PropertyEvaluator evaluator = project.evaluator();
94         String JavaDoc val = evaluator.evaluate(Util.findText(location));
95         
96         if (val == null) {
97             return ;
98         }
99         
100         File JavaDoc f = project.helper().resolveFile(val);
101             
102         if (f == null) {
103             return ;
104         }
105         
106         FileObject fo = FileUtil.toFileObject(f);
107         
108         if (fo != null && FileUtil.isParentOf(project.getProjectDirectory(), fo)) {
109             result.add(fo);
110         }
111     }
112     
113     public void notifyDeleting() throws IOException JavaDoc {
114         //TODO: invoke clean action if bound.
115
}
116     
117     public void notifyDeleted() throws IOException JavaDoc {
118         project.helper().notifyDeleted();
119     }
120
121     public void notifyCopying() throws IOException JavaDoc {
122     }
123
124     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) throws IOException JavaDoc {
125         if (original != null) {
126             project.setName(nueName);
127         }
128     }
129
130     public void notifyMoving() throws IOException JavaDoc {
131     }
132
133     public void notifyMoved(Project original, File JavaDoc originalPath, String JavaDoc nueName) throws IOException JavaDoc {
134         if (original != null) {
135             project.setName(nueName);
136         } else {
137             project.helper().notifyDeleted();
138         }
139     }
140     
141 }
142
Popular Tags