KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.NoSuchElementException JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.JComponent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.modules.web.core.Util;
32
33 import org.openide.ErrorManager;
34 import org.openide.NotifyDescriptor;
35 import org.openide.filesystems.FileObject;
36 import org.openide.WizardDescriptor;
37 import org.openide.loaders.*;
38 import org.openide.util.NbBundle;
39
40 import org.netbeans.api.java.classpath.ClassPath;
41 import org.netbeans.modules.j2ee.dd.api.web.*;
42 import org.openide.DialogDisplayer;
43
44 import org.netbeans.spi.project.ui.templates.support.Templates;
45 import org.netbeans.api.project.Project;
46 import org.netbeans.api.project.SourceGroup;
47 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
48
49 /** A template wizard iterator (sequence of panels).
50  * Used to fill in the second and subsequent panels in the New wizard.
51  * Associate this to a template inside a layer using the
52  * Sequence of Panels extra property.
53  * Create one or more panels from template as needed too.
54  *
55  * @author mk115033
56  */

57 public class ListenerIterator implements TemplateWizard.Iterator {
58
59     // CHANGEME vvv
60
//private static final long serialVersionUID = ...L;
61

62     // You should define what panels you want to use here:
63
private ListenerPanel panel;
64     protected WizardDescriptor.Panel[] createPanels (TemplateWizard wizard) {
65         Project project = Templates.getProject( wiz );
66         SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project);
67         panel = new ListenerPanel(wizard);
68         
69         WizardDescriptor.Panel packageChooserPanel;
70         if (sourceGroups.length == 0)
71             packageChooserPanel = Templates.createSimpleTargetChooser(project, sourceGroups, panel);
72         else
73             packageChooserPanel = JavaTemplates.createPackageChooser(project, sourceGroups, panel);
74
75         return new WizardDescriptor.Panel[] {
76             // Assuming you want to keep the default 2nd panel:
77
packageChooserPanel
78         };
79     }
80
81     public Set JavaDoc instantiate (TemplateWizard wiz) throws IOException JavaDoc/*, IllegalStateException*/ {
82         // Here is the default plain behavior. Simply takes the selected
83
// template (you need to have included the standard second panel
84
// in createPanels(), or at least set the properties targetName and
85
// targetFolder correctly), instantiates it in the provided
86
// position, and returns the result.
87
// More advanced wizards can create multiple objects from template
88
// (return them all in the result of this method), populate file
89
// contents on the fly, etc.
90

91         FileObject folder = Templates.getTargetFolder( wiz );
92         DataFolder targetFolder = DataFolder.findFolder( folder );
93         
94         ClassPath classPath = ClassPath.getClassPath(folder,ClassPath.SOURCE);
95         String JavaDoc listenerName = wiz.getTargetName();
96         DataObject result=null;
97         
98         if (classPath!=null) { //NOI18N
99
DataObject template = wiz.getTemplate ();
100             if (listenerName==null) {
101                 // Default name.
102
result = template.createFromTemplate (targetFolder);
103             } else {
104                 result = template.createFromTemplate (targetFolder, listenerName);
105             }
106             String JavaDoc className = classPath.getResourceName(result.getPrimaryFile(),'.',false);
107             if (result!=null && panel.createElementInDD()){
108                 FileObject webAppFo=DeployData.getWebAppFor(folder);
109                 WebApp webApp=null;
110                 if (webAppFo!=null) {
111                     webApp = DDProvider.getDefault().getDDRoot(webAppFo);
112                 }
113                 if (webApp!=null) {
114                     Listener JavaDoc[] oldListeners = webApp.getListener();
115                     boolean found=false;
116                     for (int i=0;i<oldListeners.length;i++) {
117                         if (className.equals(oldListeners[i].getListenerClass())) {
118                             found=true;
119                             break;
120                         }
121                     }
122                     if (!found) {
123                         try {
124                             Listener JavaDoc listener = (Listener JavaDoc)webApp.createBean("Listener");//NOI18N
125
listener.setListenerClass(className);
126                             StringBuffer JavaDoc desc= new StringBuffer JavaDoc();
127                             int i=0;
128                             if (panel.isContextListener()) {
129                                 desc.append("ServletContextListener"); //NOI18N
130
i++;
131                             }
132                             if (panel.isContextAttrListener()) {
133                                 if (i>0) desc.append(", ");
134                                 desc.append("ServletContextAttributeListener"); //NOI18N
135
i++;
136                             }
137                             if (panel.isSessionListener()) {
138                                 if (i>0) desc.append(", ");
139                                 desc.append("HttpSessionListener"); //NOI18N
140
i++;
141                             }
142                             if (panel.isSessionAttrListener()) {
143                                 if (i>0) desc.append(", ");
144                                 desc.append("HttpSessionAttributeListener"); //NOI18N
145
}
146                             if (panel.isRequestListener()) {
147                                 if (i>0) desc.append(", ");
148                                 desc.append("RequestListener"); //NOI18N
149
i++;
150                             }
151                             if (panel.isRequestAttrListener()) {
152                                 if (i>0) desc.append(", ");
153                                 desc.append("RequestAttributeListener"); //NOI18N
154
}
155                             listener.setDescription(desc.toString());
156                             webApp.addListener(listener);
157                             webApp.write(webAppFo);
158                         } catch (ClassNotFoundException JavaDoc ex) {//Shouldn happen since
159
}
160                     }
161                 }
162             }
163             if (result!=null) {
164                 JavaSource clazz = JavaSource.forFileObject(result.getPrimaryFile());
165                 if (clazz!=null) {
166                     ListenerGenerator gen = new ListenerGenerator(
167                         panel.isContextListener(),
168                         panel.isContextAttrListener(),
169                         panel.isSessionListener(),
170                         panel.isSessionAttrListener(),
171                         panel.isRequestListener(),
172                         panel.isRequestAttrListener());
173                     try {
174                         gen.generate(clazz);
175                     } catch (IOException JavaDoc ex){
176                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
177                     }
178                 }
179             }
180         } else {
181             String JavaDoc mes = MessageFormat.format (
182                     NbBundle.getMessage (ListenerIterator.class, "TXT_wrongFolderForClass"),
183                     new Object JavaDoc [] {"Servlet Listener"}); //NOI18N
184
NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
185             DialogDisplayer.getDefault().notify(desc);
186         }
187         return Collections.singleton (result);
188     }
189
190     // --- The rest probably does not need to be touched. ---
191

