Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / zoom / layers / ZoomCursorGraphicLayer.java @ 32311

History | View | Annotate | Download (12.2 KB)

1 32311 nbrodin
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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
package org.gvsig.georeferencing.ui.zoom.layers;
20
21
import java.awt.Color;
22
import java.awt.Cursor;
23
import java.awt.Graphics2D;
24
import java.awt.Image;
25
import java.awt.Point;
26
import java.awt.Toolkit;
27
import java.awt.event.MouseEvent;
28
import java.awt.geom.Rectangle2D;
29
30
import javax.swing.ImageIcon;
31
32
import org.gvsig.georeferencing.ui.zoom.CanvasZone;
33
import org.gvsig.georeferencing.ui.zoom.IGraphicLayer;
34
import org.gvsig.georeferencing.ui.zoom.tools.ToolEvent;
35
import org.gvsig.georeferencing.ui.zoom.tools.ToolListener;
36
37
/**
38
 * Capa gr?fica que se dibuja sobre una vista de zoom un cursor
39
 * rectangular que representa una ventanta de zoom sobre la vista
40
 * 22/12/2007
41
 * @author Nacho Brodin (nachobrodin@gmail.com)
42
 */
43
public class ZoomCursorGraphicLayer implements IGraphicLayer {
44
        //Operaciones sobre el cursor gr?fico
45
        private static final int    NONE             = -1;
46
        private static final int    REDIM_LEFT       = 0;
47
        private static final int    REDIM_RIGHT      = 1;
48
        private static final int    REDIM_UP         = 2;
49
        private static final int    REDIM_DOWN       = 3;
50
        private static final int    MOVE_UR          = 4;
51
        private static final int    MOVE_UL          = 5;
52
        private static final int    MOVE_LR          = 6;
53
        private static final int    MOVE_LL          = 7;
54
        public static final int     CENTER           = 8;
55
56
        private final int           MIN_CURSOR_SIZE  = 10;
57
        private int                 operation        = NONE;
58
59
        private int                 wCursor          = 0;
60
        private int                 hCursor          = 0;
61
        private int                 posX             = 0;
62
        private int                 posY             = 0;
63
        private Image               iconHoriz        = null;
64
        private Image               iconVert         = null;
65
        private Image               iconMove         = null;
66
        private CanvasZone          canvas           = null;
67
        //Memoria temporal de las posiciones en X y en Y previas a una operaci?n
68
        private int                 prevX, prevY;
69
        //Listener para que objetos externos sean informados de las acciones de la capa
70
        private ToolListener        listener         = null;
71
72
        private boolean             active           = true;
73
        private boolean             sleepActive      = true;
74
    private Color               cursorColor      = Color.RED;
75
76
    private boolean             initSize         = true;
77
    private int                 initWCursor      = 0;
78
        private int                 initHCursor      = 0;
79
80
        /**
81
         * Constructor. Asigna el ancho y alto del rectangulo del cursor y la
82
         * posici?n en la inicializaci?n.
83
         * @param pX Posici?n en X del cursor en la vista
84
         * @param pY Posici?n en Y del cursor en la vista
85
         * @param w Ancho del cursor en la vista
86
         * @param h Alto del cursor en la vista
87
         * @param listener Listener para acciones de finalizaci?n de la operaci?n de zoom
88
         */
89
        public ZoomCursorGraphicLayer(int pX, int pY, int w, int h, ToolListener listener) {
90
                wCursor = w;
91
                hCursor = h;
92
                posX = pX;
93
                posY = pY;
94
                this.listener = listener;
95
                try {
96
                        iconHoriz = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaHorizCursor.gif")).getImage();
97
                        iconVert = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaVertCursor.gif")).getImage();
98
                        iconMove = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaMoveCursor.gif")).getImage();
99
                } catch (NullPointerException e) {
100
101
                }
102
        }
103
104
        /**
105
         * Asigna el canvas
106
         * @param canvas
107
         */
108
        public void setCanvas(CanvasZone canvas) {
109
                this.canvas = canvas;
110
                canvas.addMouseMotionListener(this);
111
                canvas.addMouseListener(this);
112
        }
113
114
        /**
115
         * Asigna la posici?n del cursor en el canvas
116
         * @param x Posici?n en X
117
         * @param y Posici?n en Y
118
         */
119
        public void setCursorPosition(int x, int y) {
120
                this.posX = x;
121
                this.posY = y;
122
        }
123
124
        /**
125
         * Asigna el tama?o del cursor en pixeles del canvas
126
         * @param w Ancho
127
         * @param h Alto
128
         */
129
        public void setCursorSize(int w, int h) {
130
                //Salva la primera vez como tama?o de inicializaci?n
131
                if(initSize) {
132
                        this.initWCursor = w;
133
                        this.initHCursor = h;
134
                        initSize = false;
135
                }
136
                this.wCursor = w;
137
                this.hCursor = h;
138
        }
139
140
        /**
141
         * Inicializa el tama?o del cursor
142
         */
143
        public void resetCursorSize() {
144
                this.wCursor = initWCursor;
145
                this.hCursor = initHCursor;
146
        }
147
148
        /**
149
         * Obtiene las coordenadas de la ventana de zoom. Las coordenadas son devueltas
150
         * en referencia a la vista.
151
         * @return
152
         */
153
        public Rectangle2D getCursorViewCoordinates() {
154
                return new Rectangle2D.Double(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor);
155
        }
156
157
        /*
158
         * (non-Javadoc)
159
         * @see org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer#draw(java.awt.Graphics2D, org.gvsig.raster.datastruct.Extent, int, int)
160
         */
161
        public void draw(Graphics2D g, Rectangle2D ext, int w, int h) {
162
                g.setColor(cursorColor);
163
                wCursor = Math.max(wCursor, MIN_CURSOR_SIZE);
164
                hCursor = Math.max(hCursor, MIN_CURSOR_SIZE);
165
                g.drawRect(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor);
166
                g.drawLine(posX, posY - (hCursor >> 1), posX, 0);
167
                g.drawLine(posX, posY + (hCursor >> 1), posX, h);
168
                g.drawLine(0, posY, posX - (wCursor >> 1), posY);
169
                g.drawLine(posX + (wCursor >> 1), posY, w, posY);
170
        }
171
172
        /*
173
         * (non-Javadoc)
174
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
175
         */
176
        public void mouseClicked(MouseEvent e) {
177
        }
178
179
        /*
180
         * (non-Javadoc)
181
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
182
         */
183
        public void mouseEntered(MouseEvent e) {
184
        }
185
186
        /*
187
         * (non-Javadoc)
188
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
189
         */
190
        public void mouseExited(MouseEvent e) {
191
        }
192
193
        /*
194
         * (non-Javadoc)
195
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
196
         */
197
        public void mousePressed(MouseEvent e) {
198
                prevX = e.getX();
199
                prevY = e.getY();
200
        }
201
202
        /*
203
         * (non-Javadoc)
204
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
205
         */
206
        public void mouseReleased(MouseEvent e) {
207
                if(!isActive()) {
208
                        return;
209
                }
210
                if(getOperation() != NONE) {
211
                        setOperation(NONE);
212
                        if(listener != null) {
213
                                listener.endAction(new ToolEvent(this));
214
                        }
215
                }
216
        }
217
218
219
        /**
220
         * Cuando se pincha y se arrastra en los contornos se redimensiona el marco.
221
         */
222
        public void mouseDragged(MouseEvent e) {
223
                if(!isActive()) {
224
                        return;
225
                }
226
                if(getOperation() == MOVE_UR) {
227
                        posX += (e.getX() - (wCursor >> 1)) - posX;
228
                        posY += (e.getY() + (hCursor >> 1)) - posY;
229
                        return;
230
                }
231
                if(getOperation() == MOVE_UL) {
232
                        posX += (e.getX() + (wCursor >> 1)) - posX;
233
                        posY += (e.getY() + (hCursor >> 1)) - posY;
234
                        return;
235
                }
236
                if(getOperation() == MOVE_LR) {
237
                        posX += (e.getX() - (wCursor >> 1)) - posX;
238
                        posY += (e.getY() - (hCursor >> 1)) - posY;
239
                        return;
240
                }
241
                if(getOperation() == MOVE_LL) {
242
                        posX += (e.getX() + (wCursor >> 1)) - posX;
243
                        posY += (e.getY() - (hCursor >> 1)) - posY;
244
                        return;
245
                }
246
                if(getOperation() == REDIM_LEFT) {
247
                        wCursor += prevX - e.getX();
248
                        posX = e.getX() + (wCursor >> 1);
249
                        prevX = e.getX();
250
                        return;
251
                }
252
                if(getOperation() == REDIM_RIGHT) {
253
                        int prevULX = posX - (wCursor >> 1);
254
                        wCursor += e.getX() - prevX;
255
                        posX = prevULX + (wCursor >> 1);
256
                        prevX = e.getX();
257
                        return;
258
                }
259
                if(getOperation() == REDIM_UP) {
260
                        hCursor += prevY - e.getY();
261
                        posY = e.getY() + (hCursor >> 1);
262
                        prevY = e.getY();
263
                        return;
264
                }
265
                if(getOperation() == REDIM_DOWN) {
266
                        int prevULY = posY - (hCursor >> 1);
267
                        hCursor += e.getY() - prevY;
268
                        posY = prevULY + (hCursor >> 1);
269
                        prevY = e.getY();
270
                        return;
271
                }
272
        }
273
274
        /*
275
         * (non-Javadoc)
276
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
277
         */
278
        public void mouseMoved(MouseEvent e) {
279
                if(!isActive()) {
280
                        return;
281
                }
282
                int pxLeft = posX - (wCursor >> 1);
283
                int pxRight = posX + (wCursor >> 1);
284
                int pyUp = posY - (hCursor >> 1);
285
                int pyDown = posY + (hCursor >> 1);
286
287
                //Si estamos fuera del ?rea del cuadrado + 2 p?xeles ponemos el cursor por defecto y no hacemos nada
288
                if(e.getX() < (pxLeft - 2) || e.getX() > (pxRight + 2) || e.getY() < (pyUp - 2) || e.getY() > (pyDown + 2)) {
289
                        setOperation(NONE);
290
                        if(canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
291
                                canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
292
                                listener.offTool(new ToolEvent(this));
293
                        }
294
                        return;
295
                }
296
297
                if(e.getX() >= (pxRight - 2) && e.getY() <= (pyUp + 2)) {
298
                        if(iconMove != null) {
299
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
300
                        }
301
                        setOperation(MOVE_UR);
302
                        return;
303
                }
304
                if(e.getX() <= (pxLeft + 2) && e.getY() <= (pyUp + 2)) {
305
                        if(iconMove != null) {
306
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
307
                        }
308
                        setOperation(MOVE_UL);
309
                        return;
310
                }
311
                if(e.getX() <= (pxLeft + 2) && e.getY() >= (pyDown - 2)) {
312
                        if(iconMove != null) {
313
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
314
                        }
315
                        setOperation(MOVE_LL);
316
                        return;
317
                }
318
                if(e.getX() >= (pxRight - 2) && e.getY() >= (pyDown - 2)) {
319
                        if(iconMove != null) {
320
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), ""));
321
                        }
