/*
* 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.MouseEventRecordable;
import jacareto.system.Environment;
import java.awt.event.MouseEvent;
import java.util.Vector;
/**
* This structure element stands for a mouse drag.
*
* @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
* @version 1.0
*/
public class MouseDrag extends StructureElement {
/** The mouse pressed event recordable. */
private MouseEventRecordable mousePressed;
/** The mouse released event recordable. */
private MouseEventRecordable mouseReleased;
/** The x coordinate of the motion start position. */
private int startX;
/** The x coordinate of the motion stop position. */
private int stopX;
/** The y coordinate of the motion start position. */
private int startY;
/** The y coordinate of the motion stop position. */
private int stopY;
/** The name of the source component. */
private String sourceName;
/** The name of the component. */
private String componentName;
/**
* Creates a new "mouse drag" structure element.
*
* @param env the environment
* @param children the child structure elements
*/
public MouseDrag (Environment env, StructureElement[] children) {
super(env, children);
MouseEventRecordable firstMouseEvent = (MouseEventRecordable) children[0];
StructureElement tmp;
int index = children.length - 1;
do {
tmp = children[index--];
} while (! (tmp instanceof MouseEventRecordable));
MouseEventRecordable lastMouseEvent = (MouseEventRecordable) tmp;
startX = firstMouseEvent.getX ();
startY = firstMouseEvent.getY ();
stopX = lastMouseEvent.getX ();
stopY = lastMouseEvent.getY ();
sourceName = firstMouseEvent.getSourceName ();
componentName = firstMouseEvent.getComponentName ();
if (firstMouseEvent.getID () == MouseEvent.MOUSE_PRESSED) {
mousePressed = firstMouseEvent;
}
if (lastMouseEvent.getID () == MouseEvent.MOUSE_RELEASED) {
mouseReleased = lastMouseEvent;
}
}
/**
* 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;
Vector addedChildren = null;
StructureElement element = null;
RecordTokenizerState rtState = recordTokenizer.saveState ();
try {
MouseEventRecordable firstMouseEvent = (MouseEventRecordable) recordTokenizer.get ();
if ((firstMouseEvent == null) ||
((firstMouseEvent.getID () != MouseEvent.MOUSE_PRESSED) &&
(firstMouseEvent.getID () != MouseEvent.MOUSE_DRAGGED))) {
return null;
}
addedChildren = new Vector(20, 20);
addedChildren.add (firstMouseEvent);
recordTokenizer.forward ();
boolean proceed = true;
while (proceed) {
proceed = false;
element = recordTokenizer.get ();
if ((element != null) && element instanceof MouseEventRecordable &&
(((MouseEventRecordable) element).getID () == MouseEvent.MOUSE_DRAGGED)) {
addedChildren.add (element);
recordTokenizer.forward ();
proceed = true;
} else {
element = MouseChangedComponent.parse (env, recordTokenizer);
if (element == null) {
element = MouseEntered.parse (env, recordTokenizer);
}
if (element == null) {
element = MouseExited.parse (env, recordTokenizer);
}
if (element != null) {
addedChildren.add (element);
proceed = true;
}
}
}
if (firstMouseEvent.getID () == MouseEvent.MOUSE_PRESSED) {
MouseEventRecordable lastMouseEvent = (MouseEventRecordable) recordTokenizer.next ();
if (lastMouseEvent != null) {
addedChildren.add (lastMouseEvent);
// mouse pressed and released at different positions
// if no mouse drag event has occured
if ((firstMouseEvent.getID () == MouseEvent.MOUSE_PRESSED) &&
(lastMouseEvent.getID () == MouseEvent.MOUSE_RELEASED)) {
if ((lastMouseEvent.getID () != MouseEvent.MOUSE_RELEASED) ||
((addedChildren.size () == 2) &&
(firstMouseEvent.getX () == lastMouseEvent.getX ()) &&
(firstMouseEvent.getY () == lastMouseEvent.getY ()))) {
throw new Exception();
}
}
}
}
StructureElement[] children = vectorToArray (addedChildren);
result = new MouseDrag(env, children);
} catch (Exception e) {
recordTokenizer.restoreState (rtState);
}
return result;
}
/**
* Returns the x-coordinate of the motion start position.
*
* @return DOCUMENT ME!
*/
public int getStartX () {
return startX;
}
/**
* Returns the y-coordinate of the motion start position.
*
* @return DOCUMENT ME!
*/
public int getStartY () {
return startY;
}
/**
* Returns the x-coordinate of the motion stop position.
*
* @return DOCUMENT ME!
*/
public int getStopX () {
return stopX;
}
/**
* Returns the y-coordinate of the motion stop position.
*
* @return DOCUMENT ME!
*/
public int getStopY () {
return stopY;
}
/**
* Returns the source name.
*
* @return DOCUMENT ME!
*/
public String getSourceName () {
return sourceName;
}
/**
* Returns the component name.
*
* @return DOCUMENT ME!
*/
public String getComponentName () {
return componentName;
}
/**
* Returns the mouse pressed event recordable. May return <code>null</code> if there is not
* mouse pressed event.
*
* @return DOCUMENT ME!
*/
public MouseEventRecordable getMousePressed () {
return mousePressed;
}
/**
* Returns the mouse released event recordable. May return <code>null</code> if there is not
* mouse released event.
*
* @return DOCUMENT ME!
*/
public MouseEventRecordable getMouseReleased () {
return mouseReleased;
}
/**
* Returns the name of the element.
*
* @return the name
*/
public String getElementName () {
return language.getString ("Structures.MouseDrag.Name");
}
/**
* Returns a description of the element.
*
* @return the description
*/
public String getElementDescription () {
return language.getString ("Structures.MouseDrag.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 () + " (" + startX + "," + startY + ") -> (" + stopX + "," + stopY +
")";
}
/**
* Clones the element.
*
* @return DOCUMENT ME!
*/
public Object clone () {
StructureElement[] clonedChildren = getClonedChildren ();
return new MouseDrag(env, clonedChildren);
}
}
|