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