KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 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.Collections JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import org.netbeans.api.java.project.JavaProjectConstants;
29 import org.netbeans.api.project.ProjectUtils;
30 import org.netbeans.api.project.Sources;
31 import org.netbeans.modules.web.api.webmodule.WebModule;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataObject;
36 import org.openide.loaders.TemplateWizard;
37 import org.openide.util.NbBundle;
38 import org.openide.filesystems.FileObject;
39
40 import org.netbeans.api.project.Project;
41
42 import org.netbeans.spi.project.ui.templates.support.Templates;
43 import org.netbeans.api.project.SourceGroup;
44 import org.netbeans.modules.web.core.Util;
45 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
46
47 /** A template wizard iterator for new servlets, filters and
48  * listeners.
49  *
50  * @author radim.kubacki@sun.com
51  * @author ana.von.klopp@sun.com
52  * @author milan.kuchtiak@sun.com
53  */

54
55 public class ServletIterator implements TemplateWizard.Iterator {
56     
57     private static final long serialVersionUID = -4147344271705652643L;
58
59     private static final boolean debug = false;
60
61     private transient FileType fileType;
62     private transient Evaluator evaluator = null;
63     private transient DeployData deployData = null;
64
65     private transient int index;
66     private transient WizardDescriptor.Panel[] panels;
67     private transient TemplateWizard wizard;
68     private transient ServletPanel servletPanel;
69     private transient WizardDescriptor.Panel customPanel;
70
71     private ServletIterator(FileType fileType) {
72     this.fileType = fileType;
73     }
74
75     public static ServletIterator createServletIterator() {
76     return new ServletIterator(FileType.SERVLET);
77     }
78
79     public static ServletIterator createFilterIterator() {
80     return new ServletIterator(FileType.FILTER);
81     }
82
83     public void initialize (TemplateWizard wizard) {
84         
85         this.wizard = wizard;
86         index = 0;
87
88     if(fileType.equals(FileType.SERVLET) ||
89        fileType.equals(FileType.FILTER)) {
90         deployData = new ServletData(fileType);
91         evaluator = new TargetEvaluator(fileType, deployData);
92     }
93             Project project = Templates.getProject( wizard );
94             DataFolder targetFolder=null;
95             try {
96                 targetFolder = wizard.getTargetFolder();
97             } catch (IOException JavaDoc ex) {
98                 targetFolder = DataFolder.findFolder(project.getProjectDirectory());
99             }
100         evaluator.setInitialFolder(targetFolder,project);
101         
102     if(fileType == FileType.SERVLET) {
103         panels = new WizardDescriptor.Panel[] {
104                 //wizard.targetChooser (),
105
new FinishableProxyWizardPanel(createPackageChooserPanel(wizard,null)),
106         ServletPanel.createServletPanel((TargetEvaluator)evaluator, wizard)
107         };
108     }
109     else if(fileType == FileType.FILTER) {
110             customPanel = new WrapperSelection(wizard);
111         panels = new WizardDescriptor.Panel[] {
112                 //wizard.targetChooser (),
113
createPackageChooserPanel(wizard,customPanel),
114         servletPanel=ServletPanel.createServletPanel((TargetEvaluator)evaluator, wizard),
115         ServletPanel.createFilterPanel((TargetEvaluator)evaluator, wizard)
116         };
117     }
118         
119         // Creating steps.
120
Object JavaDoc prop = wizard.getProperty ("WizardPanel_contentData"); // NOI18N
121
String JavaDoc[] beforeSteps = null;
122         if (prop != null && prop instanceof String JavaDoc[]) {
123             beforeSteps = (String JavaDoc[])prop;
124         }
125         String JavaDoc[] steps = Utilities.createSteps (beforeSteps, panels);
126         
127         for (int i = 0; i < panels.length; i++) {
128             JComponent JavaDoc jc = (JComponent JavaDoc)panels[i].getComponent ();
129             if (steps[i] == null) {
130                 // Default step name to component name of panel.
131
// Mainly useful for getting the name of the target
132
// chooser to appear in the list of steps.
133
steps[i] = jc.getName ();
134             }
135         jc.putClientProperty ("WizardPanel_contentSelectedIndex", // NOI18N
136
new Integer JavaDoc (i));
137         jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
138
}
139     }
140
141     private WizardDescriptor.Panel createPackageChooserPanel(TemplateWizard wizard, WizardDescriptor.Panel customPanel) {
142         Project project = Templates.getProject(wizard);
143         SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project);
144         if (customPanel==null) {
145             if (sourceGroups.length == 0)
146                 return Templates.createSimpleTargetChooser(project, sourceGroups);
147             else
148                 return JavaTemplates.createPackageChooser(project, sourceGroups);
149         } else {
150             if (sourceGroups.length == 0)
151                 return Templates.createSimpleTargetChooser(project, sourceGroups, customPanel);
152             else
153                 return JavaTemplates.createPackageChooser(project, sourceGroups, customPanel);
154         }
155     }
156         
157     public Set JavaDoc instantiate(TemplateWizard wizard) throws IOException JavaDoc {
158
159     if(debug) log("::instantiate()");
160     // Create the target folder. The next piece is independent of
161
// the type of file we create, and it should be moved to the
162
// evaluator class instead. The exact same process
163
// should be used when checking if the directory is valid from
164
// the wizard itself.
165

166     // ------------------------- FROM HERE -------------------------
167

168         FileObject dir = Templates.getTargetFolder( wizard );
169         DataFolder df = DataFolder.findFolder( dir );
170
171         FileObject template = Templates.getTemplate( wizard );
172         if (FileType.FILTER.equals(fileType) && ((WrapperSelection)customPanel).isWrapper()) {
173             template = Templates.getTemplate( wizard );
174             FileObject templateParent = template.getParent();
175             template = templateParent.getFileObject("AdvancedFilter","java"); //NOI18N
176
}
177         
178         DataObject dTemplate = DataObject.find( template );
179         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wizard ) );
180
181     if(debug) log("\topened file"); //NOI18N
182

