1 19 20 package org.netbeans.modules.websvc.wsdl.wizard; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import org.openide.ErrorManager; 26 import org.openide.util.NbBundle; 27 import org.xml.sax.*; 28 29 33 public class WsdlUIPanel extends javax.swing.JPanel { 34 35 private static final String TARGET_URL_PREFIX = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultTNS"); private static final String DEFAULT_SERVICE_NAME = NbBundle.getMessage(WsdlUIPanel.class,"TXT_defaultServiceName"); private WsdlPanel wizardPanel; 38 private javax.swing.JTextField fileNameTF; 39 40 41 public WsdlUIPanel(WsdlPanel wizardPanel) { 42 initComponents(); 43 this.wizardPanel=wizardPanel; 44 nsTF.setText(TARGET_URL_PREFIX+DEFAULT_SERVICE_NAME); 45 } 46 47 void attachFileNameListener(javax.swing.JTextField fileNameTF) { 48 this.fileNameTF=fileNameTF; 49 if (fileNameTF!=null) { 50 nsTF.setText(TARGET_URL_PREFIX+fileNameTF.getText()); 51 DocListener list = new DocListener(); 52 javax.swing.text.Document doc = fileNameTF.getDocument(); 53 doc.addDocumentListener(list); 54 } else { 55 nsTF.setText(TARGET_URL_PREFIX+DEFAULT_SERVICE_NAME); 56 } 57 } 58 59 64 private void initComponents() { 66 java.awt.GridBagConstraints gridBagConstraints; 67 68 cbImport = new javax.swing.JCheckBox (); 69 schemaTF = new javax.swing.JTextField (); 70 browseButton = new javax.swing.JButton (); 71 schemaLB = new javax.swing.JLabel (); 72 namespaceLB = new javax.swing.JLabel (); 73 nsTF = new javax.swing.JTextField (); 74 jPanel1 = new javax.swing.JPanel (); 75 76 setLayout(new java.awt.GridBagLayout ()); 77 78 cbImport.setMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_importSchema").charAt(0)); 79 cbImport.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema")); 80 cbImport.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); 81 cbImport.setMargin(new java.awt.Insets (0, 0, 0, 0)); 82 cbImport.addItemListener(new java.awt.event.ItemListener () { 83 public void itemStateChanged(java.awt.event.ItemEvent evt) { 84 cbImportItemStateChanged(evt); 85 } 86 }); 87 88 gridBagConstraints = new java.awt.GridBagConstraints (); 89 gridBagConstraints.gridx = 0; 90 gridBagConstraints.gridy = 2; 91 gridBagConstraints.gridwidth = 3; 92 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 93 gridBagConstraints.insets = new java.awt.Insets (5, 0, 6, 5); 94 add(cbImport, gridBagConstraints); 95 cbImport.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_importSchema")); 96 97 schemaTF.setEditable(false); 98 schemaTF.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/websvc/wsdl/wizard/Bundle").getString("HINT_schemaFiles")); 99 gridBagConstraints = new java.awt.GridBagConstraints (); 100 gridBagConstraints.gridx = 1; 101 gridBagConstraints.gridy = 3; 102 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 103 gridBagConstraints.weightx = 1.0; 104 gridBagConstraints.insets = new java.awt.Insets (0, 5, 2, 0); 105 add(schemaTF, gridBagConstraints); 106 schemaTF.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "A11Y_schemaTF")); 107 108 browseButton.setMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_browse").charAt(0)); 109 browseButton.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_browse")); 110 browseButton.setEnabled(false); 111 browseButton.addActionListener(new java.awt.event.ActionListener () { 112 public void actionPerformed(java.awt.event.ActionEvent evt) { 113 browseButtonActionPerformed(evt); 114 } 115 }); 116 117 gridBagConstraints = new java.awt.GridBagConstraints (); 118 gridBagConstraints.gridx = 2; 119 gridBagConstraints.gridy = 3; 120 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 121 gridBagConstraints.insets = new java.awt.Insets (0, 5, 2, 0); 122 add(browseButton, gridBagConstraints); 123 browseButton.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "A11Y_browse")); 124 125 schemaLB.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_schemaFiles").charAt(0)); 126 schemaLB.setLabelFor(schemaTF); 127 schemaLB.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_schemaFiles")); 128 gridBagConstraints = new java.awt.GridBagConstraints (); 129 gridBagConstraints.gridx = 0; 130 gridBagConstraints.gridy = 3; 131 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 132 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 133 add(schemaLB, gridBagConstraints); 134 135 namespaceLB.setDisplayedMnemonic(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "MNEM_targetNamespace").charAt(0)); 136 namespaceLB.setLabelFor(nsTF); 137 namespaceLB.setText(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "LBL_targetNamespace")); 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.gridx = 0; 140 gridBagConstraints.gridy = 0; 141 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 142 gridBagConstraints.insets = new java.awt.Insets (5, 0, 2, 0); 143 add(namespaceLB, gridBagConstraints); 144 145 nsTF.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/websvc/wsdl/wizard/Bundle").getString("HINT_targetNamespace")); 146 gridBagConstraints = new java.awt.GridBagConstraints (); 147 gridBagConstraints.gridx = 1; 148 gridBagConstraints.gridy = 0; 149 gridBagConstraints.gridwidth = 2; 150 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 151 gridBagConstraints.weightx = 1.0; 152 gridBagConstraints.insets = new java.awt.Insets (5, 5, 2, 0); 153 add(nsTF, gridBagConstraints); 154 nsTF.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(WsdlUIPanel.class, "HINT_targetNamespace")); 155 156 gridBagConstraints = new java.awt.GridBagConstraints (); 157 gridBagConstraints.gridx = 0; 158 gridBagConstraints.gridy = 5; 159 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 160 gridBagConstraints.weighty = 1.0; 161 add(jPanel1, gridBagConstraints); 162 163 } 165 private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) { String dialogTitle = NbBundle.getMessage(WsdlUIPanel.class,"TITLE_selectSchema"); 168 String maskTitle = NbBundle.getMessage(WsdlUIPanel.class,"TXT_schemaFiles"); 169 java.io.File [] files = Utilities.selectFiles("xsd XSD", dialogTitle, maskTitle, wizardPanel.getProject()); 170 if(files == null || files.length ==0) return; 171 String original = schemaTF.getText().trim(); 172 StringBuilder fileString = new StringBuilder (original); 173 for(int i = 0; i < files.length; i++) { 174 java.io.File f = files[i]; 175 String location = f.toURI().normalize().toString(); 176 if (fileString.indexOf(location)>=0) continue; 177 if(fileString.length() > 0){ 178 fileString.append(","); 179 } 180 fileString.append(location); 181 } 182 schemaTF.setText(fileString.toString()); 183 } 185 private void cbImportItemStateChanged(java.awt.event.ItemEvent evt) { if (cbImport.isSelected()) { 188 schemaTF.setEditable(true); 189 browseButton.setEnabled(true); 190 } else { 191 schemaTF.setEditable(false); 192 browseButton.setEnabled(false); 193 } 194 } 196 197 private javax.swing.JButton browseButton; 199 private javax.swing.JCheckBox cbImport; 200 private javax.swing.JPanel jPanel1; 201 private javax.swing.JLabel namespaceLB; 202 private javax.swing.JTextField nsTF; 203 private javax.swing.JLabel schemaLB; 204 private javax.swing.JTextField schemaTF; 205 207 private class DocListener implements javax.swing.event.DocumentListener { 208 209 public void removeUpdate(javax.swing.event.DocumentEvent e) { 210 documentChanged(e); 211 } 212 213 public void insertUpdate(javax.swing.event.DocumentEvent e) { 214 documentChanged(e); 215 } 216 217 public void changedUpdate(javax.swing.event.DocumentEvent e) { 218 documentChanged(e); 219 } 220 221 private void documentChanged(javax.swing.event.DocumentEvent e) { 222 String ns = nsTF.getText(); 223 int index = ns.lastIndexOf('/'); 224 if (index>0) { 225 boolean hasExt = ns.endsWith(".wsdl"); nsTF.setText(ns.substring(0,index+1)+fileNameTF.getText().trim()+(hasExt?".wsdl":"")); } 228 } 229 } 230 231 233 static class SchemaInfo { 234 private java.net.URL url; 235 SchemaInfo(java.net.URL url) { 236 this.url=url; 237 } 238 239 java.net.URL getURL() { 240 return url; 241 } 242 243 String getNamespace() { 244 InputSource is = new InputSource(url.toExternalForm()); 245 try { 246 return parse(is); 247 } catch (java.io.IOException ex){ 248 } catch (SAXException ex){} 249 return ""; 250 } 251 252 String getSchemaName() { 253 return url.toExternalForm(); 254 } 255 256 258 private String parse(InputSource is) throws java.io.IOException , SAXException { 259 XMLReader xmlReader = org.openide.xml.XMLUtil.createXMLReader(); 260 NsHandler handler = new NsHandler(); 261 xmlReader.setContentHandler(handler); 262 try { 264 xmlReader.parse(is); 265 } catch (SAXException ex) { 266 if (!"EXIT".equals(ex.getMessage())) throw ex; } 268 String ns = handler.getNs(); 269 if (ns==null) return ""; 270 else return ns; 271 } 272 273 private static class NsHandler extends org.xml.sax.helpers.DefaultHandler { 274 private String ns; 275 276 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 277 if (qName.endsWith("schema")) { ns=attributes.getValue("targetNamespace"); throw new SAXException("EXIT"); } 281 } 282 283 String getNs() { 284 return ns; 285 } 286 } 287 288 } 289 290 String getNS() { 291 return nsTF.getText(); 292 } 293 294 String getWsName() { 295 if (fileNameTF!=null) 296 return fileNameTF.getText(); 297 else 298 return DEFAULT_SERVICE_NAME; 299 } 300 301 boolean isImport() { 302 return cbImport.isSelected(); 303 } 304 305 SchemaInfo[] getSchemas() { 306 if (cbImport.isSelected()) { 307 String schemas = schemaTF.getText(); 308 String [] urls = schemas.split(","); 309 List <SchemaInfo> infos = new ArrayList <SchemaInfo>(); 310 for (int i=0;i<urls.length;i++) { 311 String urlString=urls[i].trim(); 312 if (urlString.length()==0) continue; 313 try { 314 java.net.URL url = new java.net.URL (urlString); 315 infos.add(new SchemaInfo(url)); 316 } catch (java.net.MalformedURLException ex) { 317 try { 319 org.openide.loaders.DataFolder folder = wizardPanel.getTemplateWizard().getTargetFolder(); 320 org.openide.filesystems.FileObject fo = folder.getPrimaryFile(); 321 if ((fo.getFileObject(urlString))!=null) { 322 String parentURL = fo.getURL().toExternalForm(); 323 infos.add(new SchemaInfo(new java.net.URL (parentURL+urlString))); 324 } 325 } catch (java.io.IOException ex1) {} 326 } 327 } 328 SchemaInfo[] result = new SchemaInfo[infos.size()]; 329 infos.<SchemaInfo>toArray(result); 330 return result; 331 } 332 return new SchemaInfo[]{}; 333 } 334 } 335 | Popular Tags |