Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / fmap / tools / Behavior / GeoMoveBehavior.java @ 2924

History | View | Annotate | Download (8.67 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.io.File;
55

    
56
import javax.swing.ImageIcon;
57

    
58
import org.cresques.io.GeoRasterFile;
59
import org.cresques.px.Extent;
60

    
61
import com.hardcode.driverManager.DriverLoadException;
62
import com.iver.andami.messages.NotificationManager;
63
import com.iver.cit.gvsig.fmap.MapControl;
64
import com.iver.cit.gvsig.fmap.ViewPort;
65
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
66
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
67
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
68
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
69
import com.iver.cit.gvsig.fmap.layers.RasterFileAdapter;
70
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
71
import com.iver.cit.gvsig.fmap.tools.Listeners.PanListener;
72
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
73
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
74

    
75

    
76
/**
77
 * Behaviour que espera un listener de tipo GeoMoveListener.
78
 * Nacho Brodin (brodin_ign@gva.es)
79
 *
80
 */
81
public class GeoMoveBehavior extends Behavior {
82
        
83
        private PanListener listener;
84
        private Cursor cur = null;
85
                
86
        private final Image defaultCursor = new ImageIcon(MapControl.class.getResource(
87
        "images/CruxCursor.png")).getImage();
88
        private final Image handCursor = new ImageIcon(MapControl.class.getResource(
89
        "images/Hand.gif")).getImage();
90
                
91
        private FLyrRaster lyrRaster = null;
92
        private boolean isMoveable = false;
93
        
94
        /**
95
         * Variable booleana que est? a true si el cursor por defecto est? 
96
         * activo y a false si hay otro.
97
         */
98
        private boolean defaultCursorActive = true;
99
        
100
        /**
101
         * Puntos de inicio y final para el arrastre de la imagen.
102
         */
103
        private Point2D ptoIni = null;
104
        private Point2D ptoFin = null;
105
        
106
        private GeoreferencingDialog parent = null;
107
        /**
108
         * Crea un nuevo RectangleBehavior.
109
         *
110
         * @param zili listener.
111
         */
112
        public GeoMoveBehavior(PanListener zili, GeoreferencingDialog p) {
113
                listener = zili;
114
                parent = p;
115
                cur = zili.getCursor();
116
        }
117
        
118
        /**
119
         * Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la 
120
         * imagen.
121
         */
122
        public void mouseMoved(MouseEvent e) throws BehaviorException {
123
                ViewPort vp = getMapControl().getMapContext().getViewPort();
124
                Point2D pto = vp.toMapPoint(e.getX(), e.getY());
125
                
126
                if(        pto.getX() > lyrRaster.getMinX() &&
127
                        pto.getX() < lyrRaster.getMaxX() &&
128
                        pto.getY() > lyrRaster.getMinY() &&
129
                        pto.getY() < lyrRaster.getMaxY()){
130
                        if(defaultCursorActive){
131
                                getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
132
                                        new Point(16, 16), ""));
133
                                defaultCursorActive = false;
134
                        }
135
                }else{
136
                        if(!defaultCursorActive){
137
                                getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(defaultCursor,
138
                                        new Point(16, 16), ""));
139
                                defaultCursorActive = true;
140
                        }
141
                }
142
        }
143
        
144
        /**
145
         * Obtiene el cursor actual
146
         */
147
        public Cursor getCursor() {
148
                return this.cur;
149
        }
150
        
151
        /**
152
         * Si no est? activo el cursor por defecto capturamos el punto 
153
         * seleccionado en coordenadas del mundo real.
154
         */
155
        public void mousePressed(MouseEvent e) throws BehaviorException {
156
                if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive){
157
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
158
                        ptoIni = vp.toMapPoint(e.getPoint());
159
                        isMoveable = true;
160
                }                
161
        }
162
        
163
        /**
164
         *  Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
165
         * de destino calculando el extent nuevamente.
166
         */
