KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > wizard > WsdlIterator


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.websvc.wsdl.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29 import java.util.Set JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
33
34 import org.openide.filesystems.FileObject;
35 import org.openide.WizardDescriptor;
36 import org.openide.cookies.EditCookie;
37 import org.openide.cookies.EditorCookie;
38 import org.openide.cookies.SaveCookie;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.loaders.*;
41 import org.openide.util.NbBundle;
42
43 import org.netbeans.spi.project.ui.templates.support.Templates;
44 import org.netbeans.api.project.Project;
45 import org.netbeans.api.project.Sources;
46 import org.netbeans.api.project.SourceGroup;
47
48 /** A template wizard iterator (sequence of panels).
49  * Used to fill in the second and subsequent panels in the New wizard.
50  * Associate this to a template inside a layer using the
51  * Sequence of Panels extra property.
52  * Create one or more panels from template as needed too.
53  *
54  * @author Milan Kuchtiak
55  */

56 public class WsdlIterator implements TemplateWizard.Iterator {
57     private static final long serialVersionUID = 3020391475943498962L;
58     public static final java.awt.Dimension JavaDoc PREF_SIZE = new java.awt.Dimension JavaDoc(560,350);
59     
60     private WizardDescriptor.Panel folderPanel;
61     private transient SourceGroup[] sourceGroups;
62     //private static final long serialVersionUID = -7586964579556513549L;
63

64     public WsdlIterator() {
65     }
66     
67     // You should define what panels you want to use here:
68
protected WizardDescriptor.Panel[] createPanels(Project project) {
69         Sources sources = (Sources) project.getLookup().lookup(org.netbeans.api.project.Sources.class);
70         sourceGroups = sources.getSourceGroups(Sources.TYPE_GENERIC);
71         folderPanel=new WsdlPanel(project,sourceGroups);
72         // creates simple wizard panel with bottom panel
73
WizardDescriptor.Panel firstPanel = Templates.createSimpleTargetChooser(project,sourceGroups,folderPanel);
74         JComponent JavaDoc c = (JComponent JavaDoc)firstPanel.getComponent();
75         // the bottom panel should listen to changes on file name text field
76
String JavaDoc fileNameLabel = NbBundle.getMessage(WsdlIterator.class,"LBL_SimpleTargetChooserPanel_FileName_Label");
77         ((WsdlPanel)folderPanel).setNameTF((JTextField JavaDoc)Utilities.findTextFieldForLabel(c,fileNameLabel));
78         
79         return new WizardDescriptor.Panel[] {
80             firstPanel
81         };
82     }
83     
84     public Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc/*, IllegalStateException*/ {
85         // Here is the default plain behavior. Simply takes the selected
86
// template (you need to have included the standard second panel
87
// in createPanels(), or at least set the properties targetName and
88
// targetFolder correctly), instantiates it in the provided
89
// position, and returns the result.
90
// More advanced wizards can create multiple objects from template
91
// (return them all in the result of this method), populate file
92
// contents on the fly, etc.
93

94         final org.openide.filesystems.FileObject dir = Templates.getTargetFolder( wiz );
95         DataFolder df = DataFolder.findFolder( dir );
96         FileObject template = Templates.getTemplate( wiz );
97         WsdlPanel panel = (WsdlPanel)folderPanel;
98         boolean importSchemas=false;
99         if (panel.isImport() && panel.getSchemas().length>0) {
100             importSchemas=true;
101             FileObject templateParent = template.getParent();
102             template = templateParent.getFileObject("WSDL_import","wsdl"); //NOI18N
103
}
104         DataObject dTemplate = DataObject.find( template );
105         DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) );
106         if (dobj!=null) {
107             // Do something with the result, e.g. open it:
108

109             EditCookie edit = (EditCookie) dobj.getCookie(EditCookie.class);
110             if (edit != null) {
111                 EditorCookie editorCookie = (EditorCookie)dobj.getCookie(EditorCookie.class);
112                 editorCookie.openDocument();
113                 javax.swing.text.Document JavaDoc doc = editorCookie.getDocument();
114                 Utilities.replaceInDocument(doc,"#SERVICE_NAME",panel.getWsName()); //NOI18N
115
String JavaDoc targetNamespace = panel.getNS();
116                 if (targetNamespace.length()==0) targetNamespace = DEFAULT_TARGET_NAMESPACE;
117                 Utilities.replaceInDocument(doc,"#TARGET_NAMESPACE",targetNamespace); //NOI18N
118
if (importSchemas) {
119                     WsdlUIPanel.SchemaInfo[] infos = panel.getSchemas();
120                     StringBuffer JavaDoc typeNamespaces = new StringBuffer JavaDoc();
121                     StringBuffer JavaDoc imports = new StringBuffer JavaDoc();
122                     for (int i=0;i<infos.length;i++) {
123                         if (i>0) {
124                             typeNamespaces.append(" ");
125                             imports.append(" ");
126                         }
127                         
128                         String JavaDoc ns = infos[i].getNamespace();
129                         if (ns.length()==0) ns = "urn:WS/types"+String.valueOf(i+1); //NOI18N
130

131                         typeNamespaces.append("xmlns:ns"+String.valueOf(i+1)+"=\""+ns+"\"\n"); //NOI18N
132
imports.append("<xsd:import "); //NOI18N
133
imports.append("namespace=\""+ns+"\" "); //NOI18N
134
String JavaDoc relativePath = null;
135                         try{
136                             relativePath = getRelativePathOfSchema(dobj, infos[i].getSchemaName());
137                         }catch(URISyntaxException JavaDoc e){
138                             relativePath= infos[i].getSchemaName();
139                         }
140                         //imports.append("schemaLocation=\""+infos[i].getSchemaName()+"\"/>\n"); //NOI18N
141
imports.append("schemaLocation=\""+relativePath+"\"/>\n"); //NOI18N
142
}
143                     Utilities.replaceInDocument(doc,"#TYPE_NAMESPACES",typeNamespaces.toString()); //NOI18N
144
Utilities.replaceInDocument(doc,"#SCHEMA_IMPORTS",imports.toString()); //NOI18N
145

146                     for (int i=0;i<infos.length;i++) {
147                         if (i>0) typeNamespaces.append(" ");
148                         typeNamespaces.append("xmlns:ns"+String.valueOf(i+1)+"=\""+infos[i].getNamespace()+"\"\n"); //NOI18N
149
}
150                     Utilities.replaceInDocument(doc,"#TYPE_NAMESPACES",typeNamespaces.toString()); //NOI18N
151
}
152                 SaveCookie save = (SaveCookie)dobj.getCookie(SaveCookie.class);
153                 if (save!=null) save.save();
154             }
155             
156         }
157         return Collections.singleton(dobj);
158     }
159     
160     private String JavaDoc getRelativePathOfSchema(DataObject wsdlDO, String JavaDoc schemaURL) throws URISyntaxException JavaDoc{
161         FileObject fo = wsdlDO.getPrimaryFile();
162         File JavaDoc f = FileUtil.toFile(fo);
163         String JavaDoc relativePath = org.netbeans.modules.xml.retriever.catalog.Utilities.relativize(f.toURI(),new URI JavaDoc(schemaURL) );
164         return relativePath;
165     }
166     
167     private static final String JavaDoc DEFAULT_TARGET_NAMESPACE = "urn:WS/wsdl"; //NOI18N
168

