svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ui / zoom / layers / ZoomCursorGraphicLayer.java @ 18264
History | View | Annotate | Download (10 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.rastertools.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.rastertools.georeferencing.ui.zoom.CanvasZone; |
33 |
import org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer; |
34 |
import org.gvsig.rastertools.georeferencing.ui.zoom.tools.ToolEvent; |
35 |
import org.gvsig.rastertools.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 Color color = Color.RED; |
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 |
private ToolListener listener = null; |
70 |
|
71 |
/**
|
72 |
* Constructor. Asigna el ancho y alto del rectangulo del cursor y la
|
73 |
* posici?n en la inicializaci?n.
|
74 |
*/
|
75 |
public ZoomCursorGraphicLayer(int pX, int pY, int w, int h, ToolListener listener) { |
76 |
wCursor = w; |
77 |
hCursor = h; |
78 |
posX = pX; |
79 |
posY = pY; |
80 |
this.listener = listener;
|
81 |
try {
|
82 |
iconHoriz = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaHorizCursor.gif")).getImage(); |
83 |
iconVert = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaVertCursor.gif")).getImage(); |
84 |
iconMove = new ImageIcon(getClass().getClassLoader().getResource("images/FlechaMoveCursor.gif")).getImage(); |
85 |
} catch (NullPointerException e) { |
86 |
|
87 |
} |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Asigna el canvas
|
92 |
* @param canvas
|
93 |
*/
|
94 |
public void setCanvas(CanvasZone canvas) { |
95 |
this.canvas = canvas;
|
96 |
canvas.addMouseMotionListener(this);
|
97 |
canvas.addMouseListener(this);
|
98 |
} |
99 |
|
100 |
/**
|
101 |
* Asigna la posici?n del cursor en el canvas
|
102 |
* @param x Posici?n en X
|
103 |
* @param y Posici?n en Y
|
104 |
*/
|
105 |
public void setCursorPosition(int x, int y) { |
106 |
this.posX = x;
|
107 |
this.posY = y;
|
108 |
} |
109 |
|
110 |
/**
|
111 |
* Asigna el tama?o del cursor en pixeles del canvas
|
112 |
* @param w Ancho
|
113 |
* @param h Alto
|
114 |
*/
|
115 |
public void setCursorSize(int w, int h) { |
116 |
this.wCursor = w;
|
117 |
this.hCursor = h;
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* Obtiene las coordenadas de la ventana de zoom. Las coordenadas son devueltas
|
122 |
* en referencia a la vista.
|
123 |
* @return
|
124 |
*/
|
125 |
public Rectangle2D getCursorViewCoordinates() { |
126 |
return new Rectangle2D.Double(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor); |
127 |
} |
128 |
|
129 |
/*
|
130 |
* (non-Javadoc)
|
131 |
* @see org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer#draw(java.awt.Graphics2D, org.gvsig.raster.datastruct.Extent, int, int)
|
132 |
*/
|
133 |
public void draw(Graphics2D g, Rectangle2D ext, int w, int h) { |
134 |
g.setColor(Color.RED);
|
135 |
wCursor = Math.max(wCursor, MIN_CURSOR_SIZE);
|
136 |
hCursor = Math.max(hCursor, MIN_CURSOR_SIZE);
|
137 |
g.drawRect(posX - (wCursor >> 1), posY - (hCursor >> 1), wCursor, hCursor); |
138 |
g.drawLine(posX, posY - (hCursor >> 1), posX, 0); |
139 |
g.drawLine(posX, posY + (hCursor >> 1), posX, h);
|
140 |
g.drawLine(0, posY, posX - (wCursor >> 1), posY); |
141 |
g.drawLine(posX + (wCursor >> 1), posY, w, posY);
|
142 |
} |
143 |
|
144 |
/*
|
145 |
* (non-Javadoc)
|
146 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
147 |
*/
|
148 |
public void mouseClicked(MouseEvent e) { |
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
154 |
*/
|
155 |
public void mouseEntered(MouseEvent e) { |
156 |
} |
157 |
|
158 |
/*
|
159 |
* (non-Javadoc)
|
160 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
161 |
*/
|
162 |
public void mouseExited(MouseEvent e) { |
163 |
} |
164 |
|
165 |
/*
|
166 |
* (non-Javadoc)
|
167 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
168 |
*/
|
169 |
public void mousePressed(MouseEvent e) { |
170 |
prevX = e.getX(); |
171 |
prevY = e.getY(); |
172 |
} |
173 |
|
174 |
/*
|
175 |
* (non-Javadoc)
|
176 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
177 |
*/
|
178 |
public void mouseReleased(MouseEvent e) { |
179 |
if(getOperation() != NONE) {
|
180 |
setOperation(NONE); |
181 |
if(listener != null) |
182 |
listener.endAction(new ToolEvent(this)); |
183 |
} |
184 |
} |
185 |
|
186 |
|
187 |
/**
|
188 |
* Cuando se pincha y se arrastra en los contornos se redimensiona el marco.
|
189 |
*/
|
190 |
public void mouseDragged(MouseEvent e) { |
191 |
if(getOperation() == MOVE_UR) {
|
192 |
posX += (e.getX() - (wCursor >> 1)) - posX;
|
193 |
posY += (e.getY() + (hCursor >> 1)) - posY;
|
194 |
return;
|
195 |
} |
196 |
if(getOperation() == MOVE_UL) {
|
197 |
posX += (e.getX() + (wCursor >> 1)) - posX;
|
198 |
posY += (e.getY() + (hCursor >> 1)) - posY;
|
199 |
return;
|
200 |
} |
201 |
if(getOperation() == MOVE_LR) {
|
202 |
posX += (e.getX() - (wCursor >> 1)) - posX;
|
203 |
posY += (e.getY() - (hCursor >> 1)) - posY;
|
204 |
return;
|
205 |
} |
206 |
if(getOperation() == MOVE_LL) {
|
207 |
posX += (e.getX() + (wCursor >> 1)) - posX;
|
208 |
posY += (e.getY() - (hCursor >> 1)) - posY;
|
209 |
return;
|
210 |
} |
211 |
if(getOperation() == REDIM_LEFT) {
|
212 |
wCursor += prevX - e.getX(); |
213 |
posX = e.getX() + (wCursor >> 1);
|
214 |
prevX = e.getX(); |
215 |
return;
|
216 |
} |
217 |
if(getOperation() == REDIM_RIGHT) {
|
218 |
int prevULX = posX - (wCursor >> 1); |
219 |
wCursor += e.getX() - prevX; |
220 |
posX = prevULX + (wCursor >> 1);
|
221 |
prevX = e.getX(); |
222 |
return;
|
223 |
} |
224 |
if(getOperation() == REDIM_UP) {
|
225 |
hCursor += prevY - e.getY(); |
226 |
posY = e.getY() + (hCursor >> 1);
|
227 |
prevY = e.getY(); |
228 |
return;
|
229 |
} |
230 |
if(getOperation() == REDIM_DOWN) {
|
231 |
int prevULY = posY - (hCursor >> 1); |
232 |
hCursor += e.getY() - prevY; |
233 |
posY = prevULY + (hCursor >> 1);
|
234 |
prevY = e.getY(); |
235 |
return;
|
236 |
} |
237 |
} |
238 |
|
239 |
/*
|
240 |
* (non-Javadoc)
|
241 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
242 |
*/
|
243 |
public void mouseMoved(MouseEvent e) { |
244 |
int pxLeft = posX - (wCursor >> 1); |
245 |
int pxRight = posX + (wCursor >> 1); |
246 |
int pyUp = posY - (hCursor >> 1); |
247 |
int pyDown = posY + (hCursor >> 1); |
248 |
if(e.getX() >= (pxRight - 2) && e.getX() <= (pxRight + 2) && e.getY() >= (pyUp - 2) && e.getY() <= (pyUp + 2)) { |
249 |
if(iconMove != null) |
250 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), "")); |
251 |
setOperation(MOVE_UR); |
252 |
return;
|
253 |
} |
254 |
if(e.getX() >= (pxLeft - 2) && e.getX() <= (pxLeft + 2) && e.getY() >= (pyUp - 2) && e.getY() <= (pyUp + 2)) { |
255 |
if(iconMove != null) |
256 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), "")); |
257 |
setOperation(MOVE_UL); |
258 |
return;
|
259 |
} |
260 |
if(e.getX() >= (pxLeft - 2) && e.getX() <= (pxLeft + 2) && e.getY() >= (pyDown - 2) && e.getY() <= (pyDown + 2)) { |
261 |
if(iconMove != null) |
262 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), "")); |
263 |
setOperation(MOVE_LL); |
264 |
return;
|
265 |
} |
266 |
if(e.getX() >= (pxRight - 2) && e.getX() <= (pxRight + 2) && e.getY() >= (pyDown - 2) && e.getY() <= (pyDown + 2)) { |
267 |
if(iconMove != null) |
268 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconMove, new Point(16, 16), "")); |
269 |
setOperation(MOVE_LR); |
270 |
return;
|
271 |
} |
272 |
if(e.getX() >= (pxLeft - 1) && e.getX() <= (pxLeft + 1)) { |
273 |
if(iconHoriz != null) |
274 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), "")); |
275 |
setOperation(REDIM_LEFT); |
276 |
return;
|
277 |
} |
278 |
if(e.getX() >= (pxRight - 1) && e.getX() <= (pxRight + 1)) { |
279 |
if(iconHoriz != null) |
280 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconHoriz, new Point(16, 16), "")); |
281 |
setOperation(REDIM_RIGHT); |
282 |
return;
|
283 |
} |
284 |
if(e.getY() >= (pyUp - 1) && e.getY() <= (pyUp + 1)) { |
285 |
if(iconVert != null) |
286 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), "")); |
287 |
setOperation(REDIM_UP); |
288 |
return;
|
289 |
} |
290 |
if(e.getY() >= (pyDown - 1) && e.getY() <= (pyDown + 1)) { |
291 |
if(iconVert != null) |
292 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(iconVert, new Point(16, 16), "")); |
293 |
setOperation(REDIM_DOWN); |
294 |
return;
|
295 |
} |
296 |
setOperation(NONE); |
297 |
canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Obtiene la operaci?n sobre el cursor que hay seleccionada
|
302 |
* @return Entero que representa a la operaci?n
|
303 |
*/
|
304 |
private int getOperation() { |
305 |
return operation;
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* Asigna la operaci?n sobre el cursor que hay seleccionada
|
310 |
* @param op
|
311 |
*/
|
312 |
private void setOperation(int op) { |
313 |
operation = op; |
314 |
} |
315 |
|
316 |
/**
|
317 |
* Obtiene el color del cursor
|
318 |
* @return Color
|
319 |
*/
|
320 |
public Color getColor() { |
321 |
return color;
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* Asigna el color del cursor
|
326 |
* @param color
|
327 |
*/
|
328 |
public void setColor(Color color) { |
329 |
this.color = color;
|
330 |
} |
331 |
} |
332 |
|