Statistics
| Revision:

gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.swing / org.gvsig.raster.georeferencing.swing.impl / src / main / java / org / gvsig / raster / georeferencing / swing / impl / layer / ZoomCursorGraphicLayer.java @ 1730

History | View | Annotate | Download (12.3 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.georeferencing.swing.impl.layer;
23

    
24
import java.awt.Color;
25
import java.awt.Cursor;
26
import java.awt.Graphics2D;
27
import java.awt.Image;
28
import java.awt.Point;
29
import java.awt.Toolkit;
30
import java.awt.event.MouseEvent;
31
import java.awt.geom.Rectangle2D;
32

    
33
import org.gvsig.andami.IconThemeHelper;
34
import org.gvsig.raster.georeferencing.swing.impl.view.CanvasZone;
35
import org.gvsig.raster.georeferencing.swing.view.GeoreferencingView;
36
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer;
37
import org.gvsig.raster.georeferencing.swing.view.ToolEvent;
38
import org.gvsig.raster.georeferencing.swing.view.ToolListener;
39

    
40
/**
41
 * Capa gr?fica que se dibuja sobre una vista de zoom un cursor
42
 * rectangular que representa una ventanta de zoom sobre la vista
43
 * 22/12/2007
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class ZoomCursorGraphicLayer implements IGraphicLayer {
47
        //Operaciones sobre el cursor gr?fico
48
        private static final int    NONE             = -1;
49
        private static final int    REDIM_LEFT       = 0;
50
        private static final int    REDIM_RIGHT      = 1;
51
        private static final int    REDIM_UP         = 2;
52
        private static final int    REDIM_DOWN       = 3;
53
        private static final int    MOVE_UR          = 4;
54
        private static final int    MOVE_UL          = 5;
55
        private static final int    MOVE_LR          = 6;
56
        private static final int    MOVE_LL          = 7;
57
        
58
        private final int           MIN_CURSOR_SIZE  = 10;
59
        private int                 operation        = NONE; 
60
        
61
        private int                 wCursor          = 0;
62
        private int                 hCursor          = 0;
63
        private int                 posX             = 0;
64
        private int                 posY             = 0;
65
        private Image               iconHoriz        = null;
66
        private Image               iconVert         = null;
67
        private Image               iconMove         = null;
68
        private CanvasZone          canvas           = null;
69
        //Memoria temporal de las posiciones en X y en Y previas a una operaci?n
70
        private int                 prevX, prevY;
71
        //Listener para que objetos externos sean informados de las acciones de la capa
72
        private ToolListener        listener         = null;
73
        
74
        private boolean             active           = true;
75
        private boolean             sleepActive      = true;
76
    private Color               cursorColor      = Color.RED;
77
    
78
    private boolean             initSize         = true;
79
    private int                 initWCursor      = 0;
80
        private int                 initHCursor      = 0;
81
        
82
        private GeoreferencingView  view             = null;
83
        
84
        /**
85
         * Constructor. Asigna el ancho y alto del rectangulo del cursor y la
86
         * posici?n en la inicializaci?n.
87
         * @param pX Posici?n en X del cursor en la vista
88
         * @param pY Posici?n en Y del cursor en la vista
89
         * @param w Ancho del cursor en la vista
90
         * @param h Alto del cursor en la vista
91
         * @param listener Listener para acciones de finalizaci?n de la operaci?n de zoom
92
         */
93
        public ZoomCursorGraphicLayer(int pX, int pY, int w, int h, ToolListener listener) {
94
                wCursor = w;
95
                hCursor = h;
96
                posX = pX;
97
                posY = pY;
98
                this.listener = listener;
99
                try {
100
                        iconHoriz = IconThemeHelper.getImage("arrow_horiz-icon"); 
101
                        iconVert = IconThemeHelper.getImage("arrow_vert-icon");
102
                        iconMove = IconThemeHelper.getImage("arrow_move-icon");
103
                } catch (NullPointerException e) {
104
                        
105
                }
106
        }
107
        
108
        public void setGeoreferencingView(GeoreferencingView view) {
109
                this.view = view;
110
                this.canvas = (CanvasZone)view.getCanvas();
111
                canvas.addMouseMotionListener(this);
112
                canvas.addMouseListener(this);
113
        }
114
        
115
        /**
116
         * Asigna la posici?n del cursor en el canvas
117
         * @param x Posici?n en X
118
         * @param y Posici?n en Y
119
         */
120
        public void setCursorPosition(int x, int y) {
121
                this.posX = x;
122
                this.posY = y;
123
        }
124
        
125
        /**
126
         * Asigna el tama?o del cursor en pixeles del canvas
127
         * @param w Ancho
128
         * @param h Alto
129
         */
130
        public void setCursorSize(int w, int h) {
131
                //Salva la primera vez como tama?o de inicializaci?n
132
                if(initSize) {
133
                        this.initWCursor = w;
134
                        this.initHCursor = h;
135
                        initSize = false;
136
                }
137
                this.wCursor = w;
138
                this.hCursor = h;
139
        }
140
        
141
        /**
142
         * Inicializa el tama?o del cursor
143
         */
144
        public void resetCursorSize() {
145
                this.wCursor = initWCursor;
146
                this.hCursor = initHCursor;
147
        }
148
        
149
        /**
150
         * Obtiene las coordenadas de la ventana de zoom. Las coordenadas son devueltas
151
         * en referencia a la vista. 
152
         * @return
153
         */
154
        public Rectangle2D getCursorViewCoordinates() {
155
                return new Rectangle2D.Double(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor);
156
        }
157
         
158
        /*
159
         * (non-Javadoc)
160
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer#draw(java.awt.Graphics2D, org.gvsig.raster.datastruct.Extent, int, int)
161
         */
162
        public void draw(Graphics2D g, Rectangle2D ext, int w, int h) {
163
                g.setColor(cursorColor);
164
                wCursor = Math.max(wCursor, MIN_CURSOR_SIZE); 
165
                hCursor = Math.max(hCursor, MIN_CURSOR_SIZE);
166
                g.drawRect(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor);
167
                g.drawLine(posX, posY - (hCursor >> 1), posX, 0);
168
                g.drawLine(posX, posY + (hCursor >> 1), posX, h);
169
                g.drawLine(0, posY, posX - (wCursor >> 1), posY);
170
                g.drawLine(posX + (wCursor >> 1), posY, w, posY);
171
        }
172
        
173
        /*
174
         * (non-Javadoc)
175
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
176
         */
177
        public void mouseClicked(MouseEvent e) {
178
        }
179

    
180
        /*
181
         * (non-Javadoc)
182
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
183
         */
184
        public void mouseEntered(MouseEvent e) {
185
        }
186

    
187
        /*
188
         * (non-Javadoc)
189
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
190
         */
191
        public void mouseExited(MouseEvent e) {
192
        }
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
197
         */
198
        public void mousePressed(MouseEvent e) {
199
                prevX = e.getX();
200
                prevY = e.getY();
201
        }
202

    
203
        /*
204
         * (non-Javadoc)
205
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
206
         */
207
        public void mouseReleased(MouseEvent e) {
208
                if(!isActive())
209
                        return;
210
                if(getOperation() != NONE) {
211
                        setOperation(NONE);
212
                        if(listener != null)
213
                                listener.endAction(new ToolEvent(this, view));
214
                }
215
        }
216
        
217

    
218
        /**
219
         * Cuando se pincha y se arrastra en los contornos se redimensiona el marco.
220
         */
221
        public void mouseDragged(MouseEvent e) {
222
                if(!isActive())
223
                        return;
224
                if(getOperation() == MOVE_UR) {
225
                        posX += (e.getX() - (wCursor >> 1)) - posX;
226
                        posY += (e.getY() + (hCursor >> 1)) - posY;
227
                        return;
228
                }
229
                if(getOperation() == MOVE_UL) {
230
                        posX += (e.getX() + (wCursor >> 1)) - posX;
231
                        posY += (e.getY() + (hCursor >> 1)) - posY;
232
                        return;
233
                }
234
                if(getOperation() == MOVE_LR) {
235
                        posX += (e.getX() - (wCursor >> 1)) - posX;
236
                        posY += (e.getY() - (hCursor >> 1)) - posY;
237
                        return;
238
                }
239
                if(getOperation() == MOVE_LL) {
240
                        posX += (e.getX() + (wCursor >> 1)) - posX;
241
                        posY += (e.getY() - (hCursor >> 1)) - posY;
242
                        return;
243
                }
244
                if(getOperation() == REDIM_LEFT) {
245
                        wCursor += prevX - e.getX();
246
                        posX = e.getX() + (wCursor >> 1);
247
                        prevX = e.getX();
248
                        return;
249
                }
250
                if(getOperation() == REDIM_RIGHT) {
251
                        int prevULX = posX - (wCursor >> 1);
252
                        wCursor += e.getX() - prevX;
253
                        posX = prevULX + (wCursor >> 1);
254
                        prevX = e.getX();
255
                        return;
256
                }
257
                if(getOperation() == REDIM_UP) {
258
                        hCursor += prevY - e.getY();
259
                        posY = e.getY() + (hCursor >> 1);
260
                        prevY = e.getY();
261
                        return;
262
                }
263
                if(getOperation() == REDIM_DOWN) {
264
                        int prevULY = posY - (hCursor >> 1);
265
                        hCursor += e.getY() - prevY;
266
                        posY = prevULY + (hCursor >> 1);
267
                        prevY = e.getY();
268
                        return;
269
                }
270
        }
271

    
272
        /*
273
         * (non-Javadoc)
274
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
275
         */
276
        public void mouseMoved(MouseEvent e) {
277
                if(!isActive())
278
                        return;
279
                int pxLeft = posX - (wCursor >> 1);
280
                int pxRight = posX + (wCursor >> 1);
281
                int pyUp = posY - (hCursor >> 1);
282
                int pyDown = posY + (hCursor >> 1);
283
                
284
                //Si estamos fuera del ?rea del cuadrado + 2 p?xeles ponemos el cursor por defecto y no hacemos nada
285
                if(e.getX() < (pxLeft - 2) || e.getX() > (pxRight + 2) || e.getY() < (pyUp - 2) || e.getY() > (pyDown + 2)) {
286
                        setOperation(NONE);
287
                        if(canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
288
                                canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
289
                                listener.offTool(new ToolEvent(this, view));
290
                        }
291
                        return; 
292
                }
293
                
294
                if(e.getX() >= (pxRight - 2) && e.getY() <= (pyUp + 2)) {
295
                        if(iconMove != null)
296
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
297
                        setOperation(MOVE_UR);
298
                        return;
299
                }
300
                if(e.getX() <= (pxLeft + 2) && e.getY() <= (pyUp + 2)) {
301
                        if(iconMove != null)
302
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
303
                        setOperation(MOVE_UL);
304
                        return;
305
                }
306
                if(e.getX() <= (pxLeft + 2) && e.getY() >= (pyDown - 2)) {
307
                        if(iconMove != null)
308
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
309
                        setOperation(MOVE_LL);
310
                        return;
311
                }
312
                if(e.getX() >= (pxRight - 2) && e.getY() >= (pyDown - 2)) {
313
                        if(iconMove != null)
314
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
315
                        setOperation(MOVE_LR);
316
                        return;
317
                }
318
                if(e.getX() <= (pxLeft + 1)) {
319
                        if(iconHoriz != null)
320
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), ""));
321
                        setOperation(REDIM_LEFT);
