KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AttributeRef.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.AXIComponent.ComponentType;
33 import org.netbeans.modules.xml.axi.AXIModel;
34 import org.netbeans.modules.xml.axi.AXIType;
35 import org.netbeans.modules.xml.axi.Attribute;
36 import org.netbeans.modules.xml.schema.model.SchemaComponent;
37 import org.netbeans.modules.xml.schema.model.Attribute.Use;
38 import org.netbeans.modules.xml.schema.model.Form;
39
40 /**
41  * Represents an Attribute reference. For an Attribute reference
42  * name, type and form must be absent, that is, calls on name, type
43  * and form must be delegated to the original.
44  *
45  * See http://www.w3.org/TR/xmlschema-1/#d0e2403.
46  *
47  * @author Samaresh (Samaresh.Panda@Sun.Com)
48  */

49 public class AttributeRef extends Attribute {
50    
51     /**
52      * Creates a new instance of AttributeRef
53      */

54     public AttributeRef(AXIModel model, Attribute referent) {
55         super(model, referent);
56     }
57     
58     /**
59      * Creates a new instance of AttributeRef
60      */

61     public AttributeRef(AXIModel model, SchemaComponent component, Attribute referent) {
62         super(model, component);
63         super.setSharedComponent(referent);
64     }
65     
66     /**
67      * Returns the type of this component,
68      * may be local, shared, proxy or reference.
69      * @see ComponentType.
70      */

71     public ComponentType getComponentType() {
72         return ComponentType.REFERENCE;
73     }
74     
75     /**
76      * Returns the referent if isReference() is true.
77      */

78     public Attribute getReferent() {
79         return (Attribute)getSharedComponent();
80     }
81         
82     /**
83      * Sets the new referent.
84      */

85     public void setRef(Attribute referent) {
86         AttributeImpl oldRef = (AttributeImpl) getReferent();
87         if(oldRef == referent)
88             return;
89         oldRef.removeListener(this);
90         super.setSharedComponent(referent);
91         firePropertyChangeEvent(PROP_ATTRIBUTE_REF, oldRef, referent);
92         forceFireEvent();
93     }
94     
95     /**
96      * Returns true if it is a reference, false otherwise.
97      */

98     public boolean isReference() {
99         return true;
100     }
101     
102     /**
103      * Returns the name.
104      */

105     public String JavaDoc getName() {
106         return getReferent().getName();
107     }
108     
109     /**
110      * Sets the name.
111      */

112     public void setName(String JavaDoc name) {
113         for(Attribute a : getModel().getRoot().getAttributes()) {
114             if(a.getName().equals(name)) {
115                 setRef(a);
116                 return;
117             }
118         }
119         getReferent().setName(name);
120     }
121         
122     /**
123      * Returns the type. This is expensive, since it uses a visitor
124      * to traverse to obtain the type information.
125      */

126     public AXIType getType() {
127         return getReferent().getType();
128     }
129     
130     /**
131      * Sets the type.
132      */

133     public void setType(AXIType type) {
134         if(type instanceof Attribute) {
135             setRef((Attribute)type);
136             return;
137         }
138         
139         int index = this.getIndex();
140         AXIComponent parent = getParent();
141         Attribute a = getModel().getComponentFactory().createAttribute();
142         a.setName(getReferent().getName());
143         parent.removeChild(this);
144         parent.insertAtIndex(Attribute.PROP_ATTRIBUTE, a, index);
145         a.setType(type);
146     }
147         
148     /**
149      * Returns the form.
150      */

151     public Form getForm() {
152         return getReferent().getForm();
153     }
154     
155     /**
156      * Sets the form.
157      */

158     public void setForm(Form form) {
159         getReferent().setForm(form);
160     }
161     
162     /**
163      * Returns the fixed value.
164      */

165     public String JavaDoc getFixed() {
166         return fixedValue;
167     }
168     
169     /**
170      * Sets the fixed value.
171      */

172     public void setFixed(String JavaDoc value) {
173         String JavaDoc oldValue = getFixed();
174         if( (oldValue == null && value == null) ||
175             (oldValue != null && oldValue.equals(value)) ) {
176             return;
177         }
178         this.fixedValue = value;
179         firePropertyChangeEvent(PROP_FIXED, oldValue, value);
180     }
181     
182     /**
183      * Returns the default value.
184      */

185     public String JavaDoc getDefault() {
186         return defaultValue;
187     }
188     
189     /**
190      * Sets the default value.
191      */

192     public void setDefault(String JavaDoc value) {
193         String JavaDoc oldValue = getDefault();
194         if( (oldValue == null && value == null) ||
195             (oldValue != null && oldValue.equals(value)) ) {
196             return;
197         }
198         this.defaultValue = value;
199         firePropertyChangeEvent(PROP_DEFAULT, oldValue, value);
200     }
201     
202     /**
203      * Returns the use.
204      */

205     public Use getUse() {
206         return use;
207     }
208     
209     /**
210      * Sets the use.
211      */

212     public void setUse(Use value) {
213         Use oldValue = getUse();
214         if( (oldValue == null && value == null) ||
215             (oldValue != null && oldValue.equals(value)) ) {
216             return;
217         }
218         this.use = value;
219         firePropertyChangeEvent(PROP_USE, oldValue, value);
220     }
221     
222     /**
223      * For an element-ref or attribute-ref, most of the properties come from the actual
224      * element or attribute. So when something changes in the ref, we must forcibly fire
225      * an event so that the UI updates itself.
226      */

227     void forceFireEvent() {
228         firePropertyChangeEvent(Attribute.PROP_NAME, null, getReferent().getName());
229     }
230     
231 }
232
Popular Tags