KickJava   Java API By Example, From Geeks To Geeks.

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


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  * WizardBindingConfigurationStep.java
22  *
23  * Created on August 31, 2006, 3:24 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.awt.Dimension JavaDoc;
33 import java.beans.PropertyChangeEvent JavaDoc;
34 import java.beans.PropertyChangeListener JavaDoc;
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.ArrayList 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 import javax.swing.event.ChangeEvent JavaDoc;
43 import javax.swing.event.ChangeListener JavaDoc;
44 import javax.swing.event.DocumentEvent JavaDoc;
45 import javax.swing.event.DocumentListener JavaDoc;
46 import javax.swing.text.Document JavaDoc;
47 import org.netbeans.modules.xml.wsdl.model.Binding;
48 import org.netbeans.modules.xml.wsdl.model.Port;
49 import org.netbeans.modules.xml.wsdl.model.PortType;
50 import org.netbeans.modules.xml.wsdl.model.Service;
51 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
52 import org.netbeans.modules.xml.wsdl.model.WSDLModelFactory;
53 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementTemplateProvider;
54 import org.netbeans.modules.xml.wsdl.ui.spi.ValidationInfo;
55 import org.netbeans.modules.xml.wsdl.ui.view.BindingConfigurationPanel;
56 import org.netbeans.modules.xml.wsdl.ui.view.wizard.localized.LocalizedTemplate;
57 import org.netbeans.modules.xml.wsdl.ui.view.wizard.localized.LocalizedTemplateGroup;
58 import org.netbeans.modules.xml.xam.ModelSource;
59 import org.openide.WizardDescriptor;
60 import org.openide.filesystems.FileObject;
61 import org.openide.filesystems.FileUtil;
62 import org.openide.loaders.TemplateWizard;
63 import org.openide.util.HelpCtx;
64 import org.openide.util.NbBundle;
65 import org.openide.windows.IOProvider;
66
67 /**
68  *
69  * @author radval
70  */

