KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > wizard > WizardPortTypeConfigurationStep


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * WizardPortTypeConfigurationStep.java
22  *
23  * Created on August 31, 2006, 3:02 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui.wizard;
30
31 import java.awt.Component JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.StringWriter JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Map JavaDoc;
42
43 import javax.swing.event.ChangeEvent JavaDoc;
44 import javax.swing.event.ChangeListener JavaDoc;
45 import javax.swing.event.DocumentEvent JavaDoc;
46 import javax.swing.event.DocumentListener JavaDoc;
47 import javax.swing.text.Document JavaDoc;
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 JavaDoc;
70 import org.w3c.dom.Element JavaDoc;
71
72 /**
73  *
74  * @author radval
75  */

76 public class WizardPortTypeConfigurationStep implements WizardDescriptor.FinishablePanel {
77     
78     public static final String JavaDoc PORTTYPE_NAME = "PORTTYPE_NAME";
79     
80     public static final String JavaDoc OPERATION_NAME = "OPERATION_NAME";
81     
82     public static final String JavaDoc OPERATION_TYPE = "OPERATION_TYPE";
83    
84     public static final String JavaDoc OPERATION_INPUT = "OPERATION_INPUT";
85     
86     public static final String JavaDoc OPERATION_OUTPUT = "OPERATION_OUTPUT";
87     
88     public static final String JavaDoc OPERATION_FAULT = "OPERATION_FAULT";
89     
90     public static final String JavaDoc OPERATION_INPUT_MESSAGE = "OPERATION_INPUT_MESSAGE";
91
92     public static final String JavaDoc OPERATION_OUTPUT_MESSAGE = "OPERATION_OUTPUT_MESSAGE";
93     
94     public static final String JavaDoc OPERATION_FAULT_MESSAGE = "OPERATION_FAULT_MESSAGE";
95
96     
97     public static final String JavaDoc NAMESPACE_TO_PREFIX_MAP = "NAMESPACE_TO_PREFIX_MAP";
98     
99     public static final String JavaDoc TEMP_WSDLMODEL = "TEMP_WSDLMODEL";
100     
101     public static final String JavaDoc TEMP_WSDLFILE = "TEMP_WSDLFILE";
102     
103     public static final String JavaDoc PORTTYPE = "PORTTYPE";
104
105     public static final String JavaDoc IS_FROM_WIZARD = "IS_FROM_WIZARD";
106     
107     
108     private PortTypeConfigurationPanel mPanel;
109     
110     private Project project;
111     
112     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
113     
114     private String JavaDoc mErrorMessage;
115     
116     private WizardDescriptor wiz = null;
117     
118     private WSDLModel mTempModel;
119     
120     private PortType mPortType;
121     
122     private List JavaDoc<Message> mNewMessageList = null;
123     
124     private ExtensibilityElement mPartnerLinkTypeElement = null;
125     
126     private Collection JavaDoc<Import> mImports = null;
127     
128     private File JavaDoc tempWSDLFile = null;
129     
130     /** Creates a new instance of WizardPortTypeConfigurationStep */
131     public WizardPortTypeConfigurationStep(Project project) {
132         this.project = project;
133
134     }
135
136     public void addChangeListener(ChangeListener JavaDoc l) {
137         listeners.add(l);
138     }
139
140     public void removeChangeListener(ChangeListener JavaDoc l) {
141         listeners.remove(l);
142     }
143
144     public Component JavaDoc getComponent() {
145         if (mPanel == null) {
146             this.mPanel = new PortTypeConfigurationPanel(project);
147 // this.mPanel.setPreferredSize(new Dimension(450, 400));
148
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); // NOI18N
163
return this.mErrorMessage == null;
164         
165     }
166
167     
168     public void readSettings(Object JavaDoc settings) {
169         TemplateWizard templateWizard = (TemplateWizard)settings;
170         this.wiz = templateWizard;
171         String JavaDoc fileName = (String JavaDoc) templateWizard.getProperty(WsdlPanel.FILE_NAME);
172         if(this.mPanel.getPortTypeName() == null || this.mPanel.getPortTypeName().trim().equals("")) {
173             this.mPanel.setPortTypeName(fileName + "PortType"); //NOI18N
174
}
175         
176         if(this.mPanel.getOperationName() == null || this.mPanel.getOperationName().trim().equals("")) {
177             this.mPanel.setOperationName(fileName + "Operation"); //NOI18N
178
}
179             
180         
181     }
182
183     public void storeSettings(Object JavaDoc 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                 //Cleanup all inline schemas and remove the imported schemas from the inline schema.
208
Collection JavaDoc<WSDLSchema> wSchemas = mTempModel.getDefinitions().getTypes().getExtensibilityElements(WSDLSchema.class);
209                 for (WSDLSchema wSchema : wSchemas) {
210                     Schema schema = wSchema.getSchemaModel().getSchema();
211                     //Wizard adds all imported schemas in a inline schema with same TNS as the definitions.
212
//So remove from that schema.
213
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                 //commit the cleanup.
230
this.mTempModel.endTransaction();
231                 return;
232             }
233             
234             String JavaDoc portTypeName = this.mPanel.getPortTypeName();
235             String JavaDoc operationName = this.mPanel.getOperationName();
236             OperationType ot = this.mPanel.getOperationType();
237
238
239
240             //operation input/output/fault
241
List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = this.mPanel.getInputMessageParts();
242             List JavaDoc<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = this.mPanel.getOutputMessageParts();
243             List JavaDoc<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 JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap = mPanel.getNamespaceToPrefixMap();
249             templateWizard.putProperty(NAMESPACE_TO_PREFIX_MAP, namespaceToPrefixMap);
250
251             Map JavaDoc configurationMap = new HashMap JavaDoc();
252             //portType
253
configurationMap.put(WizardPortTypeConfigurationStep.PORTTYPE_NAME, portTypeName);
254             configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_NAME, operationName);
255             configurationMap.put(WizardPortTypeConfigurationStep.OPERATION_TYPE, ot);
256
257             //opertion type
258
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 JavaDoc doc) {
284         try {
285             String JavaDoc 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 JavaDoc 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 JavaDoc<ChangeListener JavaDoc> it = this.listeners.iterator();
319         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
320         while(it.hasNext()) {
321             ChangeListener JavaDoc l = it.next();
322             l.stateChanged(e);
323         }
324     }
325
326     public boolean isFinishPanel() {
327         return true;
328     }
329     class TextChangeListener implements DocumentListener JavaDoc {
330      
331          public void changedUpdate(DocumentEvent JavaDoc e) {
332             validate();
333          }
334          
335          public void insertUpdate(DocumentEvent JavaDoc e) {
336              validate();
337          }
338
339          public void removeUpdate(DocumentEvent JavaDoc e) {
340              validate();
341          }
342  
343     }
344 }
345
Popular Tags