001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * WizardBindingConfigurationStep.java
044: *
045: * Created on August 31, 2006, 3:24 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.wsdl.ui.wizard;
052:
053: import java.awt.Component;
054: import java.beans.PropertyChangeEvent;
055: import java.beans.PropertyChangeListener;
056: import java.util.ArrayList;
057: import java.util.HashMap;
058: import java.util.Iterator;
059: import java.util.List;
060: import java.util.Map;
061:
062: import javax.swing.event.ChangeEvent;
063: import javax.swing.event.ChangeListener;
064: import javax.swing.event.DocumentEvent;
065: import javax.swing.event.DocumentListener;
066: import javax.swing.text.Document;
067:
068: import org.netbeans.modules.xml.wsdl.bindingsupport.spi.ValidationInfo;
069: import org.netbeans.modules.xml.wsdl.bindingsupport.template.localized.LocalizedTemplate;
070: import org.netbeans.modules.xml.wsdl.bindingsupport.template.localized.LocalizedTemplateGroup;
071: import org.netbeans.modules.xml.wsdl.model.Binding;
072: import org.netbeans.modules.xml.wsdl.model.Port;
073: import org.netbeans.modules.xml.wsdl.model.PortType;
074: import org.netbeans.modules.xml.wsdl.model.Service;
075: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
076: import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
077: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
078: import org.netbeans.modules.xml.wsdl.ui.view.BindingConfigurationPanel;
079: import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.newtype.OperationPanel;
080: import org.openide.NotifyDescriptor;
081: import org.openide.WizardDescriptor;
082: import org.openide.loaders.DataObject;
083: import org.openide.loaders.TemplateWizard;
084: import org.openide.util.HelpCtx;
085: import org.openide.util.NbBundle;
086: import org.openide.windows.IOProvider;
087:
088: /**
089: *
090: * @author radval
091: */
092: public class WizardBindingConfigurationStep implements
093: WizardDescriptor.FinishablePanel {
094:
095: public static final String BINDING_NAME = "BINDING_NAME";
096:
097: public static final String BINDING_TYPE = "BINDING_TYPE";
098:
099: public static final String BINDING_SUBTYPE = "BINDING_SUBTYPE";
100:
101: public static final String SERVICE_NAME = "SERVICE_NAME";
102:
103: public static final String SERVICEPORT_NAME = "SERVICEPORT_NAME";
104:
105: public static final String BINDING = "BINDING";
106:
107: public static final String SERVICE = "SERVICE";
108:
109: public static final String PORT = "PORT";
110:
111: public static final String AUTO_CREATE_SERVICEPORT = "AUTO_CREATE_SERVICEPORT";
112:
113: private BindingConfigurationPanel mPanel;
114:
115: private String mErrorMessage;
116:
117: private String mBindingSubTypeError;
118:
119: private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
120:
121: private WizardDescriptor wiz = null;
122:
123: private WSDLModel mTempModel;
124:
125: private PortType mPortType;
126:
127: private Binding mBinding;
128:
129: private Service mService;
130:
131: private Port mPort;
132:
133: /** Creates a new instance of WizardBindingConfigurationStep */
134: public WizardBindingConfigurationStep() {
135: }
136:
137: public void addChangeListener(ChangeListener l) {
138: listeners.add(l);
139: }
140:
141: public void removeChangeListener(ChangeListener l) {
142: listeners.remove(l);
143: }
144:
145: public Component getComponent() {
146: if (mPanel == null) {
147: this .mPanel = new BindingConfigurationPanel();
148: this .mPanel.setName(NbBundle.getMessage(
149: WizardBindingConfigurationStep.class,
150: "LBL_WizardBindingConfigurationStep"));
151: TextChangeListener listener = new TextChangeListener();
152: this .mPanel.getBindingNameTextField().getDocument()
153: .addDocumentListener(listener);
154: this .mPanel.getServiceNameTextField().getDocument()
155: .addDocumentListener(listener);
156: this .mPanel.getServicePortTextField().getDocument()
157: .addDocumentListener(listener);
158: BindingConfigurationListener propListener = new BindingConfigurationListener();
159: this .mPanel.addPropertyChangeListener(propListener);
160:
161: }
162: return this .mPanel;
163: }
164:
165: public HelpCtx getHelp() {
166: return new HelpCtx(WizardBindingConfigurationStep.class);
167: }
168:
169: public boolean isValid() {
170: /* String errorMessage = null;
171: //This should be good enough to disable html code.
172: // If not try to use the StringEscapeUtils.escapeHtml from common lang.
173: if (mErrorMessage != null) {
174: errorMessage = "<html>" + Utility.escapeHtml(mErrorMessage) + "</html>";
175: }*/
176:
177: wiz.putProperty("WizardPanel_errorMessage", mErrorMessage); // NOI18N
178: return this .mErrorMessage == null;
179:
180: }
181:
182: public void readSettings(Object settings) {
183: TemplateWizard templateWizard = (TemplateWizard) settings;
184: wiz = templateWizard;
185: String fileName = (String) templateWizard
186: .getProperty(WsdlPanel.FILE_NAME);
187: if (this .mPanel.getBindingName() == null
188: || this .mPanel.getBindingName().trim().equals("")) {
189: this .mPanel.setBindingName(fileName + "Binding"); //NOI18N
190: }
191: if (this .mPanel.getServiceName() == null
192: || this .mPanel.getServiceName().trim().equals("")) {
193: this .mPanel.setServiceName(fileName + "Service"); //NOI18N
194: }
195: if (this .mPanel.getServicePortName() == null
196: || this .mPanel.getServicePortName().trim().equals("")) {
197: this .mPanel.setServicePortName(fileName + "Port"); //NOI18N
198: }
199:
200: this .mPortType = (PortType) templateWizard
201: .getProperty(WizardPortTypeConfigurationStep.PORTTYPE);
202: this .mTempModel = (WSDLModel) templateWizard
203: .getProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL);
204:
205: LocalizedTemplate bindingSubType = this .mPanel
206: .getBindingSubType();
207: processBindingSubType(bindingSubType, true);
208: }
209:
210: public void storeSettings(Object settings) {
211: TemplateWizard templateWizard = (TemplateWizard) settings;
212: if (templateWizard.getValue() == NotifyDescriptor.CANCEL_OPTION) {
213: cleanup();
214: return;
215: }
216:
217: if (templateWizard.getValue() == WizardDescriptor.PREVIOUS_OPTION) {
218: mTempModel.startTransaction();
219: cleanUpBindings();
220: mTempModel.endTransaction();
221: templateWizard.putProperty(BINDING_NAME, null);
222: templateWizard.putProperty(BINDING_TYPE, null);
223: templateWizard.putProperty(BINDING_SUBTYPE, null);
224: templateWizard.putProperty(SERVICE_NAME, null);
225: templateWizard.putProperty(SERVICEPORT_NAME, null);
226: return;
227: }
228:
229: String bindingName = this .mPanel.getBindingName();
230: LocalizedTemplateGroup bindingType = this .mPanel
231: .getBindingType();
232: LocalizedTemplate bindingSubType = this .mPanel
233: .getBindingSubType();
234: String serviceName = this .mPanel.getServiceName();
235: String servicePortName = this .mPanel.getServicePortName();
236:
237: templateWizard.putProperty(BINDING_NAME, bindingName);
238: templateWizard.putProperty(BINDING_TYPE, bindingType);
239: templateWizard.putProperty(BINDING_SUBTYPE, bindingSubType);
240: templateWizard.putProperty(SERVICE_NAME, serviceName);
241: templateWizard.putProperty(SERVICEPORT_NAME, servicePortName);
242:
243: processBindingSubType(bindingSubType);
244: }
245:
246: void cleanup() {
247: DataObject dobj = ActionHelper.getDataObject(mTempModel);
248: if (dobj != null) {
249: dobj.setModified(false);
250: try {
251: dobj.delete();
252: } catch (Exception e) {
253: //ignore
254: }
255: }
256: }
257:
258: private void cleanUpBindings() {
259: if (this .mBinding != null) {
260: this .mTempModel.getDefinitions().removeBinding(
261: this .mBinding);
262: }
263:
264: if (this .mService != null) {
265: this .mTempModel.getDefinitions().removeService(
266: this .mService);
267: }
268:
269: mBinding = null;
270: mService = null;
271: }
272:
273: private boolean isValidName(Document doc) {
274: try {
275: String text = doc.getText(0, doc.getLength());
276: boolean isValid = org.netbeans.modules.xml.xam.dom.Utils
277: .isValidNCName(text);
278: if (!isValid) {
279: mErrorMessage = NbBundle.getMessage(
280: OperationPanel.class, "ERR_MSG_INVALID_NAME",
281: text);
282: } else {
283: mErrorMessage = null;
284: }
285:
286: fireChangeEvent();
287: } catch (Exception ex) {
288: ex.printStackTrace();
289: }
290:
291: return mErrorMessage == null;
292: }
293:
294: private void validate() {
295: boolean isValidBinding = isValidName(this .mPanel
296: .getBindingNameTextField().getDocument());
297: if (!isValidBinding) {
298: fireChangeEvent();
299: return;
300: }
301:
302: boolean isValidService = isValidName(this .mPanel
303: .getServiceNameTextField().getDocument());
304: if (!isValidService) {
305: fireChangeEvent();
306: return;
307: }
308:
309: boolean isValidPort = isValidName(this .mPanel
310: .getServicePortTextField().getDocument());
311: if (!isValidPort) {
312: fireChangeEvent();
313: return;
314: }
315:
316: if (this .mBindingSubTypeError != null) {
317: this .mErrorMessage = this .mBindingSubTypeError;
318: fireChangeEvent();
319: return;
320: }
321:
322: fireChangeEvent();
323: }
324:
325: private void fireChangeEvent() {
326: Iterator<ChangeListener> it = this .listeners.iterator();
327: ChangeEvent e = new ChangeEvent(this );
328: while (it.hasNext()) {
329: ChangeListener l = it.next();
330: l.stateChanged(e);
331: }
332: }
333:
334: public boolean isFinishPanel() {
335: return true;
336: }
337:
338: private void processBindingSubType(LocalizedTemplate bindingSubType) {
339: processBindingSubType(bindingSubType, false);
340: }
341:
342: private void processBindingSubType(
343: LocalizedTemplate bindingSubType, boolean validateOnly) {
344: if (bindingSubType != null) {
345: String bindingName = this .mPanel.getBindingName();
346: LocalizedTemplateGroup bindingType = this .mPanel
347: .getBindingType();
348:
349: //service and port
350: String serviceName = this .mPanel.getServiceName();
351: String servicePortName = this .mPanel.getServicePortName();
352:
353: Map configurationMap = new HashMap();
354: configurationMap.put(
355: WizardBindingConfigurationStep.BINDING_NAME,
356: bindingName);
357: configurationMap.put(
358: WizardBindingConfigurationStep.BINDING_TYPE,
359: bindingType);
360:
361: //this could be null for a binding which does not have a sub type
362: configurationMap.put(
363: WizardBindingConfigurationStep.BINDING_SUBTYPE,
364: bindingSubType);
365:
366: //service and port
367: configurationMap.put(
368: WizardBindingConfigurationStep.SERVICE_NAME,
369: serviceName);
370: configurationMap.put(
371: WizardBindingConfigurationStep.SERVICEPORT_NAME,
372: servicePortName);
373:
374: this .mTempModel.startTransaction();
375: cleanUpBindings();
376:
377: BindingGenerator bGen = new BindingGenerator(
378: this .mTempModel, this .mPortType, configurationMap);
379: bGen.execute();
380:
381: this .mBinding = bGen.getBinding();
382: this .mService = bGen.getService();
383: this .mPort = bGen.getPort();
384:
385: if (this .mBinding != null) {
386: String targetNamespace = (String) wiz
387: .getProperty(WsdlPanel.WSDL_TARGETNAMESPACE);
388: List<ValidationInfo> vAllInfos = new ArrayList<ValidationInfo>();
389:
390: List<ValidationInfo> vBindingInfos = bindingSubType
391: .getMProvider().validate(this .mBinding);
392: if (vBindingInfos != null) {
393: vAllInfos.addAll(vBindingInfos);
394: }
395:
396: if (this .mPort != null) {
397: List<ValidationInfo> vPortInfos = bindingSubType
398: .getMProvider().validate(this .mPort);
399: if (vPortInfos != null) {
400: vAllInfos.addAll(vPortInfos);
401: }
402: }
403: if (vAllInfos.size() > 0) {
404: ValidationInfo vInfo = vAllInfos.get(0);
405: this .mBindingSubTypeError = vInfo.getDescription();
406: IOProvider.getDefault().getStdOut().print(
407: this .mBindingSubTypeError);
408: validate();
409: } else {
410: //no errors
411: this .mBindingSubTypeError = null;
412: validate();
413: if (this .mBinding != null) {
414: bindingSubType.getMProvider().postProcess(
415: targetNamespace, this .mBinding);
416: }
417: if (this .mPort != null) {
418: bindingSubType.getMProvider().postProcess(
419: targetNamespace, this .mPort);
420: }
421: this .wiz.putProperty(BINDING, this .mBinding);
422: this .wiz.putProperty(SERVICE, this .mService);
423: this .wiz.putProperty(PORT, this .mPort);
424: }
425: }
426:
427: if (validateOnly) {
428: mTempModel.rollbackTransaction();
429: mBinding = null;
430: mService = null;
431: mPort = null;
432: } else {
433: mTempModel.endTransaction();
434: }
435: }
436: }
437:
438: class TextChangeListener implements DocumentListener {
439:
440: public void changedUpdate(DocumentEvent e) {
441: validate();
442: }
443:
444: public void insertUpdate(DocumentEvent e) {
445: validate();
446: }
447:
448: public void removeUpdate(DocumentEvent e) {
449: validate();
450: }
451:
452: }
453:
454: class BindingConfigurationListener implements
455: PropertyChangeListener {
456: public void propertyChange(PropertyChangeEvent evt) {
457: String propertyName = evt.getPropertyName();
458: if (BindingConfigurationPanel.PROP_BINDING_SUBTYPE
459: .equals(propertyName)) {
460: LocalizedTemplate bindingSubType = (LocalizedTemplate) evt
461: .getNewValue();
462: processBindingSubType(bindingSubType, true);
463: } else if (BindingConfigurationPanel.PROP_BINDING_TYPE
464: .equals(propertyName)) {
465: processBindingSubType(mPanel.getBindingSubType(), true);
466: }
467: }
468: }
469: }
|