KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
23 import org.netbeans.modules.xml.retriever.catalog.Utilities;
24 import org.netbeans.modules.xml.schema.model.SchemaModel;
25 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
26 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
27 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
28 import org.netbeans.modules.xml.xam.Model;
29 import org.netbeans.modules.xml.xam.locator.CatalogModelException;
30 import org.netbeans.modules.xml.xam.ui.customizer.AbstractReferenceDecorator;
31 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
32 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceNode;
33 import org.openide.nodes.Node;
34 import org.openide.util.NbBundle;
35
36 /**
37  * The ExternalReferenceDecorator for schema components.
38  *
39  * @author Nathan Fiedler
40  */

41 public class SchemaReferenceDecorator extends AbstractReferenceDecorator {
42     /** The customizer that created this decorator. */
43     private ImportSchemaCustomizer customizer;
44     /** Used to generate unique namespace prefixes. */
45     private int prefixCounter;
46
47     /**
48      * Creates a new instance of SchemaReferenceDecorator.
49      *
50      * @param customizer provides information about the edited component.
51      */

52     public SchemaReferenceDecorator(ImportSchemaCustomizer customizer) {
53         this.customizer = customizer;
54     }
55
56     public String JavaDoc validate(ExternalReferenceNode node) {
57         if (node.hasModel()) {
58             Model model = node.getModel();
59             if (model == null) {
60                 // If it is supposed to have a model, it must not be null.
61
return NbBundle.getMessage(SchemaReferenceDecorator.class,
62                         "LBL_ReferenceDecorator_NoModel");
63             }
64             Model componentModel = customizer.getComponentModel();
65             if (model.equals(componentModel)) {
66                 return NbBundle.getMessage(SchemaReferenceDecorator.class,
67                         "LBL_ReferenceDecorator_SameModel");
68             }
69             // It had better be a schema model, but check anyway.
70
if (componentModel instanceof SchemaModel) {
71                 SchemaModel sm = (SchemaModel) componentModel;
72                 Collection JavaDoc<SchemaModelReference> references =
73                         sm.getSchema().getSchemaReferences();
74                 // Ensure the selected document is not already among the
75
// set that have been included.
76
for (SchemaModelReference ref : references) {
77                     try {
78                         SchemaModel otherModel = ref.resolveReferencedModel();
79                         if (model.equals(otherModel)) {
80                             return NbBundle.getMessage(SchemaReferenceDecorator.class,
81                                     "LBL_ReferenceDecorator_AlreadyRefd");
82                         }
83                     } catch (CatalogModelException cme) {
84                         // Ignore that one as it does not matter.
85
}
86                 }
87             }
88         }
89         String JavaDoc ns = node.getNamespace();
90         String JavaDoc namespace = customizer.getTargetNamespace();
91         if (customizer.mustNamespaceDiffer()) {
92             // This is an import, which must have no namespace, or a
93
// different one than the customized component.
94
if (ns != null && !Utilities.NO_NAME_SPACE.equals(ns) &&
95                     namespace.equals(ns)) {
96                 return NbBundle.getMessage(SchemaReferenceDecorator.class,
97                         "LBL_ReferenceDecorator_SameNamespace");
98             }
99         } else {
100             // This is an include or redefine, which must have no namespace,
101
// or the same one as the customized component.
102
if (ns != null && !Utilities.NO_NAME_SPACE.equals(ns) &&
103                     !namespace.equals(ns)) {
104                 return NbBundle.getMessage(SchemaReferenceDecorator.class,
105                         "LBL_ReferenceDecorator_DifferentNamespace");
106             }
107         }
108         return null;
109     }
110
111     public ExternalReferenceDataNode createExternalReferenceNode(Node original) {
112         return customizer.createExternalReferenceNode(original);
113     }
114
115     protected String JavaDoc generatePrefix(Model model) {
116         WSDLModel wModel = customizer.getModel();
117         if (wModel != null) {
118             return NameGenerator.getInstance().generateNamespacePrefix(
119                     null, wModel, prefixCounter++);
120         }
121         return "";
122     }
123
124     public Utilities.DocumentTypesEnum getDocumentType() {
125         return Utilities.DocumentTypesEnum.schema;
126     }
127
128     public String JavaDoc getHtmlDisplayName(String JavaDoc name, ExternalReferenceNode node) {
129         if (validate(node) != null) {
130             return "<s>" + name + "</s>";
131         }
132         return name;
133     }
134
135     public String JavaDoc getNamespace(Model model) {
136         if (model instanceof SchemaModel) {
137             SchemaModel sm = (SchemaModel) model;
138             return sm.getSchema().getTargetNamespace();
139         }
140         return null;
141     }
142 }
143
Popular Tags