001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gui.swing;
017:
018: /**
019: * a simple highlight manager
020: * @author Ian Turton
021: */
022: import java.awt.Rectangle;
023: import java.awt.event.MouseEvent;
024: import java.awt.event.MouseMotionAdapter;
025: import javax.swing.event.EventListenerList;
026: import com.vividsolutions.jts.geom.Coordinate;
027: import com.vividsolutions.jts.geom.Envelope;
028: import com.vividsolutions.jts.geom.Geometry;
029: import com.vividsolutions.jts.geom.GeometryFactory;
030: import org.opengis.filter.Filter;
031: import org.opengis.filter.FilterFactory2;
032: import org.geotools.filter.IllegalFilterException;
033: import org.geotools.gui.swing.event.HighlightChangeListener;
034: import org.geotools.gui.swing.event.HighlightChangedEvent;
035: import org.geotools.map.MapLayer;
036:
037: public class HighlightManager extends MouseMotionAdapter {
038: EventListenerList listeners = new EventListenerList();
039:
040: /**
041: * the layer to highlight
042: */
043: MapLayer highlightLayer;
044:
045: /**
046: * The filterfactory needed to create the highlight filter
047: */
048: FilterFactory2 ff = (FilterFactory2) org.geotools.factory.CommonFactoryFinder
049: .getFilterFactory(null);
050:
051: /**
052: * the geometry factory to convert the mouse point to a jts.Point
053: */
054: GeometryFactory gf = new GeometryFactory();
055:
056: /**
057: * stores our best guess as to what the name of the geometry
058: * in the highlighted layer is.
059: */
060: String geomName;
061: Filter lastFilter = null;
062:
063: public HighlightManager(MapLayer layer) {
064: setHighlightLayer(layer);
065: }
066:
067: /**
068: * listens for mouse moved events
069: * @param e - MouseEvent
070: */
071: public void mouseMoved(MouseEvent e) {
072: if (highlightLayer == null) {
073: return;
074: }
075:
076: Rectangle bounds = e.getComponent().getBounds();
077: JMapPane pane = (JMapPane) e.getSource();
078: Envelope mapArea = pane.mapArea;
079: double x = (double) (e.getX());
080: double y = (double) (e.getY());
081: double width = mapArea.getWidth();
082: double height = mapArea.getHeight();
083:
084: double mapX = ((x * width) / (double) bounds.width)
085: + mapArea.getMinX();
086: double mapY = (((bounds.getHeight() - y) * height) / (double) bounds.height)
087: + mapArea.getMinY();
088: Filter f = null;
089:
090: Geometry geometry = gf.createPoint(new Coordinate(mapX, mapY));
091:
092: try {
093: Filter bb = ff.bbox(ff.property(geomName), mapArea
094: .getMinX(), mapArea.getMinY(), mapArea.getMaxX(),
095: mapArea.getMaxY(), pane.getContext()
096: .getCoordinateReferenceSystem().toString());
097: f = ff
098: .contains(ff.property(geomName), ff
099: .literal(geometry));
100: f = ff.and(bb, f);
101: if (f == lastFilter) {
102: return;
103: }
104:
105: lastFilter = f;
106: this .highlightChanged(e.getSource(), f);
107: } catch (IllegalFilterException ex) {
108: // TODO Auto-generated catch block
109: ex.printStackTrace();
110: }
111: }
112:
113: /**
114: * add a HighlightChangedListener to this manager, note this need not
115: * be a map which is why we pass a filter back not a map point.
116: * @param l - the listener
117: */
118: public void addHighlightChangeListener(HighlightChangeListener l) {
119: listeners.add(HighlightChangeListener.class, l);
120: }
121:
122: /**
123: * Remove a highlightlistener
124: * @param l - the listener
125: */
126: public void removeHightlightChangeListener(HighlightChangeListener l) {
127: listeners.remove(HighlightChangeListener.class, l);
128: }
129:
130: /**
131: * notify listeners that the highlight has changed
132: * @param source - where the mousemovement came from
133: * @param filter - the filter which selects the
134: * highlighted feature(s)
135: */
136: public void highlightChanged(Object source, Filter filter) {
137: HighlightChangeListener[] l = (HighlightChangeListener[]) listeners
138: .getListeners(HighlightChangeListener.class);
139: HighlightChangedEvent ev = new HighlightChangedEvent(source,
140: filter);
141:
142: for (int i = 0; i < l.length; i++) {
143: l[i].highlightChanged(ev);
144: }
145: }
146:
147: /**
148: * get the highlighted layer
149: * @return - the layer
150: */
151: public MapLayer getHighlightLayer() {
152: return highlightLayer;
153: }
154:
155: /**
156: * sets the highlighted layer
157: * @param highlightLayer - the layer
158: */
159: public void setHighlightLayer(MapLayer highlightLayer) {
160: this .highlightLayer = highlightLayer;
161:
162: if (this .highlightLayer != null) {
163: geomName = this .highlightLayer.getFeatureSource()
164: .getSchema().getDefaultGeometry().getName();
165:
166: if ((geomName == null) || (geomName == "")) {
167: geomName = "the_geom";
168: }
169: }
170: }
171: }
|