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.HashMap;
046: import java.util.List;
047: import java.util.Map;
048: import org.netbeans.modules.xml.schema.model.Import;
049: import org.netbeans.modules.xml.schema.model.Schema;
050: import org.netbeans.modules.xml.schema.model.SchemaModel;
051: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
052: import org.netbeans.modules.xml.xam.Model;
053: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
054: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceCreator;
055: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
056: import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDecorator;
057: import org.openide.nodes.Node;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * An import customizer for schema in a WSDL document.
063: *
064: * @author Administrator
065: * @author Nathan Fiedler
066: */
067: public class ImportSchemaCustomizer extends
068: ExternalReferenceCreator<Schema> {
069: /** silence compiler warnings */
070: private static final long serialVersionUID = 1L;
071: private ExternalReferenceDecorator decorator;
072: /** The real model from which we get the namespace prefixes. */
073: private WSDLModel mModel;
074:
075: /**
076: * Creates a new instance of ImportCustomizer
077: *
078: * @param schema component to contain the import(s).
079: */
080: public ImportSchemaCustomizer(Schema schema, WSDLModel model) {
081: super (schema, model);
082: }
083:
084: @Override
085: protected void init(Schema component, Model model) {
086: // Set the model reference now, before the UI initialization occurs.
087: mModel = (WSDLModel) model;
088: }
089:
090: @Override
091: public void applyChanges() throws IOException {
092: super .applyChanges();
093: Schema schema = getModelComponent();
094: List<Node> nodes = getSelectedNodes();
095: for (Node node : nodes) {
096: Import schemaImport = schema.getModel().getFactory()
097: .createImport();
098: // Save the location.
099: schemaImport.setSchemaLocation(getLocation(node));
100:
101: String namespace = getNamespace(node);
102: if (mustNamespaceDiffer()) {
103: // Save the namespace.
104: schemaImport.setNamespace(namespace);
105: }
106:
107: // Save the prefix.
108: if (node instanceof ExternalReferenceDataNode) {
109: String prefix = ((ExternalReferenceDataNode) node)
110: .getPrefix();
111: if (prefix.length() > 0) {
112: if (mModel != null) {
113: AbstractDocumentComponent def = (AbstractDocumentComponent) mModel
114: .getDefinitions();
115: Map prefixes = def.getPrefixes();
116: if (!prefixes.containsKey(prefix)) {
117: def.addPrefix(prefix, namespace);
118: }
119:
120: }
121: }
122: }
123: schema.addExternalReference(schemaImport);
124: }
125: }
126:
127: public HelpCtx getHelpCtx() {
128: return new HelpCtx(ImportSchemaCustomizer.class);
129: }
130:
131: /**
132: * Return the WSDL model that contains the embedded schema model.
133: *
134: * @return the containing WSDL model.
135: */
136: public WSDLModel getModel() {
137: return mModel;
138: }
139:
140: protected String getTargetNamespace(Model model) {
141: return ((SchemaModel) model).getSchema().getTargetNamespace();
142: }
143:
144: protected Map<String, String> getPrefixes(Model model) {
145: if (mModel != null) {
146: return ((AbstractDocumentComponent) mModel.getDefinitions())
147: .getPrefixes();
148: }
149: return new HashMap();
150: }
151:
152: protected ExternalReferenceDecorator getNodeDecorator() {
153: if (decorator == null) {
154: decorator = new SchemaReferenceDecorator(this );
155: }
156: return decorator;
157: }
158:
159: public boolean mustNamespaceDiffer() {
160: return true;
161: }
162:
163: protected String referenceTypeName() {
164: return NbBundle.getMessage(ImportWSDLCreator.class,
165: "LBL_ImportCreator_Type");
166: }
167: }
|