KickJava   Java API By Example, From Geeks To Geeks.

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


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  * SchemaComponentCreator.java
22  *
23  * Created on April 20, 2006, 4:12 PM
24  *
25  */

26
27 package org.netbeans.modules.xml.schema.ui.nodes.categorized.newtype;
28
29 import org.netbeans.modules.xml.schema.model.All;
30 import org.netbeans.modules.xml.schema.model.Annotation;
31 import org.netbeans.modules.xml.schema.model.AnyAttribute;
32 import org.netbeans.modules.xml.schema.model.AnyElement;
33 import org.netbeans.modules.xml.schema.model.AppInfo;
34 import org.netbeans.modules.xml.schema.model.AttributeGroupReference;
35 import org.netbeans.modules.xml.schema.model.AttributeReference;
36 import org.netbeans.modules.xml.schema.model.Choice;
37 import org.netbeans.modules.xml.schema.model.ComplexContent;
38 import org.netbeans.modules.xml.schema.model.ComplexContentRestriction;
39 import org.netbeans.modules.xml.schema.model.ComplexExtension;
40 import org.netbeans.modules.xml.schema.model.Documentation;
41 import org.netbeans.modules.xml.schema.model.ElementReference;
42 import org.netbeans.modules.xml.schema.model.Enumeration;
43 import org.netbeans.modules.xml.schema.model.Field;
44 import org.netbeans.modules.xml.schema.model.FractionDigits;
45 import org.netbeans.modules.xml.schema.model.GlobalAttribute;
46 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup;
47 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
48 import org.netbeans.modules.xml.schema.model.GlobalElement;
49 import org.netbeans.modules.xml.schema.model.GlobalGroup;
50 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
51 import org.netbeans.modules.xml.schema.model.GroupReference;
52 import org.netbeans.modules.xml.schema.model.Import;
53 import org.netbeans.modules.xml.schema.model.Include;
54 import org.netbeans.modules.xml.schema.model.Key;
55 import org.netbeans.modules.xml.schema.model.KeyRef;
56 import org.netbeans.modules.xml.schema.model.Length;
57 import org.netbeans.modules.xml.schema.model.List;
58 import org.netbeans.modules.xml.schema.model.LocalAttribute;
59 import org.netbeans.modules.xml.schema.model.LocalComplexType;
60 import org.netbeans.modules.xml.schema.model.LocalElement;
61 import org.netbeans.modules.xml.schema.model.LocalSimpleType;
62 import org.netbeans.modules.xml.schema.model.MaxExclusive;
63 import org.netbeans.modules.xml.schema.model.MaxInclusive;
64 import org.netbeans.modules.xml.schema.model.MaxLength;
65 import org.netbeans.modules.xml.schema.model.MinExclusive;
66 import org.netbeans.modules.xml.schema.model.MinInclusive;
67 import org.netbeans.modules.xml.schema.model.MinLength;
68 import org.netbeans.modules.xml.schema.model.Notation;
69 import org.netbeans.modules.xml.schema.model.Pattern;
70 import org.netbeans.modules.xml.schema.model.Redefine;
71 import org.netbeans.modules.xml.schema.model.Schema;
72 import org.netbeans.modules.xml.schema.model.SchemaComponent;
73 import org.netbeans.modules.xml.schema.model.SchemaComponentFactory;
74 import org.netbeans.modules.xml.schema.model.Selector;
75 import org.netbeans.modules.xml.schema.model.Sequence;
76 import org.netbeans.modules.xml.schema.model.SimpleContent;
77 import org.netbeans.modules.xml.schema.model.SimpleContentRestriction;
78 import org.netbeans.modules.xml.schema.model.SimpleExtension;
79 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
80 import org.netbeans.modules.xml.schema.model.TotalDigits;
81 import org.netbeans.modules.xml.schema.model.Union;
82 import org.netbeans.modules.xml.schema.model.Unique;
83 import org.netbeans.modules.xml.schema.model.Whitespace;
84 import org.netbeans.modules.xml.schema.model.visitor.DeepSchemaVisitor;
85 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
86
87 /**
88  *
89  * @author Ajit Bhate
90  */

