1 19 20 package org.netbeans.modules.xml.wsdl.ui.wizard; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.File ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import javax.swing.JTextField ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import javax.swing.event.DocumentEvent ; 35 import javax.swing.event.DocumentListener ; 36 import javax.swing.text.BadLocationException ; 37 import javax.swing.text.Document ; 38 import org.netbeans.api.project.Project; 39 import org.netbeans.api.project.SourceGroup; 40 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 41 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory; 42 import org.netbeans.modules.xml.xam.ModelSource; 43 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 44 import org.netbeans.spi.project.ui.templates.support.Templates; 45 import org.openide.ErrorManager; 46 import org.openide.WizardDescriptor; 47 import org.openide.WizardValidationException; 48 import org.openide.cookies.EditorCookie; 49 import org.openide.filesystems.FileObject; 50 import org.openide.filesystems.FileUtil; 51 import org.openide.loaders.DataObject; 52 import org.openide.loaders.DataObjectNotFoundException; 53 import org.openide.loaders.TemplateWizard; 54 import org.openide.util.HelpCtx; 55 56 60 final class WsdlPanel implements WizardDescriptor.FinishablePanel { 61 62 public static final String FILE_NAME = "FILE_NAME"; 63 64 public static final String WSDL_TARGETNAMESPACE = "WSDL_TARGETNAMESPACE"; 65 66 public static final String WSDL_DEFINITION_NAME = "WSDL_DEFINITION_NAME"; 67 68 69 private static final String DEFAULT_TARGET_NAMESPACE = "urn:WS/wsdl"; 71 private final List <ChangeListener > listeners = new ArrayList (); 72 private WsdlUIPanel gui; 73 74 private Project project; 75 private TemplateWizard templateWizard; 76 77 private JTextField fileNameTextField; 78 79 private File tempWSDLFile = null; 80 81 private WSDLModel mTempWSDLModel = null; 82 83 private String mErrorMessage; 84 85 private TextChangeListener mListener = new TextChangeListener(); 86 87 WsdlPanel(Project project, SourceGroup[] folders) { 88 this.project = project; 89 } 90 91 TemplateWizard getTemplateWizard() { 92 return templateWizard; 93 } 94 95 void setNameTF(JTextField nameTF) { 96 gui.attachFileNameListener(nameTF); 97 if(nameTF != null) { 98 nameTF.getDocument().removeDocumentListener(mListener); nameTF.getDocument().addDocumentListener(mListener); 100 fileNameTextField = nameTF; 101 } 102 } 103 104 public Component getComponent() { 105 if (gui == null) { 106 gui = new WsdlUIPanel(this); 107 gui.getSchemaFileTextField().addPropertyChangeListener(new SchemaImportTextChangeListener()); 108 gui.getSchemaFileTextField().getDocument().addDocumentListener(new SchemaImportTextChangeListener()); 109 } 111 return gui; 112 } 113 114 public Project getProject(){ 115 return project; 116 } 117 118 public HelpCtx getHelp() { 119 return new HelpCtx(WsdlPanel.class); 120 } 121 122 123 124 public boolean isValid() { 125 if(templateWizard != null) { 126 templateWizard.putProperty ("WizardPanel_errorMessage", this.mErrorMessage); } 128 return this.mErrorMessage == null; 129 130 } 131 132 133 public void addChangeListener(ChangeListener l) { 134 listeners.add(l); 135 } 136 137 public void removeChangeListener(ChangeListener l) { 138 listeners.remove(l); 139 } 140 141 protected void fireChange() { 142 ChangeEvent e = new ChangeEvent (this); 143 Iterator it = listeners.iterator(); 144 while (it.hasNext()) { 145 ((ChangeListener )it.next()).stateChanged(e); 146 } 147 } 148 149 public void readSettings( Object settings ) { 150 templateWizard = (TemplateWizard)settings; 151 152 153 templateWizard.putProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, null); 155 templateWizard.putProperty(WizardPortTypeConfigurationStep.TEMP_WSDLFILE, null); 156 157 } 158 159 public void storeSettings(Object settings) { 160 TemplateWizard wiz = (TemplateWizard) settings; 161 162 if (WizardDescriptor.PREVIOUS_OPTION.equals(((WizardDescriptor) settings).getValue())) { 163 return; 164 } 165 if (WizardDescriptor.CANCEL_OPTION.equals(((WizardDescriptor) settings).getValue())) { 166 return; 167 } 168 169 String fileName = Templates.getTargetName(wiz); 170 wiz.putProperty(FILE_NAME, fileName); 171 String targetNamespace = getNS(); 172 wiz.putProperty(WSDL_TARGETNAMESPACE, targetNamespace); 173 String definitionName = fileName; 174 wiz.putProperty(WSDL_DEFINITION_NAME, definitionName); 175 try { 176 if (tempWSDLFile == null) { 177 tempWSDLFile = File.createTempFile(fileName + "RIT", ".wsdl"); tempWSDLFile.deleteOnExit(); 180 populateFileFromTemplate(tempWSDLFile); 181 templateWizard.putProperty( 182 WizardPortTypeConfigurationStep.TEMP_WSDLFILE, tempWSDLFile); 183 mTempWSDLModel = prepareModelFromFile(tempWSDLFile); 184 templateWizard.putProperty( 185 WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, mTempWSDLModel); 186 } else { 187 templateWizard.putProperty( 188 WizardPortTypeConfigurationStep.TEMP_WSDLMODEL, mTempWSDLModel); 189 templateWizard.putProperty( 190 WizardPortTypeConfigurationStep.TEMP_WSDLFILE, tempWSDLFile); 191 mTempWSDLModel.startTransaction(); 192 mTempWSDLModel.getDefinitions().setTargetNamespace(targetNamespace); 193 mTempWSDLModel.getDefinitions().setName(definitionName); 194 if (mTempWSDLModel.getDefinitions().getTypes() == null) { 195 mTempWSDLModel.getDefinitions().setTypes( 196 mTempWSDLModel.getFactory().createTypes()); 197 } 198 mTempWSDLModel.endTransaction(); 199 } 200 } catch (Exception e) { 201 ErrorManager.getDefault().notify(e); 202 } 203 } 204 205 215 void populateFileFromTemplate(File file) throws 216 DataObjectNotFoundException, IOException { 217 if (templateWizard == null) { 218 throw new IOException ("templateWizard not defined"); 219 } 220 FileObject template = Templates.getTemplate(templateWizard); 221 DataObject dTemplate = DataObject.find(template); 222 if (dTemplate != null) { 223 EditorCookie editorCookie = (EditorCookie) DataObject.find( 224 dTemplate.getPrimaryFile()).getCookie(EditorCookie.class); 225 editorCookie.openDocument(); 226 Document doc = editorCookie.getDocument(); 227 FileWriter writer = new FileWriter (file); 228 try { 229 writer.write(doc.getText(0, doc.getLength())); 230 } catch (BadLocationException ble) { 231 ErrorManager.getDefault().notify(ble); 233 } 234 writer.close(); 235 } 236 } 237 238 247 WSDLModel prepareModelFromFile(File file) { 248 file = FileUtil.normalizeFile(file); 249 FileObject fobj = FileUtil.toFileObject(file); 250 String definitionName = fobj.getName(); 251 ModelSource modelSource = org.netbeans.modules.xml.retriever. 252 catalog.Utilities.getModelSource(fobj, fobj.canWrite()); 253 WSDLModel model = WSDLModelFactory.getDefault().getModel(modelSource); 254 model.startTransaction(); 255 model.getDefinitions().setName(definitionName); 256 String ns = getNS(); 257 model.getDefinitions().setTargetNamespace(ns); 258 ((AbstractDocumentComponent) model.getDefinitions()).addPrefix("tns", ns); 259 if (model.getDefinitions().getTypes() == null) { 260 model.getDefinitions().setTypes(model.getFactory().createTypes()); 261 } 262 model.endTransaction(); 263 return model; 264 } 265 266 String getNS() { 267 String targetNamespace = gui.getNS(); 268 if (targetNamespace.length() == 0) { 269 targetNamespace = DEFAULT_TARGET_NAMESPACE; 270 } 271 return targetNamespace; 272 } 273 274 WsdlUIPanel.SchemaInfo[] getSchemas() { 275 return gui.getSchemas(); 276 } 277 278 boolean isImport() { 279 return gui.isImport(); 280 } 281 282 public boolean isFinishPanel() { 283 return isValid(); 284 } 285 286 private void fireChangeEvent() { 287 Iterator <ChangeListener > it = this.listeners.iterator(); 288 ChangeEvent e = new ChangeEvent (this); 289 while(it.hasNext()) { 290 ChangeListener l = it.next(); 291 l.stateChanged(e); 292 } 293 } 294 295 private boolean isValidName(Document doc) { 296 try { 297 String text = doc.getText(0, doc.getLength()); 298 boolean isValid = org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(text); 299 if(!isValid) { 300 mErrorMessage = "Name \"" + text + "\" is not a valid NCName"; 301 } else { 302 mErrorMessage = null; 303 } 304 305 } catch(Exception ex) { 306 ex.printStackTrace(); 307 } 308 309 return mErrorMessage == null; 310 } 311 312 313 314 private void validateFileName() { 315 boolean validFileName = isValidName(this.fileNameTextField.getDocument()); 316 if(!validFileName) { 317 fireChangeEvent(); 318 return; 319 } 320 } 321 322 323 class TextChangeListener implements DocumentListener { 324 325 public void changedUpdate(DocumentEvent e) { 326 validateFileName(); 327 } 328 329 public void insertUpdate(DocumentEvent e) { 330 validateFileName(); 331 } 332 333 public void removeUpdate(DocumentEvent e) { 334 validateFileName(); 335 } 336 337 } 338 339 class SchemaImportTextChangeListener implements PropertyChangeListener , DocumentListener { 340 341 public void propertyChange(PropertyChangeEvent evt) { 342 if (evt.getPropertyName().equals("VALUE_SET")) { 343 validateSchemas(); 344 } 345 } 346 347 public void insertUpdate(DocumentEvent e) { 348 validateSchemas(); 349 } 350 351 public void removeUpdate(DocumentEvent e) { 352 validateSchemas(); 353 } 354 355 public void changedUpdate(DocumentEvent e) { 356 validateSchemas(); 357 } 358 359 } 360 361 public void validateSchemas() { 362 try { 363 gui.validateSchemas(); 364 } catch (WizardValidationException e) { 365 mErrorMessage = e.getLocalizedMessage(); 366 fireChangeEvent(); 367 return; 368 } 369 mErrorMessage = null; 370 fireChangeEvent(); 371 } 372 373 374 375 376 } 377 | Popular Tags |