001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * AttributeRef.java
044: *
045: * Created on May 5, 2006, 12:04 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.axi.impl;
052:
053: import org.netbeans.modules.xml.axi.AXIComponent;
054: import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
055: import org.netbeans.modules.xml.axi.AXIModel;
056: import org.netbeans.modules.xml.axi.AXIType;
057: import org.netbeans.modules.xml.axi.Attribute;
058: import org.netbeans.modules.xml.schema.model.SchemaComponent;
059: import org.netbeans.modules.xml.schema.model.Attribute.Use;
060: import org.netbeans.modules.xml.schema.model.Form;
061:
062: /**
063: * Represents an Attribute reference. For an Attribute reference
064: * name, type and form must be absent, that is, calls on name, type
065: * and form must be delegated to the original.
066: *
067: * See http://www.w3.org/TR/xmlschema-1/#d0e2403.
068: *
069: * @author Samaresh (Samaresh.Panda@Sun.Com)
070: */
071: public class AttributeRef extends Attribute {
072:
073: /**
074: * Creates a new instance of AttributeRef
075: */
076: public AttributeRef(AXIModel model, Attribute referent) {
077: super (model, referent);
078: }
079:
080: /**
081: * Creates a new instance of AttributeRef
082: */
083: public AttributeRef(AXIModel model, SchemaComponent component,
084: Attribute referent) {
085: super (model, component);
086: super .setSharedComponent(referent);
087: }
088:
089: /**
090: * Returns the type of this component,
091: * may be local, shared, proxy or reference.
092: * @see ComponentType.
093: */
094: public ComponentType getComponentType() {
095: return ComponentType.REFERENCE;
096: }
097:
098: /**
099: * Returns the referent if isReference() is true.
100: */
101: public Attribute getReferent() {
102: return (Attribute) getSharedComponent();
103: }
104:
105: /**
106: * Sets the new referent.
107: */
108: public void setRef(Attribute referent) {
109: AttributeImpl oldRef = (AttributeImpl) getReferent();
110: if (oldRef == referent)
111: return;
112: oldRef.removeListener(this );
113: super .setSharedComponent(referent);
114: firePropertyChangeEvent(PROP_ATTRIBUTE_REF, oldRef, referent);
115: forceFireEvent();
116: }
117:
118: /**
119: * Returns true if it is a reference, false otherwise.
120: */
121: public boolean isReference() {
122: return true;
123: }
124:
125: /**
126: * Returns the name.
127: */
128: public String getName() {
129: return getReferent().getName();
130: }
131:
132: /**
133: * Sets the name.
134: */
135: public void setName(String name) {
136: for (Attribute a : getModel().getRoot().getAttributes()) {
137: if (a.getName().equals(name)) {
138: setRef(a);
139: return;
140: }
141: }
142: getReferent().setName(name);
143: }
144:
145: /**
146: * Returns the type. This is expensive, since it uses a visitor
147: * to traverse to obtain the type information.
148: */
149: public AXIType getType() {
150: return getReferent().getType();
151: }
152:
153: /**
154: * Sets the type.
155: */
156: public void setType(AXIType type) {
157: if (type instanceof Attribute) {
158: setRef((Attribute) type);
159: return;
160: }
161:
162: int index = this .getIndex();
163: AXIComponent parent = getParent();
164: Attribute a = getModel().getComponentFactory()
165: .createAttribute();
166: a.setName(getReferent().getName());
167: parent.removeChild(this );
168: parent.insertAtIndex(Attribute.PROP_ATTRIBUTE, a, index);
169: a.setType(type);
170: }
171:
172: /**
173: * Returns the form.
174: */
175: public Form getForm() {
176: return getReferent().getForm();
177: }
178:
179: /**
180: * Sets the form.
181: */
182: public void setForm(Form form) {
183: getReferent().setForm(form);
184: }
185:
186: /**
187: * Returns the fixed value.
188: */
189: public String getFixed() {
190: return fixedValue;
191: }
192:
193: /**
194: * Sets the fixed value.
195: */
196: public void setFixed(String value) {
197: String oldValue = getFixed();
198: if ((oldValue == null && value == null)
199: || (oldValue != null && oldValue.equals(value))) {
200: return;
201: }
202: this .fixedValue = value;
203: firePropertyChangeEvent(PROP_FIXED, oldValue, value);
204: }
205:
206: /**
207: * Returns the default value.
208: */
209: public String getDefault() {
210: return defaultValue;
211: }
212:
213: /**
214: * Sets the default value.
215: */
216: public void setDefault(String value) {
217: String oldValue = getDefault();
218: if ((oldValue == null && value == null)
219: || (oldValue != null && oldValue.equals(value))) {
220: return;
221: }
222: this .defaultValue = value;
223: firePropertyChangeEvent(PROP_DEFAULT, oldValue, value);
224: }
225:
226: /**
227: * Returns the use.
228: */
229: public Use getUse() {
230: return use;
231: }
232:
233: /**
234: * Sets the use.
235: */
236: public void setUse(Use value) {
237: Use oldValue = getUse();
238: if ((oldValue == null && value == null)
239: || (oldValue != null && oldValue.equals(value))) {
240: return;
241: }
242: this .use = value;
243: firePropertyChangeEvent(PROP_USE, oldValue, value);
244: }
245:
246: /**
247: * For an element-ref or attribute-ref, most of the properties come from the actual
248: * element or attribute. So when something changes in the ref, we must forcibly fire
249: * an event so that the UI updates itself.
250: */
251: void forceFireEvent() {
252: firePropertyChangeEvent(Attribute.PROP_NAME, null,
253: getReferent().getName());
254: }
255:
256: }
|