KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import org.netbeans.modules.xml.schema.model.Import;
26 import org.netbeans.modules.xml.schema.model.Schema;
27 import org.netbeans.modules.xml.schema.model.SchemaModel;
28 import org.netbeans.modules.xml.schema.ui.basic.NameGenerator;
29 import org.netbeans.modules.xml.xam.Model;
30 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
31 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceCreator;
32 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDataNode;
33 import org.netbeans.modules.xml.xam.ui.customizer.ExternalReferenceDecorator;
34 import org.openide.nodes.Node;
35 import org.openide.util.HelpCtx;
36 import org.openide.util.NbBundle;
37
38 /**
39  * An import creator.
40  *
41  * @author Administrator
42  * @author Nathan Fiedler
43  */

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

55     public ImportCreator(Schema schema) {
56         super(schema, null);
57     }
58
59     @Override JavaDoc
60     public void applyChanges() throws IOException JavaDoc {
61         super.applyChanges();
62         Schema schema = getModelComponent();
63         SchemaModel model = schema.getModel();
64         List JavaDoc<Node> nodes = getSelectedNodes();
65         for (Node node : nodes) {
66             Import imp = model.getFactory().createImport();
67             // Save the location.
68
imp.setSchemaLocation(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) schema;
84                     Map JavaDoc prefixes = adc.getPrefixes();
85                     if (!prefixes.containsKey(prefix)) {
86                         adc.addPrefix(prefix, namespace);
87                     }
88                 }
89             }
90             schema.addExternalReference(imp);
91
92             // Check whether namespace was added. Temporary fix till import
93
// dialog mandates the prefix. -- XXX: has it been done yet?
94
if (NameGenerator.getNamespacePrefix(imp.getNamespace(), schema) == null) {
95                 //create a prefix for this namespace
96
String JavaDoc prefix = NameGenerator.getInstance().generateNamespacePrefix(null, model);
97                 ((AbstractDocumentComponent) schema).addPrefix(prefix, imp.getNamespace());
98             }
99         }
100     }
101
102     public HelpCtx getHelpCtx() {
103         return new HelpCtx(ImportCreator.class);
104     }
105
106     protected String JavaDoc getTargetNamespace(Model model) {
107         return ((SchemaModel) model).getSchema().getTargetNamespace();
108     }
109
110     @SuppressWarnings JavaDoc("unchecked")
111     protected Map JavaDoc<String JavaDoc, String JavaDoc> getPrefixes(Model model) {
112         SchemaModel sm = (SchemaModel) model;
113         AbstractDocumentComponent schema =
114                 (AbstractDocumentComponent) sm.getSchema();
115         return schema.getPrefixes();
116     }
117
118     protected ExternalReferenceDecorator getNodeDecorator() {
119         if (decorator == null) {
120             decorator = new SchemaReferenceDecorator(this);
121         }
122         return decorator;
123     }
124
125     public boolean mustNamespaceDiffer() {
126         return true;
127     }
128
129     protected String JavaDoc referenceTypeName() {
130         return NbBundle.getMessage(ImportCreator.class,
131                 "LBL_ImportCreator_Type");
132     }
133 }
134
Popular Tags