ConceptualModelElement.java :  » Testing » jacareto » jacareto » conceptualmodel » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » conceptualmodel » ConceptualModelElement.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.conceptualmodel;


import jacareto.system.Environment;
import jacareto.system.EnvironmentMember;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
 * An element of a conceptual-model.
 *
 * @author <a href="mailto:markus.bois@web.de">Markus Bois</a>
 * @version 1.02
 */
public abstract class ConceptualModelElement extends EnvironmentMember {
    /** The parent. */
    private ConceptualModelElement parent;

    /** The array of the children. */
    private Vector children;

    /** The name of the element */
    private String name;

    /**
     * Creates a new conceptual-model element.
     *
     * @param env the environment
     * @param name the name of the conceptual-model element
     * @param children the child conceptual-model elements
     */
    public ConceptualModelElement (Environment env, String name, ConceptualModelElement[] children) {
        super(env);
        this.children = new Vector(5, 5);
        this.name = name;
        setChildren (children);
    }

    /**
     * Creates a new conceptual-model element.
     *
     * @param env the environment
     * @param children the child conceptual-model elements
     */
    public ConceptualModelElement (Environment env, ConceptualModelElement[] children) {
        this(env, "", children);
    }

    /**
     * Creates a new conceptual-model element.
     *
     * @param env the environment
     */
    public ConceptualModelElement (Environment env) {
        this(env, "", null);
    }

    /**
     * Returns the parent of a conceptual-model element.
     *
     * @return the parent, or <code>null</code> if there is no parent
     */
    public ConceptualModelElement getParent () {
        return parent;
    }

    /**
     * Sets the parent for this conceptual-model element.
     *
     * @param parent DOCUMENT ME!
     */
    public void setParent (ConceptualModelElement parent) {
        this.parent = parent;
    }

    /**
     * Returns an iterator on the children of the conceptual-model element.
     *
     * @return DOCUMENT ME!
     */
    public Iterator children () {
        return children.iterator ();
    }

    /**
     * Returns the children as array.
     *
     * @return DOCUMENT ME!
     */
    public ConceptualModelElement[] getChildren () {
        ConceptualModelElement[] result = new ConceptualModelElement[children.size ()];

        for (int i = 0; i < result.length; i++) {
            result[i] = (ConceptualModelElement) children.get (i);
        }

        return result;
    }

    /**
     * Sets the children. Also sets this instance as parent for all children, if the specified
     * array is not <code>null</code>.
     *
     * @param children DOCUMENT ME!
     */
    public void setChildren (ConceptualModelElement[] children) {
        if (children != null) {
            this.children = new Vector(Arrays.asList (children));

            for (int i = 0; i < children.length; i++) {
                children[i].setParent (this);
            }
        } else {
            this.children = new Vector(5, 5);
        }
    }

    /**
     * Returns the child at the specified index.
     *
     * @param index the index of the child
     *
     * @return DOCUMENT ME!
     */
    public ConceptualModelElement getChild (int index) {
        return (ConceptualModelElement) children.get (index);
    }

    /**
     * Returns the index of a given child.
     *
     * @param child DOCUMENT ME!
     *
     * @return the index, or -1 if the child is not contained
     */
    public int getIndex (ConceptualModelElement child) {
        return children.indexOf (child);
    }

    /**
     * Returns the number of the children.
     *
     * @return DOCUMENT ME!
     */
    public int getChildrenCount () {
        return children.size ();
    }

    /**
     * Adds a child to the current children.
     *
     * @param child the child to add
     */
    public void addChild (ConceptualModelElement child) {
        children.add (child);
        child.setParent (this);
    }

    /**
     * Adds an array of children.
     *
     * @param children DOCUMENT ME!
     */
    public void addChildren (ConceptualModelElement[] children) {
        for (int i = 0; i < children.length; i++) {
            addChild (children[i]);
        }
    }

    /**
     * Inserts a child at a given index.
     *
     * @param child the child to add
     * @param index the index
     */
    public void insertChild (ConceptualModelElement child, int index) {
        children.add (index, child);
        child.setParent (this);
    }

    /**
     * Removes a child.
     *
     * @param child the child to remove
     */
    public void removeChild (ConceptualModelElement child) {
        if (children.contains (child)) {
            children.removeElement (child);
            child.setParent (null);
        }
    }

    /**
     * Removes this node from its parent.
     */
    public void removeFromParent () {
        if (parent != null) {
            parent.removeChild (this);
        }
    }

    /**
     * Removes all children.
     */
    public void removeAllChildren () {
        Iterator it = children ();

        while (it.hasNext ()) {
            ((ConceptualModelElement) it.next ()).setParent (null);
        }

        children.clear ();
    }

    /**
     * Returns whether or not this element has children.
     *
     * @return DOCUMENT ME!
     */
    public boolean hasChildren () {
        return (children != null) && (children.size () > 0);
    }

    /**
     * Returns the name of the element.
     *
     * @return DOCUMENT ME!
     */
    public String getName () {
        return name;
    }

    /**
     * Returns the name of the element.
     *
     * @return the name
     */
    public abstract String getElementName ();

    /**
     * Returns a description of the element.
     *
     * @return the description
     */
    public abstract String getElementDescription ();

    /**
     * Returns a String which describes the content of the element shortly.
     *
     * @return a string with a short description of the element
     */
    public abstract String toShortString ();

    /**
     * Converts a vector of conceptual-model elements to an array of conceptua-model elements
     *
     * @param v DOCUMENT ME!
     *
     * @return the array containing all conceptual-model elements, or <code>null</code> if the
     *         vector is not convertable.
     */
    public static ConceptualModelElement[] vectorToArray (Vector v) {
        ConceptualModelElement[] result = null;

        try {
            result = new ConceptualModelElement[v.size ()];

            for (int i = 0; i < result.length; i++) {
                result[i] = (ConceptualModelElement) v.get (i);
            }
        } catch (Throwable t) {
            ;
        }

        return result;
    }

    /**
     * Converts an array of conceptual elements to a vector of conceptual elements
     *
     * @param a DOCUMENT ME!
     *
     * @return the vector containing all conceptual-model elements, or <code>null</code> if the
     *         array is not convertable.
     */
    public static Vector arrayToVector (ConceptualModelElement[] a) {
        Vector result = new Vector();

        try {
            for (int i = 0; i < a.length; i++) {
                result.add (a[i]);
            }
        } catch (Throwable t) {
            ;
        }

        return result;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.