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: package org.netbeans.modules.xml.wsdl.ui.view;
043:
044: import java.io.IOException;
045: import java.util.Map;
046: import org.netbeans.modules.xml.wsdl.model.Import;
047: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
048: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
049: import org.netbeans.modules.xml.xam.Model;
050: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
051: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDecorator;
052: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceCustomizer;
053: import org.openide.util.HelpCtx;
054:
055: /**
056: * An import customizer.
057: *
058: * @author Administrator
059: * @author Nathan Fiedler
060: */
061: public class ImportWSDLCustomizer extends
062: ExternalReferenceCustomizer<Import> {
063: private static final long serialVersionUID = 1L;
064: private ExternalReferenceDecorator decorator;
065:
066: /**
067: * Creates a new instance of ImportCustomizer
068: *
069: * @param _import component to customize.
070: */
071: public ImportWSDLCustomizer(Import _import) {
072: super (_import, null);
073: }
074:
075: @Override
076: public void applyChanges() throws IOException {
077: super .applyChanges();
078: Import _import = getModelComponent();
079: if (isLocationChanged()) {
080: // Save the file location.
081: _import.setLocation(getEditedLocation());
082: }
083:
084: String namespace = getEditedNamespace();
085: if (mustNamespaceDiffer() && isNamespaceChanged()) {
086: // Save the namespace.
087: _import.setNamespace(namespace);
088: }
089:
090: WSDLModel model = getModelComponent().getModel();
091: if (mustNamespaceDiffer() && isPrefixChanged()) {
092: // Save the prefix.
093: String prefix = getEditedPrefix();
094: if (prefix.length() > 0) {
095: // Should not have to cast, but Definitions does not
096: // expose the prefixes API.
097: AbstractDocumentComponent def = (AbstractDocumentComponent) model
098: .getDefinitions();
099: def.addPrefix(prefix, namespace);
100: }
101: }
102: }
103:
104: protected String getReferenceLocation() {
105: Import _import = getModelComponent();
106: return _import.getLocation();
107: }
108:
109: protected String getNamespace() {
110: Import _import = getModelComponent();
111: return _import.getNamespace();
112: }
113:
114: protected String getPrefix() {
115: Import _import = getModelComponent();
116: String namespace = _import.getNamespace();
117: WSDLModel model = getModelComponent().getModel();
118: AbstractDocumentComponent def = (AbstractDocumentComponent) model
119: .getDefinitions();
120: Map<String, String> prefixMap = def.getPrefixes();
121: for (Map.Entry<String, String> entry : prefixMap.entrySet()) {
122: if (entry.getValue().equals(namespace)) {
123: return entry.getKey();
124: }
125: }
126: // The alternative to casting to AbstractDocumentComponent is to attempt
127: // to collect the prefixes from the imports.
128: // Collection<Import> imports = model.getDefinitions().getImports();
129: // for (Import entry : imports) {
130: // String ns = entry.getNamespace();
131: // if (ns != null && ns.equals(namespace)) {
132: // return entry.getNamespace();
133: // }
134: // }
135: return null;
136: }
137:
138: public HelpCtx getHelpCtx() {
139: return new HelpCtx(ImportWSDLCustomizer.class);
140: }
141:
142: protected String getTargetNamespace(Model model) {
143: return ((WSDLModel) model).getDefinitions()
144: .getTargetNamespace();
145: }
146:
147: protected Map<String, String> getPrefixes(Model model) {
148: WSDLModel wm = (WSDLModel) model;
149: AbstractDocumentComponent def = (AbstractDocumentComponent) wm
150: .getDefinitions();
151: return def.getPrefixes();
152: }
153:
154: protected ExternalReferenceDecorator getNodeDecorator() {
155: if (decorator == null) {
156: decorator = new WSDLReferenceDecorator(this );
157: }
158: return decorator;
159: }
160:
161: protected String generatePrefix() {
162: WSDLModel model = getModelComponent().getModel();
163: return NameGenerator.getInstance().generateNamespacePrefix(
164: null, model);
165: }
166:
167: public boolean mustNamespaceDiffer() {
168: return true;
169: }
170: }
|