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: package org.netbeans.modules.xml.axi;
042:
043: import java.util.List;
044: import org.netbeans.modules.xml.axi.ContentModel.ContentModelType;
045: import org.netbeans.modules.xml.schema.model.SchemaComponent;
046: import org.openide.util.NbBundle;
047:
048: /**
049: * Represents a named component that can contain attributes,
050: * for example an Element or a ContentModel.
051: *
052: * @author Samaresh (Samaresh.Panda@Sun.Com)
053: */
054: public abstract class AXIContainer extends AXIComponent {
055:
056: /**
057: * Creates a new instance of AXIContainer.
058: */
059: public AXIContainer(AXIModel model) {
060: super (model);
061: }
062:
063: /**
064: * Creates a new instance of AXIContainer.
065: */
066: public AXIContainer(AXIModel model, SchemaComponent schemaComponent) {
067: super (model, schemaComponent);
068: }
069:
070: /**
071: * Creates a proxy for this AXIContainer.
072: */
073: public AXIContainer(AXIModel model, AXIComponent sharedComponent) {
074: super (model, sharedComponent);
075: }
076:
077: /**
078: * Returns the name.
079: */
080: public String getName() {
081: if (name != null)
082: return name;
083:
084: if (this instanceof Element)
085: return NbBundle.getMessage(AXIContainer.class,
086: "Unnamed-Element");
087:
088: if (this instanceof ContentModel) {
089: ContentModelType type = ((ContentModel) this ).getType();
090: switch (type) {
091: case COMPLEX_TYPE:
092: return NbBundle.getMessage(AXIContainer.class,
093: "Unnamed-ComplexType");
094: case GROUP:
095: return NbBundle.getMessage(AXIContainer.class,
096: "Unnamed-Group");
097: case ATTRIBUTE_GROUP:
098: return NbBundle.getMessage(AXIContainer.class,
099: "Unnamed-AttributeGroup");
100: }
101: }
102:
103: return NbBundle.getMessage(AXIContainer.class,
104: "Unnamed-Component");
105: }
106:
107: /**
108: * Sets the name.
109: */
110: public void setName(String name) {
111: String oldName = getName();
112: if ((oldName == null && name == null)
113: || (oldName != null && oldName.equals(name))) {
114: return;
115: }
116:
117: this .name = name;
118: firePropertyChangeEvent(PROP_NAME, oldName, name);
119: }
120:
121: /**
122: * Adds a Compositor as its child.
123: * Compositor must always be at the 0th index.
124: */
125: public void addCompositor(Compositor compositor) {
126: insertAtIndex(Compositor.PROP_COMPOSITOR, compositor, 0);
127: }
128:
129: /**
130: * Removes a Compositor.
131: */
132: public void removeCompositor(Compositor compositor) {
133: removeChild(Compositor.PROP_COMPOSITOR, compositor);
134: }
135:
136: /**
137: * Adds an Element as its child.
138: * If attributes exist, add the new child before all attributes.
139: * Attributes must always be added at the end of the list.
140: */
141: public void addElement(AbstractElement child) {
142: if (this instanceof Element) {
143: AXIType type = ((Element) this ).getType();
144: if (type != null && type instanceof ContentModel) {
145: ((ContentModel) type).addElement(child);
146: return;
147: }
148: }
149:
150: //if compositor does not exist, add one.
151: Compositor c = getCompositor();
152: if (c == null) {
153: c = getModel().getComponentFactory().createSequence();
154: addCompositor(c);
155: }
156: //add element to the compositor
157: c.appendChild(AbstractElement.PROP_ELEMENT, child);
158: }
159:
160: /**
161: * Removes an Element.
162: */
163: public void removeElement(AbstractElement element) {
164: removeChild(AbstractElement.PROP_ELEMENT, element);
165: }
166:
167: /**
168: * Adds an attribute.
169: */
170: public void addAttribute(AbstractAttribute attribute) {
171: appendChild(AbstractAttribute.PROP_ATTRIBUTE, attribute);
172: }
173:
174: /**
175: * Removes an attribute.
176: */
177: public void removeAttribute(AbstractAttribute attribute) {
178: removeChild(AbstractAttribute.PROP_ATTRIBUTE, attribute);
179: }
180:
181: /**
182: * Returns the compositor.
183: */
184: public Compositor getCompositor() {
185: for (AXIComponent child : getChildren()) {
186: if (Compositor.class.isAssignableFrom(child.getClass()))
187: return (Compositor) child;
188: }
189: return null;
190: }
191:
192: /**
193: * Returns the list of attributes.
194: */
195: public final List<AbstractAttribute> getAttributes() {
196: return getChildren(AbstractAttribute.class);
197: }
198:
199: protected String name;
200:
201: public static final String PROP_NAME = "name"; // NOI18N
202: }
|