KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > wizard > STSWizard


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.wsitconf.wizard;
21
22 import java.awt.Component JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.text.MessageFormat JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.NoSuchElementException JavaDoc;
34 import java.util.Set JavaDoc;
35
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.xml.parsers.ParserConfigurationException JavaDoc;
39 import org.netbeans.api.project.Project;
40 import org.netbeans.api.project.SourceGroup;
41 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
42 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener;
43 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
44 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelerFactory;
45 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
46 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService;
47 import org.netbeans.modules.websvc.wsitconf.util.Util;
48 import org.openide.ErrorManager;
49
50 import org.openide.WizardDescriptor;
51
52 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
53 import org.netbeans.spi.project.ui.templates.support.Templates;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.loaders.DataObject;
57 import org.openide.loaders.TemplateWizard;
58 import org.openide.util.NbBundle;
59 import org.xml.sax.SAXException JavaDoc;
60
61 /**
62  * Wizard to create a new STS Web service.
63  * @author Martin Grebac
64  */

65 public class STSWizard implements TemplateWizard.Iterator {
66     
67     private Project project;
68     
69     private WsdlModeler wsdlModeler;
70     private WsdlModel wsdlModel;
71     private WsdlService service;
72     private WsdlPort port;
73     private WsdlServiceHandler wsdlServiceHandler;
74     
75     /** Create a new wizard iterator. */
76     public STSWizard() {
77     }
78     
79     public static STSWizard create() {
80         return new STSWizard();
81     }
82     
83     public Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc {
84         FileObject template = Templates.getTemplate(wiz);
85         DataObject dTemplate = DataObject.find(template);
86         
87         OutputStream JavaDoc schemaos = null, wsdlos = null;
88         
89         try {
90             final InputStream JavaDoc schemaIS = this.getClass().getClassLoader().getResourceAsStream("org/netbeans/modules/websvc/wsitconf/resources/sts_schema.xsd"); //NOI18N
91
File JavaDoc schema = new File JavaDoc(System.getProperty("java.io.tmpdir") + "sts_schema.xsd"); //NOI18N
92
schema.createNewFile();
93             schemaos = new FileOutputStream JavaDoc(schema);
94             FileUtil.copy(schemaIS, schemaos);
95         } catch (FileNotFoundException JavaDoc ex) {
96             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
97         } catch (IOException JavaDoc ex) {
98             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
99         } finally {
100             if (schemaos != null) schemaos.close();
101         }
102
103         File JavaDoc folder = null;
104         try {
105             final InputStream JavaDoc wsdlIS = this.getClass().getClassLoader().getResourceAsStream("org/netbeans/modules/websvc/wsitconf/resources/sts.wsdl"); //NOI18N
106
folder = new File JavaDoc(System.getProperty("java.io.tmpdir") + "sts.wsdl"); //NOI18N
107
folder.createNewFile();
108             wsdlos = new FileOutputStream JavaDoc(folder);
109             FileUtil.copy(wsdlIS, wsdlos);
110         } catch (FileNotFoundException JavaDoc ex) {
111             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
112         } catch (IOException JavaDoc ex) {
113             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
114         } finally {
115             if (wsdlos != null) wsdlos.close();
116         }
117
118         if (folder == null) return null;
119         
120         final URL JavaDoc wsdlURL = folder.toURI().toURL();
121
122         wiz.putProperty(WizardProperties.WSDL_FILE_PATH, folder.getPath());
123         
124         wsdlModeler = WsdlModelerFactory.getDefault().getWsdlModeler(wsdlURL);
125         wsdlModeler.generateWsdlModel(new WsdlModelListener() {
126             public void modelCreated(WsdlModel model) {
127                 wsdlModel=model;
128                 if (wsdlModel==null) {
129                     String JavaDoc serviceName=null;
130                     String JavaDoc portName=null;
131                     try {
132                         wsdlServiceHandler = WsdlServiceHandler.parse(wsdlURL.toExternalForm());
133                         serviceName = wsdlServiceHandler.getServiceName();
134                         portName = wsdlServiceHandler.getPortName();
135                     } catch (ParserConfigurationException JavaDoc ex) {
136                     } catch (SAXException JavaDoc ex) {
137                     } catch (IOException JavaDoc ex) {}
138                 } else {
139                     List JavaDoc services = wsdlModel.getServices();
140                     if (services != null && services.size() > 0) {
141                         service = (WsdlService) services.get(0);
142                         List JavaDoc ports = service.getPorts();
143                         if (ports != null && ports.size() > 0)
144                             port = (WsdlPort) ports.get(0);
145                     }
146                 }
147
148 // if (service != null && port != null) {
149
// if (wsdlHandler!=null) {
150
// String bindingType = wsdlHandler.getBindingTypeForPort(port.getName());
151
// if (bindingType!=null) port.setSOAPVersion(bindingType);
152
// }
153
// }
154
}
155         });
156
157         int timeout = 10000;
158         while ((service == null) && (timeout > 0)) {
159             try {
160                 Thread.sleep(200);
161                 timeout -= 200;
162             } catch (InterruptedException JavaDoc ex) {
163 // ex.printStackTrace();
164
}
165         }
166         
167         if (service != null) {
168             wiz.putProperty(WizardProperties.WSDL_SERVICE, service);
169             wiz.putProperty(WizardProperties.WSDL_PORT, port);
170             wiz.putProperty(WizardProperties.WSDL_MODELER, wsdlModeler);
171             new STSWizardCreator(project, wiz).createSTS();
172         }
173         
174         return Collections.singleton(dTemplate);
175     }
176     
177     private transient int index;
178     private transient WizardDescriptor.Panel[] panels;
179     private transient TemplateWizard wiz;
180     private transient WizardDescriptor.Panel bottomPanel;
181     
182     public void initialize(TemplateWizard wiz) {
183         this.wiz = wiz;
184         index = 0;
185         
186         project = Templates.getProject(wiz);
187         
188         SourceGroup[] sourceGroups = Util.getJavaSourceGroups(project);
189 // bottomPanel = new WebServiceFromWSDL(wiz, project);
190
WizardDescriptor.Panel firstPanel; //special case: use Java Chooser
191
if (sourceGroups.length == 0)
192             firstPanel = new FinishableProxyWizardPanel(Templates.createSimpleTargetChooser(project, sourceGroups, bottomPanel));
193         else
194             firstPanel = new FinishableProxyWizardPanel(JavaTemplates.createPackageChooser(project, sourceGroups, bottomPanel, true));
195         
196         JComponent JavaDoc comp = (JComponent JavaDoc) firstPanel.getComponent();
197         Util.changeLabelInComponent(comp, NbBundle.getMessage(STSWizard.class, "LBL_JavaTargetChooserPanelGUI_ClassName_Label"),
198                 NbBundle.getMessage(STSWizard.class, "LBL_Webservice_Name") );
199         Util.hideLabelAndLabelFor(comp, NbBundle.getMessage(STSWizard.class, "LBL_JavaTargetChooserPanelGUI_CreatedFile_Label"));
200         
201         panels = new WizardDescriptor.Panel[] {
202             firstPanel,
203         };
204         
205         // Creating steps.
206
Object JavaDoc prop = this.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 = createSteps(beforeSteps, panels);
212         
213         // Make sure list of steps is accurate.
214
for (int i = 0; i < panels.length; i++) {
215             Component JavaDoc c = panels[i].getComponent();
216             if (c instanceof JComponent JavaDoc) { // assume Swing components
217
JComponent JavaDoc jc = (JComponent JavaDoc) c;
218                 // Step #.
219
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
220
// Step name (actually the whole list for reference).
221
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
222
}
223         }
224     }
225     
226     public void uninitialize(TemplateWizard wiz) {
227         if (this.wiz != null) {
228             this.wiz.putProperty(WizardProperties.WEB_SERVICE_TYPE, null);
229         }
230         panels = null;
231     }
232     
233     private String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
234         int diff = 0;
235         if (before == null) {
236             before = new String JavaDoc[0];
237         } else if (before.length > 0) {
238             diff = ("...".equals(before[before.length - 1])) ? 1 : 0; // NOI18N
239
}
240         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
241         for (int i = 0; i < res.length; i++) {
242             if (i < (before.length - diff)) {
243                 res[i] = before[i];
244             } else {
245                 res[i] = panels[i - before.length + diff].getComponent().getName();
246             }
247         }
248         return res;
249     }
250     
251     public String JavaDoc name() {
252         return MessageFormat.format(NbBundle.getMessage(STSWizard.class, "LBL_WizardStepsCount"),
253                 new String JavaDoc[] {(new Integer JavaDoc(index + 1)).toString(), (new Integer JavaDoc(panels.length)).toString()}); //NOI18N
254
}
255     
256     public boolean hasNext() {
257         return index < panels.length - 1;
258     }
259     
260     public boolean hasPrevious() {
261         return index > 0;
262     }
263     
264     public void nextPanel() {
265         if (!hasNext()) throw new NoSuchElementException JavaDoc();
266         index++;
267     }
268     
269     public void previousPanel() {
270         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
271         index--;
272     }
273     
274     public WizardDescriptor.Panel current() {
275         return panels[index];
276     }
277     
278     // If nothing unusual changes in the middle of the wizard, simply:
279
public final void addChangeListener(ChangeListener JavaDoc l) {}
280     public final void removeChangeListener(ChangeListener JavaDoc l) {}
281     
282 }
283
Popular Tags