1 19 20 package org.netbeans.modules.websvc.wsitconf.wizard; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.URL ; 30 import java.text.MessageFormat ; 31 import java.util.Collections ; 32 import java.util.List ; 33 import java.util.NoSuchElementException ; 34 import java.util.Set ; 35 36 import javax.swing.JComponent ; 37 import javax.swing.event.ChangeListener ; 38 import javax.xml.parsers.ParserConfigurationException ; 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 ; 60 61 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 76 public STSWizard() { 77 } 78 79 public static STSWizard create() { 80 return new STSWizard(); 81 } 82 83 public Set instantiate(TemplateWizard wiz) throws IOException { 84 FileObject template = Templates.getTemplate(wiz); 85 DataObject dTemplate = DataObject.find(template); 86 87 OutputStream schemaos = null, wsdlos = null; 88 89 try { 90 final InputStream schemaIS = this.getClass().getClassLoader().getResourceAsStream("org/netbeans/modules/websvc/wsitconf/resources/sts_schema.xsd"); File schema = new File (System.getProperty("java.io.tmpdir") + "sts_schema.xsd"); schema.createNewFile(); 93 schemaos = new FileOutputStream (schema); 94 FileUtil.copy(schemaIS, schemaos); 95 } catch (FileNotFoundException ex) { 96 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 97 } catch (IOException ex) { 98 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 99 } finally { 100 if (schemaos != null) schemaos.close(); 101 } 102 103 File folder = null; 104 try { 105 final InputStream wsdlIS = this.getClass().getClassLoader().getResourceAsStream("org/netbeans/modules/websvc/wsitconf/resources/sts.wsdl"); folder = new File (System.getProperty("java.io.tmpdir") + "sts.wsdl"); folder.createNewFile(); 108 wsdlos = new FileOutputStream (folder); 109 FileUtil.copy(wsdlIS, wsdlos); 110 } catch (FileNotFoundException ex) { 111 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 112 } catch (IOException 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 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 serviceName=null; 130 String portName=null; 131 try { 132 wsdlServiceHandler = WsdlServiceHandler.parse(wsdlURL.toExternalForm()); 133 serviceName = wsdlServiceHandler.getServiceName(); 134 portName = wsdlServiceHandler.getPortName(); 135 } catch (ParserConfigurationException ex) { 136 } catch (SAXException ex) { 137 } catch (IOException ex) {} 138 } else { 139 List services = wsdlModel.getServices(); 140 if (services != null && services.size() > 0) { 141 service = (WsdlService) services.get(0); 142 List ports = service.getPorts(); 143 if (ports != null && ports.size() > 0) 144 port = (WsdlPort) ports.get(0); 145 } 146 } 147 148 } 155 }); 156 157 int timeout = 10000; 158 while ((service == null) && (timeout > 0)) { 159 try { 160 Thread.sleep(200); 161 timeout -= 200; 162 } catch (InterruptedException ex) { 163 } 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 WizardDescriptor.Panel firstPanel; 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 comp = (JComponent ) 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 Object prop = this.wiz.getProperty("WizardPanel_contentData"); String [] beforeSteps = null; 208 if (prop != null && prop instanceof String []) { 209 beforeSteps = (String []) prop; 210 } 211 String [] steps = createSteps(beforeSteps, panels); 212 213 for (int i = 0; i < panels.length; i++) { 215 Component c = panels[i].getComponent(); 216 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 218 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 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 [] createSteps(String [] before, WizardDescriptor.Panel[] panels) { 234 int diff = 0; 235 if (before == null) { 236 before = new String [0]; 237 } else if (before.length > 0) { 238 diff = ("...".equals(before[before.length - 1])) ? 1 : 0; } 240 String [] res = new String [ (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 name() { 252 return MessageFormat.format(NbBundle.getMessage(STSWizard.class, "LBL_WizardStepsCount"), 253 new String [] {(new Integer (index + 1)).toString(), (new Integer (panels.length)).toString()}); } 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 (); 266 index++; 267 } 268 269 public void previousPanel() { 270 if (!hasPrevious()) throw new NoSuchElementException (); 271 index--; 272 } 273 274 public WizardDescriptor.Panel current() { 275 return panels[index]; 276 } 277 278 public final void addChangeListener(ChangeListener l) {} 280 public final void removeChangeListener(ChangeListener l) {} 281 282 } 283 | Popular Tags |