KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > ImportWSDLCreator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.ui.view;
21
22 import java.io.IOException JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.modules.xml.wsdl.model.Definitions;
26 import org.netbeans.modules.xml.wsdl.model.Import;
27 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
28 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
29 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
30 import org.netbeans.modules.xml.xam.Model;
31 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
32 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceCreator;
33 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
34 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDecorator;
35 import org.openide.nodes.Node;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38
39 /**
40  * An import creator.
41  *
42  * @author Administrator
43  * @author Nathan Fiedler
44  */

45 public class ImportWSDLCreator extends ExternalReferenceCreator<Definitions> {
46     /** silence compiler warnings */
47     private static final long serialVersionUID = 1L;
48     private ExternalReferenceDecorator decorator;
49
50     /**
51      * Creates a new instance of ImportCustomizer
52      *
53      * @param defs component to contain the import(s).
54      */

55     public ImportWSDLCreator(Definitions defs) {
56         super(defs, null);
57     }
58
59     @Override JavaDoc
60     public void applyChanges() throws IOException JavaDoc {
61         super.applyChanges();
62         Definitions def = getModelComponent();
63         WSDLModel model = def.getModel();
64         List JavaDoc<Node> nodes = getSelectedNodes();
65         for (Node node : nodes) {
66             Import imp = model.getFactory().createImport();
67             // Save the location.
68
imp.setLocation(getLocation(node));
69
70             // Save the namespace.
71
String JavaDoc namespace = getNamespace(node);
72             if (mustNamespaceDiffer()) {
73                 imp.setNamespace(namespace);
74             }
75
76             // Save the prefix.
77
if (node instanceof ExternalReferenceDataNode) {
78                 String JavaDoc prefix = ((ExternalReferenceDataNode) node).getPrefix();
79                 if (prefix.length() > 0) {
80                     // Should not have to cast, but Definitions does not
81
// expose the prefixes API.
82
AbstractDocumentComponent adc =
83                             (AbstractDocumentComponent) model.getDefinitions();
84                     Map JavaDoc prefixes = adc.getPrefixes();
85                     if (!prefixes.containsKey(prefix)) {
86                         adc.addPrefix(prefix, namespace);
87                     }
88                 }
89             }
90             def.addImport(imp);
91
92             // Check whether namespace was added. Temporary fix till import
93
// dialog mandates the prefix.
94
if (Utility.getNamespacePrefix(imp.getNamespace(), model) == null) {
95                 //create a prefix for this namespace
96
String JavaDoc prefix = NameGenerator.getInstance().generateNamespacePrefix(null, model);
97                 ((AbstractDocumentComponent) def).addPrefix(prefix, imp.getNamespace());
98             }
99         }
100     }
101
102     public HelpCtx getHelpCtx() {
103         return new HelpCtx(ImportWSDLCreator.class);
104     }
105
106     protected String JavaDoc getTargetNamespace(Model model) {
107         return ((WSDLModel) model).getDefinitions().getTargetNamespace();
108     }
109
110     protected Map JavaDoc<String JavaDoc, String JavaDoc> getPrefixes(Model model) {
111         WSDLModel wm = (WSDLModel) model;
112         AbstractDocumentComponent def =
113                 (AbstractDocumentComponent) wm.getDefinitions();
114         return def.getPrefixes();
115     }
116
117     protected ExternalReferenceDecorator getNodeDecorator() {
118         if (decorator == null) {
119             decorator = new WSDLReferenceDecorator(this);
120         }
121         return decorator;
122     }
123
124     public boolean mustNamespaceDiffer() {
125         return true;
126     }
127
128     protected String JavaDoc referenceTypeName() {
129         return NbBundle.getMessage(ImportWSDLCreator.class,
130                 "LBL_ImportCreator_Type");
131     }
132 }
133
Popular Tags