svn-gvsig-desktop / tags / v2_0_0_Build_2060 / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / MapOverviewChangeZoomListener.java @ 39341
History | View | Annotate | Download (5.8 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.app.project.documents.view.toolListeners; |
5 |
|
6 |
import java.awt.Image; |
7 |
import java.awt.event.InputEvent; |
8 |
import java.awt.event.MouseEvent; |
9 |
import java.awt.geom.Point2D; |
10 |
import java.awt.geom.Rectangle2D; |
11 |
|
12 |
import org.gvsig.andami.PluginServices; |
13 |
import org.gvsig.app.gui.styling.SymbolSelector; |
14 |
import org.gvsig.app.project.documents.view.MapOverview; |
15 |
import org.gvsig.fmap.geom.GeometryLocator; |
16 |
import org.gvsig.fmap.geom.GeometryManager; |
17 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
18 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
19 |
import org.gvsig.fmap.geom.primitive.Envelope; |
20 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
21 |
import org.gvsig.fmap.mapcontext.ViewPort; |
22 |
import org.gvsig.fmap.mapcontrol.MapControl; |
23 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
24 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
25 |
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent; |
26 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener; |
27 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
28 |
import org.slf4j.Logger; |
29 |
import org.slf4j.LoggerFactory; |
30 |
|
31 |
|
32 |
/**
|
33 |
* <p>Listener for changes of the zoom caused by selecting a rectangular area on the associated
|
34 |
* {@link MapOverview MapOverview} object with the first button of the mouse.</p>
|
35 |
*
|
36 |
* <p>If the kind of action was a movement on the associated object,
|
37 |
* updates the <i>extent</i> of its rectangular area.</p>
|
38 |
*
|
39 |
* <p>If the kind of action is the selection of a rectangular area, and is bigger than 3x3 pixels,
|
40 |
* applies a <i>zoom in</i> operation centering its <code>ViewPort</code> according the equivalent <i>extent</i>
|
41 |
* in map coordinates.</p>
|
42 |
*
|
43 |
* @see ViewPort
|
44 |
*
|
45 |
* @author jmvivo
|
46 |
*/
|
47 |
public class MapOverviewChangeZoomListener implements RectangleListener, PanListener { |
48 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
49 |
private static final Logger logger = LoggerFactory.getLogger(MapOverviewChangeZoomListener.class); |
50 |
/**
|
51 |
* The image to display when the cursor is active.
|
52 |
*/
|
53 |
private final Image izoomin = PluginServices.getIconTheme() |
54 |
.get("cursor-zoom-in").getImage();
|
55 |
|
56 |
/**
|
57 |
* The cursor used to work with this tool listener.
|
58 |
*
|
59 |
* @see #getCursor()
|
60 |
*/
|
61 |
// private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(izoomin,new Point(16, 16), "");
|
62 |
|
63 |
/**
|
64 |
* Reference to the <code>MapControl</code> object that uses.
|
65 |
*/
|
66 |
protected MapControl mapControl;
|
67 |
|
68 |
/**
|
69 |
* <p>Creates a new listener for changes of zoom at the associated {@link MapOverview MapOverview} object.</p>
|
70 |
*
|
71 |
* @param mapControl the <code>MapControl</code> object which represents the <code>MapOverview</code>
|
72 |
*/
|
73 |
public MapOverviewChangeZoomListener(MapControl mapControl) {
|
74 |
this.mapControl=mapControl;
|
75 |
} |
76 |
|
77 |
/*
|
78 |
* (non-Javadoc)
|
79 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
|
80 |
*/
|
81 |
public void rectangle(EnvelopeEvent event) throws BehaviorException { |
82 |
|
83 |
if (!checkModifiers(event.getEvent())){
|
84 |
return;
|
85 |
} |
86 |
MapOverview mov=(MapOverview) this.mapControl;
|
87 |
ViewPort vpView=mov.getAssociatedMapContext().getViewPort(); |
88 |
ViewPort vp = mov.getViewPort(); |
89 |
|
90 |
if (vp.getExtent() != null && vpView.getExtent() != null) { |
91 |
|
92 |
// Recuperamos las coordenadas del evento en px
|
93 |
Rectangle2D pxRectangle = event.getPixelCoordRect();
|
94 |
// Recuperamos las coordenadas del evento en coordenadas de la vista de localizador
|
95 |
Envelope realRectangle = event.getWorldCoordRect(); |
96 |
|
97 |
if ((pxRectangle.getWidth() < 3) && (pxRectangle.getHeight() < 3)) |
98 |
{ |
99 |
// rectangulo < 3 px no hacemos nada
|
100 |
return;
|
101 |
|
102 |
} else {
|
103 |
// Cambiamos la extension de la vista asociada al localizador
|
104 |
vpView.setEnvelope(realRectangle); |
105 |
mov.getAssociatedMapContext().invalidate(); |
106 |
vpView.setEnvelope(realRectangle); |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
/*
|
112 |
* (non-Javadoc)
|
113 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
114 |
*/
|
115 |
public Image getImageCursor() { |
116 |
return izoomin;
|
117 |
} |
118 |
|
119 |
/* (non-Javadoc)
|
120 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
121 |
*/
|
122 |
public boolean cancelDrawing() { |
123 |
return true; //??? |
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.PanListener#move(com.iver.cit.gvsig.fmap.tools.Events.MoveEvent)
|
129 |
*/
|
130 |
public void move(MoveEvent event) throws BehaviorException { |
131 |
|
132 |
|
133 |
if (!checkModifiers(event.getEvent())){
|
134 |
return;
|
135 |
} |
136 |
// System.out.println("mapOvervierChangeZoom");
|
137 |
MapOverview mov=(MapOverview) this.mapControl;
|
138 |
ViewPort vpView=mov.getAssociatedMapContext().getViewPort(); |
139 |
ViewPort vp = mov.getViewPort(); |
140 |
|
141 |
if (vp.getExtent() != null && vpView.getExtent() != null) { |
142 |
|
143 |
// Creamos un recuadro con las coordenadas del raton
|
144 |
// traducidas a la del mapa
|
145 |
// Rectangle2D realRectangle = new Rectangle2D.Double();
|
146 |
Point2D p1=vp.toMapPoint(event.getFrom());
|
147 |
Point2D p2=vp.toMapPoint(event.getTo());
|
148 |
Envelope realRectangle = null;
|
149 |
double x=0; |
150 |
double y=0; |
151 |
double xMax=0; |
152 |
double yMax=0; |
153 |
if (p1.getX()<p2.getX()){
|
154 |
x=p1.getX(); |
155 |
xMax=p2.getX(); |
156 |
}else{
|
157 |
x=p2.getX(); |
158 |
xMax=p1.getX(); |
159 |
} |
160 |
if (p1.getY()<p2.getY()){
|
161 |
y=p1.getY(); |
162 |
yMax=p2.getY(); |
163 |
}else{
|
164 |
y=p2.getY(); |
165 |
yMax=p1.getY(); |
166 |
} |
167 |
try {
|
168 |
realRectangle = geomManager.createEnvelope(x,y,xMax,yMax, SUBTYPES.GEOM2D); |
169 |
} catch (CreateEnvelopeException e) {
|
170 |
logger.error("Error creating the envelope", e);
|
171 |
} |
172 |
|
173 |
// realRectangle.setFrameFromDiagonal(vp.toMapPoint(event.getFrom()),vp.toMapPoint(event.getTo()));
|
174 |
|
175 |
// Establecemos la forma
|
176 |
mov.refreshOverView(realRectangle); |
177 |
} |
178 |
|
179 |
|
180 |
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Determines if has pressed the button 1 of the mouse.
|
185 |
*/
|
186 |
private boolean checkModifiers(MouseEvent event) { |
187 |
int modifiers = event.getModifiers();
|
188 |
int keyPressedMask = InputEvent.BUTTON1_MASK; |
189 |
return ((modifiers & keyPressedMask) == keyPressedMask);
|
190 |
} |
191 |
} |