KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.cookies.SaveCookie;
30
31 import org.openide.filesystems.FileObject;
32 import org.openide.WizardDescriptor;
33 import org.openide.loaders.*;
34 import org.openide.util.NbBundle;
35
36 import org.netbeans.spi.project.ui.templates.support.Templates;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.Sources;
39 import org.netbeans.api.project.SourceGroup;
40 import org.netbeans.modules.web.api.webmodule.WebProjectConstants;
41 import org.netbeans.modules.web.api.webmodule.WebModule;
42 import org.netbeans.api.java.project.JavaProjectConstants;
43 import org.netbeans.modules.web.core.Util;
44
45 import org.netbeans.modules.web.taglib.TLDDataObject;
46 import org.netbeans.modules.web.taglib.model.Taglib;
47 import org.netbeans.modules.web.taglib.model.TagFileType;
48
49
50 /** A template wizard iterator (sequence of panels).
51  * Used to fill in the second and subsequent panels in the New wizard.
52  * Associate this to a template inside a layer using the
53  * Sequence of Panels extra property.
54  * Create one or more panels from template as needed too.
55  *
56  * @author Milan Kuchtiak
57  */

58 public class PageIterator implements TemplateWizard.Iterator {
59
60     private static final long serialVersionUID = -7586964579556513549L;
61     
62     private transient FileType fileType;
63     private WizardDescriptor.Panel folderPanel;
64     private transient SourceGroup[] sourceGroups;
65     
66     public static PageIterator createJspIterator() {
67     return new PageIterator(FileType.JSP);
68     }
69
70     public static PageIterator createTagIterator() {
71     return new PageIterator(FileType.TAG);
72     }
73     
74     public static PageIterator createTagLibraryIterator() {
75     return new PageIterator(FileType.TAGLIBRARY);
76     }
77     
78     public static PageIterator createHtmlIterator() {
79     return new PageIterator(FileType.HTML);
80     }
81     
82     public static PageIterator createXHtmlIterator() {
83     return new PageIterator(FileType.XHTML);
84     }
85     
86     private PageIterator(FileType fileType) {
87     this.fileType = fileType;
88     }
89
90     // You should define what panels you want to use here:
91
protected WizardDescriptor.Panel[] createPanels (Project project) {
92         Sources sources = (Sources) project.getLookup().lookup(org.netbeans.api.project.Sources.class);
93         if (fileType.equals(FileType.JSP)) {
94             sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
95             if (sourceGroups==null || sourceGroups.length==0)
96                 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
97             folderPanel=new TargetChooserPanel(project,sourceGroups,fileType);
98             return new WizardDescriptor.Panel[] {
99                 folderPanel
100             };
101         }
102         else if (fileType.equals(FileType.HTML) || fileType.equals(FileType.XHTML)) {
103             SourceGroup[] docRoot = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
104             SourceGroup[] srcRoots = Util.getJavaSourceGroups(project);
105             if (docRoot != null && srcRoots != null) {
106                 sourceGroups = new SourceGroup[docRoot.length + srcRoots.length];
107                 System.arraycopy(docRoot, 0, sourceGroups, 0, docRoot.length);
108                 System.arraycopy(srcRoots, 0, sourceGroups, docRoot.length, srcRoots.length);
109             }
110             if (sourceGroups==null || sourceGroups.length==0)
111                 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
112             folderPanel=new TargetChooserPanel(project,sourceGroups,fileType);
113             return new WizardDescriptor.Panel[] {
114                 folderPanel
115             };
116         }
117         else if (fileType.equals(FileType.TAG)) {
118             sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
119             if (sourceGroups==null || sourceGroups.length==0)
120                 sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
121             if (sourceGroups==null || sourceGroups.length==0)
122                 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
123             folderPanel=new TargetChooserPanel(project,sourceGroups,fileType);
124             return new WizardDescriptor.Panel[] {
125                 folderPanel
126             };
127         }
128         else if (fileType.equals(FileType.TAGLIBRARY)) {
129             sourceGroups = sources.getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
130             if (sourceGroups==null || sourceGroups.length==0)
131                 sourceGroups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
132             if (sourceGroups==null || sourceGroups.length==0)
133                 sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
134             folderPanel=new TargetChooserPanel(project,sourceGroups,fileType);
135             return new WizardDescriptor.Panel[] {
136                 folderPanel
137             };
138         }
139         return new WizardDescriptor.Panel[] {
140             Templates.createSimpleTargetChooser(project,sourceGroups)
141         };
142     }
143
144     public Set JavaDoc instantiate (TemplateWizard wiz) throws IOException JavaDoc/*, IllegalStateException*/ {
145         // Here is the default plain behavior. Simply takes the selected
146
// template (you need to have included the standard second panel
147
// in createPanels(), or at least set the properties targetName and
148
// targetFolder correctly), instantiates it in the provided
149
// position, and returns the result.
150
// More advanced wizards can create multiple objects from template
151
// (return them all in the result of this method), populate file
152
// contents on the fly, etc.
153

154         org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz );
155         DataFolder df = DataFolder.findFolder( dir );
156         FileObject template = Templates.getTemplate( wiz );
157         FileObject templateParent = template.getParent();
158         TargetChooserPanel panel = (TargetChooserPanel)folderPanel;
159         
160         if (FileType.JSP.equals(fileType)) {
161             if (panel.isSegment()) {
162                 if (panel.isXml()) {
163                     template = templateParent.getFileObject("JSPFX","jspf"); //NOI18N
164
} else {
165                     template = templateParent.getFileObject("JSPF","jspf"); //NOI18N
166
}
167             } else {
168                 if (panel.isXml()) {
169                     template = templateParent.getFileObject("JSPX","jspx"); //NOI18N
170
}
171             }
172         } else if (FileType.TAG.equals(fileType)) {
173             if (panel.isSegment()) {
174                 if (panel.isXml()) {
175                     template = templateParent.getFileObject("TagFileFX","tagf"); //NOI18N
176
} else {
177                     template = templateParent.getFileObject("TagFileF","tagf"); //NOI18N
178
}
179             } else {
180                 if (panel.isXml()) {
181                     template = templateParent.getFileObject("TagFileX","tagx"); //NOI18N
182
}
183             }
184         } else if (FileType.TAGLIBRARY.equals(fileType)) {
185             WebModule wm = WebModule.getWebModule (dir);
186             if (wm!=null) {
187                 String JavaDoc j2eeVersion = wm.getJ2eePlatformVersion();
188                 if (WebModule.J2EE_13_LEVEL.equals(j2eeVersion)) {
189                     template = templateParent.getFileObject("TagLibrary_1_2","tld"); //NOI18N
190
}
191             }
192         }
193         DataObject dTemplate = DataObject.find( template );
194         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) );
195         if (dobj!=null) {
196             if (FileType.TAGLIBRARY.equals(fileType)) { //TLD file
197
TLDDataObject tldDO = (TLDDataObject)dobj;
198                 Taglib taglib = tldDO.getTaglib();
199                 taglib.setUri(panel.getUri());
200                 taglib.setShortName(panel.getPrefix());
201                 tldDO.write(taglib);
202             } else if (FileType.TAG.equals(fileType) && panel.isTldCheckBoxSelected()) { //Write Tag File to TLD
203
FileObject tldFo = panel.getTldFileObject();
204                 if (tldFo!=null) {
205                     if (!tldFo.canWrite()) {
206                         String JavaDoc mes = java.text.MessageFormat.format (
207                                 NbBundle.getMessage (PageIterator.class, "MSG_tldRO"),
208                                 new Object JavaDoc [] {tldFo.getNameExt()});
209                         org.openide.NotifyDescriptor desc = new org.openide.NotifyDescriptor.Message(mes,
210                             org.openide.NotifyDescriptor.Message.ERROR_MESSAGE);
211                         org.openide.DialogDisplayer.getDefault().notify(desc);
212                     } else {
213                         TLDDataObject tldDO = (TLDDataObject)DataObject.find(tldFo);
214                         Taglib taglib=null;
215                         try {
216                             taglib = tldDO.getTaglib();
217                         } catch (IOException JavaDoc ex) {
218                             String JavaDoc mes = java.text.MessageFormat.format (
219                                     NbBundle.getMessage (PageIterator.class, "MSG_tldCorrupted"),
220                                     new Object JavaDoc [] {tldFo.getNameExt()});
221                             org.openide.NotifyDescriptor desc = new org.openide.NotifyDescriptor.Message(mes,
222                                 org.openide.NotifyDescriptor.Message.ERROR_MESSAGE);
223                             org.openide.DialogDisplayer.getDefault().notify(desc);
224                         }
225                         if (taglib!=null) {
226                             TagFileType tag = new TagFileType();
227                             tag.setName(panel.getTagName());
228                             String JavaDoc packageName=null;
229                             for (int i = 0; i < sourceGroups.length && packageName == null; i++) {
230                                 packageName = org.openide.filesystems.FileUtil.getRelativePath (sourceGroups [i].getRootFolder (), dobj.getPrimaryFile());
231                             }
232                             tag.setPath("/"+packageName); //NOI18N
233
taglib.addTagFile(tag);
234                             SaveCookie save = (SaveCookie)tldDO.getCookie(SaveCookie.class);
235                             if (save!=null) save.save();
236                             try {
237                                 tldDO.write(taglib);
238                             } catch (IOException JavaDoc ex) {
239                                 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.EXCEPTION,ex);
240                             }
241                         }
242                     }
243                 }
244             }
245             
246             // Do something with the result, e.g. open it:
247
/*
248             OpenCookie open = (OpenCookie) dobj.getCookie (OpenCookie.class);
249             if (open != null) {
250                 open.open ();
251             }
252              */

