KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AttributeImpl.java
22  *
23  * Created on May 5, 2006, 12:04 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.axi.impl;
30
31 import org.netbeans.modules.xml.axi.AXIComponent;
32 import org.netbeans.modules.xml.axi.AXIModel;
33 import org.netbeans.modules.xml.axi.AXIType;
34 import org.netbeans.modules.xml.axi.Attribute;
35 import org.netbeans.modules.xml.axi.datatype.StringType;
36 import org.netbeans.modules.xml.axi.datatype.DatatypeFactory;
37 import org.netbeans.modules.xml.schema.model.SchemaComponent;
38 import org.netbeans.modules.xml.schema.model.Attribute.Use;
39 import org.netbeans.modules.xml.schema.model.Form;
40
41 /**
42  * Base and only implementation of Attribute.
43  *
44  * @author Samaresh (Samaresh.Panda@Sun.Com)
45  */

46 public final class AttributeImpl extends Attribute {
47             
48     /**
49      * Creates a new instance of AttributeImpl
50      */

51     public AttributeImpl(AXIModel model) {
52         super(model);
53         setDefaultDataType();
54     }
55     
56     /**
57      * Creates a new instance of AttributeImpl
58      */

59     public AttributeImpl(AXIModel model, SchemaComponent schemaComponent) {
60         super(model, schemaComponent);
61         setDefaultDataType();
62     }
63
64     /**
65      * Initializes the default datatype for this Attribute.
66      * Do NOT call setType() here, that'll inturn call
67      * getType(), which is expensive. Initialize instead.
68      */

69     private void setDefaultDataType() {
70         this.datatype = new StringType();
71     }
72                 
73     /**
74      * Returns true if it is a reference, false otherwise.
75      */

76     public boolean isReference() {
77         return false;
78     }
79     
80     /**
81      * Returns the referent if isReference() is true.
82      */

83     public Attribute getReferent() {
84         return null;
85     }
86     
87     /**
88      * Returns the name.
89      */

90     public String JavaDoc getName() {
91         return name;
92     }
93     
94     /**
95      * Sets the name.
96      */

97     public void setName(String JavaDoc name) {
98         String JavaDoc oldName = getName();
99         if( (oldName == null && name == null) ||
100             (oldName != null && oldName.equals(name)) ) {
101             return;
102         }
103         this.name = name;
104         firePropertyChangeEvent(PROP_NAME, oldName, name);
105     }
106         
107     /**
108      * Returns the type. This is expensive, since it uses a visitor
109      * to traverse to obtain the type information.
110      */

111     public AXIType getType() {
112         if(!datatypeInitialized && getPeer() != null) {
113             datatype = DatatypeFactory.getDefault().getDatatype(getModel(), getPeer());
114             datatypeInitialized = true;
115         }
116         
117         return datatype;
118     }
119     
120     /**
121      * Sets the type.
122      */

123     public void setType(AXIType value) {
124         if( (this == value) ||
125             (this.isGlobal() && (value instanceof Attribute)) )
126             return;
127         
128         if(value instanceof Attribute) {
129             setAttributeAsType(value);
130             return;
131         }
132         
133         AXIType oldValue = getType();
134         if( (oldValue == null && value == null) ||
135             (oldValue != null && oldValue.equals(value)) ) {
136             return;
137         }
138         this.datatype = value;
139         firePropertyChangeEvent(PROP_TYPE, oldValue, value);
140     }
141         
142     private void setAttributeAsType(final AXIType newValue) {
143         if(newValue == this)
144             return;
145         int index = this.getIndex();
146         AXIComponent parent = getParent();
147         Attribute ref = getModel().getComponentFactory().createAttributeReference((Attribute)newValue);
148         parent.removeChild(this);
149         parent.insertAtIndex(Attribute.PROP_ATTRIBUTE_REF, ref, index);
150     }
151     
152     /**
153      * Returns the form.
154      */

155     public Form getForm() {
156         return form;
157     }
158     
159     /**
160      * Sets the form.
161      */

162     public void setForm(Form value) {
163         Form oldValue = getForm();
164         if( (oldValue == null && value == null) ||
165             (oldValue != null && oldValue == value) ) {
166             return;
167         }
168         this.form = value;
169         firePropertyChangeEvent(PROP_FORM, oldValue, value);
170     }
171     
172     /**
173      * Returns the fixed value.
174      */

175     public String JavaDoc getFixed() {
176         return fixedValue;
177     }
178     
179     /**
180      * Sets the fixed value.
181      */

182     public void setFixed(String JavaDoc value) {
183         String JavaDoc oldValue = getFixed();
184         if( (oldValue == null && value == null) ||
185             (oldValue != null && oldValue.equals(value)) ) {
186             return;
187         }
188         this.fixedValue = value;
189         firePropertyChangeEvent(PROP_FIXED, oldValue, value);
190     }
191     
192     /**
193      * Returns the default value.
194      */

195     public String JavaDoc getDefault() {
196         return defaultValue;
197     }
198     
199     /**
200      * Sets the default value.
201      */

202     public void setDefault(String JavaDoc value) {
203         Object JavaDoc oldValue = getDefault();
204         if( (oldValue == null && value == null) ||
205             (oldValue != null && oldValue.equals(value)) ) {
206             return;
207         }
208         this.defaultValue = value;
209         firePropertyChangeEvent(PROP_DEFAULT, oldValue, value);
210     }
211     
212     /**
213      * Returns the use.
214      */

215     public Use getUse() {
216         return use;
217     }
218     
219     /**
220      * Sets the use.
221      */

222     public void setUse(Use value) {
223         Use oldValue = getUse();
224         if( (oldValue == null && value == null) ||
225             (oldValue != null && oldValue == value) ) {
226             return;
227         }
228         this.use = value;
229         firePropertyChangeEvent(PROP_USE, oldValue, value);
230     }
231     
232     
233     private boolean datatypeInitialized;
234 }
235
Popular Tags