322
                        return;
323
                }
324
                if(e.getX() >= (pxRight - 1)) {
325
                        if(iconHoriz != null)
326
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), ""));
327
                        setOperation(REDIM_RIGHT);
328
                        return;
329
                }
330
                if(e.getY() <= (pyUp + 1)) {
331
                        if(iconVert != null)
332
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), ""));
333
                        setOperation(REDIM_UP);
334
                        return;
335
                }
336
                if(e.getY() >= (pyDown - 1)) {
337
                        if(iconVert != null)
338
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), ""));
339
                        setOperation(REDIM_DOWN);
340
                        return;
341
                }
342
                setOperation(NONE);
343
                if(canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
344
                        canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
345
                        listener.offTool(new ToolEvent(this, view));
346
                }
347
        }
348
        
349
        /**
350
         * Obtiene la operaci?n sobre el cursor que hay seleccionada
351
         * @return Entero que representa a la operaci?n
352
         */
353
        public int getOperation() {
354
                return operation;
355
        }
356
        
357
        /**
358
         * Asigna la operaci?n sobre el cursor que hay seleccionada
359
         * @param op
360
         */
361
        private void setOperation(int op) {
362
                operation = op;
363
                if(op != NONE)
364
                        listener.onTool(new ToolEvent(this, view));
365
        }
