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 @ 1730
History | View | Annotate | Download (9.3 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 org.gvsig.andami.IconThemeHelper; |
34 |
import org.gvsig.raster.georeferencing.swing.impl.view.CanvasZone; |
35 |
import org.gvsig.raster.georeferencing.swing.model.GCPList; |
36 |
import org.gvsig.raster.georeferencing.swing.view.GeoreferencingView; |
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 |
private GeoreferencingView view = null; |
67 |
|
68 |
/**
|
69 |
* Constructor. Asigna el tipo de punto a dibujar en la
|
70 |
* capa gr?fica.
|
71 |
* @param type El valor de type viene definido por las constantes de GPGraphic
|
72 |
* @param listener Listener para acciones de finalizaci?n de la operaci?n de mover punto
|
73 |
*/
|
74 |
public GCPsGraphicLayer(int type, ToolListener listener, GCPList pointList) { |
75 |
this.type = type;
|
76 |
this.pointList = pointList;
|
77 |
this.listener = listener;
|
78 |
try {
|
79 |
icoBlue = IconThemeHelper.getImage("crux_blue-icon");
|
80 |
icoRed = IconThemeHelper.getImage("crux_red-icon");
|
81 |
} catch (NullPointerException e) { |
82 |
|
83 |
} |
84 |
} |
85 |
|
86 |
/**
|
87 |
* Obtiene el identificador del punto requerido
|
88 |
* @param pos Posici?n del punto en la capa
|
89 |
* @return
|
90 |
*/
|
91 |
public long getID(int pos) { |
92 |
try {
|
93 |
return ((GPGraphic)pointList.getGraphicPoint(pos)).getId();
|
94 |
}catch (ArrayIndexOutOfBoundsException e) { |
95 |
return -1; |
96 |
} |
97 |
} |
98 |
|
99 |
public void setGeoreferencingView(GeoreferencingView view) { |
100 |
this.view = view;
|
101 |
this.canvas = (CanvasZone)view.getCanvas();
|
102 |
canvas.addMouseMotionListener(this);
|
103 |
canvas.addMouseListener(this);
|
104 |
} |
105 |
|
106 |
/*
|
107 |
* (non-Javadoc)
|
108 |
* @see org.gvsig.rastertools.georeferencing.ui.zoom.IGraphicLayer#draw(java.awt.Graphics2D, org.gvsig.raster.datastruct.Extent, int, int)
|
109 |
*/
|
110 |
public void draw(Graphics2D g, Rectangle2D ext, int w, int h) { |
111 |
for (int i = 0; i < pointList.size(); i++) |
112 |
((GPGraphic)pointList.getGraphicPoint(i)).draw(g, type, canvas); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Asigna el flag que muestra u oculta la etiqueta del punto
|
117 |
* @param showLabel true para mostrar la etiqueta y false para ocultarla
|
118 |
*/
|
119 |
public void setShowLabel(boolean showLabel) { |
120 |
for (int i = 0; i < pointList.size(); i++) |
121 |
((GPGraphic)pointList.getGraphicPoint(i)).setShowLabel(showLabel); |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Asigna el flag que muestra u oculta el n?mero del punto
|
126 |
* @param showLabel true para mostrar el punto y false para ocultarlo
|
127 |
*/
|
128 |
/*public void setShowNumber(boolean showNumber) {
|
129 |
for (int i = 0; i < pointList.size(); i++)
|
130 |
((GPGraphic)pointList.getGraphicPoint(i)).setShowNumber(showNumber);
|
131 |
}*/
|
132 |
|
133 |
/**
|
134 |
* Obtiene el punto de la posici?n indicada por el par?metro pos
|
135 |
* @param pos Posici?n del punto
|
136 |
* @return GPGraphic
|
137 |
*/
|
138 |
public GPGraphic getPoint(int pos) { |
139 |
try {
|
140 |
return ((GPGraphic)pointList.getGraphicPoint(pos));
|
141 |
} catch (IndexOutOfBoundsException e) { |
142 |
return null; |
143 |
} |
144 |
} |
145 |
|
146 |
/**
|
147 |
* Obtiene la lista de puntos
|
148 |
* @return ArrayList
|
149 |
*/
|
150 |
public GCPList getPointList() {
|
151 |
return pointList;
|
152 |
} |
153 |
|
154 |
/*
|
155 |
* (non-Javadoc)
|
156 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
157 |
*/
|
158 |
public void mouseClicked(MouseEvent e) { |
159 |
} |
160 |
|
161 |
/*
|
162 |
* (non-Javadoc)
|
163 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
164 |
*/
|
165 |
public void mouseEntered(MouseEvent e) { |
166 |
} |
167 |
|
168 |
/*
|
169 |
* (non-Javadoc)
|
170 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
171 |
*/
|
172 |
public void mouseExited(MouseEvent e) { |
173 |
} |
174 |
|
175 |
/*
|
176 |
* (non-Javadoc)
|
177 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
178 |
*/
|
179 |
public void mousePressed(MouseEvent e) { |
180 |
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Cuando se suelta el bot?n del rat?n despu?s de haber arrastrado volvemos
|
185 |
* a dibujar el punto pero en la nueva ubicaci?n. Finalmente desactivamos la operaci?n de
|
186 |
* mover punto y ponemos el punto sobre el que operamos a null.
|
187 |
*/
|
188 |
public void mouseReleased(MouseEvent e) { |
189 |
if(!isActive() || getOperation() != MOVE)
|
190 |
return;
|
191 |
operanteGP.setDraw(true);
|
192 |
Point2D point = canvas.viewCoordsToWorld(e.getPoint());
|
193 |
switch(type) {
|
194 |
case GPGraphic.MAP: operanteGP.getGeoPoint().setMapPoint(point); break; |
195 |
case GPGraphic.PIXEL: operanteGP.getGeoPoint().setPixelPoint(point); break; |
196 |
} |
197 |
canvas.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
198 |
operanteGP = null;
|
199 |
setOperation(NONE); |
200 |
if(listener != null) |
201 |
listener.endAction(new ToolEvent(this, view)); |
202 |
} |
203 |
|
204 |
/*
|
205 |
* (non-Javadoc)
|
206 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
207 |
*/
|
208 |
public void mouseDragged(MouseEvent e) { |
209 |
if(!isActive())
|
210 |
return;
|
211 |
if(getOperation() == MOVE) {
|
212 |
operanteGP.setDraw(false);
|
213 |
if(type == GPGraphic.PIXEL && icoRed != null) |
214 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(icoRed, new Point(16, 16), "")); |
215 |
if(type == GPGraphic.MAP && icoBlue != null) |
216 |
canvas.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(icoBlue, new Point(16, 16), "")); |
217 |
} |
218 |
} |
219 |
|
220 |
/*
|
221 |
* (non-Javadoc)
|
222 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
223 |
*/
|
224 |
public void mouseMoved(MouseEvent e) { |
225 |
if(!isActive())
|
226 |
return;
|
227 |
setOperation(NONE); |
228 |
for (int i = 0; i < pointList.size(); i++) { |
229 |
GPGraphic gp = ((GPGraphic)pointList.getGraphicPoint(i)); |
230 |
int pxLeft = 0, pxRight = 0, pyUp = 0, pyDown = 0; |
231 |
|
232 |
Point2D p1 = null; |
233 |
|
234 |
if(type == GPGraphic.PIXEL) {
|
235 |
p1 = gp.getGeoPoint().getPixelPoint(); |
236 |
} |
237 |
|
238 |
if(type == GPGraphic.MAP) {
|
239 |
p1 = gp.getGeoPoint().getMapPoint(); |
240 |
} |
241 |
|
242 |
if(p1 == null) |
243 |
return;
|
244 |
|
245 |
p1 = canvas.viewCoordsFromWorld(p1); |
246 |
pxLeft = (int)p1.getX() - 4; |
247 |
pxRight = (int)p1.getX() + 4; |
248 |
pyUp = (int)p1.getY() - 4; |
249 |
pyDown = (int)p1.getY() + 4; |
250 |
|
251 |
if(e.getX() >= pxLeft && e.getX() <= pxRight && e.getY() >= pyUp && e.getY() <= pyDown) {
|
252 |
lastGP = operanteGP = gp; |
253 |
getPointList().setSelectedPoint(i); |
254 |
setOperation(MOVE); |
255 |
} |
256 |
|
257 |
} |
258 |
|
259 |
} |
260 |
|
261 |
/**
|
262 |
* Obtiene la operaci?n sobre el cursor que hay seleccionada
|
263 |
* @return Entero que representa a la operaci?n
|
264 |
*/
|
265 |
private int getOperation() { |
266 |
return operation;
|
267 |
} |
268 |
|
269 |
/**
|
270 |
* Asigna la operaci?n sobre el cursor que hay seleccionada
|
271 |
* @param op
|
272 |
*/
|
273 |
private void setOperation(int op) { |
274 |
operation = op; |
275 |
if(op == NONE)
|
276 |
listener.offTool(new ToolEvent(this, view)); |
277 |
else
|
278 |
listener.onTool(new ToolEvent(this, view)); |
279 |
} |
280 |
|
281 |
/**
|
282 |
* Consulta si est? activo el evento de pinchado y arrastrado de los puntos de
|
283 |
* control.
|
284 |
* @return
|
285 |
*/
|
286 |
public boolean isActive() { |
287 |
return active;
|
288 |
} |
289 |
|
290 |
/**
|
291 |
* Asigna el flag que activa y desactiva el evento de pinchado y arrastrado
|
292 |
* de los puntos de control.
|
293 |
* @param activeEvent
|
294 |
*/
|
295 |
public void setActive(boolean active) { |
296 |
this.active = active;
|
297 |
} |
298 |
|
299 |
/**
|
300 |
* Desactiva la herramienta temporalmente. Guarda el estado en el que estaba
|
301 |
* para restaurarlo cuando se invoque a awake
|
302 |
*/
|
303 |
public void sleep() { |
304 |
sleepActive = active; |
305 |
active = false;
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* Recupera el estado de activaci?n que ten?a antes de la ?ltima invocaci?n
|
310 |
* de sleep
|
311 |
*/
|
312 |
public void awake() { |
313 |
active = sleepActive; |
314 |
} |
315 |
|
316 |
/**
|
317 |
* Obtiene el ?ltimo punto arrastrado
|
318 |
* @return GPGraphic
|
319 |
*/
|
320 |
public GPGraphic getLastPoint() {
|
321 |
return lastGP;
|
322 |
} |
323 |
} |