169     // --- The rest probably does not need to be touched. ---
170

171     private transient int index;
172     private transient WizardDescriptor.Panel[] panels;
173     private transient TemplateWizard wiz;
174     
175     // You can keep a reference to the TemplateWizard which can
176
// provide various kinds of useful information such as
177
// the currently selected target name.
178
// Also the panels will receive wiz as their "settings" object.
179
public void initialize(TemplateWizard wiz) {
180         this.wiz = wiz;
181         index = 0;
182         Project project = Templates.getProject( wiz );
183         panels = createPanels(project);
184         
185         // Creating steps.
186
Object JavaDoc prop = wiz.getProperty("WizardPanel_contentData"); // NOI18N
187
String JavaDoc[] beforeSteps = null;
188         if (prop != null && prop instanceof String JavaDoc[]) {
189             beforeSteps = (String JavaDoc[])prop;
190         }
191         String JavaDoc[] steps = Utilities.createSteps(beforeSteps, panels);
192         
193         for (int i = 0; i < panels.length; i++) {
194             Component JavaDoc c = panels[i].getComponent();
195             if (steps[i] == null) {
196                 // Default step name to component name of panel.
197
// Mainly useful for getting the name of the target
198
// chooser to appear in the list of steps.
199
steps[i] = c.getName();
200             }
201             if (c instanceof JComponent JavaDoc) { // assume Swing components
202
JComponent JavaDoc jc = (JComponent JavaDoc) c;
203                 // Step #.
204
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
205
// Step name (actually the whole list for reference).
206
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
207
}
208         }
209     }
210     public void uninitialize(TemplateWizard wiz) {
211         this.wiz = null;
212         panels = null;
213     }
214     
215     // --- WizardDescriptor.Iterator METHODS: ---
216
// Note that this is very similar to WizardDescriptor.Iterator, but with a
217
// few more options for customization. If you e.g. want to make panels appear
218
// or disappear dynamically, go ahead.
219

220     public String JavaDoc name() {
221         return NbBundle.getMessage(WsdlIterator.class, "TITLE_x_of_y",
222                 new Integer JavaDoc(index + 1), new Integer JavaDoc(panels.length));
223     }
224     
225     public boolean hasNext() {
226         return index < panels.length - 1;
227     }
228     public boolean hasPrevious() {
229         return index > 0;
230     }
231     public void nextPanel() {
232         if (! hasNext()) throw new NoSuchElementException JavaDoc();
233         index++;
234     }
235     public void previousPanel() {
236         if (! hasPrevious()) throw new NoSuchElementException JavaDoc();
237         index--;
238     }
239     public WizardDescriptor.Panel current() {
240         return panels[index];
241     }
242     
243     // If nothing unusual changes in the middle of the wizard, simply:
244
public final void addChangeListener(ChangeListener JavaDoc l) {}
245     public final void removeChangeListener(ChangeListener JavaDoc l) {}
246     // If something changes dynamically (besides moving between panels),
247
// e.g. the number of panels changes in response to user input, then
248
// uncomment the following and call when needed:
249
// fireChangeEvent ();
250
}
251
Popular Tags