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: package org.netbeans.modules.xml.wsdl.ui.api.property;
042:
043: import javax.xml.namespace.QName;
044:
045: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
046: import org.netbeans.modules.xml.wsdl.model.Message;
047: import org.netbeans.modules.xml.wsdl.model.Part;
048: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
049: import org.netbeans.modules.xml.wsdl.ui.actions.ActionHelper;
050: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
051:
052: public class ElementOrTypeOrMessagePartProvider {
053:
054: private final ExtensibilityElement extensibilityElement;
055: private final String typeAttributeName;
056: private final String elementAttributeName;
057: private final String messageAttributeName;
058: private final String partAttributeName;
059:
060: public ElementOrTypeOrMessagePartProvider(
061: ExtensibilityElement extensibilityElement,
062: String elementAttributeName, String typeAttributeName,
063: String messageAttributeName, String partAttributeName) {
064: this .extensibilityElement = extensibilityElement;
065: this .elementAttributeName = elementAttributeName;
066: this .typeAttributeName = typeAttributeName;
067: this .messageAttributeName = messageAttributeName;
068: this .partAttributeName = partAttributeName;
069: }
070:
071: public void setValue(ElementOrTypeOrMessagePart newValue) {
072: if (newValue == null
073: || (newValue.getType() == null
074: && newValue.getElement() == null && newValue
075: .getMessagePart() == null)) {
076: return;
077: }
078: ElementOrTypeOrMessagePart oldValue = getValue();
079: if (oldValue != null && newValue.toString().equals(oldValue))
080: return;
081: if (getModel().startTransaction()) {
082: try {
083: ParameterType pType = newValue.getParameterType();
084:
085: switch (pType) {
086: case ELEMENT:
087: Utility.addNamespacePrefix(newValue.getElement()
088: .getModel().getSchema(),
089: extensibilityElement.getModel(), null);
090: Utility.addSchemaImport(newValue.getElement(),
091: extensibilityElement.getModel());
092: extensibilityElement.setAttribute(
093: elementAttributeName, newValue.toString());
094: extensibilityElement.setAttribute(
095: typeAttributeName, null);
096: extensibilityElement.setAttribute(
097: messageAttributeName, null);
098: extensibilityElement.setAttribute(
099: partAttributeName, null);
100: break;
101: case TYPE:
102: Utility.addNamespacePrefix(newValue.getType()
103: .getModel().getSchema(),
104: extensibilityElement.getModel(), null);
105: Utility.addSchemaImport(newValue.getType(),
106: extensibilityElement.getModel());
107: extensibilityElement.setAttribute(
108: elementAttributeName, null);
109: extensibilityElement.setAttribute(
110: typeAttributeName, newValue.toString());
111: extensibilityElement.setAttribute(
112: messageAttributeName, null);
113: extensibilityElement.setAttribute(
114: partAttributeName, null);
115: break;
116: case MESSAGEPART:
117: Part part = newValue.getMessagePart();
118: Message message = (Message) part.getParent();
119: Utility.addNamespacePrefix(part,
120: extensibilityElement.getModel(), null);
121: Utility.addWSDLImport(part, extensibilityElement
122: .getModel());
123: QName qname = new QName(message.getName());
124:
125: String tns = message.getModel().getDefinitions()
126: .getTargetNamespace();
127: if (tns != null) {
128: String prefix = Utility.getNamespacePrefix(tns,
129: extensibilityElement.getModel());
130: qname = prefix != null ? new QName(tns, message
131: .getName(), prefix) : new QName(tns,
132: message.getName());
133: }
134: extensibilityElement.setAttribute(
135: messageAttributeName, Utility
136: .fromQNameToString(qname));
137: extensibilityElement.setAttribute(
138: partAttributeName, part.getName());
139: extensibilityElement.setAttribute(
140: elementAttributeName, null);
141: extensibilityElement.setAttribute(
142: typeAttributeName, null);
143: break;
144: case NONE:
145: extensibilityElement.setAttribute(
146: elementAttributeName, null);
147: extensibilityElement.setAttribute(
148: typeAttributeName, null);
149: extensibilityElement.setAttribute(
150: typeAttributeName, null);
151: extensibilityElement.setAttribute(
152: elementAttributeName, null);
153: }
154: } finally {
155: getModel().endTransaction();
156: }
157: ActionHelper.selectNode(extensibilityElement);
158: }
159: }
160:
161: public ElementOrTypeOrMessagePart getValue() {
162: if (extensibilityElement.getModel() == null) { //this seems to happen during deletion.
163: return null;
164: }
165:
166: ParameterType parameterType = ParameterType.NONE;
167:
168: String message = extensibilityElement
169: .getAttribute(messageAttributeName);
170: String part = extensibilityElement
171: .getAttribute(partAttributeName);
172: String type = extensibilityElement
173: .getAttribute(typeAttributeName);
174: String element = extensibilityElement
175: .getAttribute(elementAttributeName);
176:
177: String value = null;
178:
179: if (element != null) {
180: parameterType = ParameterType.ELEMENT;
181: value = element;
182: } else if (type != null) {
183: value = type;
184: parameterType = ParameterType.TYPE;
185: } else if (message != null) {
186: parameterType = ParameterType.MESSAGEPART;
187: value = message;
188: return new ElementOrTypeOrMessagePart(getQName(value),
189: extensibilityElement.getModel(), part);
190: } else {
191: return null;
192: }
193:
194: QName qname = getQName(value);
195: return new ElementOrTypeOrMessagePart(qname,
196: extensibilityElement.getModel(), parameterType);
197: }
198:
199: private QName getQName(String value) {
200: if (value != null && value.trim().length() > 0) {
201: String[] parts = value.split(":");
202: if (parts != null && parts.length == 2) {
203: String prefix = parts[0];
204: String localPart = parts[1];
205: String namespace = Utility.getNamespaceURI(prefix,
206: extensibilityElement);
207: return new QName(namespace, localPart, prefix);
208: }
209: return new QName(getModel().getDefinitions()
210: .getTargetNamespace(), value);
211: }
212: return new QName("");
213: }
214:
215: public WSDLModel getModel() {
216: return extensibilityElement.getModel();
217: }
218:
219: public static enum ParameterType {
220:
221: ELEMENT, TYPE, MESSAGEPART, NONE
222: }
223: }
|