root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / geolocation / behavior / GeoMoveBehavior.java @ 12569
History | View | Annotate | Download (10.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.rastertools.geolocation.behavior; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.awt.Component; |
23 |
import java.awt.Cursor; |
24 |
import java.awt.Graphics; |
25 |
import java.awt.Graphics2D; |
26 |
import java.awt.HeadlessException; |
27 |
import java.awt.Image; |
28 |
import java.awt.Point; |
29 |
import java.awt.Rectangle; |
30 |
import java.awt.Toolkit; |
31 |
import java.awt.event.MouseEvent; |
32 |
import java.awt.event.MouseWheelEvent; |
33 |
import java.awt.geom.AffineTransform; |
34 |
import java.awt.geom.NoninvertibleTransformException; |
35 |
import java.awt.geom.Point2D; |
36 |
import java.awt.image.BufferedImage; |
37 |
|
38 |
import javax.swing.ImageIcon; |
39 |
import javax.swing.JOptionPane; |
40 |
|
41 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
42 |
import org.gvsig.rastertools.RasterToolsUtil; |
43 |
|
44 |
import com.iver.andami.PluginServices; |
45 |
import com.iver.andami.ui.mdiManager.IWindow; |
46 |
import com.iver.cit.gvsig.fmap.MapControl; |
47 |
import com.iver.cit.gvsig.fmap.ViewPort; |
48 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
49 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
50 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
51 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PanListener; |
52 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
53 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
54 |
|
55 |
|
56 |
/**
|
57 |
* Behaviour que espera un listener de tipo GeoMoveListener.
|
58 |
* Nacho Brodin (nachobrodin@gmail.com)
|
59 |
*
|
60 |
*/
|
61 |
public class GeoMoveBehavior extends Behavior { |
62 |
|
63 |
private final Image defaultCursor = new ImageIcon(MapControl.class.getResource( |
64 |
"images/CruxCursor.png")).getImage();
|
65 |
private final Image handCursor = new ImageIcon(MapControl.class.getResource( |
66 |
"images/Hand.gif")).getImage();
|
67 |
|
68 |
private Cursor cur = null; |
69 |
private boolean isMoveable = false; |
70 |
|
71 |
/**
|
72 |
* Variable booleana que est? a true si el cursor por defecto est?
|
73 |
* activo y a false si hay otro.
|
74 |
*/
|
75 |
private boolean defaultCursorActive = true; |
76 |
|
77 |
/**
|
78 |
* Puntos de inicio y final para el arrastre de la imagen.
|
79 |
*/
|
80 |
private Point2D ptoIni = null; |
81 |
private Point2D ptoFin = null; |
82 |
private IView theView = null; |
83 |
private PanListener listener;
|
84 |
|
85 |
/**
|
86 |
* Crea un nuevo RectangleBehavior.
|
87 |
*
|
88 |
* @param zili listener.
|
89 |
*/
|
90 |
public GeoMoveBehavior(PanListener zili) {
|
91 |
listener = zili; |
92 |
cur = zili.getCursor(); |
93 |
} |
94 |
|
95 |
/**
|
96 |
* Funci?n que carga la capa si todav?a no lo est?.
|
97 |
*/
|
98 |
private FLyrRasterSE getLayer() throws InstantiationException, ClassCastException{ |
99 |
//Cargamos la capa
|
100 |
|
101 |
IWindow w = PluginServices.getMDIManager().getActiveWindow(); |
102 |
if(w instanceof IView) { |
103 |
theView = (IView)w; |
104 |
FLayers lyrs = theView.getMapControl().getMapContext().getLayers(); |
105 |
for (int i = 0; i < lyrs.getActives().length; i++) { |
106 |
if(lyrs.getActives()[i] instanceof FLyrRasterSE) |
107 |
return (FLyrRasterSE)lyrs.getActives()[i];
|
108 |
} |
109 |
} |
110 |
return null; |
111 |
} |
112 |
|
113 |
/**
|
114 |
* Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la
|
115 |
* imagen.
|
116 |
*/
|
117 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
118 |
|
119 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
120 |
|
121 |
try{
|
122 |
FLyrRasterSE lyr = getLayer(); |
123 |
if(lyr == null) |
124 |
return;
|
125 |
|
126 |
Point2D pto = vp.toMapPoint(e.getX(), e.getY());
|
127 |
|
128 |
try {
|
129 |
if( lyr.isInside(pto)){
|
130 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
|
131 |
new Point(16, 16), "")); |
132 |
defaultCursorActive = false;
|
133 |
}else{
|
134 |
if(!defaultCursorActive){
|
135 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(defaultCursor,
|
136 |
new Point(16, 16), "")); |
137 |
defaultCursorActive = true;
|
138 |
} |
139 |
} |
140 |
} catch (HeadlessException e1) { |
141 |
e1.printStackTrace(); |
142 |
} catch (IndexOutOfBoundsException e1) { |
143 |
e1.printStackTrace(); |
144 |
} |
145 |
}catch(InstantiationException exc){ |
146 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
147 |
PluginServices.getText(this, "error_capa_puntos")); |
148 |
}catch(ClassCastException exc){ |
149 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
150 |
PluginServices.getText(this, "error_capa_puntos")); |
151 |
} |
152 |
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Obtiene el cursor actual
|
157 |
*/
|
158 |
public Cursor getCursor() { |
159 |
return this.cur; |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Si no est? activo el cursor por defecto capturamos el punto
|
164 |
* seleccionado en coordenadas del mundo real.
|
165 |
*/
|
166 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
167 |
if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive){ |
168 |
|
169 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
170 |
ptoIni = vp.toMapPoint(e.getPoint()); |
171 |
|
172 |
isMoveable = true;
|
173 |
} |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
|
178 |
* de destino calculando el extent nuevamente.
|
179 |
*/
|
180 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
181 |
if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){ |
182 |
|
183 |
FLyrRasterSE lyr = null;
|
184 |
try {
|
185 |
lyr = getLayer(); |
186 |
}catch(InstantiationException exc){ |
187 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_carga_capa"), (Component)PluginServices.getMainFrame()); |
188 |
} |
189 |
if(lyr == null) |
190 |
return;
|
191 |
|
192 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
193 |
ptoFin = vp.toMapPoint(e.getPoint()); |
194 |
|
195 |
//Asignamos la nueva matriz de transformaci?n a la capa de georreferenciaci?n
|
196 |
AffineTransform atOld = lyr.getAffineTransform();
|
197 |
AffineTransform atNew = null; |
198 |
|
199 |
double distX = ptoFin.getX() - ptoIni.getX();
|
200 |
double distY = ptoFin.getY() - ptoIni.getY();
|
201 |
|
202 |
atNew = new AffineTransform(atOld.getScaleX(), atOld.getShearY(), |
203 |
atOld.getShearX(), atOld.getScaleY(), |
204 |
atOld.getTranslateX() + distX, |
205 |
atOld.getTranslateY() + distY); |
206 |
lyr.setAffineTransform(atNew); |
207 |
|
208 |
theView.getMapControl().getMapContext().invalidate(); |
209 |
|
210 |
isMoveable = false;
|
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Funci?n de pintado del canvas.Pintamos un marco a la imagen para saber
|
217 |
* donde la movemos.
|
218 |
*/
|
219 |
public void paintComponent(Graphics g) { |
220 |
super.paintComponent(g);
|
221 |
/*if(isMoveable){
|
222 |
BufferedImage img = getMapControl().getImage();
|
223 |
|
224 |
ViewPort vp = getMapControl().getMapContext().getViewPort();
|
225 |
|
226 |
//Obtenemos las esquinas del rectangulo sin rotar
|
227 |
Rectangle r = new Rectangle();
|
228 |
Point2D ul = vp.fromMapPoint( lyrGeoRaster.getExtentWithoutRot().getMinX(),
|
229 |
lyrGeoRaster.getExtentWithoutRot().getMinY());
|
230 |
Point2D lr = vp.fromMapPoint( lyrGeoRaster.getExtentWithoutRot().getMaxX(),
|
231 |
lyrGeoRaster.getExtentWithoutRot().getMaxY());
|
232 |
r.setFrameFromDiagonal(ul, lr);
|
233 |
|
234 |
g.drawImage(img, 0, 0, null);
|
235 |
g.setColor(Color.red);
|
236 |
|
237 |
try {
|
238 |
AffineTransform at = new AffineTransform();
|
239 |
at.setToShear( lyrGeoRaster.getAffineTransform().getShearX() / lyrGeoRaster.getAffineTransform().getScaleX(),
|
240 |
lyrGeoRaster.getAffineTransform().getShearY() / lyrGeoRaster.getAffineTransform().getScaleY());
|
241 |
|
242 |
//Calculamos el punto inicial y final del desplazamiento. Hay que desrrotar estos para lograr un desplazamiento
|
243 |
//homogeneo independientemente de en que lado de la imagen hallamos pinchado. Si no hacemos esto el puntero del
|
244 |
//rat?n se mover? a distinta velocidad de la imagen y esta variar? dependiendo de en que parte se haya pinchado.
|
245 |
|
246 |
Point2D ini = vp.fromMapPoint(ptoIni.getX() , ptoIni.getY());
|
247 |
Point2D fin = vp.fromMapPoint(ptoFin.getX() , ptoFin.getY());
|
248 |
at.inverseTransform(ini, ini);
|
249 |
at.inverseTransform(fin, fin);
|
250 |
double distX = fin.getX() - ini.getX();
|
251 |
double distY = fin.getY() - ini.getY();
|
252 |
|
253 |
//Giramos el graphics se dibuja y se vuelve a dejar como estaba
|
254 |
((Graphics2D)g).transform(at);
|
255 |
Point2D pt = new Point2D.Double(r.x, r.y);
|
256 |
at.inverseTransform(pt, pt);
|
257 |
g.drawRect((int)pt.getX() + (int)distX, (int)pt.getY() + (int)distY, r.width, r.height);
|
258 |
((Graphics2D)g).transform(at.createInverse());
|
259 |
} catch (NoninvertibleTransformException e1) {
|
260 |
e1.printStackTrace();
|
261 |
}
|
262 |
}*/
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
|
267 |
* poder ver el marco de arrastre.
|
268 |
*/
|
269 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
270 |
if(isMoveable){
|
271 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
272 |
ptoFin = vp.toMapPoint(e.getPoint()); |
273 |
getMapControl().repaint(); |
274 |
} |
275 |
} |
276 |
|
277 |
/* (non-Javadoc)
|
278 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
279 |
*/
|
280 |
public ToolListener getListener() {
|
281 |
return null; |
282 |
} |
283 |
/* (non-Javadoc)
|
284 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getMapControl()
|
285 |
*/
|
286 |
public MapControl getMapControl() {
|
287 |
return super.getMapControl(); |
288 |
} |
289 |
/* (non-Javadoc)
|
290 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
|
291 |
*/
|
292 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
293 |
super.mouseClicked(e);
|
294 |
} |
295 |
|
296 |
/* (non-Javadoc)
|
297 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
|
298 |
*/
|
299 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
300 |
super.mouseEntered(e);
|
301 |
} |
302 |
/* (non-Javadoc)
|
303 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
|
304 |
*/
|
305 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
306 |
super.mouseExited(e);
|
307 |
} |
308 |
|
309 |
/* (non-Javadoc)
|
310 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
311 |
*/
|
312 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
313 |
super.mouseWheelMoved(e);
|
314 |
} |
315 |
|
316 |
/* (non-Javadoc)
|
317 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
|
318 |
*/
|
319 |
public void setMapControl(MapControl mc) { |
320 |
super.setMapControl(mc);
|
321 |
} |
322 |
|
323 |
} |