KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > newtype > AdvancedSchemaComponentNewType


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 /*
21  * AdvancedSchemaComponentNewType.java
22  *
23  * Created on January 19, 2006, 1:31 PM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
30
31 // java imports
32
import java.awt.Dialog JavaDoc;
33 import org.netbeans.modules.xml.schema.model.Import;
34 import org.netbeans.modules.xml.schema.model.Include;
35 import org.netbeans.modules.xml.schema.model.Redefine;
36 import org.netbeans.modules.xml.schema.model.Schema;
37
38 //netbeans imports
39
import org.openide.DialogDisplayer;
40 import org.openide.NotifyDescriptor;
41 import org.openide.util.NbBundle;
42 import org.openide.util.datatransfer.NewType;
43
44 //local imports
45
import org.netbeans.modules.xml.schema.model.SchemaModel;
46 import org.netbeans.modules.xml.schema.model.SchemaComponent;
47 import org.netbeans.modules.xml.schema.model.SchemaComponentReference;
48 import org.netbeans.modules.xml.schema.ui.basic.UIUtilities;
49 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.ImportCreator;
50 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.IncludeCreator;
51 import org.netbeans.modules.xml.schema.ui.nodes.categorized.customizer.RedefineCreator;
52 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
53 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
54 import org.openide.DialogDescriptor;
55 import org.openide.filesystems.FileObject;
56 import org.openide.loaders.DataObject;
57 import org.openide.loaders.DataObjectNotFoundException;
58
59 /**
60  *
61  * @author Ajit Bhate (ajit.bhate@Sun.Com)
62  */