322
                        setOperation(MOVE_LR);
323
                        return;
324
                }
325
                if(e.getX() <= (pxLeft + 1)) {
326
                        if(iconHoriz != null) {
327
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), ""));
328
                        }
329
                        setOperation(REDIM_LEFT);
330
                        return;
331
                }
332
                if(e.getX() >= (pxRight - 1)) {
333
                        if(iconHoriz != null) {
334
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), ""));
335
                        }
336
                        setOperation(REDIM_RIGHT);
337
                        return;
338
                }
339
                if(e.getY() <= (pyUp + 1)) {
340
                        if(iconVert != null) {
341
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), ""));
342
                        }
343
                        setOperation(REDIM_UP);
344
                        return;
345
                }
346
                if(e.getY() >= (pyDown - 1)) {
347
                        if(iconVert != null) {
348
                                canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), ""));
349
                        }
350
                        setOperation(REDIM_DOWN);
351
                        return;
352
                }
353
                setOperation(NONE);
354
                if(canvas.getCursor().getType() != Cursor.DEFAULT_CURSOR) {
355
                        canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
356
                        listener.offTool(new ToolEvent(this));
357
                }
358
        }
359
360
        /**
361
         * Obtiene la operaci?n sobre el cursor que hay seleccionada
362
         * @return Entero que representa a la operaci?n
363
         */
