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