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.interactionmodel;
025:
026: import jacareto.struct.StructureElement;
027: import jacareto.system.Environment;
028: import jacareto.system.EnvironmentMember;
029:
030: import java.util.Arrays;
031: import java.util.Iterator;
032: import java.util.Vector;
033:
034: /**
035: * An element of a interaction-model.
036: *
037: * @author <a href="mailto:markus.bois@web.de">Markus Bois</a>
038: * @version 1.02
039: */
040: public abstract class InteractionModelElement extends EnvironmentMember {
041: /** The parent. */
042: private InteractionModelElement parent;
043:
044: /** The array of the children. */
045: private Vector children;
046:
047: /** The id of the element in the interaction-model */
048: private String id;
049:
050: /** The name of the element */
051: private String name;
052:
053: /**
054: * Creates a new interaction-model element.
055: *
056: * @param env the environment
057: * @param name the name of the interaction-model element
058: * @param children the child interaction-model elements
059: * @param id the id in the interaction-model
060: */
061: public InteractionModelElement(Environment env, String name,
062: InteractionModelElement[] children, String id) {
063: super (env);
064: this .children = new Vector(5, 5);
065:
066: if (!id.equals("") && (id != null)) {
067: this .id = id;
068: } else {
069: if (!(this instanceof InteractionModelRoot)
070: && !(this instanceof DynamicsElement)
071: && !(this instanceof BasicInteractionsElement)) {
072: getLogger()
073: .error(
074: language
075: .getString("InteractionModel.Error.IDMissing")
076: + " '" + name + "'");
077: }
078: }
079:
080: this .name = name;
081: setChildren(children);
082: }
083:
084: /**
085: * Creates a new interaction-model element.
086: *
087: * @param env the environment
088: * @param children the child interaction-model elements
089: * @param id the id in the interaction-model
090: */
091: public InteractionModelElement(Environment env,
092: InteractionModelElement[] children, String id) {
093: this (env, "", children, id);
094: }
095:
096: /**
097: * Creates a new interaction-model element.
098: *
099: * @param env the environment
100: */
101: public InteractionModelElement(Environment env) {
102: this (env, "", null, "");
103: }
104:
105: /**
106: * Creates a new interaction-model element.
107: *
108: * @param env the environment
109: * @param children the child interaction-model elements
110: */
111: public InteractionModelElement(Environment env,
112: InteractionModelElement[] children) {
113: this (env, "", children, "");
114: }
115:
116: /**
117: * Returns the parent of a interaction-model element.
118: *
119: * @return the parent, or <code>null</code> if there is no parent
120: */
121: public InteractionModelElement getParent() {
122: return parent;
123: }
124:
125: /**
126: * Sets the parent for this interaction-model element.
127: *
128: * @param parent DOCUMENT ME!
129: */
130: public void setParent(InteractionModelElement parent) {
131: this .parent = parent;
132: }
133:
134: /**
135: * Returns an iterator on the children of the interaction-model element.
136: *
137: * @return DOCUMENT ME!
138: */
139: public Iterator children() {
140: return children.iterator();
141: }
142:
143: /**
144: * Returns the children as array.
145: *
146: * @return DOCUMENT ME!
147: */
148: public InteractionModelElement[] getChildren() {
149: InteractionModelElement[] result = new InteractionModelElement[children
150: .size()];
151:
152: for (int i = 0; i < result.length; i++) {
153: result[i] = (InteractionModelElement) children.get(i);
154: }
155:
156: return result;
157: }
158:
159: /**
160: * Sets the children. Also sets this instance as parent for all children, if the specified
161: * array is not <code>null</code>.
162: *
163: * @param children DOCUMENT ME!
164: */
165: public void setChildren(InteractionModelElement[] children) {
166: if (children != null) {
167: this .children = new Vector(Arrays.asList(children));
168:
169: for (int i = 0; i < children.length; i++) {
170: children[i].setParent(this );
171: }
172: } else {
173: this .children = new Vector(5, 5);
174: }
175: }
176:
177: /**
178: * Returns the child at the specified index.
179: *
180: * @param index the index of the child
181: *
182: * @return DOCUMENT ME!
183: */
184: public InteractionModelElement getChild(int index) {
185: return (InteractionModelElement) children.get(index);
186: }
187:
188: /**
189: * Returns the index of a given child.
190: *
191: * @param child DOCUMENT ME!
192: *
193: * @return the index, or -1 if the child is not contained
194: */
195: public int getIndex(InteractionModelElement child) {
196: return children.indexOf(child);
197: }
198:
199: /**
200: * Returns the number of the children.
201: *
202: * @return DOCUMENT ME!
203: */
204: public int getChildrenCount() {
205: return children.size();
206: }
207:
208: /**
209: * Adds a child to the current children.
210: *
211: * @param child the child to add
212: */
213: public void addChild(InteractionModelElement child) {
214: children.add(child);
215: child.setParent(this );
216: }
217:
218: /**
219: * Adds an array of children.
220: *
221: * @param children DOCUMENT ME!
222: */
223: public void addChildren(InteractionModelElement[] children) {
224: for (int i = 0; i < children.length; i++) {
225: addChild(children[i]);
226: }
227: }
228:
229: /**
230: * Inserts a child at a given index.
231: *
232: * @param child the child to add
233: * @param index the index
234: */
235: public void insertChild(InteractionModelElement child, int index) {
236: children.add(index, child);
237: child.setParent(this );
238: }
239:
240: /**
241: * Removes a child.
242: *
243: * @param child the child to remove
244: */
245: public void removeChild(InteractionModelElement child) {
246: if (children.contains(child)) {
247: children.removeElement(child);
248: child.setParent(null);
249: }
250: }
251:
252: /**
253: * Removes this node from its parent.
254: */
255: public void removeFromParent() {
256: if (parent != null) {
257: parent.removeChild(this );
258: }
259: }
260:
261: /**
262: * Removes all children.
263: */
264: public void removeAllChildren() {
265: Iterator it = children();
266:
267: while (it.hasNext()) {
268: ((InteractionModelElement) it.next()).setParent(null);
269: }
270:
271: children.clear();
272: }
273:
274: /**
275: * Returns whether or not this element has children.
276: *
277: * @return DOCUMENT ME!
278: */
279: public boolean hasChildren() {
280: return (children != null) && (children.size() > 0);
281: }
282:
283: /**
284: * Returns the id of the interaction-model-element.
285: *
286: * @return DOCUMENT ME!
287: */
288: public String getID() {
289: return id;
290: }
291:
292: /**
293: * Returns the name of the element.
294: *
295: * @return DOCUMENT ME!
296: */
297: public String getName() {
298: return name;
299: }
300:
301: /**
302: * Returns the name of the element.
303: *
304: * @return the name
305: */
306: public abstract String getElementName();
307:
308: /**
309: * Returns a description of the element.
310: *
311: * @return the description
312: */
313: public abstract String getElementDescription();
314:
315: /**
316: * Returns a String which describes the content of the element shortly.
317: *
318: * @return a string with a short description of the element
319: */
320: public abstract String toShortString();
321:
322: /**
323: * Returns the start knot of the record representation
324: *
325: * @return a {@link jacareto.struct.StructureElement} that is a interaction-model structure
326: */
327: public StructureElement getStructElement() {
328: return null;
329: }
330:
331: /**
332: * Converts a vector of interaction-model elements to an array of interaction-model elements
333: *
334: * @param v DOCUMENT ME!
335: *
336: * @return the array containing all interaction-model elements, or <code>null</code> if the
337: * vector is not convertable.
338: */
339: public static InteractionModelElement[] vectorToArray(Vector v) {
340: InteractionModelElement[] result = null;
341:
342: try {
343: result = new InteractionModelElement[v.size()];
344:
345: for (int i = 0; i < result.length; i++) {
346: result[i] = (InteractionModelElement) v.get(i);
347: }
348: } catch (Throwable t) {
349: ;
350: }
351:
352: return result;
353: }
354:
355: /**
356: * Converts a vector of interaction-model elements to an array of interaction-model elements
357: *
358: * @param a DOCUMENT ME!
359: *
360: * @return the array containing all interaction-model elements, or <code>null</code> if the
361: * vector is not convertable.
362: */
363: public static Vector arrayToVector(InteractionModelElement[] a) {
364: Vector result = new Vector();
365:
366: try {
367: for (int i = 0; i < a.length; i++) {
368: result.add(a[i]);
369: }
370: } catch (Throwable t) {
371: ;
372: }
373:
374: return result;
375: }
376: }
|