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

Java Open Source » Testing » jacareto 
jacareto » jacareto » struct » ListSelectionChange.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.ListSelectionEventRecordable;
import jacareto.record.MouseEventRecordable;
import jacareto.system.Environment;

import java.awt.event.MouseEvent;

/**
 * The selection of a list has changed
 *
 * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
 * @version 1.01
 */
public class ListSelectionChange extends StructureElement {
    /**
     * Creates a new "list selection change" structure element.
     *
     * @param env the environment
     * @param children the child structure elements
     */
    public ListSelectionChange (Environment env, StructureElement[] children) {
        super(env, children);
    }

    /**
     * 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;

        RecordTokenizerState rtState = recordTokenizer.saveState ();

        // mouse pressed, list selection, mouse released, list selection, mouse clicked
        try {
            MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
            ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
            MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
            ListSelectionEventRecordable list2 = (ListSelectionEventRecordable) recordTokenizer.next ();
            MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer.next ();

            boolean cond = true;
            cond = cond && (mousePressed.getID () == MouseEvent.MOUSE_PRESSED) &&
                (mouseReleased.getID () == MouseEvent.MOUSE_RELEASED) &&
                (mouseClicked.getID () == MouseEvent.MOUSE_CLICKED);

            if (cond) {
                RecordTokenizerState beforeClick = recordTokenizer.saveState ();
                MouseClick secondClick = (MouseClick) MouseClick.parse (env, recordTokenizer);

                if ((secondClick != null) &&
                        (secondClick.getClickedChild ().getClickCount () != 2)) {
                    secondClick = null;
                    recordTokenizer.restoreState (beforeClick);
                }

                StructureElement[] mouseChildren = new StructureElement[3];
                mouseChildren[0] = mousePressed;
                mouseChildren[1] = mouseReleased;
                mouseChildren[2] = mouseClicked;

                MouseClick firstClick = new MouseClick(env, mouseChildren);
                children = new StructureElement[3];

                int index = 0;

                if (secondClick != null) {
                    MouseClick[] mClickChildren = new MouseClick[2];
                    mClickChildren[0] = firstClick;
                    mClickChildren[1] = secondClick;

                    MultipleMouseClick mClick = new MultipleMouseClick(env, mClickChildren);
                    children[index++] = mClick;
                } else {
                    children[index++] = firstClick;
                }

                children[index++] = list1;
                children[index++] = list2;
            } else {
                throw new Exception();
            }
        } catch (Throwable t) {
            recordTokenizer.restoreState (rtState);
        }

        // mouse pressed, focus change, mouse released, list selection, (list selection), mouse clicked
        if (children == null) {
            try {
                MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
                FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
                MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list2 = null;

                StructureElement tmp = recordTokenizer.get (0);

                if ((tmp != null) && tmp instanceof ListSelectionEventRecordable) {
                    list2 = (ListSelectionEventRecordable) tmp;
                    recordTokenizer.forward ();
                }

                MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer.next ();

                boolean cond = true;
                cond = cond && (focusChange != null);
                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;

                    if (list2 != null) {
                        children = new StructureElement[4];
                    } else {
                        children = new StructureElement[3];
                    }

                    int index = 0;
                    children[index++] = new MouseClick(env, mouseChildren);
                    children[index++] = focusChange;
                    children[index++] = list1;

                    if (list2 != null) {
                        children[index++] = list2;
                    }
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // mouse pressed, list selection, focus change, mouse drag, mouse released, list selection
        if (children == null) {
            try {
                MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
                FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
                MouseDrag mouseDrag = (MouseDrag) MouseDrag.parse (env, recordTokenizer);
                MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list2 = (ListSelectionEventRecordable) recordTokenizer.next ();

                boolean cond = true;
                cond = cond && (focusChange != null) && (mouseDrag != null);
                cond = cond && (mousePressed.getID () == MouseEvent.MOUSE_PRESSED) &&
                    (mouseReleased.getID () == MouseEvent.MOUSE_RELEASED);
                cond = cond && (list1 != null) && (list2 != null);

                if (cond) {
                    children = new StructureElement[6];

                    int index = 0;
                    children[index++] = mousePressed;
                    children[index++] = list1;
                    children[index++] = focusChange;
                    children[index++] = mouseDrag;
                    children[index++] = mouseReleased;
                    children[index++] = list2;
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // mouse pressed, list selection, focus change, mouse released, list selection, mouse clicked
        if (children == null) {
            try {
                MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
                FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
                MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list2 = (ListSelectionEventRecordable) recordTokenizer.next ();
                MouseEventRecordable mouseClicked = (MouseEventRecordable) recordTokenizer.next ();

                boolean cond = true;
                cond = cond && (focusChange != null);
                cond = cond && (mousePressed.getID () == MouseEvent.MOUSE_PRESSED) &&
                    (mouseReleased.getID () == MouseEvent.MOUSE_RELEASED) &&
                    (mouseClicked.getID () == MouseEvent.MOUSE_CLICKED);
                cond = cond && (list1 != null) && (list2 != null);

                if (cond) {
                    RecordTokenizerState beforeClick = recordTokenizer.saveState ();
                    MouseClick secondClick = (MouseClick) MouseClick.parse (env, recordTokenizer);

                    if ((secondClick != null) &&
                            (secondClick.getClickedChild ().getClickCount () != 2)) {
                        secondClick = null;
                        recordTokenizer.restoreState (beforeClick);
                    }

                    StructureElement[] mouseChildren = new StructureElement[3];
                    mouseChildren[0] = mousePressed;
                    mouseChildren[1] = mouseReleased;
                    mouseChildren[2] = mouseClicked;

                    MouseClick firstClick = new MouseClick(env, mouseChildren);

                    children = new StructureElement[4];

                    int index = 0;

                    if (secondClick != null) {
                        MouseClick[] mClickChildren = new MouseClick[2];
                        mClickChildren[0] = firstClick;
                        mClickChildren[1] = secondClick;

                        MultipleMouseClick mClick = new MultipleMouseClick(env, mClickChildren);
                        children[index++] = mClick;
                    } else {
                        children[index++] = firstClick;
                    }

                    children[index++] = focusChange;
                    children[index++] = list1;
                    children[index++] = list2;
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // focus change, list selection, list selection,
        if (children == null) {
            try {
                FocusChange focusChange = (FocusChange) FocusChange.parse (env, recordTokenizer);
                ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list2 = (ListSelectionEventRecordable) recordTokenizer.next ();

                boolean cond = (focusChange != null) && (list1 != null) && (list2 != null);

                if (cond) {
                    children = new StructureElement[3];

                    int index = 0;
                    children[index++] = focusChange;
                    children[index++] = list1;
                    children[index++] = list2;
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // mouse pressed, list selection, mouse drag, mouse released, list selection
        if (children == null) {
            try {
                MouseEventRecordable mousePressed = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list1 = (ListSelectionEventRecordable) recordTokenizer.next ();
                MouseDrag mouseDrag = (MouseDrag) MouseDrag.parse (env, recordTokenizer);
                MouseEventRecordable mouseReleased = (MouseEventRecordable) recordTokenizer.next ();
                ListSelectionEventRecordable list2 = (ListSelectionEventRecordable) recordTokenizer.next ();

                boolean cond = true;
                cond = cond && (mouseDrag != null);
                cond = cond && (mousePressed.getID () == MouseEvent.MOUSE_PRESSED) &&
                    (mouseReleased.getID () == MouseEvent.MOUSE_RELEASED);
                cond = cond && (list1 != null) && (list2 != null);

                if (cond) {
                    children = new StructureElement[5];

                    int index = 0;
                    children[index++] = mousePressed;
                    children[index++] = mouseDrag;
                    children[index++] = list1;
                    children[index++] = mouseReleased;
                    children[index++] = list2;
                } else {
                    throw new Exception();
                }
            } catch (Throwable t) {
                recordTokenizer.restoreState (rtState);
            }
        }

        // Create the result;
        if (children != null) {
            result = new ListSelectionChange(env, children);
        } else {
            recordTokenizer.restoreState (rtState);
        }

        return result;
    }

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

    /**
     * Returns a description of the element.
     *
     * @return the description
     */
    public String getElementDescription () {
        return language.getString ("Structures.ListSelectionChange.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 () {
        return getElementName ();
    }

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

        return new ListSelectionChange(env, clonedChildren);
    }
}
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.