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: * AttributeImpl.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.AXIModel;
055: import org.netbeans.modules.xml.axi.AXIType;
056: import org.netbeans.modules.xml.axi.Attribute;
057: import org.netbeans.modules.xml.axi.datatype.StringType;
058: import org.netbeans.modules.xml.axi.datatype.DatatypeFactory;
059: import org.netbeans.modules.xml.schema.model.SchemaComponent;
060: import org.netbeans.modules.xml.schema.model.Attribute.Use;
061: import org.netbeans.modules.xml.schema.model.Form;
062:
063: /**
064: * Base and only implementation of Attribute.
065: *
066: * @author Samaresh (Samaresh.Panda@Sun.Com)
067: */
068: public final class AttributeImpl extends Attribute {
069:
070: /**
071: * Creates a new instance of AttributeImpl
072: */
073: public AttributeImpl(AXIModel model) {
074: super (model);
075: setDefaultDataType();
076: }
077:
078: /**
079: * Creates a new instance of AttributeImpl
080: */
081: public AttributeImpl(AXIModel model, SchemaComponent schemaComponent) {
082: super (model, schemaComponent);
083: setDefaultDataType();
084: }
085:
086: /**
087: * Initializes the default datatype for this Attribute.
088: * Do NOT call setType() here, that'll inturn call
089: * getType(), which is expensive. Initialize instead.
090: */
091: private void setDefaultDataType() {
092: this .datatype = new StringType();
093: }
094:
095: /**
096: * Returns true if it is a reference, false otherwise.
097: */
098: public boolean isReference() {
099: return false;
100: }
101:
102: /**
103: * Returns the referent if isReference() is true.
104: */
105: public Attribute getReferent() {
106: return null;
107: }
108:
109: /**
110: * Returns the name.
111: */
112: public String getName() {
113: return name;
114: }
115:
116: /**
117: * Sets the name.
118: */
119: public void setName(String name) {
120: String oldName = getName();
121: if ((oldName == null && name == null)
122: || (oldName != null && oldName.equals(name))) {
123: return;
124: }
125: this .name = name;
126: firePropertyChangeEvent(PROP_NAME, oldName, name);
127: }
128:
129: /**
130: * Returns the type. This is expensive, since it uses a visitor
131: * to traverse to obtain the type information.
132: */
133: public AXIType getType() {
134: if (!datatypeInitialized && getPeer() != null) {
135: datatype = DatatypeFactory.getDefault().getDatatype(
136: getModel(), getPeer());
137: datatypeInitialized = true;
138: }
139:
140: return datatype;
141: }
142:
143: /**
144: * Sets the type.
145: */
146: public void setType(AXIType value) {
147: if ((this == value)
148: || (this .isGlobal() && (value instanceof Attribute)))
149: return;
150:
151: if (value instanceof Attribute) {
152: setAttributeAsType(value);
153: return;
154: }
155:
156: AXIType oldValue = getType();
157: if ((oldValue == null && value == null)
158: || (oldValue != null && oldValue.equals(value))) {
159: return;
160: }
161: this .datatype = value;
162: firePropertyChangeEvent(PROP_TYPE, oldValue, value);
163: }
164:
165: private void setAttributeAsType(final AXIType newValue) {
166: if (newValue == this )
167: return;
168: int index = this .getIndex();
169: AXIComponent parent = getParent();
170: Attribute ref = getModel().getComponentFactory()
171: .createAttributeReference((Attribute) newValue);
172: parent.removeChild(this );
173: parent.insertAtIndex(Attribute.PROP_ATTRIBUTE_REF, ref, index);
174: }
175:
176: /**
177: * Returns the form.
178: */
179: public Form getForm() {
180: return form;
181: }
182:
183: /**
184: * Sets the form.
185: */
186: public void setForm(Form value) {
187: Form oldValue = getForm();
188: if ((oldValue == null && value == null)
189: || (oldValue != null && oldValue == value)) {
190: return;
191: }
192: this .form = value;
193: firePropertyChangeEvent(PROP_FORM, oldValue, value);
194: }
195:
196: /**
197: * Returns the fixed value.
198: */
199: public String getFixed() {
200: return fixedValue;
201: }
202:
203: /**
204: * Sets the fixed value.
205: */
206: public void setFixed(String value) {
207: String oldValue = getFixed();
208: if ((oldValue == null && value == null)
209: || (oldValue != null && oldValue.equals(value))) {
210: return;
211: }
212: this .fixedValue = value;
213: firePropertyChangeEvent(PROP_FIXED, oldValue, value);
214: }
215:
216: /**
217: * Returns the default value.
218: */
219: public String getDefault() {
220: return defaultValue;
221: }
222:
223: /**
224: * Sets the default value.
225: */
226: public void setDefault(String value) {
227: Object oldValue = getDefault();
228: if ((oldValue == null && value == null)
229: || (oldValue != null && oldValue.equals(value))) {
230: return;
231: }
232: this .defaultValue = value;
233: firePropertyChangeEvent(PROP_DEFAULT, oldValue, value);
234: }
235:
236: /**
237: * Returns the use.
238: */
239: public Use getUse() {
240: return use;
241: }
242:
243: /**
244: * Sets the use.
245: */
246: public void setUse(Use value) {
247: Use oldValue = getUse();
248: if ((oldValue == null && value == null)
249: || (oldValue != null && oldValue == value)) {
250: return;
251: }
252: this .use = value;
253: firePropertyChangeEvent(PROP_USE, oldValue, value);
254: }
255:
256: private boolean datatypeInitialized;
257: }
|