1 19 20 package org.netbeans.modules.websvc.wsdl.wizard; 21 22 import java.awt.Component ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.util.Collections ; 28 import java.util.NoSuchElementException ; 29 import java.util.Set ; 30 import javax.swing.JComponent ; 31 import javax.swing.JTextField ; 32 import javax.swing.event.ChangeListener ; 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 56 public class WsdlIterator implements TemplateWizard.Iterator { 57 private static final long serialVersionUID = 3020391475943498962L; 58 public static final java.awt.Dimension PREF_SIZE = new java.awt.Dimension (560,350); 59 60 private WizardDescriptor.Panel folderPanel; 61 private transient SourceGroup[] sourceGroups; 62 64 public WsdlIterator() { 65 } 66 67 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 WizardDescriptor.Panel firstPanel = Templates.createSimpleTargetChooser(project,sourceGroups,folderPanel); 74 JComponent c = (JComponent )firstPanel.getComponent(); 75 String fileNameLabel = NbBundle.getMessage(WsdlIterator.class,"LBL_SimpleTargetChooserPanel_FileName_Label"); 77 ((WsdlPanel)folderPanel).setNameTF((JTextField )Utilities.findTextFieldForLabel(c,fileNameLabel)); 78 79 return new WizardDescriptor.Panel[] { 80 firstPanel 81 }; 82 } 83 84 public Set instantiate(TemplateWizard wiz) throws IOException { 85 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"); } 104 DataObject dTemplate = DataObject.find( template ); 105 DataObject dobj = dTemplate.createFromTemplate( df, Templates.getTargetName( wiz ) ); 106 if (dobj!=null) { 107 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 doc = editorCookie.getDocument(); 114 Utilities.replaceInDocument(doc,"#SERVICE_NAME",panel.getWsName()); String targetNamespace = panel.getNS(); 116 if (targetNamespace.length()==0) targetNamespace = DEFAULT_TARGET_NAMESPACE; 117 Utilities.replaceInDocument(doc,"#TARGET_NAMESPACE",targetNamespace); if (importSchemas) { 119 WsdlUIPanel.SchemaInfo[] infos = panel.getSchemas(); 120 StringBuffer typeNamespaces = new StringBuffer (); 121 StringBuffer imports = new StringBuffer (); 122 for (int i=0;i<infos.length;i++) { 123 if (i>0) { 124 typeNamespaces.append(" "); 125 imports.append(" "); 126 } 127 128 String ns = infos[i].getNamespace(); 129 if (ns.length()==0) ns = "urn:WS/types"+String.valueOf(i+1); 131 typeNamespaces.append("xmlns:ns"+String.valueOf(i+1)+"=\""+ns+"\"\n"); imports.append("<xsd:import "); imports.append("namespace=\""+ns+"\" "); String relativePath = null; 135 try{ 136 relativePath = getRelativePathOfSchema(dobj, infos[i].getSchemaName()); 137 }catch(URISyntaxException e){ 138 relativePath= infos[i].getSchemaName(); 139 } 140 imports.append("schemaLocation=\""+relativePath+"\"/>\n"); } 143 Utilities.replaceInDocument(doc,"#TYPE_NAMESPACES",typeNamespaces.toString()); Utilities.replaceInDocument(doc,"#SCHEMA_IMPORTS",imports.toString()); 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"); } 150 Utilities.replaceInDocument(doc,"#TYPE_NAMESPACES",typeNamespaces.toString()); } 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 getRelativePathOfSchema(DataObject wsdlDO, String schemaURL) throws URISyntaxException { 161 FileObject fo = wsdlDO.getPrimaryFile(); 162 File f = FileUtil.toFile(fo); 163 String relativePath = org.netbeans.modules.xml.retriever.catalog.Utilities.relativize(f.toURI(),new URI (schemaURL) ); 164 return relativePath; 165 } 166 167 private static final String DEFAULT_TARGET_NAMESPACE = "urn:WS/wsdl"; 169 171 private transient int index; 172 private transient WizardDescriptor.Panel[] panels; 173 private transient TemplateWizard wiz; 174 175 public void initialize(TemplateWizard wiz) { 180 this.wiz = wiz; 181 index = 0; 182 Project project = Templates.getProject( wiz ); 183 panels = createPanels(project); 184 185 Object prop = wiz.getProperty("WizardPanel_contentData"); String [] beforeSteps = null; 188 if (prop != null && prop instanceof String []) { 189 beforeSteps = (String [])prop; 190 } 191 String [] steps = Utilities.createSteps(beforeSteps, panels); 192 193 for (int i = 0; i < panels.length; i++) { 194 Component c = panels[i].getComponent(); 195 if (steps[i] == null) { 196 steps[i] = c.getName(); 200 } 201 if (c instanceof JComponent ) { JComponent jc = (JComponent ) c; 203 jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer (i)); jc.putClientProperty("WizardPanel_contentData", steps); } 208 } 209 } 210 public void uninitialize(TemplateWizard wiz) { 211 this.wiz = null; 212 panels = null; 213 } 214 215 220 public String name() { 221 return NbBundle.getMessage(WsdlIterator.class, "TITLE_x_of_y", 222 new Integer (index + 1), new Integer (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 (); 233 index++; 234 } 235 public void previousPanel() { 236 if (! hasPrevious()) throw new NoSuchElementException (); 237 index--; 238 } 239 public WizardDescriptor.Panel current() { 240 return panels[index]; 241 } 242 243 public final void addChangeListener(ChangeListener l) {} 245 public final void removeChangeListener(ChangeListener l) {} 246 } 251 | Popular Tags |