KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > treeeditor > newtype > ImportSchemaNewType


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.treeeditor.newtype;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24
25 import org.netbeans.modules.xml.schema.model.Schema;
26 import org.netbeans.modules.xml.schema.model.SchemaModel;
27 import org.netbeans.modules.xml.wsdl.model.Definitions;
28 import org.netbeans.modules.xml.wsdl.model.Types;
29 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
30 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
31 import org.netbeans.modules.xml.wsdl.model.extensions.xsd.WSDLSchema;
32 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.UIUtilities;
33 import org.netbeans.modules.xml.wsdl.ui.view.ImportSchemaCustomizer;
34 import org.openide.DialogDescriptor;
35 import org.openide.DialogDisplayer;
36 import org.openide.util.NbBundle;
37 import org.openide.util.datatransfer.NewType;
38
39 /**
40  * NewType for importing a schema file.
41  *
42  * @author Nathan Fiedler
43  */

44 public class ImportSchemaNewType extends NewType {
45     /** Component in which to create import. */
46     private WSDLComponent component;
47
48     /**
49      * Creates a new instance of ImportWSDLNewType.
50      *
51      * @param component the WSDL component in which to create the import.
52      */

53     public ImportSchemaNewType(WSDLComponent component) {
54         this.component = component;
55     }
56
57     @Override JavaDoc
58     public String JavaDoc getName() {
59         return NbBundle.getMessage(ImportSchemaNewType.class,
60                 "LBL_NewType_ImportSchema");
61     }
62
63     @Override JavaDoc
64     public void create() throws IOException JavaDoc {
65         // Create the new import with empty attributes.
66
WSDLModel model = component.getModel();
67         Definitions def = model.getDefinitions();
68         model.startTransaction();
69         Types types = def.getTypes();
70         if (types == null) {
71             types = model.getFactory().createTypes();
72         }
73         Schema schema = null;
74         String JavaDoc tns = def.getTargetNamespace();
75         if (tns != null) {
76             Collection JavaDoc<Schema> schemas = types.getSchemas();
77             if (schemas != null) {
78                 for (Schema s : schemas) {
79                     if (s.getTargetNamespace() != null && s.getTargetNamespace().equals(tns)) {
80                         schema = s;
81                         break;
82                     }
83                 }
84             }
85         }
86         WSDLSchema wsdlSchema = null;
87         if (schema == null) {
88             wsdlSchema = model.getFactory().createWSDLSchema();
89             SchemaModel schemaModel = wsdlSchema.getSchemaModel();
90             schema = schemaModel.getSchema();
91             schema.setTargetNamespace(model.getDefinitions().getTargetNamespace());
92         }
93
94         // The customizer will create the new import(s).
95
// Note this happens during the transaction, which is unforunate
96
// but supposedly unavoidable.
97
ImportSchemaCustomizer customizer = new ImportSchemaCustomizer(schema, model);
98         DialogDescriptor descriptor = UIUtilities.getCreatorDialog(
99                 customizer, NbBundle.getMessage(ImportSchemaNewType.class,
100                 "LBL_NewType_ImportCustomizer"), true);
101         descriptor.setValid(false);
102         Object JavaDoc result = DialogDisplayer.getDefault().notify(descriptor);
103         
104         // If okay, add the import to the model.
105
if (result == DialogDescriptor.OK_OPTION) {
106             // The customizer has, by this time, already made its changes
107
// to the model, so there is nothing for us to do here.
108
if (def.getTypes() == null) {
109                 def.setTypes(types);
110             }
111             if (wsdlSchema != null) {
112                 types.addExtensibilityElement(wsdlSchema);
113             }
114         }
115         // In either case, end the transaction.
116
model.endTransaction();
117     }
118 }
119
Popular Tags