366
        
367
        /**
368
         * Desactiva la herramienta temporalmente. Guarda el estado en el que estaba
369
         * para restaurarlo cuando se invoque a awake
370
         */
371
        public void sleep() {
372
                sleepActive = active;
373
                active = false;
374
        }
375
        
376
        /**
377
         * Recupera el estado de activaci?n que ten?a antes de la ?ltima invocaci?n 
378
         * de sleep
379
         */
380
        public void awake() {
381
                active = sleepActive;
382
        }
383
        
384
        /**
385
         * Consulta si es posible interactuar con el la capa de cursor de zoom
386
         * @return 
387
         */
388
        public boolean isActive() {
389
                return active;
390
        }
391

    
392
        /**
393
         * Asigna el flag que activa y desactiva la interactuaci?n con capa de control de zoom
394
         * @param activeEvent
395
         */
396
        public void setActive(boolean active) {
397
                this.active = active;
398
        }
399

    
400
        /**
401
         * Obtiene el color del cursor
402
         * @return Color
403
         */
404
        public Color getColor() {
405
                return cursorColor;
406
        }
407

    
408
        /**
409
         * Asigna el color del cursor
410
         * @param color
411
         */
412
        public void setColor(Color color) {
413
                this.cursorColor = color;
414
        }
415
        
416
        public void recalcPixelDrawCoordinates() {
417
                
418
        }
419
        
420
        public void recalcMapDrawCoordinates() {
421
                
422
        }
423
}
424