183     // If the user does not want to add the file to the
184
// deployment descriptor, return
185
if(!deployData.makeEntry()) {
186         return Collections.singleton(dobj);
187     }
188
189     TargetEvaluator te = (TargetEvaluator)evaluator;
190
191     if(debug) log("\tcreate dd entries"); //NOI18N
192

193         // needed to be able to finish ServletWizard from the second panel
194
if (deployData.getClassName().length()==0) {
195             String JavaDoc targetName = wizard.getTargetName();
196             FileObject targetFolder = Templates.getTargetFolder(wizard);
197             String JavaDoc packageName = null;
198             Project project = Templates.getProject( wizard );
199             Sources sources = ProjectUtils.getSources(project);
200             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
201             for (int i = 0; i < groups.length && packageName == null; i++) {
202                 if (WebModule.getWebModule (groups [i].getRootFolder ()) != null) {
203                     packageName = FileUtil.getRelativePath (groups [i].getRootFolder (), targetFolder);
204                 }
205             }
206             if (packageName!=null)
207                 packageName = packageName.replace('/','.');
208             else packageName="";
209             // compute (and set) the servlet-class
210
deployData.setClassName(packageName.length()==0?targetName:packageName+"."+targetName);
211             // compute (and set) the servlet-name and url-pattern
212
String JavaDoc servletName = ((ServletData)deployData).createDDServletName(targetName);
213             ((ServletData)deployData).createDDServletMapping(servletName);
214         }
215         deployData.createDDEntries();
216
217     if(debug) log("\tURI param"); //NOI18N
218
return Collections.singleton(dobj);
219     }
220
221     private void log(String JavaDoc s) {
222     System.out.println("ServletIterator" + s);
223     }
224     
225
226     public void uninitialize (TemplateWizard wizard) {
227         this.wizard = null;
228         panels = null;
229     }
230
231     // --- WizardDescriptor.Iterator METHODS: ---
232

233     public String JavaDoc name () {
234         return NbBundle.getMessage (ServletIterator.class, "TITLE_x_of_y",
235         new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
236     }
237     
238     // If the user has elected to place the file in a regular
239
// directory (not a web module) then we don't show the DD info
240
// panel.
241
public boolean hasNext () {
242     if(debug) log("::hasNext()"); //NOI18N
243
if(debug) log("\tindex is " + index); //NOI18N
244
return index < panels.length - 1 && deployData.hasDD();
245     }
246     
247     public boolean hasPrevious () {
248         return index > 0;
249     }
250     public void nextPanel () {
251         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
252         index++;
253     }
254     public void previousPanel () {
255         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
256         index--;
257     }
258     public WizardDescriptor.Panel current () {
259         return panels[index];
260     }
261     
262     // PENDING - Ann suggests updating the available panels based on
263
// changes. Here is what should happen:
264
// 1. If target is directory, disable DD panels
265
// 2. If target is web module but the user does not want to make a
266
// DD entry, disable second DD panel for Filters.
267

268     // If nothing unusual changes in the middle of the wizard, simply:
269
public final void addChangeListener (ChangeListener JavaDoc l) {}
270     public final void removeChangeListener (ChangeListener JavaDoc l) {}
271     // If something changes dynamically (besides moving between panels),
272
// e.g. the number of panels changes in response to user input, then
273
// uncomment the following and call when needed:
274
// fireChangeEvent ();
275
/*
276     private transient Set listeners = new HashSet (1); // Set<ChangeListener>
277     public final void addChangeListener (ChangeListener l) {
278         synchronized (listeners) {
279             listeners.add (l);
280         }
281     }
282     public final void removeChangeListener (ChangeListener l) {
283         synchronized (listeners) {
284             listeners.remove (l);
285         }
286     }
287     protected final void fireChangeEvent () {
288         Iterator it;
289         synchronized (listeners) {
290             it = new HashSet (listeners).iterator ();
291         }
292         ChangeEvent ev = new ChangeEvent (this);
293         while (it.hasNext ()) {
294             ((ChangeListener) it.next ()).stateChanged (ev);
295         }
296     }
297     private void readObject (ObjectInputStream in) throws IOException, ClassNotFoundException {
298         in.defaultReadObject ();
299         listeners = new HashSet (1);
300     }
301      */

302
303 }
304
Popular Tags