KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > customizer > 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.schema.ui.nodes.categorized.customizer;
21
22 import java.util.Collection JavaDoc;
23 import org.netbeans.modules.xml.retriever.catalog.Utilities;
24 import org.netbeans.modules.xml.schema.model.Schema;
25 import org.netbeans.modules.xml.schema.model.SchemaModel;
26 import org.netbeans.modules.xml.schema.model.SchemaModelReference;
27 import org.netbeans.modules.xml.schema.ui.basic.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.AbstractReferenceCustomizer;
31 import org.netbeans.modules.xml.xam.ui.customizer.AbstractReferenceDecorator;
32 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
33 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceNode;
34 import org.openide.nodes.Node;
35 import org.openide.util.NbBundle;
36
37 /**
38  * The ExternalReferenceDecorator for schema components.
39  *
40  * @author Nathan Fiedler
41  */

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

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