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