91 public class SchemaComponentCreator extends DeepSchemaVisitor
92 {
93     protected enum Operation { VERIFY_ADD, ADD, REMOVE , SHOW_CUSTOMIZER};
94     
95     // operation to perform
96
private Operation operation;
97     // parent
98
private SchemaComponent parent;
99     // child
100
private SchemaComponent child;
101     
102     // container where child can be added
103
private SchemaComponent container;
104     
105     // customizer for child
106
private Customizer customizer;
107     
108     // indicates if child can be added to container
109
// used to prevent overwriting of existing definitions, or honor mutual
110
// exclusivity of certain components
111
private boolean addAllowed = true;
112
113     //levels to visit in findContainer
114
private int visitLevels = 1;
115     private int currentLevel = 0;
116     
117     /** Creates a new instance of SchemaComponentCreator */
118     public SchemaComponentCreator()
119     {
120     }
121     
122     /**
123      * Creates a customizer for newly created component, if needed.
124      * @param child Child component which customizer to create.
125      * @param parent The parent under which child is to be added
126      * @return The customizer for child component.
127      * Null if child does not have customizer.
128      */

129     public Customizer createCustomizer(SchemaComponent child,
130             SchemaComponent parent)
131     {
132         //init fields
133
Customizer result = null;
134         this.operation = Operation.SHOW_CUSTOMIZER;
135         this.parent = parent;
136         this.child = null;
137         setCustomizer(null);
138         // process
139
child.accept(this);
140         result = getCustomizer();
141         //reset fields
142
setCustomizer(null);
143         this.parent = null;
144         return result;
145     }
146     
147     /**
148      * Finds container for child to be added under given parent.
149      * @param parent The parent under which child is to be added
150      * @param child Child component to be added
151      * @return The schema component under parent (can be same as parent)
152      * where child can be added. Null if child cant be added.
153      */

154     public SchemaComponent findContainer(SchemaComponent parent, SchemaComponent child)
155     {
156         //init fields
157
SchemaComponent result = null;
158         this.operation = Operation.VERIFY_ADD;
159         this.parent = null;
160         this.child = child;
161         this.container = null;
162         setCustomizer(null);
163         // process
164
parent.accept(this);
165         result = this.container;
166         //reset fields
167
this.container = null;
168         return result;
169     }
170     
171     /**
172      * Adds child component directly under given parent.
173      * @param parent The parent under which child is to be added
174      * @param child Child component to be added
175      */

176     public void add(SchemaComponent parent, SchemaComponent child)
177     {
178         //init fields
179
this.operation = Operation.ADD;
180         this.parent = parent;
181         this.child = null;
182         this.container = null;
183         setCustomizer(null);
184         // process
185
child.accept(this);
186         //reset fields
187
this.parent = null;
188         this.operation = null;
189     }
190     
191     // if operation is null just checks if component can be added
192
protected void visitChildren(SchemaComponent sc)
193     {
194         if(getOperation() == Operation.VERIFY_ADD)
195         {
196             if(sc.canPaste(getChild()) && isAddAllowed())
197             {
198                 container = sc;
199                 return;
200             }
201             if(getCurrentLevel()<getVisitLevels())
202             {
203                 setCurrentLevel(getCurrentLevel()+1);
204                 setAddAllowed(true);
205                 super.visitChildren(sc);
206                 setCurrentLevel(getCurrentLevel()-1);
207             }
208         }
209         else if(getOperation() == Operation.ADD)
210         {
211             getParent().getModel().addChildComponent(getParent(), sc,-1);
212         }
213         else if(getOperation() == Operation.REMOVE)
214         {
215             getParent().getModel().removeChildComponent(sc);
216         }
217     }
218     
219     protected Operation getOperation()
220     {
221         return operation;
222     }
223     
224     protected SchemaComponent getParent()
225     {
226         return parent;
227     }
228     
229     protected void setParent(SchemaComponent parent)
230     {
231         this.parent = parent;
232     }
233
234     protected SchemaComponent getChild()
235     {
236         return child;
237     }
238
239     private int getVisitLevels()
240     {
241         return visitLevels;
242     }
243
244     /**
245      * This api sets the levels for deep visiting during finding container
246      * operation. Default level is 1, which will not do schema deep visiting.
247      * If set to 2, the children of the component will be tested.
248      */

249     protected void setVisitLevels(int visitLevels)
250     {
251         if(visitLevels<1)
252             throw new IllegalArgumentException JavaDoc("visit levels must be positive integer");
253         this.visitLevels = visitLevels;
254     }
255
256     private int getCurrentLevel()
257     {
258         return currentLevel;
259     }
260
261     private void setCurrentLevel(int currentLevel)
262     {
263         this.currentLevel = currentLevel;
264     }
265     
266     /**
267      * This api checks if given child component can be added to the container.
268      * Subclass implementations should invoke setAddAllowed to allow/prevent
269      * addition of child, depending upon current definition of container
270      */

271     protected boolean isAddAllowed()
272     {
273         return addAllowed;
274     }
275
276     /**
277      * This api sets the canAdd flag, which is indicator of whether given child
278      * component can be added to the container.
279      * Subclass implementations should pass in true to allow and false to prevent
280      * addition of child to current container, depending upon current definition.
281      */

282     protected void setAddAllowed(boolean addAllowed)
283     {
284         this.addAllowed = addAllowed;
285     }
286
287     public static SchemaComponent createComponent(SchemaComponentFactory factory,
288             Class JavaDoc<? extends SchemaComponent> type)
289     {
290         SchemaComponent result = null;
291         if(All.class.isAssignableFrom(type))
292         {
293             result = factory.createAll();
294         }
295         else if(Annotation.class.isAssignableFrom(type))
296         {
297             result = factory.createAnnotation();
298         }
299         else if(AnyElement.class.isAssignableFrom(type))
300         {
301             result = factory.createAny();
302         }
303         else if(AnyAttribute.class.isAssignableFrom(type))
304         {
305             result = factory.createAnyAttribute();
306         }
307         else if(AppInfo.class.isAssignableFrom(type))
308         {
309             result = factory.createAppInfo();
310         }
311         else if(AttributeGroupReference.class.isAssignableFrom(type))
312         {
313             result = factory.createAttributeGroupReference();
314         }
315         else if(Choice.class.isAssignableFrom(type))
316         {
317             result = factory.createChoice();
318         }
319         else if(ComplexContent.class.isAssignableFrom(type))
320         {
321             result = factory.createComplexContent();
322         }
323         else if(ComplexContentRestriction.class.isAssignableFrom(type))
324         {
325             result = factory.createComplexContentRestriction();
326         }
327         else if(ComplexExtension.class.isAssignableFrom(type))
328         {
329             result = factory.createComplexExtension();
330         }
331         else if(Documentation.class.isAssignableFrom(type))
332         {
333             result = factory.createDocumentation();
334         }
335         else if(Enumeration.class.isAssignableFrom(type))
336         {
337             result = factory.createEnumeration();
338         }
339         else if(Field.class.isAssignableFrom(type))
340         {
341             result = factory.createField();
342         }
343         else if(FractionDigits.class.isAssignableFrom(type))
344         {result = factory.createFractionDigits();
345         }
346         else if(GlobalAttribute.class.isAssignableFrom(type))
347         {
348             result = factory.createGlobalAttribute();
349         }
350         else if(GlobalAttributeGroup.class.isAssignableFrom(type))
351         {
352             result = factory.createGlobalAttributeGroup();
353         }
354         else if(GlobalComplexType.class.isAssignableFrom(type))
355         {
356             result = factory.createGlobalComplexType();
357         }
358         else if(GlobalElement.class.isAssignableFrom(type))
359         {result = factory.createGlobalElement();
360         }
361         else if(GlobalSimpleType.class.isAssignableFrom(type))
362         {
363             result = factory.createGlobalSimpleType();
364         }
365         else if(GlobalGroup.class.isAssignableFrom(type))
366         {
367             result = factory.createGroupDefinition();
368         }
369         else if(GroupReference.class.isAssignableFrom(type))
370         {
371             result = factory.createGroupReference();
372         }
373         else if(Import.class.isAssignableFrom(type))
374         {
375             result = factory.createImport();
376         }
377         else if(Include.class.isAssignableFrom(type))
378         {
379             result = factory.createInclude();
380         }
381         else if(Key.class.isAssignableFrom(type))
382         {
383             result = factory.createKey();
384         }
385         else if(KeyRef.class.isAssignableFrom(type))
386         {
387             result = factory.createKeyRef();
388         }
389         else if(Length.class.isAssignableFrom(type))
390         {
391             result = factory.createLength();
392         }
393         else if(List.class.isAssignableFrom(type))
394         {
395             result = factory.createList();
396         }
397         else if(LocalAttribute.class.isAssignableFrom(type))
398         {
399             result = factory.createLocalAttribute();
400         }
401         else if(AttributeReference.class.isAssignableFrom(type))
402         {
403             result = factory.createAttributeReference();
404         }
405         else if(LocalComplexType.class.isAssignableFrom(type))
406         {
407             result = factory.createLocalComplexType();
408         }
409         else if(LocalElement.class.isAssignableFrom(type))
410         {
411             result = factory.createLocalElement();
412         }
413         else if(ElementReference.class.isAssignableFrom(type))
414         {
415             result = factory.createElementReference();
416         }
417         else if(LocalSimpleType.class.isAssignableFrom(type))
418         {
419             result = factory.createLocalSimpleType();
420         }
421         else if(MaxExclusive.class.isAssignableFrom(type))
422         {
423             result = factory.createMaxExclusive();
424         }
425         else if(MaxInclusive.class.isAssignableFrom(type))
426         {
427             result = factory.createMaxInclusive();
428         }
429         else if(MaxLength.class.isAssignableFrom(type))
430         {
431             result = factory.createMaxLength();
432         }
433         else if(MinInclusive.class.isAssignableFrom(type))
434         {
435             result = factory.createMinInclusive();
436         }
437         else if(MinExclusive.class.isAssignableFrom(type))
438         {
439             result = factory.createMinExclusive();
440         }
441         else if(MinLength.class.isAssignableFrom(type))
442         {
443             result = factory.createMinLength();
444         }
445         else if(Notation.class.isAssignableFrom(type))
446         {
447             result = factory.createNotation();
448         }
449         else if(Pattern.class.isAssignableFrom(type))
450         {
451             result = factory.createPattern();
452         }
453         else if(Redefine.class.isAssignableFrom(type))
454         {
455             result = factory.createRedefine();
456         }
457         else if(Schema.class.isAssignableFrom(type))
458         {
459             result = factory.createSchema();
460         }
461         else if(Sequence.class.isAssignableFrom(type))
462         {
463             result = factory.createSequence();
464         }
465         else if(Selector.class.isAssignableFrom(type))
466         {
467             result = factory.createSelector();
468         }
469         else if(SimpleContent.class.isAssignableFrom(type))
470         {
471             result = factory.createSimpleContent();
472         }
473         else if(SimpleContentRestriction.class.isAssignableFrom(type))
474         {
475             result = factory.createSimpleContentRestriction();
476         }
477         else if(SimpleExtension.class.isAssignableFrom(type))
478         {
479             result = factory.createSimpleExtension();
480         }
481         else if(SimpleTypeRestriction.class.isAssignableFrom(type))
482         {
483             result = factory.createSimpleTypeRestriction();
484         }
485         else if(TotalDigits.class.isAssignableFrom(type))
486         {
487             result = factory.createTotalDigits();
488         }
489         else if(Union.class.isAssignableFrom(type))
490         {
491             result = factory.createUnion();
492         }
493         else if(Unique.class.isAssignableFrom(type))
494         {
495             result = factory.createUnique();
496         }
497         else if(Whitespace.class.isAssignableFrom(type))
498         {
499             result = factory.createWhitespace();
500         }
501         return result;
502     }
503
504     protected Customizer getCustomizer()
505     {
506         return customizer;
507     }
508
509     protected void setCustomizer(Customizer customizer)
510     {
511         this.customizer = customizer;
512     }
513
514 }
515
Popular Tags