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.catching;
025:
026: import jacareto.eventmask.AWTEventMaskGroup;
027: import jacareto.eventmask.MouseEventMask;
028: import jacareto.eventmask.MouseMotionEventMask;
029: import jacareto.system.Environment;
030:
031: import java.awt.Component;
032: import java.awt.event.MouseEvent;
033:
034: import java.util.EventObject;
035:
036: /**
037: * This class saves the component name and the coordinates of the last mouse event. This instance
038: * sets its event mask automatically; you do not have to do this.
039: *
040: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
041: * @version 1.01
042: */
043: public class LastMousePosition extends Catcher {
044: /** Indicates that at least one mouse event has been handled by this object. */
045: private boolean isValid;
046:
047: /** The x-coordinate of the last mouse event. */
048: private int lastX;
049:
050: /** The y-coordinate of the last mouse event. */
051: private int lastY;
052:
053: /** The component of the last mouse event. */
054: private Component lastComponent;
055:
056: /**
057: * Creates a new last mouse position object
058: *
059: * @param env the environment
060: */
061: public LastMousePosition(Environment env) {
062: super (env);
063: isValid = false;
064:
065: // Setting the event mask
066: AWTEventMaskGroup eventMaskGroup = new AWTEventMaskGroup(env,
067: AWTEventMaskGroup.INIT_EMPTY);
068: eventMaskGroup.addMask(new MouseEventMask(env,
069: MouseEventMask.ALL_INCLUDED));
070: eventMaskGroup.addMask(new MouseMotionEventMask(env,
071: MouseMotionEventMask.ALL_INCLUDED));
072: setAWTEventMask(eventMaskGroup);
073: }
074:
075: /**
076: * Handles the specified event. This event must be a mouse event or a mouse motion event to be
077: * handled.
078: *
079: * @param event the event
080: *
081: * @return <code>true</code> (The other catchers are allowed to get the handled event.)
082: */
083: public boolean handleEvent(EventObject event) {
084: MouseEvent mouseEvent = (MouseEvent) event;
085: lastX = mouseEvent.getX();
086: lastY = mouseEvent.getY();
087: lastComponent = ((Component) mouseEvent.getSource());
088: isValid = true;
089:
090: return true;
091: }
092:
093: /**
094: * Returns <code>true</code> if at least one mouse event has been handled.
095: *
096: * @return guess!
097: */
098: public boolean isValid() {
099: return isValid;
100: }
101:
102: /**
103: * Returns the x-coordinate of the last mouse event. The result of this method is just ok when
104: * the method {@link #isValid()} returns <code>true</code>.
105: *
106: * @return guess!
107: */
108: public int getLastX() {
109: return lastX;
110: }
111:
112: /**
113: * Returns the y-coordinate of the last mouse event. The result of this method is just ok when
114: * the method {@link #isValid()} returns <code>true</code>.
115: *
116: * @return guess!
117: */
118: public int getLastY() {
119: return lastY;
120: }
121:
122: /**
123: * Returns the component of the last mouse event. The result of this method is just ok when the
124: * method {@link #isValid()} returns <code>true</code>.
125: *
126: * @return guess!
127: */
128: public Component getLastComponent() {
129: return lastComponent;
130: }
131: }
|