167
        public void mouseReleased(MouseEvent e) throws BehaviorException {
168
                if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){
169
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
170
                        ptoFin = vp.toMapPoint(e.getPoint());
171
                        double distX = ptoFin.getX() - ptoIni.getX();
172
                        double distY = ptoFin.getY() - ptoIni.getY();
173
                        Extent ext = new Extent(lyrRaster.getMinX() + distX, 
174
                                                                        lyrRaster.getMinY() + distY,
175
                                                                        lyrRaster.getMaxX() + distX, 
176
                                                                        lyrRaster.getMaxY() + distY);
177
                        if(this.lyrRaster != null){
178
                                ((FLyrRaster)getMapControl().getMapContext().getLayers().getLayer(lyrRaster.getName())).setExtent(ext);
179
                                getMapControl().getMapContext().invalidate();
180
                        }
181
                        isMoveable = false;
182
                        getMapControl().repaint();
183
                }
184
        }
185
        
186
        /**
187
         * Funci?n de pintado del canvas.Pintamos un marco a la imagen para saber 
188
         * donde la movemos. 
189
         */
190
        public void paintComponent(Graphics g) {
191
                super.paintComponent(g);
192
                if(isMoveable){
193
                        BufferedImage img = getMapControl().getImage();
194
                        g.drawImage(img, 0, 0, null);
195
                        g.setColor(Color.red);
196
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
197
        
198
                        Rectangle r = new Rectangle();
199
                        
200
                        Point2D ul = vp.fromMapPoint(this.lyrRaster.getMinX(), this.lyrRaster.getMinY());
201
                        Point2D lr = vp.fromMapPoint(this.lyrRaster.getMaxX(), this.lyrRaster.getMaxY());
202
                        r.setFrameFromDiagonal(ul, lr);
203
                        
204
                        Point2D ini = vp.fromMapPoint(ptoIni.getX() , ptoIni.getY());
205
                        Point2D fin = vp.fromMapPoint(ptoFin.getX() , ptoFin.getY());
206
                        
207
                        double distX = fin.getX() - ini.getX();
208
                        double distY = fin.getY() - ini.getY();
209
                        
210
                        g.drawRect(r.x + (int)distX, r.y + (int)distY, r.width, r.height);
211
                }
212
        }
213
        
214
        /**
215
         * Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
216
         * poder ver el marco de arrastre.
217
         */
218
        public void mouseDragged(MouseEvent e) throws BehaviorException {
219
                if(isMoveable){
220
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
221
                        ptoFin = vp.toMapPoint(e.getPoint());
222
                        getMapControl().repaint();
223
                }
224
        }
225
        
226
        /* (non-Javadoc)
227
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
228
         */
229
        public ToolListener getListener() {
230
                // TODO Auto-generated method stub
231
                return null;
232
        }
233
        /* (non-Javadoc)
234
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getMapControl()
235
         */
236
        public MapControl getMapControl() {
237
                // TODO Auto-generated method stub
238
                return super.getMapControl();
239
        }
240
        /* (non-Javadoc)
241
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
242
         */
243
        public void mouseClicked(MouseEvent e) throws BehaviorException {
244
                // TODO Auto-generated method stub
245
                super.mouseClicked(e);
246
        }
247

    
248
        /* (non-Javadoc)
249
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
250
         */
251
        public void mouseEntered(MouseEvent e) throws BehaviorException {
252
                // TODO Auto-generated method stub
253
                super.mouseEntered(e);
254
        }
255
        /* (non-Javadoc)
256
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
257
         */
258
        public void mouseExited(MouseEvent e) throws BehaviorException {
259
                // TODO Auto-generated method stub
260
                super.mouseExited(e);
261
        }
262

    
263
        /* (non-Javadoc)
264
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
265
         */
266
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
267
                // TODO Auto-generated method stub
268
                super.mouseWheelMoved(e);
269
        }
270

    
271
        /* (non-Javadoc)
272
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
273
         */
274
        public void setMapControl(MapControl mc) {
275
                // TODO Auto-generated method stub
276
                super.setMapControl(mc);
277
        }
278
                        
279
        /**
280
         * @return Returns the lyrRaster.
281
         */
282
        public FLyrRaster getLyrRaster() {
283
                return lyrRaster;
284
        }
285

    
286
        /**
287
         * @param lyrRaster The lyrRaster to set.
288
         */
289
        public void setLyrRaster(FLyrRaster lyrRaster) {
290
                this.lyrRaster = lyrRaster;
291
        }
292

    
293
}