71 public class WizardBindingConfigurationStep implements WizardDescriptor.FinishablePanel {
72     
73     public static final String JavaDoc BINDING_NAME = "BINDING_NAME";
74     
75     public static final String JavaDoc BINDING_TYPE = "BINDING_TYPE";
76     
77     public static final String JavaDoc BINDING_SUBTYPE = "BINDING_SUBTYPE";
78
79     public static final String JavaDoc SERVICE_NAME = "SERVICE_NAME";
80
81     public static final String JavaDoc SERVICEPORT_NAME = "SERVICEPORT_NAME";
82     
83     public static final String JavaDoc BINDING = "BINDING";
84
85     public static final String JavaDoc SERVICE = "SERVICE";
86     
87     public static final String JavaDoc PORT = "PORT";
88     
89     
90     
91     private BindingConfigurationPanel mPanel;
92     
93     private String JavaDoc mErrorMessage;
94     
95     private String JavaDoc mBindingSubTypeError;
96     
97     private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc();
98     
99     private WizardDescriptor wiz = null;
100    
101     private WSDLModel mTempModel;
102     
103     private PortType mPortType;
104     
105     private Binding mBinding;
106     
107     private Service mService;
108     
109     private Port mPort;
110     
111     /** Creates a new instance of WizardBindingConfigurationStep */
112     public WizardBindingConfigurationStep() {
113     }
114
115     public void addChangeListener(ChangeListener JavaDoc l) {
116         listeners.add(l);
117     }
118
119     public void removeChangeListener(ChangeListener JavaDoc l) {
120         listeners.remove(l);
121     }
122
123     public Component JavaDoc getComponent() {
124         if (mPanel == null) {
125             this.mPanel = new BindingConfigurationPanel();
126 // this.mPanel.setPreferredSize(new Dimension(450, 400));
127
this.mPanel.setName(NbBundle.getMessage(WizardBindingConfigurationStep.class, "LBL_WizardBindingConfigurationStep"));
128             TextChangeListener listener = new TextChangeListener();
129             this.mPanel.getBindingNameTextField().getDocument().addDocumentListener(listener);
130             this.mPanel.getServiceNameTextField().getDocument().addDocumentListener(listener);
131             this.mPanel.getServicePortTextField().getDocument().addDocumentListener(listener);
132             BindingConfigurationListener propListener = new BindingConfigurationListener();
133             this.mPanel.addPropertyChangeListener(propListener);
134             
135         }
136         return this.mPanel;
137     }
138
139     
140     
141     public HelpCtx getHelp() {
142         return new HelpCtx(WizardBindingConfigurationStep.class);
143     }
144
145     public boolean isValid() {
146        wiz.putProperty ("WizardPanel_errorMessage", this.mErrorMessage); // NOI18N
147
return this.mErrorMessage == null;
148
149     }
150
151     public void readSettings(Object JavaDoc settings) {
152         TemplateWizard templateWizard = (TemplateWizard)settings;
153         wiz = templateWizard;
154         String JavaDoc fileName = (String JavaDoc) templateWizard.getProperty(WsdlPanel.FILE_NAME);
155         if(this.mPanel.getBindingName() == null || this.mPanel.getBindingName().trim().equals("")) {
156             this.mPanel.setBindingName(fileName + "Binding"); //NOI18N
157
}
158         if(this.mPanel.getServiceName() == null || this.mPanel.getServiceName().trim().equals("")) {
159             this.mPanel.setServiceName(fileName + "Service"); //NOI18N
160
}
161         if(this.mPanel.getServicePortName() == null || this.mPanel.getServicePortName().trim().equals("")) {
162             this.mPanel.setServicePortName(fileName + "Port"); //NOI18N
163
}
164         
165         this.mPortType = (PortType) templateWizard.getProperty(WizardPortTypeConfigurationStep.PORTTYPE);
166         this.mTempModel = (WSDLModel) templateWizard.getProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL);
167         
168 /* LocalizedTemplate bindingSubType = this.mPanel.getBindingSubType();
169         processBindingSubType(bindingSubType);*/

170     }
171
172     public void storeSettings(Object JavaDoc settings) {
173         TemplateWizard templateWizard = (TemplateWizard)settings;
174         if(templateWizard.getValue() == TemplateWizard.CANCEL_OPTION) {
175             return;
176         }
177         
178         if (templateWizard.getValue() == TemplateWizard.PREVIOUS_OPTION) {
179             mTempModel.startTransaction();
180             cleanUpBindings();
181             mTempModel.endTransaction();
182             templateWizard.putProperty(BINDING_NAME, null);
183             templateWizard.putProperty(BINDING_TYPE, null);
184             templateWizard.putProperty(BINDING_SUBTYPE, null);
185             templateWizard.putProperty(SERVICE_NAME, null);
186             templateWizard.putProperty(SERVICEPORT_NAME, null);
187             return;
188         }
189         
190         String JavaDoc bindingName = this.mPanel.getBindingName();
191         LocalizedTemplateGroup bindingType = this.mPanel.getBindingType();
192         LocalizedTemplate bindingSubType = this.mPanel.getBindingSubType();
193         String JavaDoc serviceName = this.mPanel.getServiceName();
194         String JavaDoc servicePortName = this.mPanel.getServicePortName();
195         
196         templateWizard.putProperty(BINDING_NAME, bindingName);
197         templateWizard.putProperty(BINDING_TYPE, bindingType);
198         templateWizard.putProperty(BINDING_SUBTYPE, bindingSubType);
199         templateWizard.putProperty(SERVICE_NAME, serviceName);
200         templateWizard.putProperty(SERVICEPORT_NAME, servicePortName);
201         
202         processBindingSubType(bindingSubType);
203     }
204     
205     private void cleanUpBindings() {
206         if(this.mBinding != null) {
207             this.mTempModel.getDefinitions().removeBinding(this.mBinding);
208         }
209         
210         if(this.mService != null) {
211             this.mTempModel.getDefinitions().removeService(this.mService);
212         }
213         
214         mBinding = null;
215         mService = null;
216     }
217
218     private boolean isValidName(Document JavaDoc doc) {
219         try {
220             String JavaDoc text = doc.getText(0, doc.getLength());
221             boolean isValid = org.netbeans.modules.xml.xam.dom.Utils.isValidNCName(text);
222             if(!isValid) {
223                 mErrorMessage = "Name \"" + text + "\" is not a valid NCName";
224             } else {
225                 mErrorMessage = null;
226             }
227             
228             fireChangeEvent();
229         } catch(Exception JavaDoc ex) {
230             ex.printStackTrace();
231         }
232         
233         return mErrorMessage == null;
234     }
235     
236     private void validate() {
237         boolean isValidBinding = isValidName(this.mPanel.getBindingNameTextField().getDocument());
238         if(!isValidBinding) {
239             fireChangeEvent();
240             return;
241         }
242         
243         boolean isValidService = isValidName(this.mPanel.getServiceNameTextField().getDocument());
244         if(!isValidService) {
245             fireChangeEvent();
246             return;
247         }
248         
249         boolean isValidPort = isValidName(this.mPanel.getServicePortTextField().getDocument());
250         if(!isValidPort) {
251             fireChangeEvent();
252             return;
253         }
254         
255         if(this.mBindingSubTypeError != null) {
256             this.mErrorMessage = this.mBindingSubTypeError;
257             fireChangeEvent();
258             return;
259         }
260         
261         fireChangeEvent();
262     }
263     
264     private void fireChangeEvent() {
265         Iterator JavaDoc<ChangeListener JavaDoc> it = this.listeners.iterator();
266         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
267         while(it.hasNext()) {
268             ChangeListener JavaDoc l = it.next();
269             l.stateChanged(e);
270         }
271     }
272
273     public boolean isFinishPanel() {
274         return true;
275     }
276     
277     private void processBindingSubType(LocalizedTemplate bindingSubType) {
278         if(bindingSubType != null) {
279             String JavaDoc bindingName = this.mPanel.getBindingName();
280             LocalizedTemplateGroup bindingType = this.mPanel.getBindingType();
281             
282             
283             //service and port
284
String JavaDoc serviceName = this.mPanel.getServiceName();
285             String JavaDoc servicePortName = this.mPanel.getServicePortName();
286             
287             Map JavaDoc configurationMap = new HashMap JavaDoc();
288             configurationMap.put(WizardBindingConfigurationStep.BINDING_NAME, bindingName);
289             configurationMap.put(WizardBindingConfigurationStep.BINDING_TYPE, bindingType);
290            
291             //this could be null for a binding which does not have a sub type
292
configurationMap.put(WizardBindingConfigurationStep.BINDING_SUBTYPE, bindingSubType);
293             
294             //service and port
295
configurationMap.put(WizardBindingConfigurationStep.SERVICE_NAME, serviceName);
296             configurationMap.put(WizardBindingConfigurationStep.SERVICEPORT_NAME, servicePortName);
297             
298             this.mTempModel.startTransaction();
299             cleanUpBindings();
300             
301             BindingGenerator bGen = new BindingGenerator(this.mTempModel, this.mPortType, configurationMap);
302             bGen.execute();
303             
304             this.mBinding = bGen.getBinding();
305             this.mService = bGen.getService();
306             this.mPort = bGen.getPort();
307             
308             if(this.mBinding != null) {
309                 String JavaDoc targetNamespace = (String JavaDoc) wiz.getProperty(WsdlPanel.WSDL_TARGETNAMESPACE);
310                 List JavaDoc<ValidationInfo> vAllInfos =new ArrayList JavaDoc<ValidationInfo>();
311                 
312                 List JavaDoc<ValidationInfo> vBindingInfos = bindingSubType.getMProvider().validate(this.mBinding);
313                 if(vBindingInfos != null) {
314                     vAllInfos.addAll(vBindingInfos);
315                 }
316                 
317                 if(this.mPort != null) {
318                     List JavaDoc<ValidationInfo> vPortInfos = bindingSubType.getMProvider().validate(this.mPort);
319                     if(vPortInfos != null) {
320                         vAllInfos.addAll(vPortInfos);
321                     }
322                 }
323                 if(vAllInfos != null && vAllInfos.size() > 0) {
324                     ValidationInfo vInfo = vAllInfos.get(0);
325                     this.mBindingSubTypeError = vInfo.getDescription();
326                     IOProvider.getDefault().getStdOut().print(this.mBindingSubTypeError);
327                     validate();
328                 } else {
329                     //no errors
330
this.mBindingSubTypeError = null;
331                     validate();
332                     if(this.mBinding != null) {
333                         bindingSubType.getMProvider().postProcess(targetNamespace, this.mBinding);
334                     }
335                     if(this.mPort != null) {
336                         bindingSubType.getMProvider().postProcess(targetNamespace, this.mPort);
337                     }
338                     this.wiz.putProperty(BINDING, this.mBinding);
339                     this.wiz.putProperty(SERVICE, this.mService);
340                     this.wiz.putProperty(PORT, this.mPort);
341                 }
342             }
343             
344             this.mTempModel.endTransaction();
345         }
346     }
347     
348     class TextChangeListener implements DocumentListener JavaDoc {
349      
350          public void changedUpdate(DocumentEvent JavaDoc e) {
351             validate();
352          }
353          
354          public void insertUpdate(DocumentEvent JavaDoc e) {
355              validate();
356          }
357
358          public void removeUpdate(DocumentEvent JavaDoc e) {
359              validate();
360          }
361     
362     }
363     
364     class BindingConfigurationListener implements PropertyChangeListener JavaDoc {
365         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
366             String JavaDoc propertyName = evt.getPropertyName();
367             if(BindingConfigurationPanel.PROP_BINDING_SUBTYPE.equals(propertyName)) {
368                 LocalizedTemplate bindingSubType = (LocalizedTemplate) evt.getNewValue();
369                 processBindingSubType(bindingSubType);
370             } else if(BindingConfigurationPanel.PROP_BINDING_TYPE.equals(propertyName)) {
371                 processBindingSubType(mPanel.getBindingSubType());
372             }
373         }
374     }
375 }
376
377
Popular Tags