001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.conceptualmodel;
025:
026: import jacareto.system.Environment;
027: import jacareto.system.EnvironmentMember;
028:
029: import java.util.Arrays;
030: import java.util.Iterator;
031: import java.util.Vector;
032:
033: /**
034: * An element of a conceptual-model.
035: *
036: * @author <a href="mailto:markus.bois@web.de">Markus Bois</a>
037: * @version 1.02
038: */
039: public abstract class ConceptualModelElement extends EnvironmentMember {
040: /** The parent. */
041: private ConceptualModelElement parent;
042:
043: /** The array of the children. */
044: private Vector children;
045:
046: /** The name of the element */
047: private String name;
048:
049: /**
050: * Creates a new conceptual-model element.
051: *
052: * @param env the environment
053: * @param name the name of the conceptual-model element
054: * @param children the child conceptual-model elements
055: */
056: public ConceptualModelElement(Environment env, String name,
057: ConceptualModelElement[] children) {
058: super (env);
059: this .children = new Vector(5, 5);
060: this .name = name;
061: setChildren(children);
062: }
063:
064: /**
065: * Creates a new conceptual-model element.
066: *
067: * @param env the environment
068: * @param children the child conceptual-model elements
069: */
070: public ConceptualModelElement(Environment env,
071: ConceptualModelElement[] children) {
072: this (env, "", children);
073: }
074:
075: /**
076: * Creates a new conceptual-model element.
077: *
078: * @param env the environment
079: */
080: public ConceptualModelElement(Environment env) {
081: this (env, "", null);
082: }
083:
084: /**
085: * Returns the parent of a conceptual-model element.
086: *
087: * @return the parent, or <code>null</code> if there is no parent
088: */
089: public ConceptualModelElement getParent() {
090: return parent;
091: }
092:
093: /**
094: * Sets the parent for this conceptual-model element.
095: *
096: * @param parent DOCUMENT ME!
097: */
098: public void setParent(ConceptualModelElement parent) {
099: this .parent = parent;
100: }
101:
102: /**
103: * Returns an iterator on the children of the conceptual-model element.
104: *
105: * @return DOCUMENT ME!
106: */
107: public Iterator children() {
108: return children.iterator();
109: }
110:
111: /**
112: * Returns the children as array.
113: *
114: * @return DOCUMENT ME!
115: */
116: public ConceptualModelElement[] getChildren() {
117: ConceptualModelElement[] result = new ConceptualModelElement[children
118: .size()];
119:
120: for (int i = 0; i < result.length; i++) {
121: result[i] = (ConceptualModelElement) children.get(i);
122: }
123:
124: return result;
125: }
126:
127: /**
128: * Sets the children. Also sets this instance as parent for all children, if the specified
129: * array is not <code>null</code>.
130: *
131: * @param children DOCUMENT ME!
132: */
133: public void setChildren(ConceptualModelElement[] children) {
134: if (children != null) {
135: this .children = new Vector(Arrays.asList(children));
136:
137: for (int i = 0; i < children.length; i++) {
138: children[i].setParent(this );
139: }
140: } else {
141: this .children = new Vector(5, 5);
142: }
143: }
144:
145: /**
146: * Returns the child at the specified index.
147: *
148: * @param index the index of the child
149: *
150: * @return DOCUMENT ME!
151: */
152: public ConceptualModelElement getChild(int index) {
153: return (ConceptualModelElement) children.get(index);
154: }
155:
156: /**
157: * Returns the index of a given child.
158: *
159: * @param child DOCUMENT ME!
160: *
161: * @return the index, or -1 if the child is not contained
162: */
163: public int getIndex(ConceptualModelElement child) {
164: return children.indexOf(child);
165: }
166:
167: /**
168: * Returns the number of the children.
169: *
170: * @return DOCUMENT ME!
171: */
172: public int getChildrenCount() {
173: return children.size();
174: }
175:
176: /**
177: * Adds a child to the current children.
178: *
179: * @param child the child to add
180: */
181: public void addChild(ConceptualModelElement child) {
182: children.add(child);
183: child.setParent(this );
184: }
185:
186: /**
187: * Adds an array of children.
188: *
189: * @param children DOCUMENT ME!
190: */
191: public void addChildren(ConceptualModelElement[] children) {
192: for (int i = 0; i < children.length; i++) {
193: addChild(children[i]);
194: }
195: }
196:
197: /**
198: * Inserts a child at a given index.
199: *
200: * @param child the child to add
201: * @param index the index
202: */
203: public void insertChild(ConceptualModelElement child, int index) {
204: children.add(index, child);
205: child.setParent(this );
206: }
207:
208: /**
209: * Removes a child.
210: *
211: * @param child the child to remove
212: */
213: public void removeChild(ConceptualModelElement child) {
214: if (children.contains(child)) {
215: children.removeElement(child);
216: child.setParent(null);
217: }
218: }
219:
220: /**
221: * Removes this node from its parent.
222: */
223: public void removeFromParent() {
224: if (parent != null) {
225: parent.removeChild(this );
226: }
227: }
228:
229: /**
230: * Removes all children.
231: */
232: public void removeAllChildren() {
233: Iterator it = children();
234:
235: while (it.hasNext()) {
236: ((ConceptualModelElement) it.next()).setParent(null);
237: }
238:
239: children.clear();
240: }
241:
242: /**
243: * Returns whether or not this element has children.
244: *
245: * @return DOCUMENT ME!
246: */
247: public boolean hasChildren() {
248: return (children != null) && (children.size() > 0);
249: }
250:
251: /**
252: * Returns the name of the element.
253: *
254: * @return DOCUMENT ME!
255: */
256: public String getName() {
257: return name;
258: }
259:
260: /**
261: * Returns the name of the element.
262: *
263: * @return the name
264: */
265: public abstract String getElementName();
266:
267: /**
268: * Returns a description of the element.
269: *
270: * @return the description
271: */
272: public abstract String getElementDescription();
273:
274: /**
275: * Returns a String which describes the content of the element shortly.
276: *
277: * @return a string with a short description of the element
278: */
279: public abstract String toShortString();
280:
281: /**
282: * Converts a vector of conceptual-model elements to an array of conceptua-model elements
283: *
284: * @param v DOCUMENT ME!
285: *
286: * @return the array containing all conceptual-model elements, or <code>null</code> if the
287: * vector is not convertable.
288: */
289: public static ConceptualModelElement[] vectorToArray(Vector v) {
290: ConceptualModelElement[] result = null;
291:
292: try {
293: result = new ConceptualModelElement[v.size()];
294:
295: for (int i = 0; i < result.length; i++) {
296: result[i] = (ConceptualModelElement) v.get(i);
297: }
298: } catch (Throwable t) {
299: ;
300: }
301:
302: return result;
303: }
304:
305: /**
306: * Converts an array of conceptual elements to a vector of conceptual elements
307: *
308: * @param a DOCUMENT ME!
309: *
310: * @return the vector containing all conceptual-model elements, or <code>null</code> if the
311: * array is not convertable.
312: */
313: public static Vector arrayToVector(ConceptualModelElement[] a) {
314: Vector result = new Vector();
315:
316: try {
317: for (int i = 0; i < a.length; i++) {
318: result.add(a[i]);
319: }
320: } catch (Throwable t) {
321: ;
322: }
323:
324: return result;
325: }
326: }
|