root / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / tools / Behavior / GeoMoveBehavior.java @ 2987
History | View | Annotate | Download (10.8 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.Cursor; |
45 |
import java.awt.Graphics; |
46 |
import java.awt.Image; |
47 |
import java.awt.Point; |
48 |
import java.awt.Rectangle; |
49 |
import java.awt.Toolkit; |
50 |
import java.awt.event.MouseEvent; |
51 |
import java.awt.event.MouseWheelEvent; |
52 |
import java.awt.geom.Point2D; |
53 |
import java.awt.image.BufferedImage; |
54 |
import java.awt.image.Raster; |
55 |
|
56 |
import javax.swing.ImageIcon; |
57 |
|
58 |
import org.cresques.px.Extent; |
59 |
|
60 |
import com.iver.andami.PluginServices; |
61 |
import com.iver.cit.gvsig.fmap.MapControl; |
62 |
import com.iver.cit.gvsig.fmap.ViewPort; |
63 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
64 |
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster; |
65 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
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(){ |
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 && lyr.getName().startsWith("*")) |
127 |
this.lyrGeoRaster = (FLyrGeoRaster)lyr;
|
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la
|
133 |
* imagen.
|
134 |
*/
|
135 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
136 |
|
137 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
138 |
|
139 |
loadLayer(); |
140 |
|
141 |
Point2D pto = vp.toMapPoint(e.getX(), e.getY());
|
142 |
|
143 |
if( pto.getX() > lyrGeoRaster.getAssignExtent().getMin().getX() &&
|
144 |
pto.getX() < lyrGeoRaster.getAssignExtent().getMax().getX() && |
145 |
pto.getY() > lyrGeoRaster.getAssignExtent().getMin().getY() && |
146 |
pto.getY() < lyrGeoRaster.getAssignExtent().getMax().getY()){ |
147 |
if(defaultCursorActive){
|
148 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
|
149 |
new Point(16, 16), "")); |
150 |
defaultCursorActive = false;
|
151 |
} |
152 |
}else{
|
153 |
if(!defaultCursorActive){
|
154 |
getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(defaultCursor,
|
155 |
new Point(16, 16), "")); |
156 |
defaultCursorActive = true;
|
157 |
} |
158 |
} |
159 |
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* Obtiene el cursor actual
|
164 |
*/
|
165 |
public Cursor getCursor() { |
166 |
return this.cur; |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Si no est? activo el cursor por defecto capturamos el punto
|
171 |
* seleccionado en coordenadas del mundo real.
|
172 |
*/
|
173 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
174 |
if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive){ |
175 |
|
176 |
loadLayer(); |
177 |
|
178 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
179 |
ptoIni = vp.toMapPoint(e.getPoint()); |
180 |
|
181 |
//Cargamos la imagen de arrastre
|
182 |
/* Point2D ul = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMin().getX(),
|
183 |
lyrGeoRaster.getAssignExtent().getMin().getY());
|
184 |
Point2D lr = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMax().getX(),
|
185 |
lyrGeoRaster.getAssignExtent().getMax().getY());
|
186 |
|
187 |
double ulx = 0D, uly = 0D, lrx = 0D, lry = 0D;
|
188 |
if(ul.getX() > vp.getExtent().getMinX())
|
189 |
ulx = ul.getX();
|
190 |
else
|
191 |
ulx = vp.getExtent().getMinX();
|
192 |
|
193 |
if(ul.getY() > vp.getExtent().getMinY())
|
194 |
uly = ul.getY();
|
195 |
else
|
196 |
uly = vp.getExtent().getMinY();
|
197 |
|
198 |
if(lr.getX() < vp.getExtent().getMaxX())
|
199 |
lrx = lr.getX();
|
200 |
else
|
201 |
lrx = vp.getExtent().getMaxX();
|
202 |
|
203 |
if(lr.getY() < vp.getExtent().getMaxY())
|
204 |
lry = lr.getY();
|
205 |
else
|
206 |
lry = vp.getExtent().getMaxY();
|
207 |
|
208 |
BufferedImage img = getMapControl().getImage();
|
209 |
System.out.println("-->"+img.getMinX()+" "+img.getMinY()+" "+img.getWidth()+" "+img.getHeight());
|
210 |
Rectangle r = new Rectangle((int)ulx, (int)uly, (int)Math.abs(lrx-ulx), (int)Math.abs(lry-uly));
|
211 |
System.out.println("--->"+ulx+" "+uly+" "+lrx+" "+lry);
|
212 |
Raster raster = img.getData(r);
|
213 |
miniGeoRaster.setData(raster);*/
|
214 |
/*miniGeoRaster = new BufferedImage((int)Math.abs(lrx-ulx), (int)Math.abs(lry-uly), BufferedImage.TYPE_INT_ARGB);
|
215 |
int[] rasterData = new int[r.width * r.height * 100];
|
216 |
rasterData = img.getRGB(r.x, r.y, r.width, r.height, rasterData, 0, r.width);
|
217 |
miniGeoRaster.setRGB(0, 0, r.width, r.height, rasterData, 0, r.width);*/
|
218 |
|
219 |
|
220 |
isMoveable = true;
|
221 |
} |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
|
226 |
* de destino calculando el extent nuevamente.
|
227 |
*/
|
228 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
229 |
if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){ |
230 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
231 |
ptoFin = vp.toMapPoint(e.getPoint()); |
232 |
double distX = ptoFin.getX() - ptoIni.getX();
|
233 |
double distY = ptoFin.getY() - ptoIni.getY();
|
234 |
Extent ext = new Extent(lyrGeoRaster.getAssignExtent().getMin().getX() + distX,
|
235 |
lyrGeoRaster.getAssignExtent().getMin().getY() + distY, |
236 |
lyrGeoRaster.getAssignExtent().getMax().getX() + distX, |
237 |
lyrGeoRaster.getAssignExtent().getMax().getY() + distY); |
238 |
if(this.lyrGeoRaster != null){ |
239 |
((FLyrGeoRaster)getMapControl().getMapContext().getLayers().getLayer(lyrGeoRaster.getName())).setAssignExtent(ext); |
240 |
getMapControl().getMapContext().invalidate(); |
241 |
} |
242 |
isMoveable = false;
|
243 |
getMapControl().repaint(); |
244 |
} |
245 |
this.lyrGeoRaster = null; |
246 |
} |
247 |
|
248 |
/**
|
249 |
* Funci?n de pintado del canvas.Pintamos un marco a la imagen para saber
|
250 |
* donde la movemos.
|
251 |
*/
|
252 |
public void paintComponent(Graphics g) { |
253 |
super.paintComponent(g);
|
254 |
if(isMoveable){
|
255 |
BufferedImage img = getMapControl().getImage();
|
256 |
|
257 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
258 |
|
259 |
Rectangle r = new Rectangle(); |
260 |
|
261 |
Point2D ul = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMin().getX(),
|
262 |
lyrGeoRaster.getAssignExtent().getMin().getY()); |
263 |
Point2D lr = vp.fromMapPoint(lyrGeoRaster.getAssignExtent().getMax().getX(),
|
264 |
lyrGeoRaster.getAssignExtent().getMax().getY()); |
265 |
r.setFrameFromDiagonal(ul, lr); |
266 |
|
267 |
Point2D ini = vp.fromMapPoint(ptoIni.getX() , ptoIni.getY());
|
268 |
Point2D fin = vp.fromMapPoint(ptoFin.getX() , ptoFin.getY());
|
269 |
|
270 |
double distX = fin.getX() - ini.getX();
|
271 |
double distY = fin.getY() - ini.getY();
|
272 |
|
273 |
g.drawImage(img, 0, 0, null); |
274 |
g.setColor(Color.red);
|
275 |
|
276 |
//g.drawImage(miniGeoRaster, r.x + (int)distX, r.y + (int)distY, null);
|
277 |
g.drawRect(r.x + (int)distX, r.y + (int)distY, r.width, r.height); |
278 |
} |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
|
283 |
* poder ver el marco de arrastre.
|
284 |
*/
|
285 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
286 |
if(isMoveable){
|
287 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
288 |
ptoFin = vp.toMapPoint(e.getPoint()); |
289 |
getMapControl().repaint(); |
290 |
} |
291 |
} |
292 |
|
293 |
/* (non-Javadoc)
|
294 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
295 |
*/
|
296 |
public ToolListener getListener() {
|
297 |
// TODO Auto-generated method stub
|
298 |
return null; |
299 |
} |
300 |
/* (non-Javadoc)
|
301 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getMapControl()
|
302 |
*/
|
303 |
public MapControl getMapControl() {
|
304 |
// TODO Auto-generated method stub
|
305 |
return super.getMapControl(); |
306 |
} |
307 |
/* (non-Javadoc)
|
308 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
|
309 |
*/
|
310 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
311 |
// TODO Auto-generated method stub
|
312 |
super.mouseClicked(e);
|
313 |
} |
314 |
|
315 |
/* (non-Javadoc)
|
316 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
|
317 |
*/
|
318 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
319 |
// TODO Auto-generated method stub
|
320 |
super.mouseEntered(e);
|
321 |
} |
322 |
/* (non-Javadoc)
|
323 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
|
324 |
*/
|
325 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
326 |
// TODO Auto-generated method stub
|
327 |
super.mouseExited(e);
|
328 |
} |
329 |
|
330 |
/* (non-Javadoc)
|
331 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
332 |
*/
|
333 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
334 |
// TODO Auto-generated method stub
|
335 |
super.mouseWheelMoved(e);
|
336 |
} |
337 |
|
338 |
/* (non-Javadoc)
|
339 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
|
340 |
*/
|
341 |
public void setMapControl(MapControl mc) { |
342 |
// TODO Auto-generated method stub
|
343 |
super.setMapControl(mc);
|
344 |
} |
345 |
|
346 |
} |