1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui.wizard; 30 31 import java.awt.Component ; 32 import java.io.File ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 import java.io.StringWriter ; 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 42 43 import javax.swing.event.ChangeEvent ; 44 import javax.swing.event.ChangeListener ; 45 import javax.swing.event.DocumentEvent ; 46 import javax.swing.event.DocumentListener ; 47 import javax.swing.text.Document ; 48 49 import org.netbeans.api.project.Project; 50 import org.netbeans.modules.xml.schema.model.Import; 51 import org.netbeans.modules.xml.schema.model.Schema; 52 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement; 53 import org.netbeans.modules.xml.wsdl.model.Message; 54 import org.netbeans.modules.xml.wsdl.model.PortType; 55 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 56 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory; 57 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema; 58 import org.netbeans.modules.xml.wsdl.ui.view.OperationType; 59 import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel; 60 import org.netbeans.modules.xml.wsdl.ui.view.PortTypeConfigurationPanel; 61 import org.netbeans.modules.xml.xam.ModelSource; 62 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 63 import org.openide.WizardDescriptor; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileUtil; 66 import org.openide.loaders.TemplateWizard; 67 import org.openide.util.HelpCtx; 68 import org.openide.util.NbBundle; 69 import org.w3c.dom.Comment ; 70 import org.w3c.dom.Element ; 71 72 76 public class WizardPortTypeConfigurationStep implements WizardDescriptor.FinishablePanel { 77 78 public static final String PORTTYPE_NAME = "PORTTYPE_NAME"; 79 80 public static final String OPERATION_NAME = "OPERATION_NAME"; 81 82 public static final String OPERATION_TYPE = "OPERATION_TYPE"; 83 84 public static final String OPERATION_INPUT = "OPERATION_INPUT"; 85 86 public static final String OPERATION_OUTPUT = "OPERATION_OUTPUT"; 87 88 public static final String OPERATION_FAULT = "OPERATION_FAULT"; 89 90 public static final String OPERATION_INPUT_MESSAGE = "OPERATION_INPUT_MESSAGE"; 91 92 public static final String OPERATION_OUTPUT_MESSAGE = "OPERATION_OUTPUT_MESSAGE"; 93 94 public static final String OPERATION_FAULT_MESSAGE = "OPERATION_FAULT_MESSAGE"; 95 96 97 public static final String NAMESPACE_TO_PREFIX_MAP = "NAMESPACE_TO_PREFIX_MAP"; 98 99 public static final String TEMP_WSDLMODEL = "TEMP_WSDLMODEL"; 100 101 public static final String TEMP_WSDLFILE = "TEMP_WSDLFILE"; 102 103 public static final String PORTTYPE = "PORTTYPE"; 104 105 public static final String IS_FROM_WIZARD = "IS_FROM_WIZARD"; 106 107 108 private PortTypeConfigurationPanel mPanel; 109 110 private Project project; 111 112 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 113 114 private String mErrorMessage; 115 116 private WizardDescriptor wiz = null; 117 118 private WSDLModel mTempModel; 119 120 private PortType mPortType; 121 122 private List <Message> mNewMessageList = null; 123 124 private ExtensibilityElement mPartnerLinkTypeElement = null; 125 126 private Collection <Import> mImports = null; 127 128 private File tempWSDLFile = null; 129 130 131 public WizardPortTypeConfigurationStep(Project project) { 132 this.project = project; 133 134 } 135 136 public void addChangeListener(ChangeListener l) { 137 listeners.add(l); 138 } 139 140 public void removeChangeListener(ChangeListener l) { 141 listeners.remove(l); 142 } 143 144 public Component getComponent() { 145 if (mPanel == null) { 146 this.mPanel = new PortTypeConfigurationPanel(project); 147 this.mPanel.setName(NbBundle.getMessage(WizardPortTypeConfigurationStep.class, "LBL_WizardPortTypeConfigurationStep")); 149 TextChangeListener listener = new TextChangeListener(); 150 151 this.mPanel.getPortTypeNameTextField().getDocument().addDocumentListener(listener); 152 this.mPanel.getOperationNameTextField().getDocument().addDocumentListener(listener); 153 } 154 return this.mPanel; 155 } 156 157 public HelpCtx getHelp() { 158 return new HelpCtx(WizardPortTypeConfigurationStep.class); 159 } 160 161 public boolean isValid() { 162 wiz.putProperty ("WizardPanel_errorMessage", this.mErrorMessage); return this.mErrorMessage == null; 164 165 } 166 167 168 public void readSettings(Object settings) { 169 TemplateWizard templateWizard = (TemplateWizard)settings; 170 this.wiz = templateWizard; 171 String fileName = (String ) templateWizard.getProperty(WsdlPanel.FILE_NAME); 172 if(this.mPanel.getPortTypeName() == null || this.mPanel.getPortTypeName().trim().equals("")) { 173 this.mPanel.setPortTypeName(fileName + "PortType"); } 175 176 if(this.mPanel.getOperationName() == null || this.mPanel.getOperationName().trim().equals("")) { 177 this.mPanel.setOperationName(fileName + "Operation"); } 179 180 181 } 182 183 public void storeSettings(Object settings) { 184 TemplateWizard templateWizard = (TemplateWizard)settings; 185 if(templateWizard.getValue() == TemplateWizard.CANCEL_OPTION) { 186 return; 187 } 188 189 this.mTempModel = (WSDLModel) templateWizard.getProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL); 190 if(this.mTempModel != null) { 191 this.mTempModel.startTransaction(); 192 if(this.mPortType != null) { 193 this.mTempModel.getDefinitions().removePortType(this.mPortType); 194 } 195 196 if(this.mNewMessageList != null) { 197 for (Message msg : mNewMessageList) { 198 this.mTempModel.getDefinitions().removeMessage(msg); 199 } 200 } 201 202 if(this.mPartnerLinkTypeElement != null) { 203 this.mTempModel.getDefinitions().removeExtensibilityElement(this.mPartnerLinkTypeElement); 204 } 205 206 if(this.mImports != null) { 207 Collection <WSDLSchema> wSchemas = mTempModel.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class); 209 for (WSDLSchema wSchema : wSchemas) { 210 Schema schema = wSchema.getSchemaModel().getSchema(); 211 if (schema.getTargetNamespace().equals(mTempModel.getDefinitions().getTargetNamespace())) { 214 for (Import imp : mImports) { 215 schema.removeExternalReference(imp); 216 } 217 } 218 mTempModel.getDefinitions().getTypes().removeExtensibilityElement(wSchema); 219 } 220 } 221 222 223 mPortType = null; 224 mNewMessageList = null; 225 mPartnerLinkTypeElement = null; 226 mImports = null; 227 228 if (templateWizard.getValue() == TemplateWizard.PREVIOUS_OPTION) { 229 this.mTempModel.endTransaction(); 231 return; 232 } 233 234 String portTypeName = this.mPanel.getPortTypeName(); 235 String operationName = this.mPanel.getOperationName(); 236 OperationType ot = this.mPanel.getOperationType(); 237 238 239 240 List <PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = this.mPanel.getInputMessageParts(); 242 List <PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = this.mPanel.getOutputMessageParts(); 243 List <PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = this.mPanel.getFaultMessageParts(); 244 245 templateWizard.putProperty(OPERATION_INPUT, inputMessageParts); 246 templateWizard.putProperty(OPERATION_OUTPUT, outputMessageParts); 247 templateWizard.putProperty(OPERATION_FAULT, faultMessageParts); 248 Map <String , String > namespaceToPrefixMap = mPanel.getNamespaceToPrefixMap(); 249 templateWizard.putProperty(NAMESPACE_TO_PREFIX_MAP, namespaceToPrefixMap); 250 251 Map configurationMap = new HashMap (); 252 configurationMap.put(WizardPortTypeConfigurationStep.PORTTYPE_NAME, portTypeName); 254 configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_NAME, operationName); 255 configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_TYPE, ot); 256 257 configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_INPUT, inputMessageParts); 259 configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_OUTPUT, outputMessageParts); 260 configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_FAULT, faultMessageParts); 261 configurationMap.put(WizardPortTypeConfigurationStep.NAMESPACE_TO_PREFIX_MAP, namespaceToPrefixMap); 262 configurationMap.put(WizardPortTypeConfigurationStep.IS_FROM_WIZARD, Boolean.TRUE); 263 264 templateWizard.putProperty(PORTTYPE_NAME, portTypeName); 265 templateWizard.putProperty(OPERATION_NAME, operationName); 266 templateWizard.putProperty(OPERATION_TYPE, ot); 267 268 PortTypeGenerator ptGen = new PortTypeGenerator(this.mTempModel, configurationMap); 269 ptGen.execute(); 270 this.mPortType = ptGen.getPortType(); 271 this.mNewMessageList = ptGen.getNewMessages(); 272 this.mPartnerLinkTypeElement = ptGen.getPartnerLinkType(); 273 this.mImports = ptGen.getImports(); 274 275 this.mTempModel.endTransaction(); 276 277 templateWizard.putProperty(PORTTYPE, this.mPortType); 278 } 279 280 } 281 282 283 private boolean isValidName(Document doc) { 284 try { 285 String text = doc.getText(0, doc.getLength()); 286 boolean isValid = org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(text); 287 if(!isValid) { 288 mErrorMessage = "Name \"" + text + "\" is not a valid NCName"; 289 } else { 290 mErrorMessage = null; 291 } 292 293 } catch(Exception ex) { 294 ex.printStackTrace(); 295 } 296 297 return mErrorMessage == null; 298 } 299 300 private void validate() { 301 boolean validPortType = isValidName(this.mPanel.getPortTypeNameTextField().getDocument()); 302 if(!validPortType) { 303 fireChangeEvent(); 304 return; 305 } 306 307 boolean validOperation = isValidName(this.mPanel.getOperationNameTextField().getDocument()); 308 309 if(!validOperation) { 310 fireChangeEvent(); 311 return; 312 } 313 314 fireChangeEvent(); 315 } 316 317 private void fireChangeEvent() { 318 Iterator <ChangeListener > it = this.listeners.iterator(); 319 ChangeEvent e = new ChangeEvent (this); 320 while(it.hasNext()) { 321 ChangeListener l = it.next(); 322 l.stateChanged(e); 323 } 324 } 325 326 public boolean isFinishPanel() { 327 return true; 328 } 329 class TextChangeListener implements DocumentListener { 330 331 public void changedUpdate(DocumentEvent e) { 332 validate(); 333 } 334 335 public void insertUpdate(DocumentEvent e) { 336 validate(); 337 } 338 339 public void removeUpdate(DocumentEvent e) { 340 validate(); 341 } 342 343 } 344 } 345 | Popular Tags |