KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > impl > ElementImpl


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.axi.impl;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.axi.AXIComponent;
25 import org.netbeans.modules.xml.axi.AXIModel;
26 import org.netbeans.modules.xml.axi.AXIType;
27 import org.netbeans.modules.xml.axi.AbstractAttribute;
28 import org.netbeans.modules.xml.axi.Compositor;
29 import org.netbeans.modules.xml.axi.ContentModel;
30 import org.netbeans.modules.xml.axi.Element;
31 import org.netbeans.modules.xml.axi.datatype.Datatype;
32 import org.netbeans.modules.xml.axi.visitor.AXIVisitor;
33 import org.netbeans.modules.xml.schema.model.Form;
34 import org.netbeans.modules.xml.schema.model.SchemaComponent;
35 import org.netbeans.modules.xml.schema.model.SimpleType;
36
37 /**
38  * Element implementation.
39  * @author Samaresh (Samaresh.Panda@Sun.Com)
40  */

41 public final class ElementImpl extends Element {
42     
43     /**
44      * Creates a new instance of ElementImpl
45      */

46     public ElementImpl(AXIModel model) {
47         super(model);
48     }
49     
50     /**
51      * Creates a new instance of ElementImpl
52      */

53     public ElementImpl(AXIModel model, SchemaComponent schemaComponent) {
54         super(model, schemaComponent);
55     }
56     
57     /**
58      * Returns true if it is a reference, false otherwise.
59      */

60     public boolean isReference() {
61         return false;
62     }
63     
64     /**
65      * Returns the referent if isReference() is true.
66      */

67     public Element getReferent() {
68         return null;
69     }
70     
71     /**
72      * Returns abstract property.
73      */

74     public boolean getAbstract() {
75         return isAbstract;
76     }
77     
78     /**
79      * Sets the abstract property.
80      */

81     public void setAbstract(boolean value) {
82         boolean oldValue = getAbstract();
83         if(oldValue != value) {
84             this.isAbstract = value;
85             firePropertyChangeEvent(PROP_ABSTRACT, oldValue, value);
86         }
87     }
88     
89     /**
90      * Returns the block.
91      */

92     public String JavaDoc getBlock() {
93         return block;
94     }
95     
96     /**
97      * Sets the block property.
98      */

99     public void setBlock(String JavaDoc value) {
100         String JavaDoc oldValue = getBlock();
101         if( (oldValue == null && value == null) ||
102                 (oldValue != null && oldValue.equals(value)) ) {
103             return;
104         }
105         this.block = value;
106         firePropertyChangeEvent(PROP_BLOCK, oldValue, value);
107     }
108     
109     /**
110      * Returns the final property.
111      */

112     public String JavaDoc getFinal() {
113         return finalValue;
114     }
115     
116     /**
117      * Sets the final property.
118      */

119     public void setFinal(String JavaDoc value) {
120         String JavaDoc oldValue = getFinal();
121         if( (oldValue == null && value == null) ||
122                 (oldValue != null && oldValue.equals(value)) ) {
123             return;
124         }
125         this.finalValue = value;
126         firePropertyChangeEvent(PROP_FINAL, oldValue, value);
127     }
128     
129     /**
130      * Returns the fixed value.
131      */

132     public String JavaDoc getFixed() {
133         return fixedValue;
134     }
135     
136     /**
137      * Sets the fixed value.
138      */

139     public void setFixed(String JavaDoc value) {
140         String JavaDoc oldValue = getFixed();
141         if( (oldValue == null && value == null) ||
142                 (oldValue != null && oldValue.equals(value)) ) {
143             return;
144         }
145         this.fixedValue = value;
146         firePropertyChangeEvent(PROP_FIXED, oldValue, value);
147     }
148     
149     /**
150      * Returns the default value.
151      */

152     public String JavaDoc getDefault() {
153         return defaultValue;
154     }
155     
156     /**
157      * Sets the default value.
158      */

159     public void setDefault(String JavaDoc value) {
160         String JavaDoc oldValue = getDefault();
161         if( (oldValue == null && value == null) ||
162                 (oldValue != null && oldValue.equals(value)) ) {
163             return;
164         }
165         this.defaultValue = value;
166         firePropertyChangeEvent(PROP_DEFAULT, oldValue, value);
167     }
168     
169     /**
170      * Returns the form.
171      */

172     public Form getForm() {
173         return form;
174     }
175     
176     /**
177      * Sets the form.
178      */

179     public void setForm(Form value) {
180         Form oldValue = getForm();
181         if(oldValue != value) {
182             this.form = value;
183             firePropertyChangeEvent(PROP_FORM, oldValue, value);
184         }
185     }
186     
187     /**
188      * Returns the nillable.
189      */

190     public boolean getNillable() {
191         return isNillable;
192     }
193     
194     /**
195      * Sets the nillable property.
196      */

197     public void setNillable(boolean value) {
198         boolean oldValue = getNillable();
199         if(oldValue != value) {
200             this.isNillable = value;
201             firePropertyChangeEvent(PROP_NILLABLE, oldValue, value);
202         }
203     }
204     
205     /**
206      * Returns the AXIType.
207      */

208     public AXIType getType() {
209         if(axiType != null)
210             return axiType;
211         
212         this.axiType = Util.getAXIType(this, typeSchemaComponent);
213         return axiType;
214     }
215     
216     /**
217      * Sets the AXIType.
218      */

219     public void setType(AXIType newValue) {
220         if( (this == newValue) ||
221                 (this.isGlobal() && (newValue instanceof Element)) )
222             return;
223         
224         if(newValue instanceof Element) {
225             setElementAsType(newValue);
226             return;
227         }
228         
229         AXIType oldValue = getType();
230         if(!Util.canSetType(oldValue, newValue))
231             return;
232         
233         updateChildren(oldValue, newValue);
234         this.axiType = newValue;
235         firePropertyChangeEvent(PROP_TYPE, oldValue, newValue);
236         setTypeSchemaComponent(getSchemaType(newValue));
237         if(newValue instanceof ContentModel) {
238             ((ContentModel)newValue).addListener(this);
239         }
240     }
241     
242     private void setElementAsType(final AXIType newValue) {
243         if(newValue == this)
244             return;
245         int index = this.getIndex();
246         AXIComponent parent = getParent();
247         Element ref = getModel().getComponentFactory().createElementReference((Element)newValue);
248         parent.removeChild(this);
249         parent.insertAtIndex(Element.PROP_ELEMENT_REF, ref, index);
250     }
251     
252     /**
253      * Overwrites populateChildren of AXIComponent.
254      *
255      * An AXI element can keep LocalElement, GlobalElement or
256      * ElementReference as its peer. For the local and global,
257      * element, the element type's children becomes this AXI
258      * element's children. For ElementReference, the global element's
259      * type becomes this AXI element's children.
260      */

261     public void populateChildren(List JavaDoc<AXIComponent> children) {
262         if(getPeer() == null) {
263             return;
264         }
265         
266         //populate children from the element's type
267
SchemaComponent type = Util.getSchemaType((AXIModelImpl)getModel(), getPeer());
268         setTypeSchemaComponent(type);
269         
270         if(type == null || type instanceof SimpleType)
271             return;
272         
273         AXIModelBuilder builder = new AXIModelBuilder(this);
274         builder.populateChildren(type, false, children);
275     }
276     
277     /**
278      * Returns the last value for the element's type.
279      */

280     SchemaComponent getTypeSchemaComponent() {
281         return typeSchemaComponent;
282     }
283     
284     /**
285      * Sets the new value for the element's type.
286      */

287     void setTypeSchemaComponent(SchemaComponent type) {
288         this.typeSchemaComponent = type;
289     }
290     
291     private SchemaComponent getSchemaType(AXIType axiType) {
292         if(axiType instanceof Datatype)
293             return null;
294         if(axiType instanceof ContentModel)
295             return ((ContentModel)axiType).getPeer();
296         if(axiType instanceof AnonymousType)
297             return ((AnonymousType)axiType).getPeer();
298         
299         return null;
300     }
301     
302     /**
303      * OLD VALUE NEW VALUE RESULT
304      * SType LCT return
305      * SType GCT add proxy children
306      * LCT SType remove all and return
307      * LCT GCT remove all and add proxy children
308      * GCT SType remove all and return
309      * GCT LCT not allowed
310      */

311     private void updateChildren(AXIType oldValue, AXIType newValue) {
312         //do not remove children if the old type was SimpleType and
313
//user added children on top of it, that makes the type as anonymous.
314
if(oldValue instanceof Datatype && newValue instanceof AnonymousType) {
315             return;
316         }
317                 
318         //remove all children anyway
319
removeAllChildren();
320         if( (newValue == null) || (newValue instanceof Datatype) ) {
321             return;
322         }
323         
324         //remove listener from old content model
325
if(oldValue != null && oldValue instanceof ContentModel) {
326             ContentModel cm = (ContentModel)oldValue;
327             cm.removeListener(this);
328         }
329         
330         //add proxy children for the new content model
331
if(newValue instanceof ContentModel)
332             Util.addProxyChildren(this, (ContentModel)newValue, null);
333         
334         if(newValue instanceof AnonymousType) {
335             List JavaDoc<AXIComponent> children = new ArrayList JavaDoc<AXIComponent>();
336             AXIModelBuilder builder = new AXIModelBuilder(this);
337             builder.populateChildren( ((AnonymousType)newValue).getPeer(), false, children);
338             for(AXIComponent child : children) {
339                 this.appendChild(child);
340             }
341         }
342     }
343     
344     /**
345      * Represents a local complex type.
346      */

347     public static class AnonymousType implements AXIType {
348         private SchemaComponent schemaComponent;
349         public AnonymousType(SchemaComponent schemaComponent) {
350             this.schemaComponent = schemaComponent;
351         }
352         public String JavaDoc getName() {
353             return null;
354         }
355         public void accept(AXIVisitor visitor) {
356         }
357         public SchemaComponent getPeer() {
358             return schemaComponent;
359         }
360     }
361     
362     /**
363      * Overwrites addCompositor of AXIContainer.
364      */

365     public void addCompositor(Compositor compositor) {
366         if( getType() != null && getType() instanceof ContentModel &&
367                 getModel() != ((ContentModel)getType()).getModel() ) {
368             //no drops allowed when the element's type
369
//belongs to some other model
370
return;
371         }
372         
373         if(getType() instanceof Datatype) {
374             setType(new AnonymousType(null));
375         }
376         super.addCompositor(compositor);
377     }
378     
379     /**
380      * Overwrites addAttribute of AXIContainer.
381      */

382     public void addAttribute(AbstractAttribute attribute) {
383         AXIType type = getType();
384         if(type != null && type instanceof ContentModel) {
385             ((ContentModel)type).addAttribute(attribute);
386             return;
387         }
388         
389         if(type instanceof Datatype) {
390             setType(new AnonymousType(null));
391         }
392     
393         super.addAttribute(attribute);
394     }
395     
396     
397     private AXIType axiType;
398     private SchemaComponent typeSchemaComponent;
399 }
400
Popular Tags