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: * WizardPortTypeConfigurationStep.java
044: *
045: * Created on August 31, 2006, 3:02 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.util.ArrayList;
055: import java.util.Collection;
056: import java.util.HashMap;
057: import java.util.Iterator;
058: import java.util.List;
059: import java.util.Map;
060:
061: import javax.swing.event.ChangeEvent;
062: import javax.swing.event.ChangeListener;
063: import javax.swing.event.DocumentEvent;
064: import javax.swing.event.DocumentListener;
065: import javax.swing.text.Document;
066:
067: import org.netbeans.api.project.Project;
068: import org.netbeans.modules.xml.schema.model.Import;
069: import org.netbeans.modules.xml.schema.model.Schema;
070: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
071: import org.netbeans.modules.xml.wsdl.model.Message;
072: import org.netbeans.modules.xml.wsdl.model.PortType;
073: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
074: import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
075: import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
076: import org.netbeans.modules.xml.wsdl.ui.view.OperationConfigurationPanel;
077: import org.netbeans.modules.xml.wsdl.ui.view.OperationType;
078: import org.netbeans.modules.xml.wsdl.ui.view.PartAndElementOrTypeTableModel;
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:
087: /**
088: *
089: * @author radval
090: */
091: public class WizardPortTypeConfigurationStep implements
092: WizardDescriptor.FinishablePanel {
093:
094: public static final String PORTTYPE_NAME = "PORTTYPE_NAME";
095:
096: public static final String OPERATION_NAME = "OPERATION_NAME";
097:
098: public static final String OPERATION_TYPE = "OPERATION_TYPE";
099:
100: public static final String OPERATION_INPUT = "OPERATION_INPUT";
101:
102: public static final String OPERATION_OUTPUT = "OPERATION_OUTPUT";
103:
104: public static final String OPERATION_FAULT = "OPERATION_FAULT";
105:
106: public static final String OPERATION_INPUT_MESSAGE = "OPERATION_INPUT_MESSAGE";
107:
108: public static final String OPERATION_OUTPUT_MESSAGE = "OPERATION_OUTPUT_MESSAGE";
109:
110: public static final String OPERATION_FAULT_MESSAGE = "OPERATION_FAULT_MESSAGE";
111:
112: public static final String NAMESPACE_TO_PREFIX_MAP = "NAMESPACE_TO_PREFIX_MAP";
113:
114: public static final String TEMP_WSDLMODEL = "TEMP_WSDLMODEL";
115:
116: public static final String TEMP_WSDLFILE = "TEMP_WSDLFILE";
117:
118: public static final String PORTTYPE = "PORTTYPE";
119:
120: public static final String IS_FROM_WIZARD = "IS_FROM_WIZARD";
121:
122: public static final String AUTOGEN_PARTNERLINKTYPE = "AUTOGEN_PARTNERLINKTYPE";
123:
124: private OperationConfigurationPanel mPanel;
125:
126: private Project project;
127:
128: private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
129:
130: private String mErrorMessage;
131:
132: private WizardDescriptor wiz = null;
133:
134: private PortType mPortType;
135:
136: private List<Message> mNewMessageList = null;
137:
138: private ExtensibilityElement mPartnerLinkTypeElement = null;
139:
140: private Collection<Import> mImports = null;
141:
142: /** Creates a new instance of WizardPortTypeConfigurationStep */
143: public WizardPortTypeConfigurationStep(Project project) {
144: this .project = project;
145:
146: }
147:
148: public void addChangeListener(ChangeListener l) {
149: listeners.add(l);
150: }
151:
152: public void removeChangeListener(ChangeListener l) {
153: listeners.remove(l);
154: }
155:
156: public Component getComponent() {
157: if (mPanel == null) {
158: this .mPanel = new OperationConfigurationPanel(project);
159: this .mPanel.setName(NbBundle.getMessage(
160: WizardPortTypeConfigurationStep.class,
161: "LBL_WizardPortTypeConfigurationStep"));
162: TextChangeListener listener = new TextChangeListener();
163:
164: this .mPanel.getPortTypeNameTextField().getDocument()
165: .addDocumentListener(listener);
166: this .mPanel.getOperationNameTextField().getDocument()
167: .addDocumentListener(listener);
168: }
169: return this .mPanel;
170: }
171:
172: public HelpCtx getHelp() {
173: return new HelpCtx(WizardPortTypeConfigurationStep.class);
174: }
175:
176: public boolean isValid() {
177: /* String errorMessage = null;
178: //This should be good enough to disable html code.
179: // If not try to use the StringEscapeUtils.escapeHtml from common lang.
180: if (mErrorMessage != null) {
181: errorMessage = "<html>" + Utility.escapeHtml(mErrorMessage) + "</html>";
182: }*/
183:
184: wiz.putProperty("WizardPanel_errorMessage", mErrorMessage); // NOI18N
185: return this .mErrorMessage == null;
186:
187: }
188:
189: public void readSettings(Object settings) {
190: TemplateWizard templateWizard = (TemplateWizard) settings;
191: this .wiz = templateWizard;
192: String fileName = (String) templateWizard
193: .getProperty(WsdlPanel.FILE_NAME);
194: if (this .mPanel.getPortTypeName() == null
195: || this .mPanel.getPortTypeName().trim().equals("")) {
196: this .mPanel.setPortTypeName(fileName + "PortType"); //NOI18N
197: }
198:
199: if (this .mPanel.getOperationName() == null
200: || this .mPanel.getOperationName().trim().equals("")) {
201: this .mPanel.setOperationName(fileName + "Operation"); //NOI18N
202: }
203:
204: }
205:
206: public void storeSettings(Object settings) {
207: TemplateWizard templateWizard = (TemplateWizard) settings;
208: WSDLModel tempModel = (WSDLModel) templateWizard
209: .getProperty(WizardPortTypeConfigurationStep.TEMP_WSDLMODEL);
210: Object option = templateWizard.getValue();
211: if (option == NotifyDescriptor.CANCEL_OPTION
212: || option == WizardDescriptor.PREVIOUS_OPTION) {
213: DataObject dobj = ActionHelper.getDataObject(tempModel);
214: if (dobj != null) {
215: dobj.setModified(false);
216: try {
217: dobj.delete();
218: } catch (Exception e) {
219: //ignore
220: }
221: }
222: templateWizard.putProperty(
223: WizardPortTypeConfigurationStep.TEMP_WSDLMODEL,
224: null);
225: mPortType = null;
226: mNewMessageList = null;
227: mPartnerLinkTypeElement = null;
228: mImports = null;
229: return;
230: }
231:
232: if (tempModel != null) {
233: tempModel.startTransaction();
234: if (this .mPortType != null) {
235: tempModel.getDefinitions().removePortType(
236: this .mPortType);
237: }
238:
239: if (this .mNewMessageList != null) {
240: for (Message msg : mNewMessageList) {
241: tempModel.getDefinitions().removeMessage(msg);
242: }
243: }
244:
245: if (this .mPartnerLinkTypeElement != null) {
246: tempModel.getDefinitions().removeExtensibilityElement(
247: this .mPartnerLinkTypeElement);
248: }
249:
250: if (this .mImports != null) {
251: //Cleanup all inline schemas and remove the imported schemas from the inline schema.
252: Collection<WSDLSchema> wSchemas = tempModel
253: .getDefinitions().getTypes()
254: .getExtensibilityElements(WSDLSchema.class);
255: for (WSDLSchema wSchema : wSchemas) {
256: Schema schema = wSchema.getSchemaModel()
257: .getSchema();
258: //Wizard adds all imported schemas in a inline schema with same TNS as the definitions.
259: //So remove from that schema.
260: if (schema.getTargetNamespace().equals(
261: tempModel.getDefinitions()
262: .getTargetNamespace())) {
263: for (Import imp : mImports) {
264: schema.removeExternalReference(imp);
265: }
266: }
267: tempModel.getDefinitions().getTypes()
268: .removeExtensibilityElement(wSchema);
269: }
270: }
271:
272: mPortType = null;
273: mNewMessageList = null;
274: mPartnerLinkTypeElement = null;
275: mImports = null;
276:
277: String portTypeName = this .mPanel.getPortTypeName();
278: String operationName = this .mPanel.getOperationName();
279: OperationType ot = this .mPanel.getOperationType();
280: boolean autoGenPLT = mPanel.isAutoGeneratePartnerLinkType();
281:
282: //operation input/output/fault
283: List<PartAndElementOrTypeTableModel.PartAndElementOrType> inputMessageParts = this .mPanel
284: .getInputMessageParts();
285: List<PartAndElementOrTypeTableModel.PartAndElementOrType> outputMessageParts = this .mPanel
286: .getOutputMessageParts();
287: List<PartAndElementOrTypeTableModel.PartAndElementOrType> faultMessageParts = this .mPanel
288: .getFaultMessageParts();
289:
290: templateWizard.putProperty(OPERATION_INPUT,
291: inputMessageParts);
292: templateWizard.putProperty(OPERATION_OUTPUT,
293: outputMessageParts);
294: templateWizard.putProperty(OPERATION_FAULT,
295: faultMessageParts);
296: Map<String, String> namespaceToPrefixMap = mPanel
297: .getNamespaceToPrefixMap();
298: templateWizard.putProperty(NAMESPACE_TO_PREFIX_MAP,
299: namespaceToPrefixMap);
300:
301: Map configurationMap = new HashMap();
302: //portType
303: configurationMap.put(
304: WizardPortTypeConfigurationStep.PORTTYPE_NAME,
305: portTypeName);
306: configurationMap.put(
307: WizardPortTypeConfigurationStep.OPERATION_NAME,
308: operationName);
309: configurationMap.put(
310: WizardPortTypeConfigurationStep.OPERATION_TYPE, ot);
311: configurationMap
312: .put(
313: WizardPortTypeConfigurationStep.AUTOGEN_PARTNERLINKTYPE,
314: autoGenPLT);
315:
316: //opertion type
317: configurationMap.put(
318: WizardPortTypeConfigurationStep.OPERATION_INPUT,
319: inputMessageParts);
320: configurationMap.put(
321: WizardPortTypeConfigurationStep.OPERATION_OUTPUT,
322: outputMessageParts);
323: configurationMap.put(
324: WizardPortTypeConfigurationStep.OPERATION_FAULT,
325: faultMessageParts);
326: configurationMap
327: .put(
328: WizardPortTypeConfigurationStep.NAMESPACE_TO_PREFIX_MAP,
329: namespaceToPrefixMap);
330: configurationMap.put(
331: WizardPortTypeConfigurationStep.IS_FROM_WIZARD,
332: Boolean.TRUE);
333:
334: templateWizard.putProperty(PORTTYPE_NAME, portTypeName);
335: templateWizard.putProperty(OPERATION_NAME, operationName);
336: templateWizard.putProperty(OPERATION_TYPE, ot);
337:
338: PortTypeGenerator ptGen = new PortTypeGenerator(tempModel,
339: configurationMap);
340: ptGen.execute();
341: this .mPortType = ptGen.getPortType();
342: this .mNewMessageList = ptGen.getNewMessages();
343: this .mPartnerLinkTypeElement = ptGen.getPartnerLinkType();
344: this .mImports = ptGen.getImports();
345:
346: tempModel.endTransaction();
347:
348: templateWizard.putProperty(PORTTYPE, this .mPortType);
349: }
350:
351: }
352:
353: private boolean isValidName(Document doc) {
354: try {
355: String text = doc.getText(0, doc.getLength());
356: boolean isValid = org.netbeans.modules.xml.xam.dom.Utils
357: .isValidNCName(text);
358: if (!isValid) {
359: mErrorMessage = NbBundle.getMessage(
360: OperationPanel.class, "ERR_MSG_INVALID_NAME",
361: text);
362: } else {
363: mErrorMessage = null;
364: }
365:
366: } catch (Exception ex) {
367: ex.printStackTrace();
368: }
369:
370: return mErrorMessage == null;
371: }
372:
373: private void validate() {
374: boolean validPortType = isValidName(this .mPanel
375: .getPortTypeNameTextField().getDocument());
376: if (!validPortType) {
377: fireChangeEvent();
378: return;
379: }
380:
381: boolean validOperation = isValidName(this .mPanel
382: .getOperationNameTextField().getDocument());
383:
384: if (!validOperation) {
385: fireChangeEvent();
386: return;
387: }
388:
389: fireChangeEvent();
390: }
391:
392: private void fireChangeEvent() {
393: Iterator<ChangeListener> it = this .listeners.iterator();
394: ChangeEvent e = new ChangeEvent(this );
395: while (it.hasNext()) {
396: ChangeListener l = it.next();
397: l.stateChanged(e);
398: }
399: }
400:
401: public boolean isFinishPanel() {
402: return true;
403: }
404:
405: class TextChangeListener implements DocumentListener {
406:
407: public void changedUpdate(DocumentEvent e) {
408: validate();
409: }
410:
411: public void insertUpdate(DocumentEvent e) {
412: validate();
413: }
414:
415: public void removeUpdate(DocumentEvent e) {
416: validate();
417: }
418:
419: }
420: }
|