63 public class AdvancedSchemaComponentNewType extends NewType {
64     
65     private SchemaComponentReference<? extends SchemaComponent> reference;
66     private Class JavaDoc<? extends SchemaComponent> childType;
67     private AdvancedSchemaComponentCreator creator;
68     private SchemaComponent component;
69     private SchemaComponent container;
70     
71     /**
72      * Creates a new instance of AdvancedSchemaComponentNewType
73      */

74     public AdvancedSchemaComponentNewType(SchemaComponentReference<? extends SchemaComponent>
75             reference, Class JavaDoc<? extends SchemaComponent> childType) {
76         super();
77         this.reference=reference;
78         this.childType=childType;
79         this.creator = new AdvancedSchemaComponentCreator();
80     }
81     
82     public String JavaDoc getName() {
83         return NbBundle.getMessage(AdvancedSchemaComponentNewType.class,
84                 "LBL_NewType_".concat(getChildType().getSimpleName()));
85     }
86     
87     public void create() {
88         if (!canCreate()) {
89             showIncompleteDefinitionMessage();
90             return;
91         }
92         SchemaModel model = getSchemaComponent().getModel();
93         assert model != null;
94         boolean showComponent = false;
95         try {
96             if(customize()) {
97                 model.startTransaction();
98                 addComponent(container);
99                 showComponent = true;
100             }
101         } finally {
102             if (model.isIntransaction()) {
103                 model.endTransaction();
104             }
105         }
106         
107         // Select in view, only if component was successfully created.
108
if (showComponent) {
109             try {
110                 FileObject fobj = (FileObject) model.getModelSource().
111                         getLookup().lookup(FileObject.class);
112                 if (fobj != null) {
113                     DataObject dobj = DataObject.find(fobj);
114                     if (dobj != null) {
115                         ViewComponentCookie svc = (ViewComponentCookie) dobj.getCookie(
116                                 ViewComponentCookie.class);
117                         if (svc != null) {
118                             svc.view(ViewComponentCookie.View.STRUCTURE,
119                                     getComponent());
120                         }
121                     }
122                 }
123             } catch (DataObjectNotFoundException donfe) {
124             }
125         }
126     }
127     
128     public boolean canCreate() {
129         if(getComponent()==null) {
130             setComponent(createComponent());
131             setContainer(findContainer());
132         }
133         return getContainer()!=null;
134     }
135     
136     /**
137      * The container of the new type.
138      * In most cases it will be getSchemaComponent(), but need to ensure correct type.
139      * It uses AdvancedSchemaComponentCreator to find appropriate container.
140      */

141     protected SchemaComponent findContainer() {
142         return getCreator().findContainer(getSchemaComponent(), getComponent());
143     }
144     
145     /**
146      * This api adds required new type to the container.
147      * This is called from create.
148      * The create method ensures a transaction and does error reporting.
149      */

150     protected void addComponent(SchemaComponent container) {
151         getCreator().add(container, getComponent());
152     }
153     
154     /**
155      * This api creates required new type.
156      * uses SchemaComponentCreator to add
157      */

158     protected SchemaComponent createComponent() {
159         return SchemaComponentCreator.createComponent(getSchemaComponent().
160                 getModel().getFactory(), getChildType());
161     }
162     
163     protected SchemaComponent getSchemaComponent() {
164         return getReference().get();
165     }
166     
167     protected SchemaComponentReference<? extends SchemaComponent> getReference() {
168         return reference;
169     }
170     
171     protected Class JavaDoc<? extends SchemaComponent> getChildType() {
172         return childType;
173     }
174     
175     
176     protected AdvancedSchemaComponentCreator getCreator() {
177         return creator;
178     }
179     
180     /**
181      * getter for newly created component
182      */

183     protected SchemaComponent getComponent() {
184         return component;
185     }
186     
187     /**
188      * setter for newly created component
189      */

190     protected void setComponent(SchemaComponent component) {
191         this.component = component;
192     }
193     
194     /**
195      * getter for container
196      */

197     protected SchemaComponent getContainer() {
198         return container;
199     }
200     
201     /**
202      * setter for container
203      */

204     protected void setContainer(SchemaComponent container) {
205         this.container = container;
206     }
207     
208     /**
209      * This apis check if newtype needs a customizer and returns true,
210      * if customizer is not needed or if user OKs customization, false otherwise.
211      */

212     protected boolean customize() {
213         // XXX: This bit is an ugly hack, need a better way to create a
214
// different customizer depending on whether the component
215
// is new versus existing.
216
SchemaComponent comp = getComponent();
217         Customizer customizer;
218         boolean created = true;
219         if (comp instanceof Import) {
220             SchemaModel model = getSchemaComponent().getModel();
221             Schema schema = model.getSchema();
222             customizer = new ImportCreator(schema);
223         } else if (comp instanceof Include) {
224             SchemaModel model = getSchemaComponent().getModel();
225             Schema schema = model.getSchema();
226             customizer = new IncludeCreator(schema);
227         } else if (comp instanceof Redefine) {
228             SchemaModel model = getSchemaComponent().getModel();
229             Schema schema = model.getSchema();
230             customizer = new RedefineCreator(schema);
231         } else {
232             customizer = getCreator().createCustomizer(comp, getContainer());
233             created = false;
234         }
235         if(customizer==null || customizer.getComponent()==null) return true;
236         DialogDescriptor descriptor = UIUtilities.
237                 getCustomizerDialog(customizer,getName(),true);
238         Dialog JavaDoc dlg = DialogDisplayer.getDefault().createDialog(descriptor);
239         dlg.setTitle(NbBundle.getMessage(AdvancedSchemaComponentNewType.class,
240                 "LBL_Customizer_".concat(getChildType().getSimpleName())));
241         dlg.getAccessibleContext().setAccessibleDescription(dlg.getTitle());
242         dlg.setVisible(true);
243         // For the created case, return false so that the component will not
244
// be created again, and then not have any customization performed
245
// on it; the creators have already created the component(s).
246
return !created && descriptor.getValue() == DialogDescriptor.OK_OPTION;
247     }
248     /**
249      * This will show a message to user if this newtype can't be created
250      *
251      */

252     private void showIncompleteDefinitionMessage() {
253         String JavaDoc message = NbBundle.getMessage(AdvancedSchemaComponentNewType.class,
254                 "MSG_NewType_IncompleteDefinition", getName().toLowerCase());
255         NotifyDescriptor.Message descriptor =
256                 new NotifyDescriptor.Message(message);
257         DialogDisplayer.getDefault().notify(descriptor);
258     }
259 }
260
Popular Tags