364
        public int getOperation() {
365
                return operation;
366
        }
367
368
        /**
369
         * Asigna la operaci?n sobre el cursor que hay seleccionada
370
         * @param op
371
         */
372
        public void setOperation(int op) {
373
                operation = op;
374
                if(op != NONE) {
375
                        listener.onTool(new ToolEvent(this));
376
                }
377
        }
378
379
        /**
380
         * Desactiva la herramienta temporalmente. Guarda el estado en el que estaba
381
         * para restaurarlo cuando se invoque a awake
382
         */
383
        public void sleep() {
384
                sleepActive = active;
385
                active = false;
386
        }
387
388
        /**
389
         * Recupera el estado de activaci?n que ten?a antes de la ?ltima invocaci?n
390
         * de sleep
391
         */
392
        public void awake() {
393
                active = sleepActive;
394
        }
395
396
        /**
397
         * Consulta si es posible interactuar con el la capa de cursor de zoom
398
         * @return
399
         */
400
        public boolean isActive() {
401
                return active;
402
        }
403
404
        /**
405
         * Asigna el flag que activa y desactiva la interactuaci?n con capa de control de zoom
406
         * @param activeEvent
407
         */
408
        public void setActive(boolean active) {
409
                this.active = active;
410
        }
411
412
        /**
413
         * Obtiene el color del cursor
414
         * @return Color
415
         */
416
        public Color getColor() {
417
                return cursorColor;
418
        }
419
420
        /**
421
         * Asigna el color del cursor
422
         * @param color
423
         */
424
        public void setColor(Color color) {
425
                this.cursorColor = color;
426
        }
427
}