root / branches / v2_0_0_prep / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / zoom / layers / GCPsGraphicLayer.java @ 32311
History | View | Annotate | Download (11.2 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.Cursor; |
22 |
import java.awt.Graphics2D; |
23 |
import java.awt.Image; |
24 |
import java.awt.Point; |
25 |
import java.awt.Toolkit; |
26 |
import java.awt.event.MouseEvent; |
27 |
import java.awt.geom.Point2D; |
28 |
import java.awt.geom.Rectangle2D; |
29 |
import java.util.ArrayList; |
30 |
|
31 |
import javax.swing.ImageIcon; |
32 |
|
33 |
import org.gvsig.georeferencing.ui.zoom.CanvasZone; |
34 |
import org.gvsig.georeferencing.ui.zoom.IGraphicLayer; |
35 |
import org.gvsig.georeferencing.ui.zoom.tools.ToolEvent; |
36 |
import org.gvsig.georeferencing.ui.zoom.tools.ToolListener; |
37 |
import org.gvsig.raster.datastruct.GeoPoint; |
38 |
|
39 |
/**
|
40 |
* Capa gr?fica que se dibuja sobre un ZoomControl y que dibuja
|
41 |
* una lista de puntos de control.
|
42 |
* 22/12/2007
|
43 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
44 |
*/
|
45 |
public class GCPsGraphicLayer implements IGraphicLayer { |
46 |
public boolean active = true; |
47 |
|
48 |
//Operaciones sobre los puntos de control
|
49 |
private static final int NONE = -1; |
50 |
private static final int MOVE = 0; |
51 |
|
52 |
private ArrayList pointList = null; |
53 |
private int type = GPGraphic.MAP; |
54 |
private Image icoBlue = null; |
55 |
private Image icoRed = null; |
56 |
private CanvasZone canvas = null; |
57 |
private int operation = NONE; |
58 |
//Punto de control sobre el que se realiza la operaci?n
|
59 |
private GPGraphic operanteGP = null; |
60 |
private GPGraphic lastGP = null; |
61 |
//Listener para que objetos externos sean informados de las acciones de la capa
|
62 |
private ToolListener listener = null; |
63 |
private boolean sleepActive = false; |
64 |
|
65 |
/**
|
66 |
* Constructor. Asigna el tipo de punto a dibujar en la
|
67 |
* capa gr?fica.
|
68 |
* @param type El valor de type viene definido por las constantes de GPGraphic
|
69 |
* @param listener Listener para acciones de finalizaci?n de la operaci?n de mover punto
|
70 |
*/
|
71 |
public GCPsGraphicLayer(int type, ToolListener listener) { |
72 |
this.type = type;
|
73 |
pointList = new ArrayList(); |
74 |
this.listener = listener;
|
75 |
try {
|
76 |
icoBlue = new ImageIcon(getClass().getClassLoader().getResource("images/icoBlue.png")).getImage(); |
77 |
icoRed = new ImageIcon(getClass().getClassLoader().getResource("images/icoRed.png")).getImage(); |
78 |
} catch (NullPointerException e) { |
79 |
|
80 |
} |
81 |
} |
82 |
|
83 |
/**
|
84 |
* A?ade un punto de control gr?fico a la lista. Se hace una distinci?n entre MapGeoPoint
|
85 |
* y RasterGeoPoint para el calculo de las coordenadas de la vista donde se dibuja. Si es
|
86 |
* de tipo map se utilizar?n la informaci?n de coordenadas reales contenidas en el objeto
|
87 |
* GeoPoint para transformarlas a coordenadas de la visualizaci?n.
|
88 |
*
|
89 |
* @param gp Punto de control
|
90 |
* @param id identificador del punto de control
|
91 |
*/
|
92 |
public void addMapGeoPoint(GeoPoint gp, long id) { |
93 |
GPGraphic graphicGCP = new GPGraphic(GPGraphic.MAP, gp);
|
94 |
graphicGCP.setId(id); |
95 |
Point2D p = canvas.viewCoordsFromWorld(gp.mapPoint);
|
96 |
graphicGCP.setDrawCoords(p); |
97 |
pointList.add(graphicGCP); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* A?ade un punto de control gr?fico a la lista. Se hace una distinci?n entre MapGeoPoint
|
102 |
* y RasterGeoPoint para el calculo de las coordenadas de la vista donde se dibuja. Si es
|
103 |
* de tipo raster se utilizar?n la informaci?n de coordenadas pixel contenidas en el objeto
|
104 |
* GeoPoint para transformarlas a coordenadas de la visualizaci?n.
|
105 |
*
|
106 |
* @param gp Punto de control
|
107 |
* @param id identificador del punto de control
|
108 |
*/
|
109 |
public void addPixelGeoPoint(GeoPoint gp, long id) { |
110 |
GPGraphic graphicGCP = new GPGraphic(GPGraphic.PIXEL, gp);
|
111 |
graphicGCP.setId(id); |
112 |
Point2D p = canvas.viewCoordsFromWorld(gp.pixelPoint);
|
113 |
graphicGCP.setDrawCoords(p); |
114 |
pointList.add(graphicGCP); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Elimina un punto de la lista a partir de su posici?n
|
119 |
* @param position
|
120 |
* @return
|
121 |
*/
|
122 |
public boolean removePixelGeoPoint(int position) { |
123 |
try {
|
124 |
pointList.remove(position); |
125 |
return true; |
126 |
} catch (IndexOutOfBoundsException e) { |
127 |
return false; |
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Elimina todos los puntos de la capa
|
133 |
*/
|
134 |
public void removeAllPoints() { |
135 |
pointList.clear(); |
136 |
} |
137 |
|
138 |
/**
|
139 |
* Recalcula las coordenadas de dibujado
|
140 |
*/
|
141 |
public void recalcPixelDrawCoordinates() { |
142 |
for (int i = 0; i < pointList.size(); i++) { |
143 |
GPGraphic gp = ((GPGraphic)pointList.get(i)); |
144 |
Point2D p = gp.getGeoPoint().pixelPoint;
|
145 |
p = canvas.viewCoordsFromWorld(p); |
146 |
gp.setDrawCoords(p); |
147 |
} |
148 |
} |
149 |
|
150 |
/**
|
151 |
* Recalcula las coordenadas de dibujado
|
152 |
*/
|
153 |
public void recalcMapDrawCoordinates() { |
154 |
for (int i = 0; i < pointList.size(); i++) { |
155 |
GPGraphic gp = ((GPGraphic)pointList.get(i)); |
156 |
Point2D p = gp.getGeoPoint().mapPoint;
|
157 |
p = canvas.viewCoordsFromWorld(p); |
158 |
gp.setDrawCoords(p); |
159 |
} |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Obtiene el identificador del punto requerido
|
164 |
* @param pos Posici?n del punto en la capa
|
165 |
* @return
|
166 |
*/
|
167 |
public long getID(int pos) { |
168 |
try {
|
169 |
return ((GPGraphic)pointList.get(pos)).getId();
|
170 |
}catch (ArrayIndexOutOfBoundsException e) { |
171 |
return -1; |
172 |
} |
173 |
} |
174 |
|
175 |
/**
|
176 |
* Asigna el canvas
|
177 |
* @param canvas
|
178 |
*/
|
179 |
public void setCanvas(CanvasZone canvas) { |
180 |
canvas.addMouseMotionListener(this);
|
181 |
canvas.addMouseListener(this);
|
182 |
this.canvas = canvas;
|
183 |
} |
184 |
|
185 |
/*
|
186 |
* (non-Javadoc)
|
187 |
* @see org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer#draw(java.awt.Graphics2D, org.gvsig.raster.datastruct.Extent, int, int)
|
188 |
*/
|
189 |
public void draw(Graphics2D g, Rectangle2D ext, int w, int h) { |
190 |
for (int i = 0; i < pointList.size(); i++) |
191 |
((GPGraphic)pointList.get(i)).draw(g); |
192 |
} |
193 |
|
194 |
/**
|
195 |
* Asigna el flag que muestra u oculta la etiqueta del punto
|
196 |
* @param showLabel true para mostrar la etiqueta y false para ocultarla
|
197 |
*/
|
198 |
public void setShowLabel(boolean showLabel) { |
199 |
for (int i = 0; i < pointList.size(); i++) |
200 |
((GPGraphic)pointList.get(i)).setShowLabel(showLabel); |
201 |
} |
202 |
|
203 |
/**
|
204 |
* Asigna el flag que muestra u oculta el n?mero del punto
|
205 |
* @param showLabel true para mostrar el punto y false para ocultarlo
|
206 |
*/
|
207 |
public void setShowNumber(boolean showNumber) { |
208 |
for (int i = 0; i < pointList.size(); i++) |
209 |
((GPGraphic)pointList.get(i)).setShowNumber(showNumber); |
210 |
} |
211 |
|
212 |
/**
|
213 |
* Obtiene el punto de la posici?n indicada por el par?metro pos
|
214 |
* @param pos Posici?n del punto
|
215 |
* @return GPGraphic
|
216 |
*/
|
217 |
public GPGraphic getPoint(int pos) { |
218 |
try {
|
219 |
return ((GPGraphic)pointList.get(pos));
|
220 |
} catch (IndexOutOfBoundsException e) { |
221 |
return null; |
222 |
} |
223 |
} |
224 |
|
225 |
/**
|
226 |
* Obtiene la lista de puntos
|
227 |
* @return ArrayList
|
228 |
*/
|
229 |
public ArrayList getPointList() { |
230 |
return pointList;
|
231 |
} |
232 |
|
233 |
/*
|
234 |
* (non-Javadoc)
|
235 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
236 |
*/
|
237 |
public void mouseClicked(MouseEvent e) { |
238 |
} |
239 |
|
240 |
/*
|
241 |
* (non-Javadoc)
|
242 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
243 |
*/
|
244 |
public void mouseEntered(MouseEvent e) { |
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
250 |
*/
|
251 |
public void mouseExited(MouseEvent e) { |
252 |
} |
253 |
|
254 |
/*
|
255 |
* (non-Javadoc)
|
256 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
257 |
*/
|
258 |
public void mousePressed(MouseEvent e) { |
259 |
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* Cuando se suelta el bot?n del rat?n despu?s de haber arrastrado volvemos
|
264 |
* a dibujar el punto pero en la nueva ubicaci?n. Despu?s calculamos las nuevas
|
265 |
* coordenadas del punto y las asignamos. Finalmente desactivamos la operaci?n de
|
266 |
* mover punto y ponemos el punto sobre el que operamos a null.
|
267 |
*/
|
268 |
public void mouseReleased(MouseEvent e) { |
269 |
if(!isActive() || getOperation() != MOVE)
|
270 |
return;
|
271 |
operanteGP.setDraw(true);
|
272 |
operanteGP.setDrawCoords(e.getPoint()); |
273 |
Point2D point = canvas.viewCoordsToWorld(e.getPoint());
|
274 |
switch(type) {
|
275 |
case GPGraphic.MAP: operanteGP.getGeoPoint().mapPoint = point; break; |
276 |
case GPGraphic.PIXEL: operanteGP.getGeoPoint().pixelPoint = point; break; |
277 |
} |
278 |
canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
279 |
operanteGP = null;
|
280 |
setOperation(NONE); |
281 |
if(listener != null) |
282 |
listener.endAction(new ToolEvent(this)); |
283 |
} |
284 |
|
285 |
/*
|
286 |
* (non-Javadoc)
|
287 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
288 |
*/
|
289 |
public void mouseDragged(MouseEvent e) { |
290 |
if(!isActive())
|
291 |
return;
|
292 |
if(getOperation() == MOVE) {
|
293 |
operanteGP.setDraw(false);
|
294 |
if(operanteGP.getType() == GPGraphic.PIXEL && icoRed != null) |
295 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(icoRed, new Point(16, 16), "")); |
296 |
if(operanteGP.getType() == GPGraphic.MAP && icoBlue != null) |
297 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(icoBlue, new Point(16, 16), "")); |
298 |
} |
299 |
} |
300 |
|
301 |
/*
|
302 |
* (non-Javadoc)
|
303 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
304 |
*/
|
305 |
public void mouseMoved(MouseEvent e) { |
306 |
if(!isActive())
|
307 |
return;
|
308 |
setOperation(NONE); |
309 |
for (int i = 0; i < pointList.size(); i++) { |
310 |
GPGraphic gp = ((GPGraphic)pointList.get(i)); |
311 |
int pxLeft = (int)gp.getDrawCoords().getX() - 4; |
312 |
int pxRight = (int)gp.getDrawCoords().getX() + 4; |
313 |
int pyUp = (int)gp.getDrawCoords().getY() - 4; |
314 |
int pyDown = (int)gp.getDrawCoords().getY() + 4; |
315 |
if(e.getX() >= pxLeft && e.getX() <= pxRight && e.getY() >= pyUp && e.getY() <= pyDown) {
|
316 |
lastGP = operanteGP = gp; |
317 |
setOperation(MOVE); |
318 |
} |
319 |
|
320 |
} |
321 |
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* Obtiene la operaci?n sobre el cursor que hay seleccionada
|
326 |
* @return Entero que representa a la operaci?n
|
327 |
*/
|
328 |
private int getOperation() { |
329 |
return operation;
|
330 |
} |
331 |
|
332 |
/**
|
333 |
* Asigna la operaci?n sobre el cursor que hay seleccionada
|
334 |
* @param op
|
335 |
*/
|
336 |
private void setOperation(int op) { |
337 |
operation = op; |
338 |
if(op == NONE)
|
339 |
listener.offTool(new ToolEvent(this)); |
340 |
else
|
341 |
listener.onTool(new ToolEvent(this)); |
342 |
} |
343 |
|
344 |
/**
|
345 |
* Consulta si est? activo el evento de pinchado y arrastrado de los puntos de
|
346 |
* control.
|
347 |
* @return
|
348 |
*/
|
349 |
public boolean isActive() { |
350 |
return active;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* Asigna el flag que activa y desactiva el evento de pinchado y arrastrado
|
355 |
* de los puntos de control.
|
356 |
* @param activeEvent
|
357 |
*/
|
358 |
public void setActive(boolean active) { |
359 |
this.active = active;
|
360 |
} |
361 |
|
362 |
/**
|
363 |
* Desactiva la herramienta temporalmente. Guarda el estado en el que estaba
|
364 |
* para restaurarlo cuando se invoque a awake
|
365 |
*/
|
366 |
public void sleep() { |
367 |
sleepActive = active; |
368 |
active = false;
|
369 |
} |
370 |
|
371 |
/**
|
372 |
* Recupera el estado de activaci?n que ten?a antes de la ?ltima invocaci?n
|
373 |
* de sleep
|
374 |
*/
|
375 |
public void awake() { |
376 |
active = sleepActive; |
377 |
} |
378 |
|
379 |
/**
|
380 |
* Obtiene el ?ltimo punto arrastrado
|
381 |
* @return GPGraphic
|
382 |
*/
|
383 |
public GPGraphic getLastPoint() {
|
384 |
return lastGP;
|
385 |
} |
386 |
} |