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: * ElementRef.java
044: *
045: * Created on May 5, 2006, 11:15 AM
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 java.util.List;
054: import org.netbeans.modules.xml.axi.AXIComponent;
055: import org.netbeans.modules.xml.axi.AXIComponent.ComponentType;
056: import org.netbeans.modules.xml.axi.AXIModel;
057: import org.netbeans.modules.xml.axi.AXIType;
058: import org.netbeans.modules.xml.axi.AbstractAttribute;
059: import org.netbeans.modules.xml.axi.AbstractElement;
060: import org.netbeans.modules.xml.axi.Compositor;
061: import org.netbeans.modules.xml.axi.ContentModel;
062: import org.netbeans.modules.xml.axi.Element;
063: import org.netbeans.modules.xml.axi.datatype.Datatype;
064: import org.netbeans.modules.xml.schema.model.Form;
065: import org.netbeans.modules.xml.schema.model.SchemaComponent;
066:
067: /**
068: * Represents an Element reference. For an Element reference
069: * most of the calls are delegated to the original Element,
070: * except for calls related to min and max occurs.
071: *
072: * See http://www.w3.org/TR/xmlschema-1/#d0e4233.
073: * @author Samaresh (Samaresh.Panda@Sun.Com)
074: */
075: public class ElementRef extends Element {
076:
077: /**
078: * Creates a new instance of ElementRef
079: */
080: public ElementRef(AXIModel model, Element referent) {
081: super (model, referent);
082: }
083:
084: /**
085: * Creates a new instance of ElementRef
086: */
087: public ElementRef(AXIModel model, SchemaComponent component,
088: Element referent) {
089: super (model, component);
090: super .setSharedComponent(referent);
091: }
092:
093: /**
094: * Returns the type of this component,
095: * may be local, shared, proxy or reference.
096: * @see ComponentType.
097: */
098: public ComponentType getComponentType() {
099: return ComponentType.REFERENCE;
100: }
101:
102: /**
103: * Returns the referent if isReference() is true.
104: */
105: public Element getReferent() {
106: return (Element) getSharedComponent();
107: }
108:
109: /**
110: * Sets the new referent.
111: */
112: public void setRef(Element referent) {
113: ElementImpl oldRef = (ElementImpl) getReferent();
114: if (oldRef == referent)
115: return;
116: oldRef.removeListener(this );
117: setSharedComponent(referent);
118: firePropertyChangeEvent(PROP_ELEMENT_REF, oldRef, referent);
119: forceFireEvent();
120: if (canVisitChildren()) {
121: removeAllChildren();
122: Util.addProxyChildren(this , referent, null);
123: }
124: }
125:
126: /**
127: * Returns true if it is a reference, false otherwise.
128: */
129: public boolean isReference() {
130: return true;
131: }
132:
133: /**
134: * Returns the name.
135: */
136: public String getName() {
137: return getReferent().getName();
138: }
139:
140: /**
141: * Sets the name.
142: */
143: public void setName(String name) {
144: getReferent().setName(name);
145: }
146:
147: /**
148: * Returns abstract property.
149: */
150: public boolean getAbstract() {
151: return getReferent().getAbstract();
152: }
153:
154: /**
155: * Sets the abstract property.
156: */
157: public void setAbstract(boolean value) {
158: getReferent().setAbstract(value);
159: }
160:
161: /**
162: * Returns the block.
163: */
164: public String getBlock() {
165: return getReferent().getBlock();
166: }
167:
168: /**
169: * Sets the block property.
170: */
171: public void setBlock(String value) {
172: getReferent().setBlock(value);
173: }
174:
175: /**
176: * Returns the final property.
177: */
178: public String getFinal() {
179: return getReferent().getFinal();
180: }
181:
182: /**
183: * Sets the final property.
184: */
185: public void setFinal(String value) {
186: getReferent().setFinal(value);
187: }
188:
189: /**
190: * Returns the fixed value.
191: */
192: public String getFixed() {
193: return getReferent().getFixed();
194: }
195:
196: /**
197: * Sets the fixed value.
198: */
199: public void setFixed(String value) {
200: getReferent().setFixed(value);
201: }
202:
203: /**
204: * Returns the default value.
205: */
206: public String getDefault() {
207: return getReferent().getDefault();
208: }
209:
210: /**
211: * Sets the default value.
212: */
213: public void setDefault(String value) {
214: getReferent().setDefault(value);
215: }
216:
217: /**
218: * Returns the form.
219: */
220: public Form getForm() {
221: return getReferent().getForm();
222: }
223:
224: /**
225: * Sets the form.
226: */
227: public void setForm(Form value) {
228: getReferent().setForm(value);
229: }
230:
231: /**
232: * Returns the nillable.
233: */
234: public boolean getNillable() {
235: return getReferent().getNillable();
236: }
237:
238: /**
239: * Sets the nillable property.
240: */
241: public void setNillable(boolean value) {
242: getReferent().setNillable(value);
243: }
244:
245: /**
246: * Adds a Compositor as its child.
247: * Compositor must always be at the 0th index.
248: */
249: public void addCompositor(Compositor compositor) {
250: getReferent().addCompositor(compositor);
251: }
252:
253: /**
254: * Removes a Compositor.
255: */
256: public void removeCompositor(Compositor compositor) {
257: getReferent().removeCompositor(compositor);
258: }
259:
260: /**
261: * Adds an Element as its child.
262: * If attributes exist, add the new child before all attributes.
263: * Attributes must always be added at the end of the list.
264: */
265: public void addElement(AbstractElement child) {
266: getReferent().addElement(child);
267: }
268:
269: /**
270: * Removes an Element.
271: */
272: public void removeElement(AbstractElement element) {
273: getReferent().removeElement(element);
274: }
275:
276: /**
277: * Adds an attribute.
278: */
279: public void addAttribute(AbstractAttribute attribute) {
280: getReferent().addAttribute(attribute);
281: }
282:
283: /**
284: * Removes an attribute.
285: */
286: public void removeAttribute(AbstractAttribute attribute) {
287: getReferent().removeAttribute(attribute);
288: }
289:
290: public AXIType getType() {
291: return getReferent().getType();
292: }
293:
294: /**
295: * sets the type of this element.
296: */
297: public void setType(AXIType type) {
298: if (type instanceof Element) {
299: setRef((Element) type);
300: return;
301: }
302:
303: int index = this .getIndex();
304: AXIComponent parent = getParent();
305: Element e = getModel().getComponentFactory().createElement();
306: e.setName(getReferent().getName());
307: parent.removeChild(this );
308: parent.insertAtIndex(Element.PROP_ELEMENT, e, index);
309: e.setType(type);
310: }
311:
312: /**
313: * Returns the compositor.
314: */
315: public Compositor getCompositor() {
316: return getReferent().getCompositor();
317: }
318:
319: /**
320: * For an element-ref or attribute-ref, most of the properties come from the actual
321: * element or attribute. So when something changes in the ref, we must forcibly fire
322: * an event so that the UI updates itself.
323: */
324: void forceFireEvent() {
325: firePropertyChangeEvent(Element.PROP_NAME, null, getReferent()
326: .getName());
327: }
328: }
|