ItemStateChange.java :  » Testing » jacareto » jacareto » struct » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » struct » ItemStateChange.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.struct;


import jacareto.parse.RecordTokenizer;
import jacareto.parse.RecordTokenizerState;
import jacareto.record.ActionEventRecordable;
import jacareto.record.ItemEventRecordable;
import jacareto.record.MouseEventRecordable;
import jacareto.system.Environment;

import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;

import java.util.Vector;

/**
 * An item has been selected or deselected.
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.01
 */
public class ItemStateChange extends StructureElement {
    /** Whether or not the item has been selected. */
    private boolean selected;

    /** The action command (if there is one). */
    private String actionName;

    /** The item event recordable contained in this item state change. */
    private ItemEventRecordable itemEventRecordable;

    /** The source name. */
    private String sourceName;

    /** The source class. */
    private String sourceClass;

    /**
     * Creates a new "item state change" structure element. One of the children must be an item
     * event recordable, one should be an action event recordable (but has not to).
     *
     * @param env the environment
     * @param children the child structure elements
     */
    public ItemStateChange (Environment env, StructureElement[] children) {
        super(env, children);

        ActionEventRecordable action = null;

        for (int i = 0; i < children.length; i++) {
            if (children[i] instanceof ItemEventRecordable) {
                itemEventRecordable = (ItemEventRecordable) children[i];
            } else if (children[i] instanceof ActionEventRecordable) {
                action = (ActionEventRecordable) children[i];
            }
        }

        this.sourceName = itemEventRecordable.getSourceName ();
        this.sourceClass = itemEventRecordable.getSourceClass ();
        selected = itemEventRecordable.getStateChange () == ItemEvent.SELECTED;
        actionName = "";

        if (action != null) {
            actionName = action.getActionCommand ();
        }
    }

    /**
     * Creates a new "item state change" structure element.
     *
     * @param env the environment
     * @param children the child structure elements
     * @param actionName the action command (if there is one)
     * @param selected whether or not the item has been selected
     */
    public ItemStateChange (Environment env, StructureElement[] children, String actionName,
        boolean selected) {
        super(env, children);
        this.selected = selected;
        this.actionName = actionName;

        for (int i = 0; i < children.length; i++) {
            if (children[i] instanceof ItemEventRecordable) {
                itemEventRecordable = (ItemEventRecordable) children[i];

                break;
            }
        }

        this.sourceName = itemEventRecordable.getSourceName ();
        this.sourceClass = itemEventRecordable.getSourceClass ();
    }

    /**
     * Parses a record which is tokenized by the given record tokenizer.
     *
     * @param env DOCUMENT ME!
     * @param recordTokenizer the record tokenizer
     *
     * @return a structure element, or <code>null</code> if this class cannot parse the record at
     *         the current position
     */
    public static StructureElement parse (Environment env, RecordTokenizer recordTokenizer) {
        StructureElement result = null;
        StructureElement[] children = null;

        ItemEventRecordable itemEvent = null;
        ActionEventRecordable action = null;

        RecordTokenizerState rtState = recordTokenizer.saveState ();

        // mouse pressed, item event, focus change, mouse released, mouse clicked
        try {
            MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
            itemEvent = (ItemEventRecordable) recordTokenizer.next ();

            FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
            MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
            MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer.next ();

            boolean cond = itemEvent != null;
            cond = cond &&
                ((action == null) || itemEvent.getSourceName ().equals (action.getSourceName ()));
            cond = cond && (mousePressed.getID () == MouseEvent.MOUSE_PRESSED) &&
                (mouseReleased.getID () == MouseEvent.MOUSE_RELEASED) &&
                (mouseClicked.getID () == MouseEvent.MOUSE_CLICKED);

            if (cond) {
                StructureElement[] mouseChildren = new StructureElement[3];
                mouseChildren[0] = mousePressed;
                mouseChildren[1] = mouseReleased;
                mouseChildren[2] = mouseClicked;

                MouseClick mouseClick = new MouseClick(env, mouseChildren);
                Vector addedChildren = new Vector(3);
                addedChildren.add (mouseClick);

                if (focusChange != null) {
                    addedChildren.add (focusChange);
                }

                addedChildren.add (itemEvent);
                children = vectorToArray (addedChildren);
            } else {
                throw new Exception();
            }
        } catch (Throwable t) {
            recordTokenizer.restoreState (rtState);
        }

        // (focus change), item event, (action event)
        if (children == null) {
            try {
                FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
                itemEvent = (ItemEventRecordable) recordTokenizer.next ();
                action = null;

                StructureElement tmp = recordTokenizer.get ();

                if ((tmp != null) && tmp instanceof ActionEventRecordable) {
                    action = (ActionEventRecordable) tmp;
                    recordTokenizer.forward ();
                }

                boolean cond = itemEvent != null;
                cond = cond &&
                    ((action == null) ||
                    itemEvent.getSourceName ().equals (action.getSourceName ()));

                if (cond) {
                    Vector addedChildren = new Vector(5);

                    if (focusChange != null) {
                        addedChildren.add (focusChange);
                    }

                    addedChildren.add (itemEvent);

                    if (action != null) {
                        addedChildren.add (action);
                    }

                    children = vectorToArray (addedChildren);
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // Create the result;
        if (children != null) {
            boolean selected = itemEvent.getStateChange () == ItemEvent.SELECTED;
            String actionName = "";

            if (action != null) {
                actionName = action.getActionCommand ();
            }

            //result = new ItemStateChange (env, children);
            result = new ItemStateChange(env, children, actionName, selected);
        } else {
            recordTokenizer.restoreState (rtState);
        }

        return result;
    }

    /**
     * Returns the name of the element.
     *
     * @return the name
     */
    public String getElementName () {
        return language.getString ("Structures.ItemStateChange.Name");
    }

    /**
     * Returns a description of the element.
     *
     * @return the description
     */
    public String getElementDescription () {
        return language.getString ("Structures.ItemStateChange.Description");
    }

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

        if ((actionName != null) && ! actionName.equals ("")) {
            result += ("\"" + actionName + "\" ");

            if (selected) {
                result += language.getString ("Structures.ItemStateChange.Selected");
            } else {
                result += language.getString ("Structures.ItemStateChange.Deselected");
            }
        } else {
            if (selected) {
                result = language.getString ("Structures.ItemStateChange.ItemSelected");
            } else {
                result = language.getString ("Structures.ItemStateChange.ItemDeselected");
            }
        }

        return result;
    }

    /**
     * Returns whether or not the item has been selected
     *
     * @return DOCUMENT ME!
     */
    public boolean isSelected () {
        return selected;
    }

    /**
     * Returns the action command (if there is one).
     *
     * @return DOCUMENT ME!
     */
    public String getActionName () {
        return actionName;
    }

    /**
     * Returns the source name.
     *
     * @return DOCUMENT ME!
     */
    public String getSourceName () {
        return sourceName;
    }

    /**
     * Returns the source class.
     *
     * @return DOCUMENT ME!
     */
    public String getSourceClass () {
        return sourceClass;
    }

    /**
     * Returns the item event recordable contained in this structure element.
     *
     * @return DOCUMENT ME!
     */
    public ItemEventRecordable getItemEventRecordable () {
        return itemEventRecordable;
    }

    /**
     * Clones the element.
     *
     * @return DOCUMENT ME!
     */
    public Object clone () {
        StructureElement[] clonedChildren = getClonedChildren ();

        return new ItemStateChange(env, clonedChildren, actionName, selected);
    }

    public boolean hasProcTime () {
        return true;
    }
}
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.