KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.MessageFormat JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.netbeans.api.java.source.JavaSource;
31 import org.netbeans.modules.web.core.Util;
32
33 import org.openide.filesystems.FileObject;
34 import org.openide.WizardDescriptor;
35 import org.openide.cookies.SaveCookie;
36 import org.openide.loaders.*;
37 import org.openide.util.NbBundle;
38 import org.openide.ErrorManager;
39 import org.openide.NotifyDescriptor;
40 import org.openide.DialogDisplayer;
41
42 import org.netbeans.spi.project.ui.templates.support.Templates;
43 import org.netbeans.api.project.Project;
44 import org.netbeans.api.project.Sources;
45 import org.netbeans.api.project.SourceGroup;
46 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
47 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
48
49 import org.netbeans.modules.web.taglib.TLDDataObject;
50 import org.netbeans.modules.web.taglib.model.Taglib;
51 import org.netbeans.modules.web.taglib.model.TagType;
52 import org.netbeans.modules.web.taglib.model.TldAttributeType;
53
54 /** A template wizard iterator (sequence of panels).
55  * Used to fill in the second and subsequent panels in the New wizard.
56  * Associate this to a template inside a layer using the
57  * Sequence of Panels extra property.
58  * Create one or more panels from template as needed too.
59  *
60  * @author mk115033
61  */

62 public class TagHandlerIterator implements TemplateWizard.Iterator {
63     private transient FileType fileType;
64     private WizardDescriptor.Panel packageChooserPanel,tagHandlerSelectionPanel,tagInfoPanel;
65     
66     // You should define what panels you want to use here:
67
protected WizardDescriptor.Panel[] createPanels (Project project,TemplateWizard wiz) {
68         Sources sources = (Sources) project.getLookup().lookup(org.netbeans.api.project.Sources.class);
69         SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project);
70         tagHandlerSelectionPanel = new TagHandlerSelection(wiz);
71         
72         if (sourceGroups.length == 0)
73             packageChooserPanel = Templates.createSimpleTargetChooser(project, sourceGroups, tagHandlerSelectionPanel);
74         else
75             packageChooserPanel = JavaTemplates.createPackageChooser(project,sourceGroups,tagHandlerSelectionPanel);
76         
77         sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
78         if (sourceGroups==null || sourceGroups.length==0)
79             sourceGroups = Util.getJavaSourceGroups(project);
80         if (sourceGroups==null || sourceGroups.length==0)
81             sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
82         tagInfoPanel = new TagInfoPanel(wiz, project, sourceGroups);
83         return new WizardDescriptor.Panel[] {
84             packageChooserPanel,
85             tagInfoPanel
86         };
87     }
88
89     public Set JavaDoc instantiate (TemplateWizard wiz) throws IOException JavaDoc/*, IllegalStateException*/ {
90         // Here is the default plain behavior. Simply takes the selected
91
// template (you need to have included the standard second panel
92
// in createPanels(), or at least set the properties targetName and
93
// targetFolder correctly), instantiates it in the provided
94
// position, and returns the result.
95
// More advanced wizards can create multiple objects from template
96
// (return them all in the result of this method), populate file
97
// contents on the fly, etc.
98

99         org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz );
100         DataFolder df = DataFolder.findFolder( dir );
101         
102         FileObject template = Templates.getTemplate( wiz );
103         
104         if (((TagHandlerSelection)tagHandlerSelectionPanel).isBodyTagSupport()) {
105             FileObject templateParent = template.getParent();
106             template = templateParent.getFileObject("BodyTagHandler","java"); //NOI18N
107
}
108         DataObject dTemplate = DataObject.find( template );
109         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) );
110         // writing to TLD File
111
TagInfoPanel tldPanel = (TagInfoPanel)tagInfoPanel;
112         Object JavaDoc[][] attrs = tldPanel.getAttributes();
113         boolean isBodyTag = ((TagHandlerSelection)tagHandlerSelectionPanel).isBodyTagSupport();
114         
115         // writing setters to tag handler
116
if (attrs.length>0 || isBodyTag) {
117             JavaSource clazz = JavaSource.forFileObject(dobj.getPrimaryFile());
118             boolean evaluateBody = !((TagInfoPanel)tagInfoPanel).isEmpty();
119             TagHandlerGenerator generator = new TagHandlerGenerator(clazz,attrs,isBodyTag, evaluateBody);
120             try {
121                 generator.generate();
122             } catch (IOException JavaDoc ex){
123                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
124             }
125         }
126         
127         // writing to TLD file
128
if (tldPanel.writeToTLD()) {
129             FileObject tldFo = tldPanel.getTLDFile();
130             if (tldFo!=null) {
131                 if (!tldFo.canWrite()) {
132                     String JavaDoc mes = MessageFormat.format (
133                             NbBundle.getMessage (TagHandlerIterator.class, "MSG_tldRO"),
134                             new Object JavaDoc [] {tldFo.getNameExt()});
135                     NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
136                     DialogDisplayer.getDefault().notify(desc);
137                 } else {
138                     TLDDataObject tldDO = (TLDDataObject)DataObject.find(tldFo);
139                     Taglib taglib=null;
140                     try {
141                         taglib = tldDO.getTaglib();
142                     } catch (IOException JavaDoc ex) {
143                         String JavaDoc mes = MessageFormat.format (
144                                 NbBundle.getMessage (TagHandlerIterator.class, "MSG_tldCorrupted"),
145                                 new Object JavaDoc [] {tldFo.getNameExt()});
146                         NotifyDescriptor desc = new NotifyDescriptor.Message(mes,NotifyDescriptor.Message.ERROR_MESSAGE);
147                         DialogDisplayer.getDefault().notify(desc);
148                     }
149                     if (taglib!=null) {
150                         TagType tag = new TagType();
151                         tag.setName(tldPanel.getTagName());
152                         tag.setTagClass(tldPanel.getClassName());
153                         if (tldPanel.isEmpty()) {
154                             tag.setBodyContent("empty"); //NOI18N
155
} else if (tldPanel.isScriptless()) {
156                             tag.setBodyContent(isBodyTag?"JSP":"scriptless"); //NOI18N
157
} else if (tldPanel.isTegdependent()) {
158                             tag.setBodyContent("tagdependent"); //NOI18N
159
}
160                         //Object[][] attrs = tldPanel.getAttributes();
161
for (int i=0;i<attrs.length;i++) {
162                             TldAttributeType attr = new TldAttributeType();
163                             attr.setName((String JavaDoc)attrs[i][0]);
164                             attr.setType((String JavaDoc)attrs[i][1]);
165                             boolean required = ((Boolean JavaDoc)attrs[i][2]).booleanValue();
166                             if (required) attr.setRequired("true"); //NOI18N
167
boolean rtexpr = ((Boolean JavaDoc)attrs[i][3]).booleanValue();
168                             if (rtexpr) attr.setRtexprvalue("true"); //NOI18N
169
tag.addAttribute(attr);
170                         }
171                         taglib.addTag(tag);
172                         SaveCookie save = (SaveCookie)tldDO.getCookie(SaveCookie.class);
173                         if (save!=null) save.save();
174                         try {
175                             tldDO.write(taglib);
176                         } catch (IOException JavaDoc ex) {
177                             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex);
178                         }
179                     }
180                 }
181             }
182         }
183         
184         return Collections.singleton(dobj);
185     }
186
187     // --- The rest probably does not need to be touched. ---
188

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

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