KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > AXIContainer


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 package org.netbeans.modules.xml.axi;
20
21 import java.util.List JavaDoc;
22 import org.netbeans.modules.xml.schema.model.SchemaComponent;
23
24 /**
25  * Represents a named component that can contain attributes,
26  * for example an Element or a ContentModel.
27  *
28  * @author Samaresh (Samaresh.Panda@Sun.Com)
29  */

30 public abstract class AXIContainer extends AXIComponent {
31     
32     /**
33      * Creates a new instance of AXIContainer.
34      */

35     public AXIContainer(AXIModel model) {
36         super(model);
37     }
38     
39     /**
40      * Creates a new instance of AXIContainer.
41      */

42     public AXIContainer(AXIModel model, SchemaComponent schemaComponent) {
43         super(model, schemaComponent);
44     }
45     
46     /**
47      * Creates a proxy for this AXIContainer.
48      */

49     public AXIContainer(AXIModel model, AXIComponent sharedComponent) {
50         super(model, sharedComponent);
51     }
52     
53     /**
54      * Returns the name.
55      */

56     public String JavaDoc getName() {
57         return name;
58     }
59     
60     /**
61      * Sets the name.
62      */

63     public void setName(String JavaDoc name) {
64         String JavaDoc oldName = getName();
65         if( (oldName == null && name == null) ||
66                 (oldName != null && oldName.equals(name)) ) {
67             return;
68         }
69         
70         this.name = name;
71         firePropertyChangeEvent(PROP_NAME, oldName, name);
72     }
73     
74     /**
75      * Adds a Compositor as its child.
76      * Compositor must always be at the 0th index.
77      */

78     public void addCompositor(Compositor compositor) {
79         insertAtIndex(Compositor.PROP_COMPOSITOR, compositor, 0);
80     }
81     
82     /**
83      * Removes a Compositor.
84      */

85     public void removeCompositor(Compositor compositor) {
86         removeChild(Compositor.PROP_COMPOSITOR, compositor);
87     }
88     
89     /**
90      * Adds an Element as its child.
91      * If attributes exist, add the new child before all attributes.
92      * Attributes must always be added at the end of the list.
93      */

94     public void addElement(AbstractElement child) {
95         if(this instanceof Element) {
96             AXIType type = ((Element)this).getType();
97             if(type != null && type instanceof ContentModel) {
98                 ((ContentModel)type).addElement(child);
99                 return;
100             }
101         }
102         
103         //if compositor does not exist, add one.
104
Compositor c = getCompositor();
105         if(c == null) {
106             c = getModel().getComponentFactory().createSequence();
107             addCompositor(c);
108         }
109         //add element to the compositor
110
c.appendChild(AbstractElement.PROP_ELEMENT, child);
111     }
112     
113     /**
114      * Removes an Element.
115      */

116     public void removeElement(AbstractElement element) {
117         removeChild(AbstractElement.PROP_ELEMENT, element);
118     }
119     
120     /**
121      * Adds an attribute.
122      */

123     public void addAttribute(AbstractAttribute attribute) {
124         appendChild(AbstractAttribute.PROP_ATTRIBUTE, attribute);
125     }
126     
127     /**
128      * Removes an attribute.
129      */

130     public void removeAttribute(AbstractAttribute attribute) {
131         removeChild(AbstractAttribute.PROP_ATTRIBUTE, attribute);
132     }
133     
134     /**
135      * Returns the compositor.
136      */

137     public Compositor getCompositor() {
138         if(getChildren().size() > 0) {
139             AXIComponent component = getChildren().get(0);
140             if(component instanceof Compositor)
141                 return (Compositor)component;
142         }
143         return null;
144     }
145     
146     /**
147      * Returns the list of attributes.
148      */

149     public final List JavaDoc<AbstractAttribute> getAttributes() {
150         return getChildren(AbstractAttribute.class);
151     }
152     
153     protected String JavaDoc name;
154     
155     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
156
}
157
Popular Tags