001: /*
002: * Geotools 2 - OpenSource mapping toolkit
003: * (C) 2003, Geotools Project Management Committee (PMC)
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.geotools.gui.swing.event;
020:
021: import java.awt.event.MouseEvent;
022:
023: //import java.awt.geom.AffineTransform;
024: import java.awt.geom.Point2D;
025:
026: //import org.geotools.ct.MathTransform;
027: import org.opengis.referencing.operation.MathTransform;
028:
029: // OpenGIS dependencies
030: import org.opengis.referencing.operation.TransformException;
031:
032: //import org.geotools.pt.CoordinatePoint;
033: import org.geotools.geometry.GeneralDirectPosition;
034:
035: //import org.geotools.cs.CoordinateSystem;
036: import org.geotools.referencing.crs.AbstractCRS;
037:
038: /**
039: * A MouseEvent which contains methods to obtain coordinates in real world
040: * CoordinateSystem as well as Screen Coordinates.
041: * All {@link MouseListener}s that have registered for
042: * {@link org.geotools.gui.swing.MapPaneImpl} mouseEvents will receive
043: * events of this class.
044: * Listeners implementations can implements their code as below:
045: *
046: * <blockquote><pre>
047: * public void mouseClicked(MouseEvent e) {
048: * GeoMouseEvent event = (GeoMouseEvent) e;
049: * // Process event here...
050: * }
051: * </pre></blockquote>
052: *
053: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/mappane/src/main/java/org/geotools/gui/swing/event/GeoMouseEvent.java $
054: * @version $Id: GeoMouseEvent.java 25694 2007-05-30 23:33:52Z ianturton $
055: * @author Cameron Shorter
056: */
057: public final class GeoMouseEvent extends MouseEvent {
058: /**
059: * The transform which will convert screenCoordinates
060: * to CoordinateSystem coordinates.
061: */
062: final MathTransform transform;
063:
064: /**
065: * The coordinate system for ({@link #x},{@link #y}) or <code>null</code>
066: * if the coordinate has not yet been computed. This coordinate system
067: * must be two-dimensional.
068: */
069:
070: //private transient AbstractCRS coordinateSystem;
071: /**
072: * A mouseClick event which also contains methods to transform from
073: * pixels to the Coordinate System of the Renderer.
074: * @param event The original mouse event.
075: * @param transform The transform which will convert screenCoordinates
076: * to CoordinateSystem coordinates.
077: */
078: public GeoMouseEvent(final MouseEvent event,
079: final MathTransform transform) {
080: super (event.getComponent(), // the Component that originated the event
081: event.getID(), // the integer that identifies the event
082: event.getWhen(), // a long int that gives the time the
083: // event occurred
084: event.getModifiers(), // the modifier keys down during event
085: // (shift, ctrl, alt, meta)
086: event.getX(), // the horizontal x coordinate for the
087: // mouse location
088: event.getY(), // the vertical y coordinate for the mouse
089: // location
090: event.getClickCount(), // the number of mouse clicks associated
091: // with event
092: event.isPopupTrigger(), // a boolean, true if this event is a
093: // trigger for a popup-menu
094: event.getButton()); // which of the mouse buttons has changed
095: // state (JDK 1.4 only).
096:
097: this .transform = transform;
098: }
099:
100: /**
101: * Returns the "real world" mouse's position. The coordinates are expressed
102: * in Context's CoordinateSystem.
103: *
104: * @param dest A pre-allocated variable to store the mouse's location
105: * in CoordinateSystems, can be set to <code>null</code>.
106: * @return The mouse's location in CoordinateSystem coordinates.
107: * @throws TransformException when transform is invalid.
108: */
109: public GeneralDirectPosition getMapCoordinate(
110: GeneralDirectPosition dest) throws TransformException {
111: if (dest == null) {
112: dest = new GeneralDirectPosition(getX(), getY());
113: } else {
114: dest.setLocation(new Point2D.Double(getX(), getY()));
115: }
116:
117: transform.transform(dest, dest);
118:
119: return dest;
120: }
121: }
|