KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > wizards > TargetEvaluator


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.web.wizards;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileStateInvalidException;
29 import org.openide.loaders.DataFolder;
30 import org.openide.util.NbBundle;
31 import org.openide.util.Utilities;
32 import org.netbeans.modules.web.api.webmodule.WebModule;
33 import org.netbeans.api.java.classpath.ClassPath;
34 import org.netbeans.api.java.project.JavaProjectConstants;
35 import org.netbeans.api.project.FileOwnerQuery;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectUtils;
38 import org.netbeans.api.project.SourceGroup;
39 import org.netbeans.api.project.Sources;
40
41 class TargetEvaluator extends Evaluator {
42
43     private final boolean debug = false;
44
45     private ArrayList JavaDoc pathItems = null;
46     private DeployData deployData = null;
47     private String JavaDoc errorMessage = null;
48     private String JavaDoc fileName;
49     private boolean initialized = false;
50     private String JavaDoc className;
51     
52     TargetEvaluator(FileType fileType, DeployData deployData) {
53     super(fileType);
54     if(debug) {
55         log("::CONSTRUCTOR");
56         log("file type is " + getFileType().toString());
57     }
58     this.deployData = deployData;
59     }
60
61     String JavaDoc getErrorMessage() {
62     if(errorMessage == null) return "";
63     else return errorMessage;
64     }
65
66     /**
67      * Used to get the deploy data object
68      */

69     DeployData getDeployData() {
70     return deployData;
71     }
72
73     /**
74      * Used by the various wizard panels to display the classname of
75      * the target
76      */

77     String JavaDoc getClassName() {
78         return className;
79     /*
80     if(pathItems == null || pathItems.isEmpty()) return "";
81     else {
82         StringBuffer buf = new StringBuffer();
83         Iterator iterator = pathItems.iterator();
84         while(iterator.hasNext()) {
85         buf.append((String)(iterator.next()));
86         if(iterator.hasNext())
87             buf.append("."); //NOI18N
88         
89         }
90         return buf.toString();
91     }
92         */

93     }
94
95     /**
96      * Used by the various wizard panels to display the classname of
97      * the target
98      */

99     
100     //void setClassName(String fileName, FileObject targetFolder) {
101
void setClassName(String JavaDoc fileName, String JavaDoc targetFolder) {
102         if (targetFolder.length()>0)
103             className=targetFolder+"."+fileName;
104         else className=fileName;
105         this.fileName=fileName;
106         /*
107     if(debug) log("::setClassName(" + fileName + ")"); //NOI18N
108         if (!initialized) {
109             initialized=true;
110         } else {
111             pathItems.remove (pathItems.size()-1);
112         }
113         pathItems.add(fileName);
114         
115         try {
116             checkFile(pathItems.iterator(),targetFolder);
117             this.fileName=fileName;
118             if(debug)
119                 log("\tNumber of path items: " + pathItems.size()); //NOI18N
120             return;
121         } catch (IOException ex) {}
122         
123         setAlternativeName(fileName,targetFolder);
124     if(debug)
125         log("\tNumber of path items: " + pathItems.size()); //NOI18N
126         */

127     }
128     
129     /**
130      * Used by the DD info panels to generate default names
131      */

132     String JavaDoc getFileName() {
133         return fileName;
134     }
135
136     /**
137      * Used by the servlet wizard when creating the files
138      */

139     Iterator JavaDoc getPathItems() {
140         if(debug) log("::getPathItems()"+pathItems.size()); //NOI18N;
141
return pathItems.iterator();
142     }
143     
144     String JavaDoc getTargetPath() {
145     return super.getTargetPath(pathItems.iterator());
146     }
147     
148     /**
149      * Used by the ObjectNameWizard panel to set the target folder
150      * gotten from the system wizard initially.
151      */

152     
153     void setInitialFolder(DataFolder selectedFolder, Project p) {
154     if(selectedFolder == null) {
155         if(debug) log("\t" + "No target folder!"); //NOI18N
156
return;
157     }
158         FileObject targetFolder = selectedFolder.getPrimaryFile();
159         Sources sources = ProjectUtils.getSources(p);
160         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
161         String JavaDoc packageName = null;
162         for (int i = 0; i < groups.length && packageName == null; i++) {
163             packageName = org.openide.filesystems.FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder);
164             deployData.setWebApp(DeployData.getWebAppFor(groups [i].getRootFolder ()));
165         }
166         if (packageName==null) packageName="";
167         setInitialPath(packageName);
168     }
169     
170     /**
171      * Used by the system wizard to check whether the input so far is valid
172      */

173     boolean isValid() {
174         return true;
175     }
176     
177     
178
179     /**
180      * Calculates the package name for a new Servlet/Filter/Listener
181      * based on the path to the file system relative to the target
182      * directory. If the user selected a directory from the web module
183      * file system under WEB-INF/classes, then we strip off the
184      * WEB-INF/classes portion from the path name.
185      */

186     
187     private void setInitialPath(String JavaDoc dirPath) {
188
189     if(debug) log("::setInitialPath()");
190         
191     pathItems = new ArrayList JavaDoc();
192         
193     String JavaDoc path[] = dirPath.split("/"); //NOI18N
194
if(path.length > 0) {
195         for(int i=0; i<path.length; ++i) {
196         if(!path[i].equals("")) {
197             pathItems.add(path[i]);
198         }
199         }
200     }
201         if(debug) log("::setInitialPath():pathItems.size() "+pathItems.size());
202     }
203
204     private static void log(String JavaDoc s) {
205     System.out.println("TargetEvaluator" + s);
206     }
207     
208     private void setAlternativeName (String JavaDoc fileName, FileObject targetFolder) {
209     int index = 0;
210     String JavaDoc tempName = fileName;
211     boolean pathOK = false;
212         while(!pathOK) {
213         pathItems.remove(tempName);
214         tempName = fileName.concat("_").concat(String.valueOf(++index));
215         pathItems.add(tempName);
216         try {
217         checkFile(pathItems.iterator(),targetFolder);
218         pathOK = true;
219                 this.fileName=tempName;
220         }
221         catch(IOException JavaDoc ioex) {
222                 pathOK = true;
223             }
224         }
225     }
226
227 }
228
Popular Tags