KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > customizer > ImportCustomizer


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.io.IOException JavaDoc;
23 import java.util.Map JavaDoc;
24 import org.netbeans.modules.xml.schema.ui.basic.NameGenerator;
25 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDecorator;
26 import org.openide.util.HelpCtx;
27 import org.netbeans.modules.xml.schema.model.Import;
28 import org.netbeans.modules.xml.schema.model.Schema;
29 import org.netbeans.modules.xml.schema.model.SchemaModel;
30 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
31 import org.netbeans.modules.xml.xam.Model;
32 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
33 import org.netbeans.modules.xml.xam.ui.ModelCookie;
34 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceCustomizer;
35 import org.openide.filesystems.FileObject;
36 import org.openide.loaders.DataObject;
37
38 /**
39  * An import customizer.
40  *
41  * @author Administrator
42  * @author Nathan Fiedler
43  */

44 public class ImportCustomizer extends ExternalReferenceCustomizer<Import> {
45     private static final long serialVersionUID = 1L;
46     private ExternalReferenceDecorator decorator;
47
48     /**
49      * Creates a new instance of ImportCustomizer
50      *
51      * @param _import component to customize.
52      */

53     public ImportCustomizer(Import _import) {
54         super(_import, null);
55     }
56
57     @Override JavaDoc
58     public void applyChanges() throws IOException JavaDoc {
59         super.applyChanges();
60         Import _import = getModelComponent();
61         if (isLocationChanged()) {
62             // Save the location.
63
_import.setSchemaLocation(getEditedLocation());
64         }
65
66         String JavaDoc namespace = getEditedNamespace();
67         if (mustNamespaceDiffer() && isNamespaceChanged()) {
68             // Save the namespace.
69
_import.setNamespace(namespace);
70         }
71
72 // XXX: Ignore whether the prefix changed or not, just use the
73
// value, if it is non-null, and save it.
74
// Change this back once there is an ImportCreator class that
75
// extends ExternalReferenceCreator.
76
if (mustNamespaceDiffer()/* && isPrefixChanged()*/) {
77             // Save the prefix.
78
SchemaModel model = getModelComponent().getModel();
79             String JavaDoc prefix = getEditedPrefix();
80             if (prefix.length() > 0) {
81                 // This overwrites any existing value for the same prefix.
82
model.getSchema().addPrefix(prefix, namespace);
83             }
84         }
85     }
86
87     protected String JavaDoc getReferenceLocation() {
88         Import _import = getModelComponent();
89         return _import.getSchemaLocation();
90     }
91
92     protected String JavaDoc getNamespace() {
93         Import _import = getModelComponent();
94         return _import.getNamespace();
95     }
96
97     /**
98      * Search the prefixes defined in the given model for one that matches
99      * the given namespace value.
100      *
101      * @param model the schema model to search.
102      * @param namespace the namespace to look for.
103      * @return matching prefix, or null if none found.
104      */

105     private static String JavaDoc findPrefix(SchemaModel model, String JavaDoc namespace) {
106         Map JavaDoc<String JavaDoc, String JavaDoc> prefixMap = model.getSchema().getPrefixes();
107         for (Map.Entry JavaDoc<String JavaDoc, String JavaDoc> entry : prefixMap.entrySet()) {
108             if (entry.getValue().equals(namespace)) {
109                 return entry.getKey();
110             }
111         }
112         return null;
113     }
114
115     /**
116      * Search the prefixes defined in the given model for one that matches
117      * the given namespace value.
118      *
119      * @param model the WSDL model to search.
120      * @param namespace the namespace to look for.
121      * @return matching prefix, or null if none found.
122      */

123     @SuppressWarnings JavaDoc("unchecked")
124     private static String JavaDoc findPrefix(WSDLModel model, String JavaDoc namespace) {
125         AbstractDocumentComponent def =
126                 (AbstractDocumentComponent) model.getDefinitions();
127         Map JavaDoc<Object JavaDoc, Object JavaDoc> prefixMap = def.getPrefixes();
128         for (Map.Entry JavaDoc<Object JavaDoc, Object JavaDoc> entry : prefixMap.entrySet()) {
129             if (entry.getValue().equals(namespace)) {
130                 return entry.getKey().toString();
131             }
132         }
133         return null;
134     }
135
136     protected String JavaDoc getPrefix() {
137         Import _import = getModelComponent();
138         String JavaDoc namespace = _import.getNamespace();
139         SchemaModel model = getModelComponent().getModel();
140         String JavaDoc prefix = findPrefix(model, namespace);
141         if (prefix != null) {
142             return prefix;
143         }
144         // We may be embedded in another model (e.g. WSDL model), so
145
// attempt to get the model that contains this one (the embedded
146
// model delegates everything to the parent, so calling
147
// getModelSource() will return the parent model source).
148
FileObject fobj = (FileObject) model.getModelSource().getLookup().
149                 lookup(FileObject.class);
150         if (fobj != null) {
151             try {
152                 DataObject dobj = DataObject.find(fobj);
153                 ModelCookie modelCookie = (ModelCookie) dobj.getCookie(
154                         ModelCookie.class);
155                 if (modelCookie != null) {
156                     Model model2 = modelCookie.getModel();
157                     if (model2 != null && !model.equals(model2)) {
158                         if (model2 instanceof SchemaModel) {
159                             return findPrefix((SchemaModel) model2, namespace);
160                         } else if (model2 instanceof WSDLModel) {
161                             return findPrefix((WSDLModel) model2, namespace);
162                         }
163                     }
164                 }
165             } catch (IOException JavaDoc ioe) {
166                 // ignore and fall through
167
}
168         }
169         return null;
170     }
171
172     public HelpCtx getHelpCtx() {
173         return new HelpCtx(ImportCustomizer.class);
174     }
175
176     protected String JavaDoc getTargetNamespace(Model model) {
177         if (model instanceof SchemaModel) {
178             SchemaModel sm = (SchemaModel) model;
179             Schema schema = sm.getSchema();
180             if (schema != null) {
181                 return schema.getTargetNamespace();
182             }
183         }
184         return null;
185     }
186
187     protected Map JavaDoc<String JavaDoc, String JavaDoc> getPrefixes(Model model) {
188         if (model instanceof SchemaModel) {
189             SchemaModel sm = (SchemaModel) model;
190             Schema schema = sm.getSchema();
191             if (schema != null) {
192                 return schema.getPrefixes();
193             }
194         }
195         return null;
196     }
197
198     protected ExternalReferenceDecorator getNodeDecorator() {
199         if (decorator == null) {
200             decorator = new SchemaReferenceDecorator(this);
201         }
202         return decorator;
203     }
204
205     protected String JavaDoc generatePrefix() {
206         SchemaModel model = getModelComponent().getModel();
207         return NameGenerator.getInstance().generateNamespacePrefix(null, model);
208     }
209
210     public boolean mustNamespaceDiffer() {
211         return true;
212     }
213 }
214
Popular Tags