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