svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / tools / Behavior / GeoMoveBehavior.java @ 4621
History | View | Annotate | Download (12.9 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.tools.Behavior; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Component; |
45 |
import java.awt.Cursor; |
46 |
import java.awt.Graphics; |
47 |
import java.awt.Image; |
48 |
import java.awt.Point; |
49 |
import java.awt.Rectangle; |
50 |
import java.awt.Toolkit; |
51 |
import java.awt.event.MouseEvent; |
52 |
import java.awt.event.MouseWheelEvent; |
53 |
import java.awt.geom.Point2D; |
54 |
import java.awt.image.BufferedImage; |
55 |
|
56 |
import javax.swing.ImageIcon; |
57 |
import javax.swing.JOptionPane; |
58 |
|
59 |
import org.cresques.px.Extent; |
60 |
|
61 |
import com.iver.andami.PluginServices; |
62 |
import com.iver.cit.gvsig.fmap.MapControl; |
63 |
import com.iver.cit.gvsig.fmap.ViewPort; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
65 |
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster; |
66 |
import com.iver.cit.gvsig.fmap.layers.FLyrPoints.GeoPoint; |
67 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
68 |
import com.iver.cit.gvsig.fmap.tools.Listeners.PanListener; |
69 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
70 |
import com.iver.cit.gvsig.gui.View; |
71 |
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog; |
72 |
|
73 |
|
74 |
/**
|
75 |
* Behaviour que espera un listener de tipo GeoMoveListener.
|
76 |
* Nacho Brodin (brodin_ign@gva.es)
|
77 |
*
|
78 |
*/
|
79 |
public class GeoMoveBehavior extends Behavior { |
80 |
|
81 |
private final Image defaultCursor = new ImageIcon(MapControl.class.getResource( |
82 |
"images/CruxCursor.png")).getImage();
|
83 |
private final Image handCursor = new ImageIcon(MapControl.class.getResource( |
84 |
"images/Hand.gif")).getImage();
|
85 |
|
86 |
private Cursor cur = null; |
87 |
private FLyrGeoRaster lyrGeoRaster = null; |
88 |
private boolean isMoveable = false; |
89 |
|
90 |
/**
|
91 |
* Variable booleana que est? a true si el cursor por defecto est?
|
92 |
* activo y a false si hay otro.
|
93 |
*/
|
94 |
private boolean defaultCursorActive = true; |
95 |
|
96 |
/**
|
97 |
* Puntos de inicio y final para el arrastre de la imagen.
|
98 |
*/
|
99 |
private Point2D ptoIni = null; |
100 |
private Point2D ptoFin = null; |
101 |
private com.iver.cit.gvsig.gui.View theView = null; |
102 |
private GeoreferencingDialog parent = null; |
103 |
private PanListener listener;
|
104 |
|
105 |
/**
|
106 |
* Crea un nuevo RectangleBehavior.
|
107 |
*
|
108 |
* @param zili listener.
|
109 |
*/
|
110 |
public GeoMoveBehavior(PanListener zili, GeoreferencingDialog p) {
|
111 |
listener = zili; |
112 |
parent = p; |
113 |
cur = zili.getCursor(); |
114 |
} |
115 |
|
116 |
/**
|
117 |
* Funci?n que carga la capa si todav?a no lo est?.
|
118 |
*/
|
119 |
private void loadLayer() throws InstantiationException, ClassCastException{ |
120 |
//Cargamos la capa
|
121 |
|
122 |
theView = (com.iver.cit.gvsig.gui.View) PluginServices.getMDIManager().getActiveView(); |
123 |
for(int i=0;i<theView.getMapControl().getMapContext().getLayers().getLayersCount();i++){ |
124 |
FLayer lyr = theView.getMapControl().getMapContext().getLayers().getLayer(i); |
125 |
if( lyr instanceof FLyrGeoRaster && |
126 |
lyr.getName().startsWith("*") &&
|
127 |
lyr.isActive()) |
128 |
this.lyrGeoRaster = (FLyrGeoRaster)lyr;
|
129 |
} |
130 |
if(lyrGeoRaster == null) |
131 |
throw new InstantiationException("No se ha podido cargar la capa de georreferenciaci?n"); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la
|
136 |
* imagen.
|
137 |
*/
|
138 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
139 |
|
140 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
141 |
|
142 |
try{
|
143 |
loadLayer(); |
144 |
|
145 |
Point2D pto = vp.toMapPoint(e.getX(), e.getY());
|
146 |
|
147 |
if( pto.getX() > lyrGeoRaster.getAssignExtent().getMin().getX() &&
|
148 |
pto.getX() < lyrGeoRaster.getAssignExtent().getMax().getX() && |
149 |
pto.getY() > lyrGeoRaster.getAssignExtent().getMin().getY() && |
150 |
pto.getY() < lyrGeoRaster.getAssignExtent().getMax().getY()){ |
151 |
if(defaultCursorActive){
|
152 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
|
153 |
new Point(16, 16), "")); |
154 |
defaultCursorActive = false;
|
155 |
} |
156 |
}else{
|
157 |
if(!defaultCursorActive){
|
158 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(defaultCursor,
|
159 |
new Point(16, 16), "")); |
160 |
defaultCursorActive = true;
|
161 |
} |
162 |
} |
163 |
}catch(InstantiationException exc){ |
164 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
165 |
PluginServices.getText(this, "error_capa_puntos")); |
166 |
}catch(ClassCastException exc){ |
167 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
168 |
PluginServices.getText(this, "error_capa_puntos")); |
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Obtiene el cursor actual
|
175 |
*/
|
176 |
public Cursor getCursor() { |
177 |
return this.cur; |
178 |
} |
179 |
|
180 |
/**
|
181 |
* Si no est? activo el cursor por defecto capturamos el punto
|
182 |
* seleccionado en coordenadas del mundo real.
|
183 |
*/
|
184 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
185 |
if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive){ |
186 |
|
187 |
try{
|
188 |
loadLayer(); |
189 |
}catch(InstantiationException exc){ |
190 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
191 |
PluginServices.getText(this, "error_capa_puntos")); |
192 |
return;
|
193 |
}catch(ClassCastException exc){ |
194 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
195 |
PluginServices.getText(this, "error_capa_puntos")); |
196 |
return;
|
197 |
} |
198 |
|
199 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
200 |
ptoIni = vp.toMapPoint(e.getPoint()); |
201 |
|
202 |
//Cargamos la imagen de arrastre
|
203 |
/* Point2D ul = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMin().getX(),
|
204 |
lyrGeoRaster.getAssignExtent().getMin().getY());
|
205 |
Point2D lr = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMax().getX(),
|
206 |
lyrGeoRaster.getAssignExtent().getMax().getY());
|
207 |
|
208 |
double ulx = 0D, uly = 0D, lrx = 0D, lry = 0D;
|
209 |
if(ul.getX() > vp.getExtent().getMinX())
|
210 |
ulx = ul.getX();
|
211 |
else
|
212 |
ulx = vp.getExtent().getMinX();
|
213 |
|
214 |
if(ul.getY() > vp.getExtent().getMinY())
|
215 |
uly = ul.getY();
|
216 |
else
|
217 |
uly = vp.getExtent().getMinY();
|
218 |
|
219 |
if(lr.getX() < vp.getExtent().getMaxX())
|
220 |
lrx = lr.getX();
|
221 |
else
|
222 |
lrx = vp.getExtent().getMaxX();
|
223 |
|
224 |
if(lr.getY() < vp.getExtent().getMaxY())
|
225 |
lry = lr.getY();
|
226 |
else
|
227 |
lry = vp.getExtent().getMaxY();
|
228 |
|
229 |
BufferedImage img = getMapControl().getImage();
|
230 |
System.out.println("-->"+img.getMinX()+" "+img.getMinY()+" "+img.getWidth()+" "+img.getHeight());
|
231 |
Rectangle r = new Rectangle((int)ulx, (int)uly, (int)Math.abs(lrx-ulx), (int)Math.abs(lry-uly));
|
232 |
System.out.println("--->"+ulx+" "+uly+" "+lrx+" "+lry);
|
233 |
Raster raster = img.getData(r);
|
234 |
miniGeoRaster.setData(raster);*/
|
235 |
/*miniGeoRaster = new BufferedImage((int)Math.abs(lrx-ulx), (int)Math.abs(lry-uly), BufferedImage.TYPE_INT_ARGB);
|
236 |
int[] rasterData = new int[r.width * r.height * 100];
|
237 |
rasterData = img.getRGB(r.x, r.y, r.width, r.height, rasterData, 0, r.width);
|
238 |
miniGeoRaster.setRGB(0, 0, r.width, r.height, rasterData, 0, r.width);*/
|
239 |
|
240 |
|
241 |
isMoveable = true;
|
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
|
247 |
* de destino calculando el extent nuevamente.
|
248 |
*/
|
249 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
250 |
if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){ |
251 |
|
252 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
253 |
ptoFin = vp.toMapPoint(e.getPoint()); |
254 |
double distX = ptoFin.getX() - ptoIni.getX();
|
255 |
double distY = ptoFin.getY() - ptoIni.getY();
|
256 |
if(this.lyrGeoRaster != null){ |
257 |
Extent ext = new Extent(lyrGeoRaster.getAssignExtent().getMin().getX() + distX,
|
258 |
lyrGeoRaster.getAssignExtent().getMin().getY() + distY, |
259 |
lyrGeoRaster.getAssignExtent().getMax().getX() + distX, |
260 |
lyrGeoRaster.getAssignExtent().getMax().getY() + distY); |
261 |
|
262 |
//Asignamos el nuevo extent a la capa
|
263 |
((FLyrGeoRaster)getMapControl().getMapContext().getLayers().getLayer(lyrGeoRaster.getName())).setAssignExtent(ext); |
264 |
|
265 |
//Si la capa tiene puntos de control actualizamos el punto que tiene coordenadas
|
266 |
//en pixeles de la imagen.
|
267 |
for(int i=0; i<lyrGeoRaster.getFLyrPoints().getCountPoints(); i++){ |
268 |
|
269 |
Point2D center = lyrGeoRaster.img2World(lyrGeoRaster.getFLyrPoints().getPoint(i).pixelPoint);
|
270 |
lyrGeoRaster.getFLyrPoints().setMiniExtent( i, |
271 |
center, |
272 |
lyrGeoRaster.getGeoDialog().getZoomControlLeft().getCanvas().initViewPort(vp, center, lyrGeoRaster.getFLyrPoints().getPoint(i).leftViewPort), |
273 |
false);
|
274 |
} |
275 |
|
276 |
//Repintamos la vista y las mini im?genes
|
277 |
int selection = lyrGeoRaster.getGeoDialog().getSelectedPoint();
|
278 |
GeoPoint gp = lyrGeoRaster.getFLyrPoints().getPoint(selection); |
279 |
Point2D px = null; |
280 |
Point2D map = null; |
281 |
if(gp != null){ |
282 |
px = gp.pixelPoint; |
283 |
map = gp.mapPoint; |
284 |
} |
285 |
|
286 |
lyrGeoRaster.updateData(selection + 1, px, map, lyrGeoRaster.getGeoDialog(), theView);
|
287 |
if(px == null && map == null) |
288 |
theView.getMapControl().getMapContext().invalidate(); |
289 |
|
290 |
} |
291 |
isMoveable = false;
|
292 |
//getMapControl().repaint();
|
293 |
//getMapControl().drawMap(true);
|
294 |
} |
295 |
this.lyrGeoRaster = null; |
296 |
} |
297 |
|
298 |
/**
|
299 |
* Funci?n de pintado del canvas.Pintamos un marco a la imagen para saber
|
300 |
* donde la movemos.
|
301 |
*/
|
302 |
public void paintComponent(Graphics g) { |
303 |
super.paintComponent(g);
|
304 |
if(isMoveable){
|
305 |
BufferedImage img = getMapControl().getImage();
|
306 |
|
307 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
308 |
|
309 |
Rectangle r = new Rectangle(); |
310 |
|
311 |
Point2D ul = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMin().getX(),
|
312 |
lyrGeoRaster.getAssignExtent().getMin().getY()); |
313 |
Point2D lr = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMax().getX(),
|
314 |
lyrGeoRaster.getAssignExtent().getMax().getY()); |
315 |
r.setFrameFromDiagonal(ul, lr); |
316 |
|
317 |
Point2D ini = vp.fromMapPoint(ptoIni.getX() , ptoIni.getY());
|
318 |
Point2D fin = vp.fromMapPoint(ptoFin.getX() , ptoFin.getY());
|
319 |
|
320 |
double distX = fin.getX() - ini.getX();
|
321 |
double distY = fin.getY() - ini.getY();
|
322 |
|
323 |
g.drawImage(img, 0, 0, null); |
324 |
g.setColor(Color.red);
|
325 |
|
326 |
//g.drawImage(miniGeoRaster, r.x + (int)distX, r.y + (int)distY, null);
|
327 |
g.drawRect(r.x + (int)distX, r.y + (int)distY, r.width, r.height); |
328 |
} |
329 |
} |
330 |
|
331 |
/**
|
332 |
* Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
|
333 |
* poder ver el marco de arrastre.
|
334 |
*/
|
335 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
336 |
if(isMoveable){
|
337 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
338 |
ptoFin = vp.toMapPoint(e.getPoint()); |
339 |
getMapControl().repaint(); |
340 |
} |
341 |
} |
342 |
|
343 |
/* (non-Javadoc)
|
344 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
345 |
*/
|
346 |
public ToolListener getListener() {
|
347 |
// TODO Auto-generated method stub
|
348 |
return null; |
349 |
} |
350 |
/* (non-Javadoc)
|
351 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getMapControl()
|
352 |
*/
|
353 |
public MapControl getMapControl() {
|
354 |
// TODO Auto-generated method stub
|
355 |
return super.getMapControl(); |
356 |
} |
357 |
/* (non-Javadoc)
|
358 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
|
359 |
*/
|
360 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
361 |
// TODO Auto-generated method stub
|
362 |
super.mouseClicked(e);
|
363 |
} |
364 |
|
365 |
/* (non-Javadoc)
|
366 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
|
367 |
*/
|
368 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
369 |
// TODO Auto-generated method stub
|
370 |
super.mouseEntered(e);
|
371 |
} |
372 |
/* (non-Javadoc)
|
373 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
|
374 |
*/
|
375 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
376 |
// TODO Auto-generated method stub
|
377 |
super.mouseExited(e);
|
378 |
} |
379 |
|
380 |
/* (non-Javadoc)
|
381 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
382 |
*/
|
383 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
384 |
// TODO Auto-generated method stub
|
385 |
super.mouseWheelMoved(e);
|
386 |
} |
387 |
|
388 |
/* (non-Javadoc)
|
389 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
|
390 |
*/
|
391 |
public void setMapControl(MapControl mc) { |
392 |
// TODO Auto-generated method stub
|
393 |
super.setMapControl(mc);
|
394 |
} |
395 |
|
396 |
} |