192     private transient int index;
193     private transient WizardDescriptor.Panel[] panels;
194     private transient TemplateWizard wiz;
195
196     private static final long serialVersionUID = -7586964579556513549L;
197     
198     // You can keep a reference to the TemplateWizard which can
199
// provide various kinds of useful information such as
200
// the currently selected target name.
201
// Also the panels will receive wiz as their "settings" object.
202
public void initialize (TemplateWizard wiz) {
203         this.wiz = wiz;
204         index = 0;
205         panels = createPanels (wiz);
206         
207         // Creating steps.
208
Object JavaDoc prop = wiz.getProperty ("WizardPanel_contentData"); // NOI18N
209
String JavaDoc[] beforeSteps = null;
210         if (prop != null && prop instanceof String JavaDoc[]) {
211             beforeSteps = (String JavaDoc[])prop;
212         }
213         String JavaDoc[] steps = Utilities.createSteps (beforeSteps, panels);
214         
215         for (int i = 0; i < panels.length; i++) {
216             Component JavaDoc c = panels[i].getComponent ();
217             if (steps[i] == null) {
218                 // Default step name to component name of panel.
219
// Mainly useful for getting the name of the target
220
// chooser to appear in the list of steps.
221
steps[i] = c.getName ();
222             }
223             if (c instanceof JComponent JavaDoc) { // assume Swing components
224
JComponent JavaDoc jc = (JComponent JavaDoc) c;
225                 // Step #.
226
jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
227
// Step name (actually the whole list for reference).
228
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
229
}
230         }
231     }
232     public void uninitialize (TemplateWizard wiz) {
233         this.wiz = null;
234         panels = null;
235     }
236
237     // --- WizardDescriptor.Iterator METHODS: ---
238
// Note that this is very similar to WizardDescriptor.Iterator, but with a
239
// few more options for customization. If you e.g. want to make panels appear
240
// or disappear dynamically, go ahead.
241

242     public String JavaDoc name () {
243         return NbBundle.getMessage(ListenerIterator.class, "TITLE_x_of_y",
244             new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
245     }
246
247     public boolean hasNext () {
248         return index < panels.length - 1;
249     }
250     public boolean hasPrevious () {
251         return index > 0;
252     }
253     public void nextPanel () {
254         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
255         index++;
256     }
257     public void previousPanel () {
258         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
259         index--;
260     }
261     public WizardDescriptor.Panel current () {
262         return panels[index];
263     }
264
265     // If nothing unusual changes in the middle of the wizard, simply:
266
public final void addChangeListener (ChangeListener JavaDoc l) {}
267     public final void removeChangeListener (ChangeListener JavaDoc l) {}
268     // If something changes dynamically (besides moving between panels),
269
// e.g. the number of panels changes in response to user input, then
270
// uncomment the following and call when needed:
271
// fireChangeEvent ();
272
}
273
Popular Tags