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.replay;
025:
026: import jacareto.record.MouseEventRecordable;
027: import jacareto.replay.exception.ComponentNotFoundException;
028: import jacareto.struct.StructureElement;
029: import jacareto.system.Environment;
030:
031: import java.awt.Component;
032: import java.awt.Point;
033: import java.awt.event.MouseEvent;
034:
035: /**
036: * A replayer which is responsible for mouse events.
037: *
038: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
039: * @version 1.01
040: */
041: public class MouseEventReplayer extends Replayer {
042: /** The x coordinate of the last mouse pressed event. */
043: double lastPressedX;
044:
045: /** The y coordinate of the last mouse pressed event. */
046: double lastPressedY;
047:
048: /** The source name of the last mouse pressed event. */
049: String lastPressedSourceName;
050:
051: /**
052: * Creates a new replayer.
053: *
054: * @param env the environment
055: * @param replay the Replay object
056: */
057: public MouseEventReplayer(Environment env, Replay replay) {
058: super (env, replay);
059:
060: lastPressedX = -1;
061: lastPressedY = -1;
062: lastPressedSourceName = null;
063: }
064:
065: /**
066: * Returns whether or not this replayer is responsible for the given structure element. This is
067: * true if the recordable is an {@link MouseEventRecordable} whose id is contained by a mouse
068: * event mask.
069: *
070: * @param element the structure element
071: *
072: * @return <code>true</code> if this replayer is responsible for this object; otherwise
073: * <code>false</code>.
074: */
075: public boolean handlesElement(StructureElement element) {
076: try {
077: int ID = ((MouseEventRecordable) element).getID();
078:
079: return (ID != MouseEvent.MOUSE_MOVED)
080: && (ID != MouseEvent.MOUSE_DRAGGED);
081: } catch (Throwable t) {
082: return false;
083: }
084: }
085:
086: /**
087: * Dispatches a mouse event given in the specified element which must be of the type {@link
088: * MouseEventRecordable}. Before dispatching the event this method causes the actual Thread to
089: * sleep the time specified by the mouse event recordable or by the <code>Replay</code>
090: * object.
091: *
092: * @param element the structure element this replayer should work with
093: *
094: * @return DOCUMENT ME!
095: *
096: * @throws ComponentNotFoundException DOCUMENT ME!
097: */
098: public boolean replay(StructureElement element)
099: throws ComponentNotFoundException {
100: MouseEventRecordable mouseEventRecordable = (MouseEventRecordable) element;
101: int eventId = mouseEventRecordable.getID();
102: JacaretoRobot robot = replay.getRobot();
103: ReplayMode replayMode = replay.getMode();
104: int mouseMode = replayMode.getMouseMode();
105:
106: // Get the component belonging to the event
107: Component source = null;
108:
109: try {
110: source = replay.getVisibleComponent(mouseEventRecordable
111: .getSourceName(), mouseEventRecordable
112: .getComponentName());
113: } catch (ComponentNotFoundException cnf) {
114: if (eventId == MouseEvent.MOUSE_RELEASED) {
115: robot.mouseRelease(mouseEventRecordable, null);
116: }
117:
118: throw (cnf);
119: }
120:
121: // if the event is a mouse entered or a mouse exited event, the events will
122: // just be executed in PSEUDOMOUSE mode, because in REALMOUSE mode they are
123: // generated automatically by the robot's actions.
124: if ((eventId == MouseEvent.MOUSE_ENTERED)
125: || (eventId == MouseEvent.MOUSE_EXITED)
126: || (eventId == MouseEvent.MOUSE_CLICKED)) {
127: /*if (replayMode.getTimeMode () == ReplayMode.REALTIME) {
128: replay.snooze (mouseEventRecordable.getDuration () -
129: mouseEventRecordable.getProcTime ());
130: }*/
131: if ((mouseMode == ReplayMode.PSEUDOMOUSE)
132: || (mouseMode == ReplayMode.NOMOUSE)) {
133: robot.dispatchEvent(mouseEventRecordable
134: .getEvent(source));
135: }
136:
137: return true;
138: }
139:
140: if (replayMode.getMouseMode() == ReplayMode.REALMOUSE) {
141: replay.getComponents().toFront(source);
142: }
143:
144: // mark the event if mark mode is enabled
145: if (replayMode.getIsMarkMode()) {
146: replay.mark(source, new Point(mouseEventRecordable.getX(),
147: mouseEventRecordable.getY()));
148: }
149:
150: // sleep the corresponding time
151: if (replayMode.getTimeMode() == ReplayMode.REALTIME) {
152: // replay.snooze (mouseEventRecordable.getDuration ());
153: } else if ((eventId == MouseEvent.MOUSE_PRESSED)
154: || ((eventId == MouseEvent.MOUSE_RELEASED) && !(mouseEventRecordable
155: .getSourceName().equals(lastPressedSourceName)
156: && (mouseEventRecordable.getX() == lastPressedX) && (mouseEventRecordable
157: .getY() == lastPressedY)))) {
158: replay.snooze(replayMode.getFixedTimeDelay());
159: }
160:
161: // Move the mouse to the position and dispatch the event
162: boolean mouseMoved = robot.mouseMove(mouseEventRecordable,
163: source);
164:
165: //boolean mouseMoved = true;
166: //The following line does not work together with "mouse dragged" events
167: //replay.executeEvent ( e.getEvent() );
168: //better:
169: if (mouseMoved) {
170: if (eventId == MouseEvent.MOUSE_PRESSED) {
171: robot.mousePress(mouseEventRecordable, source);
172: lastPressedSourceName = mouseEventRecordable
173: .getSourceName();
174: lastPressedX = mouseEventRecordable.getX();
175: lastPressedY = mouseEventRecordable.getY();
176:
177: if (replayMode.getPlaysMouseSounds()) {
178: replay.getSounds().playMousePressed();
179: }
180: } else if (eventId == MouseEvent.MOUSE_RELEASED) {
181: robot.mouseRelease(mouseEventRecordable, source);
182:
183: if (replayMode.getPlaysMouseSounds()) {
184: replay.getSounds().playMouseReleased();
185: }
186: }
187:
188: return true;
189: } else {
190: return false;
191: }
192: }
193: }
|