253         }
254         return Collections.singleton(dobj);
255     }
256
257     // --- The rest probably does not need to be touched. ---
258

259     private transient int index;
260     private transient WizardDescriptor.Panel[] panels;
261     private transient TemplateWizard wiz;
262
263     // You can keep a reference to the TemplateWizard which can
264
// provide various kinds of useful information such as
265
// the currently selected target name.
266
// Also the panels will receive wiz as their "settings" object.
267
public void initialize (TemplateWizard wiz) {
268         this.wiz = wiz;
269         index = 0;
270         Project project = Templates.getProject( wiz );
271         panels = createPanels (project);
272         
273         // Creating steps.
274
Object JavaDoc prop = wiz.getProperty ("WizardPanel_contentData"); // NOI18N
275
String JavaDoc[] beforeSteps = null;
276         if (prop != null && prop instanceof String JavaDoc[]) {
277             beforeSteps = (String JavaDoc[])prop;
278         }
279         String JavaDoc[] steps = Utilities.createSteps (beforeSteps, panels);
280         
281         for (int i = 0; i < panels.length; i++) {
282             Component JavaDoc c = panels[i].getComponent ();
283             if (steps[i] == null) {
284                 // Default step name to component name of panel.
285
// Mainly useful for getting the name of the target
286
// chooser to appear in the list of steps.
287
steps[i] = c.getName ();
288             }
289             if (c instanceof JComponent JavaDoc) { // assume Swing components
290
JComponent JavaDoc jc = (JComponent JavaDoc) c;
291                 // Step #.
292
jc.putClientProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (i)); // NOI18N
293
// Step name (actually the whole list for reference).
294
jc.putClientProperty ("WizardPanel_contentData", steps); // NOI18N
295
}
296         }
297     }
298     public void uninitialize (TemplateWizard wiz) {
299         this.wiz = null;
300         panels = null;
301     }
302
303     // --- WizardDescriptor.Iterator METHODS: ---
304
// Note that this is very similar to WizardDescriptor.Iterator, but with a
305
// few more options for customization. If you e.g. want to make panels appear
306
// or disappear dynamically, go ahead.
307

308     public String JavaDoc name () {
309         return NbBundle.getMessage(PageIterator.class, "TITLE_x_of_y",
310             new Integer JavaDoc (index + 1), new Integer JavaDoc (panels.length));
311     }
312     
313     public boolean hasNext () {
314         return index < panels.length - 1;
315     }
316     public boolean hasPrevious () {
317         return index > 0;
318     }
319     public void nextPanel () {
320         if (! hasNext ()) throw new NoSuchElementException JavaDoc ();
321         index++;
322     }
323     public void previousPanel () {
324         if (! hasPrevious ()) throw new NoSuchElementException JavaDoc ();
325         index--;
326     }
327     public WizardDescriptor.Panel current () {
328         return panels[index];
329     }
330     
331     // If nothing unusual changes in the middle of the wizard, simply:
332
public final void addChangeListener (ChangeListener JavaDoc l) {}
333     public final void removeChangeListener (ChangeListener JavaDoc l) {}
334     // If something changes dynamically (besides moving between panels),
335
// e.g. the number of panels changes in response to user input, then
336
// uncomment the following and call when needed:
337
// fireChangeEvent ();
338
}
339
Popular Tags