svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / MapOverviewPanListener.java @ 40558
History | View | Annotate | Download (6.83 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.project.documents.view.toolListeners; |
25 |
|
26 |
import java.awt.Image; |
27 |
import java.awt.event.InputEvent; |
28 |
import java.awt.event.MouseEvent; |
29 |
import java.awt.geom.Point2D; |
30 |
|
31 |
import org.gvsig.andami.PluginServices; |
32 |
import org.gvsig.app.gui.styling.SymbolSelector; |
33 |
import org.gvsig.app.project.documents.view.MapOverview; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.GeometryManager; |
36 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
37 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.mapcontext.ViewPort; |
40 |
import org.gvsig.fmap.mapcontrol.MapControl; |
41 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
42 |
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent; |
43 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
/**
|
51 |
* <p>Listener for moving the extent of the associated {@link MapOverview MapOverview} object
|
52 |
* according the movement between the initial and final points of line determined by the movement
|
53 |
* dragging with the third button of the mouse.</p>
|
54 |
*
|
55 |
* <p>Updates the extent of its <code>ViewPort</code> with the new position.</p>
|
56 |
*
|
57 |
* @author Vicente Caballero Navarro
|
58 |
*/
|
59 |
public class MapOverviewPanListener implements PanListener { |
60 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
61 |
private static final Logger logger = LoggerFactory.getLogger(MapOverviewPanListener.class); |
62 |
|
63 |
/**
|
64 |
* The image to display when the cursor is active.
|
65 |
*/
|
66 |
private final Image icursor = PluginServices.getIconTheme().get("cursor-crux").getImage(); |
67 |
|
68 |
/**
|
69 |
* The cursor used to work with this tool listener.
|
70 |
*
|
71 |
* @see #getCursor()
|
72 |
*/
|
73 |
// private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(icursor,
|
74 |
// new Point(16, 16), "");
|
75 |
|
76 |
/**
|
77 |
* Reference to the <code>MapControl</code> object that uses.
|
78 |
*/
|
79 |
protected MapControl mapControl;
|
80 |
|
81 |
/**
|
82 |
* <p>Creates a new listener for changing the position of the extent of the associated {@link MapOverview MapOverview} object.</p>
|
83 |
*
|
84 |
* @param mapControl the <code>MapControl</code> object which represents the <code>MapOverview</code>
|
85 |
*/
|
86 |
public MapOverviewPanListener(MapControl mapControl) {
|
87 |
this.mapControl = mapControl;
|
88 |
} |
89 |
|
90 |
/*
|
91 |
* (non-Javadoc)
|
92 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
|
93 |
*/
|
94 |
public Image getImageCursor() { |
95 |
return icursor;
|
96 |
} |
97 |
|
98 |
/*
|
99 |
* (non-Javadoc)
|
100 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
101 |
*/
|
102 |
public boolean cancelDrawing() { |
103 |
return true; |
104 |
} |
105 |
|
106 |
/*
|
107 |
public void move(MoveEvent event) throws BehaviorException {
|
108 |
if (!checkModifiers(event.getEvent().getModifiers())){
|
109 |
return;
|
110 |
}
|
111 |
|
112 |
ViewPort vp = mapControl.getMapContext().getViewPort();
|
113 |
MapOverview mov=(MapOverview) this.mapControl;
|
114 |
ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
|
115 |
if (vp.getExtent() != null) {
|
116 |
Point2D p = vp.toMapPoint(event.getTo());
|
117 |
Rectangle2D r = (Rectangle2D) vpView.getExtent().clone();
|
118 |
r.setRect(p.getX() - (r.getWidth() / 2),
|
119 |
p.getY() - (r.getHeight() / 2), r.getWidth(), r.getHeight());
|
120 |
//vpView.setExtent(r);
|
121 |
double scale;
|
122 |
double escalaX;
|
123 |
double escalaY;
|
124 |
double newHeight;
|
125 |
double newWidth;
|
126 |
double xCenter = r.getCenterX();
|
127 |
double yCenter = r.getCenterY();
|
128 |
|
129 |
escalaX = mapControl.getWidth() / r.getWidth();
|
130 |
escalaY = mapControl.getHeight() / r.getHeight();
|
131 |
Rectangle2D adjustedExtent = new Rectangle2D.Double();
|
132 |
|
133 |
if (escalaX < escalaY) {
|
134 |
scale = escalaX;
|
135 |
newHeight = mapControl.getHeight() / scale;
|
136 |
adjustedExtent.setRect(xCenter - (r.getWidth() / 2.0),
|
137 |
yCenter - (newHeight / 2.0), r.getWidth(), newHeight);
|
138 |
} else {
|
139 |
scale = escalaY;
|
140 |
newWidth = mapControl.getWidth() / scale;
|
141 |
adjustedExtent.setRect(xCenter - (newWidth / 2.0),
|
142 |
yCenter - (r.getHeight() / 2.0), newWidth,
|
143 |
r.getHeight());
|
144 |
}
|
145 |
mov.refreshOverView(adjustedExtent);
|
146 |
}
|
147 |
|
148 |
}
|
149 |
*/
|
150 |
|
151 |
/*
|
152 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PanListener#move(MoveEvent)
|
153 |
*/
|
154 |
public void move(MoveEvent event) throws BehaviorException { |
155 |
|
156 |
|
157 |
if (!checkModifiers(event.getEvent())){
|
158 |
return;
|
159 |
} |
160 |
System.out.println("mapOvervierPan"); |
161 |
MapOverview mov=(MapOverview) this.mapControl;
|
162 |
ViewPort vp = mov.getViewPort(); |
163 |
ViewPort vpView=mov.getAssociatedMapContext().getViewPort(); |
164 |
|
165 |
if (vp.getExtent() != null && vpView.getExtent() != null) { |
166 |
|
167 |
// recogemos la forma de la vista actual
|
168 |
Envelope curEnvelope = vpView.getAdjustedExtent(); |
169 |
// traducimos las coordenadas en px de la ultima posicion del raton
|
170 |
// a coordenadas de la vista
|
171 |
Point2D thePoint= vp.toMapPoint( event.getTo());
|
172 |
|
173 |
double diffx =
|
174 |
(curEnvelope.getMaximum(0) - curEnvelope.getMinimum(0)) / 2; |
175 |
double diffy =
|
176 |
(curEnvelope.getMaximum(1) - curEnvelope.getMinimum(1)) / 2; |
177 |
// Creamos un envelope del mismo tama�o
|
178 |
// con la coordenadas del punto del raton,
|
179 |
// teniendo en cuenta que estas sera el
|
180 |
// centro del recuadro
|
181 |
|
182 |
Envelope envelope; |
183 |
try {
|
184 |
envelope = geomManager.createEnvelope( |
185 |
thePoint.getX() - diffx, |
186 |
thePoint.getY() - diffy, |
187 |
thePoint.getX() + diffx, |
188 |
thePoint.getY() + diffy, |
189 |
SUBTYPES.GEOM2D |
190 |
); |
191 |
// cambiamos la posicion
|
192 |
mov.refreshOverView(envelope); |
193 |
vpView.setEnvelope(envelope); |
194 |
} catch (CreateEnvelopeException e) {
|
195 |
logger.error("Error creating the envelope", e);
|
196 |
} |
197 |
mov.getAssociatedMapContext().invalidate(); |
198 |
} |
199 |
} |
200 |
|
201 |
/**
|
202 |
* Determines if has pressed the button 3 of the mouse.
|
203 |
*/
|
204 |
private boolean checkModifiers(MouseEvent event) { |
205 |
|
206 |
int modifiers = event.getModifiers();
|
207 |
/*
|
208 |
int keyPressedMask = InputEvent.BUTTON2_MASK;
|
209 |
|
210 |
*** No se porque el boton derecho del raton devuelve
|
211 |
*** un modificador 'Meta + BUTTON3'. Pensaba que deberia
|
212 |
*** devolver 'BUTTON2' ????
|
213 |
*/
|
214 |
int keyPressedMask = InputEvent.BUTTON3_MASK; |
215 |
return ((modifiers & keyPressedMask) == keyPressedMask);
|
216 |
} |
217 |
} |