Revision 1475
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/view/CanvasZone.java | ||
---|---|---|
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.raster.georeferencing.swing.impl.view; |
|
20 |
|
|
21 |
import java.awt.Color; |
|
22 |
import java.awt.Graphics; |
|
23 |
import java.awt.Graphics2D; |
|
24 |
import java.awt.event.MouseEvent; |
|
25 |
import java.awt.event.MouseListener; |
|
26 |
import java.awt.event.MouseMotionListener; |
|
27 |
import java.awt.geom.Point2D; |
|
28 |
import java.awt.geom.Rectangle2D; |
|
29 |
import java.awt.image.BufferedImage; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.List; |
|
32 |
|
|
33 |
import javax.swing.JPanel; |
|
34 |
|
|
35 |
import org.gvsig.raster.georeferencing.swing.impl.tool.BaseViewTool; |
|
36 |
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer; |
|
37 |
import org.gvsig.raster.georeferencing.swing.view.ViewEvent; |
|
38 |
import org.gvsig.raster.georeferencing.swing.view.ViewListener; |
|
39 |
|
|
40 |
/** |
|
41 |
* Zona de dibujado del raster |
|
42 |
* 21/12/2007 |
|
43 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
44 |
*/ |
|
45 |
public class CanvasZone extends JPanel implements MouseListener, MouseMotionListener { |
|
46 |
private static final long serialVersionUID = 1308683333757367640L; |
|
47 |
|
|
48 |
private BufferedImage image = null; |
|
49 |
private double scale = 1; |
|
50 |
private Rectangle2D extent = null; |
|
51 |
private double pixelSize = 1; |
|
52 |
private Point2D center = null; |
|
53 |
private List<IGraphicLayer> graphicLayers = new ArrayList<IGraphicLayer>(); |
|
54 |
/** |
|
55 |
* ?ltimo extent aplicado. Si no ha variado el siguiente a aplicar no hace falta que releamos de nuevo |
|
56 |
*/ |
|
57 |
private Rectangle2D lastExtent = null; |
|
58 |
//Ultimo Image con la capa dibujada y con la transformaci?n que tiene en cuenta los desplazamientos dentro de un pixel |
|
59 |
//Este buffer no varia hasta que se hace la siguiente petici?n de setDrawParams |
|
60 |
private BufferedImage lastImage = null; |
|
61 |
//lastImage sobre el que se pintan las capas gr?ficas. Este buffer varia en cada repaint. El orden en el que se carga |
|
62 |
//es: se vuelva el lastImage, se pintan las capas gr?ficas, se pintan las tools. |
|
63 |
private BufferedImage lastImageWithLayers = null; |
|
64 |
|
|
65 |
private boolean clear = false; |
|
66 |
private BaseViewTool selectedTool = null; |
|
67 |
|
|
68 |
private Point2D realCoord = new Point2D.Double(0, 0); |
|
69 |
//private Point2D viewCoord = new Point2D.Double(0, 0); |
|
70 |
private boolean showInfo = false; |
|
71 |
/** |
|
72 |
* Informa de que la esquina superior izquierda corresponde con el valor de m?nimo X y |
|
73 |
* m?ximo Y. En caso de ser false esta esquina ser?a de m?nimo X y m?nimo Y. |
|
74 |
*/ |
|
75 |
private boolean minxMaxyUL = true; |
|
76 |
|
|
77 |
private Color backgroundColor = Color.BLACK; |
|
78 |
private Color textColor = Color.RED; |
|
79 |
|
|
80 |
/** |
|
81 |
* Normalmente no se hace una petici?n al dibujado del raster si el extent no ha variado. Si esta variable |
|
82 |
* est? a true fuerza que haya una petici?n de redibujado aunque el extent del raster no haya cambiado. |
|
83 |
* Esto solo se hace para una petici?n. La siguiente vuelve a estar a false. |
|
84 |
*/ |
|
85 |
private boolean forceRequest = false; |
|
86 |
|
|
87 |
private ViewListener viewListener = null; |
|
88 |
/** |
|
89 |
* Creamos una sola instancia de Event para no tener que malgastar recursos |
|
90 |
* creando una en cada dibujado |
|
91 |
*/ |
|
92 |
private ViewEvent viewEvent = null; |
|
93 |
|
|
94 |
/** |
|
95 |
* Asigna los par?metros de dibujado |
|
96 |
* @param img Buffer con un ?rea de datos |
|
97 |
* @param ext Rectangle2D del ?rea de datos dada |
|
98 |
* @param pixelSize Tama?o de pixel |
|
99 |
* @param center Punto del ?rea de datos donde se quiere centrar el dibujado del buffer |
|
100 |
*/ |
|
101 |
public void setDrawParams(BufferedImage img, Rectangle2D ext, double pixelSize, Point2D center) { |
|
102 |
this.image = img; |
|
103 |
this.extent = ext; |
|
104 |
this.pixelSize = pixelSize; |
|
105 |
this.center = center; |
|
106 |
this.addMouseListener(this); |
|
107 |
this.addMouseMotionListener(this); |
|
108 |
repaint(); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* Asigna el listener de eventos de la vista |
|
113 |
* @param listener |
|
114 |
*/ |
|
115 |
public void setViewListener(ViewListener listener) { |
|
116 |
this.viewListener = listener; |
|
117 |
viewEvent = new ViewEvent(this); |
|
118 |
} |
|
119 |
|
|
120 |
/** |
|
121 |
* Asigna un nuevo centro de visualizaci?n |
|
122 |
* @param center |
|
123 |
*/ |
|
124 |
public void setCenter(Point2D center) { |
|
125 |
this.center = center; |
|
126 |
repaint(); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Conversi?n de un punto en coordenadas del canvas a reales |
|
131 |
* @param p |
|
132 |
* @return |
|
133 |
*/ |
|
134 |
public Point2D viewCoordsToWorld(Point2D p) { |
|
135 |
int w = getVisibleRect().width; |
|
136 |
int h = getVisibleRect().height; |
|
137 |
double cx = extent.getMinX() + ((p.getX() * extent.getWidth()) / w); |
|
138 |
double cy = 0; |
|
139 |
if(minxMaxyUL) //Cuando las Y decrecen de arriba a abajo |
|
140 |
cy = extent.getMaxY() - (p.getY() * extent.getHeight()) / h; |
|
141 |
else //Cuando las Y crecen de arriba a abajo |
|
142 |
cy = extent.getMinY() + (p.getY() * extent.getHeight()) / h; |
|
143 |
return new Point2D.Double(cx, cy); |
|
144 |
} |
|
145 |
|
|
146 |
/** |
|
147 |
* Conversi?n de un punto en coordenadas del canvas a reales |
|
148 |
* @param p |
|
149 |
* @return |
|
150 |
*/ |
|
151 |
public Point2D viewCoordsFromWorld(Point2D p) { |
|
152 |
int w = getVisibleRect().width; |
|
153 |
int h = getVisibleRect().height; |
|
154 |
double cx = ((p.getX() - extent.getMinX()) * w) / extent.getWidth(); |
|
155 |
double cy = 0; |
|
156 |
if(minxMaxyUL) //Cuando las Y decrecen de arriba a abajo |
|
157 |
cy = ((extent.getMaxY() - p.getY()) * h) / extent.getHeight(); |
|
158 |
else //Cuando las Y crecen de arriba a abajo |
|
159 |
cy = ((p.getY() - extent.getMinY()) * h) / extent.getHeight(); |
|
160 |
return new Point2D.Double(cx, cy); |
|
161 |
} |
|
162 |
|
|
163 |
/** |
|
164 |
* Obtiene el extent del canvas en coordenadas del mundo real |
|
165 |
* @return Rectangle2D |
|
166 |
*/ |
|
167 |
public Rectangle2D getExtent() { |
|
168 |
if(lastExtent == null) |
|
169 |
return extent; |
|
170 |
return lastExtent; |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* Asigna un nuevo centro de visualizaci?n en coordenadas del |
|
175 |
* componente. |
|
176 |
* @param center |
|
177 |
*/ |
|
178 |
public void setViewCenter(Point2D c) { |
|
179 |
int w = getVisibleRect().width; |
|
180 |
int h = getVisibleRect().height; |
|
181 |
|
|
182 |
double cx = (c.getX() * lastExtent.getWidth()) / w; |
|
183 |
double cy = (c.getY() * lastExtent.getHeight()) / h; |
|
184 |
setPixelCenter(new Point2D.Double(cx, cy)); |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel |
|
189 |
* del ?rea de dibujado (canvas). El nuevo centro ser? calculado en coordenadas |
|
190 |
* del mapa. |
|
191 |
* @param center |
|
192 |
*/ |
|
193 |
public void setPixelCenter(Point2D c) { |
|
194 |
int w = getVisibleRect().width; |
|
195 |
int h = getVisibleRect().height; |
|
196 |
|
|
197 |
//Calculamos el extent del canvas |
|
198 |
Rectangle2D ext = getCanvasExtent(w, h, scale); |
|
199 |
|
|
200 |
//Calculamos el nuevo centro en coordenadas reales |
|
201 |
double wWC = (c.getX() / scale) * pixelSize; |
|
202 |
double hWC = (c.getY() / scale) * pixelSize; |
|
203 |
this.center = new Point2D.Double(ext.getMinX() + wWC, |
|
204 |
ext.getMinY() - hWC); |
|
205 |
repaint(); |
|
206 |
} |
|
207 |
|
|
208 |
/** |
|
209 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel. Esta llamada tiene |
|
210 |
* en cuenta solo p?xeles completos. No centra sobre porciones de pixel cuando el zoom es |
|
211 |
* mayor de 1:1. El nuevo centro es en coordenadas del mapa pero siempre centrar? |
|
212 |
* en la esquina inferior izquierda del pixel. |
|
213 |
* @param center |
|
214 |
*/ |
|
215 |
public void setPixelCenter(int x, int y) { |
|
216 |
int w = getVisibleRect().width; |
|
217 |
int h = getVisibleRect().height; |
|
218 |
|
|
219 |
//Calculamos el extent del canvas |
|
220 |
Rectangle2D ext = getCanvasExtent(w, h, scale); |
|
221 |
|
|
222 |
//Calculamos el nuevo centro en coordenadas reales |
|
223 |
double wWC = (x / scale) * pixelSize; |
|
224 |
double hWC = (y / scale) * pixelSize; |
|
225 |
Point2D center = new Point2D.Double(ext.getMinX() + wWC, |
|
226 |
ext.getMinY() - hWC); |
|
227 |
|
|
228 |
//Calculamos la coordena pixel a la que pertenece esa coordenada real |
|
229 |
int pxX = (int)((center.getX() * (w / scale)) / ext.getWidth()); |
|
230 |
int pxY = (int)((center.getY() * (h / scale)) / ext.getHeight()); |
|
231 |
|
|
232 |
//Despu?s de haber convertido a pixel y redondeado la coordenada a entero volvemos a convertirla en real |
|
233 |
double wcX = (pxX * ext.getWidth()) / (w / scale); |
|
234 |
double wcY = (pxY * ext.getHeight()) / (h / scale); |
|
235 |
|
|
236 |
this.center = new Point2D.Double(wcX, wcY); |
|
237 |
repaint(); |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Asigna una capa gr?fica |
|
242 |
* @param layer IGraphicLayer |
|
243 |
*/ |
|
244 |
public void setGraphicLayer(IGraphicLayer layer) { |
|
245 |
graphicLayers.add(layer); |
|
246 |
} |
|
247 |
|
|
248 |
/** |
|
249 |
* Obtiene la lista de capas gr?ficas |
|
250 |
* @return |
|
251 |
*/ |
|
252 |
public List<IGraphicLayer> getGraphicLayers() { |
|
253 |
return graphicLayers; |
|
254 |
} |
|
255 |
|
|
256 |
/** |
|
257 |
* Asigna la escala para el nuevo zoom |
|
258 |
* @param scale |
|
259 |
*/ |
|
260 |
public void setZoom(double scale) { |
|
261 |
this.scale = scale; |
|
262 |
repaint(); |
|
263 |
} |
|
264 |
|
|
265 |
/** |
|
266 |
* Obtiene la escala aplicada en el dibujado |
|
267 |
* @return double |
|
268 |
*/ |
|
269 |
public double getZoom() { |
|
270 |
return scale; |
|
271 |
} |
|
272 |
|
|
273 |
/** |
|
274 |
* Obtiene el extent actual asignado al canvas |
|
275 |
* @return Rectangle2D |
|
276 |
*/ |
|
277 |
public Rectangle2D getCanvasExtent() { |
|
278 |
return lastExtent; |
|
279 |
} |
|
280 |
|
|
281 |
/** |
|
282 |
* Asigna el extent del canvas |
|
283 |
* @param r |
|
284 |
*/ |
|
285 |
public void setCanvasExtent(Rectangle2D r) { |
|
286 |
this.lastExtent = r; |
|
287 |
} |
|
288 |
|
|
289 |
/** |
|
290 |
* Obtiene el tama?o de pixel |
|
291 |
* @return double |
|
292 |
*/ |
|
293 |
public double getPixelSize() { |
|
294 |
return pixelSize; |
|
295 |
} |
|
296 |
|
|
297 |
/** |
|
298 |
* Obtiene el centro del canvas |
|
299 |
* @return Point2D |
|
300 |
*/ |
|
301 |
public Point2D getCenter() { |
|
302 |
return center; |
|
303 |
} |
|
304 |
|
|
305 |
/** |
|
306 |
* Obtiene el buffer de la vista activa y lo dibuja sobre el panel |
|
307 |
* con los datos de escala y desplazamiento seleccionados. |
|
308 |
*/ |
|
309 |
protected void paintComponent(Graphics g) { |
|
310 |
if(image == null) |
|
311 |
return; |
|
312 |
|
|
313 |
if(viewListener != null) |
|
314 |
viewListener.startDraw(viewEvent); |
|
315 |
|
|
316 |
int w = getVisibleRect().width; |
|
317 |
int h = getVisibleRect().height; |
|
318 |
Rectangle2D ext = getCanvasExtent(w, h, scale); |
|
319 |
|
|
320 |
if(lastImage == null || !equal(lastExtent, ext)) { |
|
321 |
lastImage = new BufferedImage((int)w, (int)h, BufferedImage.TYPE_INT_RGB); |
|
322 |
lastImageWithLayers = new BufferedImage((int)w, (int)h, BufferedImage.TYPE_INT_RGB); |
|
323 |
} |
|
324 |
|
|
325 |
if(clear) { |
|
326 |
g.setColor(backgroundColor); |
|
327 |
g.fillRect(0, 0, w, h); |
|
328 |
return; |
|
329 |
} |
|
330 |
|
|
331 |
//Dibujamos el buffer sobre el grafics |
|
332 |
Graphics graphicsDB = draw(ext, w, h); |
|
333 |
|
|
334 |
//Dibujamos todas las capas registradas |
|
335 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
336 |
((IGraphicLayer)graphicLayers.get(i)).draw((Graphics2D)graphicsDB, ext, w, h); |
|
337 |
|
|
338 |
lastExtent = ext; |
|
339 |
|
|
340 |
if(selectedTool != null) |
|
341 |
selectedTool.draw(graphicsDB); |
|
342 |
|
|
343 |
if(showInfo) |
|
344 |
showInfo(graphicsDB); |
|
345 |
|
|
346 |
g.drawImage(lastImageWithLayers, 0, 0, this); |
|
347 |
graphicsDB.dispose(); |
|
348 |
|
|
349 |
if(viewListener != null) |
|
350 |
viewListener.endDraw(viewEvent); |
|
351 |
} |
|
352 |
|
|
353 |
/** |
|
354 |
* Muestra informaci?n sobre la vista |
|
355 |
* @param g |
|
356 |
*/ |
|
357 |
private void showInfo(Graphics g) { |
|
358 |
g.setColor(textColor); |
|
359 |
g.drawString("X: " + clipDecimals(realCoord.getX(), 3), 12, 18); |
|
360 |
g.drawString("Y: " + clipDecimals(realCoord.getY(), 3), 12, 32); |
|
361 |
//g.drawString("X': " + MathUtils.tailDecimals(viewCoord.getX(), 3), 12, 46); |
|
362 |
//g.drawString("Y': " + MathUtils.tailDecimals(viewCoord.getY(), 3), 12, 60); |
|
363 |
} |
|
364 |
|
|
365 |
public double clipDecimals(double num, int n) { |
|
366 |
long m = (long) Math.pow(10, n); |
|
367 |
long aux = Math.round(num * m); |
|
368 |
return (double) aux / (double) m; |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* Compara dos extents y devuelve true si son iguales y false si son distintos |
|
373 |
* @param e1 Extent 1 |
|
374 |
* @param e2 Extent 2 |
|
375 |
* @return true si son iguales y false si son diferentes |
|
376 |
*/ |
|
377 |
private boolean equal(Rectangle2D e1, Rectangle2D e2) { |
|
378 |
return (e1 != null && e2 != null && e1.getMinX() == e2.getMinX() && e1.getMinY() == e2.getMinY() |
|
379 |
&& e1.getMaxX() == e2.getMaxX() && e1.getMaxY() == e2.getMaxY()); |
|
380 |
} |
|
381 |
|
|
382 |
/** |
|
383 |
* Obtiene el Extent del canvas. Este canvas tiene un ancho en pixeles |
|
384 |
* de w y un alto de h. Tiene en cuenta la escala a la que se quiere dibujar |
|
385 |
* para devolver el extent cuando el zoom ya est? aplicado. |
|
386 |
* @param w Ancho del canvas en p?xeles |
|
387 |
* @param h Alto del canvas en p?xeles |
|
388 |
* @return Rectangle2D |
|
389 |
*/ |
|
390 |
private Rectangle2D getCanvasExtent(double w, double h, double scale) { |
|
391 |
double tW = ((w / scale) / 2) * pixelSize; |
|
392 |
double tH = ((h / scale) / 2) * pixelSize; |
|
393 |
double minX = center.getX() - tW; |
|
394 |
double minY = center.getY() - tH; |
|
395 |
double width = Math.abs((center.getX() + tW) - minX); |
|
396 |
double height = Math.abs((center.getY() + tH) - minY); |
|
397 |
return new Rectangle2D.Double(minX, minY, width, height); |
|
398 |
} |
|
399 |
|
|
400 |
/** |
|
401 |
* <P> |
|
402 |
* Dibujado del buffer de datos sobre el Graphics. |
|
403 |
* </P><P> |
|
404 |
* No podemos aplicar un escalado al |
|
405 |
* Graphics y dibujar porque cuando el zoom es mayor a 1 los pixeles no empiezan a dibujarse |
|
406 |
* siempre en la esquina superior izquierda y al Graphics solo podemos ordenarle el dibujado |
|
407 |
* en coordenadas enteras. Para solucionarlo debemos copiar el trozo de buffer a dibujar teniendo |
|
408 |
* en cuenta el desplazamiento de la esquina superior izquierda de un pixel. |
|
409 |
* </P> |
|
410 |
* @param g |
|
411 |
* @param ext |
|
412 |
* @param w |
|
413 |
* @param h |
|
414 |
*/ |
|
415 |
private Graphics draw(Rectangle2D ext, double w, double h) { |
|
416 |
if(!equal(lastExtent, ext) || forceRequest) { |
|
417 |
//Hallamos la coordenada pixel del buffer de la esquina superior izquierda del extent |
|
418 |
double pxX = ((ext.getMinX() - extent.getMinX()) * (w / scale)) / ext.getWidth(); |
|
419 |
double pxY = ((extent.getMinY() - ext.getMinY()) * (h / scale)) / ext.getHeight(); |
|
420 |
|
|
421 |
//Creamos el buffer y lo cargamos teniendo en cuenta el desplazamiento inicial |
|
422 |
double step = 1 / scale; |
|
423 |
|
|
424 |
double xValue = pxX; |
|
425 |
double yValue = pxY; |
|
426 |
|
|
427 |
for (int i = 0; i < w; i++) { |
|
428 |
yValue = pxY; |
|
429 |
for (int j = 0; j < h; j++) { |
|
430 |
if((int)xValue >= 0 && (int)yValue >= 0 && (int)xValue < image.getWidth() && (int)yValue < image.getHeight()) { |
|
431 |
lastImage.setRGB(i, j, image.getRGB((int)xValue, (int)yValue)); |
|
432 |
lastImageWithLayers.setRGB(i, j, image.getRGB((int)xValue, (int)yValue)); |
|
433 |
} else { |
|
434 |
lastImage.setRGB(i, j, 0xffffffff); |
|
435 |
lastImageWithLayers.setRGB(i, j, 0xffffffff); |
|
436 |
} |
|
437 |
yValue += step; |
|
438 |
} |
|
439 |
xValue += step; |
|
440 |
} |
|
441 |
if(viewListener != null) |
|
442 |
viewListener.zoomViewChanged(viewEvent); |
|
443 |
forceRequest = false; |
|
444 |
} else |
|
445 |
((Graphics2D)lastImageWithLayers.getGraphics()).drawImage(lastImage, 0, 0, null); |
|
446 |
|
|
447 |
return lastImageWithLayers.getGraphics(); |
|
448 |
} |
|
449 |
|
|
450 |
/* |
|
451 |
* (non-Javadoc) |
|
452 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) |
|
453 |
*/ |
|
454 |
public void mouseClicked(MouseEvent e) { |
|
455 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
456 |
((IGraphicLayer)graphicLayers.get(i)).mouseClicked(e); |
|
457 |
repaint(); |
|
458 |
} |
|
459 |
|
|
460 |
/* |
|
461 |
* (non-Javadoc) |
|
462 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) |
|
463 |
*/ |
|
464 |
public void mouseEntered(MouseEvent e) { |
|
465 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
466 |
((IGraphicLayer)graphicLayers.get(i)).mouseEntered(e); |
|
467 |
repaint(); |
|
468 |
} |
|
469 |
|
|
470 |
/* |
|
471 |
* (non-Javadoc) |
|
472 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) |
|
473 |
*/ |
|
474 |
public void mouseExited(MouseEvent e) { |
|
475 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
476 |
((IGraphicLayer)graphicLayers.get(i)).mouseExited(e); |
|
477 |
repaint(); |
|
478 |
} |
|
479 |
|
|
480 |
/* |
|
481 |
* (non-Javadoc) |
|
482 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) |
|
483 |
*/ |
|
484 |
public void mousePressed(MouseEvent e) { |
|
485 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
486 |
((IGraphicLayer)graphicLayers.get(i)).mousePressed(e); |
|
487 |
repaint(); |
|
488 |
} |
|
489 |
|
|
490 |
/* |
|
491 |
* (non-Javadoc) |
|
492 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) |
|
493 |
*/ |
|
494 |
public void mouseReleased(MouseEvent e) { |
|
495 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
496 |
((IGraphicLayer)graphicLayers.get(i)).mouseReleased(e); |
|
497 |
repaint(); |
|
498 |
} |
|
499 |
|
|
500 |
/* |
|
501 |
* (non-Javadoc) |
|
502 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent) |
|
503 |
*/ |
|
504 |
public void mouseDragged(MouseEvent e) { |
|
505 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
506 |
((IGraphicLayer)graphicLayers.get(i)).mouseDragged(e); |
|
507 |
repaint(); |
|
508 |
} |
|
509 |
|
|
510 |
/* |
|
511 |
* (non-Javadoc) |
|
512 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent) |
|
513 |
*/ |
|
514 |
public void mouseMoved(MouseEvent e) { |
|
515 |
realCoord = viewCoordsToWorld((Point2D)e.getPoint()); |
|
516 |
//viewCoord = e.getPoint(); |
|
517 |
for (int i = 0; i < graphicLayers.size(); i++) |
|
518 |
((IGraphicLayer)graphicLayers.get(i)).mouseMoved(e); |
|
519 |
repaint(); |
|
520 |
} |
|
521 |
|
|
522 |
/** |
|
523 |
* Asigna la tool seleccionada |
|
524 |
* @param selectedTool |
|
525 |
*/ |
|
526 |
public void setSelectedTool(BaseViewTool selectedTool) { |
|
527 |
this.selectedTool = selectedTool; |
|
528 |
} |
|
529 |
|
|
530 |
/** |
|
531 |
* Obtiene la herramienta seleccionada |
|
532 |
* @return BaseViewTool |
|
533 |
*/ |
|
534 |
public BaseViewTool getSelectedTool() { |
|
535 |
return selectedTool; |
|
536 |
} |
|
537 |
|
|
538 |
/** |
|
539 |
* Activa o desactiva el mostrado de informaci?n |
|
540 |
* @param showInfo |
|
541 |
*/ |
|
542 |
public void setShowInfo(boolean showInfo) { |
|
543 |
this.showInfo = showInfo; |
|
544 |
} |
|
545 |
|
|
546 |
/** |
|
547 |
* Asigna el valor para el flag minxMaxyUL. Este flag informa de que la esquina |
|
548 |
* superior izquierda corresponde con el valor de m?nimo X y m?ximo Y. En caso |
|
549 |
* de ser false esta esquina ser?a de m?nimo X y m?nimo Y. |
|
550 |
* @param v |
|
551 |
*/ |
|
552 |
public void setMinxMaxyUL(boolean v) { |
|
553 |
this.minxMaxyUL = v; |
|
554 |
} |
|
555 |
|
|
556 |
/** |
|
557 |
* Obtiene el valor para el flag minxMaxyUL. Este flag informa de que la esquina |
|
558 |
* superior izquierda corresponde con el valor de m?nimo X y m?ximo Y. En caso |
|
559 |
* de ser false esta esquina ser?a de m?nimo X y m?nimo Y. |
|
560 |
* @param v |
|
561 |
*/ |
|
562 |
public boolean getMinxMaxyUL() { |
|
563 |
return minxMaxyUL; |
|
564 |
} |
|
565 |
|
|
566 |
/** |
|
567 |
* Asigna el color del texto |
|
568 |
* @param textColor |
|
569 |
*/ |
|
570 |
public void setTextColor(Color textColor) { |
|
571 |
this.textColor = textColor; |
|
572 |
repaint(); |
|
573 |
} |
|
574 |
|
|
575 |
/** |
|
576 |
* Obtiene el color del texto |
|
577 |
* @return |
|
578 |
*/ |
|
579 |
public Color getTextColor() { |
|
580 |
return textColor; |
|
581 |
} |
|
582 |
|
|
583 |
/** |
|
584 |
* Obtiene el color de fondo |
|
585 |
* @return |
|
586 |
*/ |
|
587 |
public Color getBackgroundColor() { |
|
588 |
return backgroundColor; |
|
589 |
} |
|
590 |
|
|
591 |
/** |
|
592 |
* Asigna el color de fondo |
|
593 |
* @param backgroundColor |
|
594 |
*/ |
|
595 |
public void setBackgroundColor(Color backgroundColor) { |
|
596 |
this.backgroundColor = backgroundColor; |
|
597 |
} |
|
598 |
|
|
599 |
/** |
|
600 |
* Normalmente no se hace una petici?n al dibujado del raster si el extent no ha variado. Si esta variable |
|
601 |
* est? a true fuerza que haya una petici?n de redibujado aunque el extent del raster no haya cambiado. |
|
602 |
* Esto solo se hace para una petici?n. La siguiente vuelve a estar a false. |
|
603 |
* @return |
|
604 |
*/ |
|
605 |
public boolean isForceRequest() { |
|
606 |
return forceRequest; |
|
607 |
} |
|
608 |
|
|
609 |
/** |
|
610 |
* Normalmente no se hace una petici?n al dibujado del raster si el extent no ha variado. Si esta variable |
|
611 |
* est? a true fuerza que haya una petici?n de redibujado aunque el extent del raster no haya cambiado. |
|
612 |
* Esto solo se hace para una petici?n. La siguiente vuelve a estar a false. |
|
613 |
* @param forceRequest |
|
614 |
*/ |
|
615 |
public void setForceRequest(boolean forceRequest) { |
|
616 |
this.forceRequest = forceRequest; |
|
617 |
} |
|
618 |
} |
|
0 | 619 |
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/view/ViewRecord.java | ||
---|---|---|
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.raster.georeferencing.swing.impl.view; |
|
20 |
|
|
21 |
import java.util.ArrayList; |
|
22 |
import java.util.List; |
|
23 |
|
|
24 |
/** |
|
25 |
* Historial de peticiones para poder recuperar las anteriores a |
|
26 |
* la actual. |
|
27 |
* |
|
28 |
* 11/01/2008 |
|
29 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
|
30 |
*/ |
|
31 |
public class ViewRecord { |
|
32 |
|
|
33 |
private List<Object> record = new ArrayList<Object>(); |
|
34 |
private int pos = -1; |
|
35 |
|
|
36 |
/** |
|
37 |
* Asigna la petici?n siguiente |
|
38 |
* @param request |
|
39 |
*/ |
|
40 |
public void setRequest(Object request) { |
|
41 |
record.add(++ pos, request); |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* Obtiene la petici?n anterior |
|
46 |
* @return |
|
47 |
*/ |
|
48 |
public Object getRequest() { |
|
49 |
if(pos > 0) { |
|
50 |
pos --; |
|
51 |
return record.get(pos); |
|
52 |
} else if(pos == 0) |
|
53 |
return record.get(pos); |
|
54 |
return null; |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
58 |
* Vacia el historial |
|
59 |
*/ |
|
60 |
public void clear() { |
|
61 |
record.clear(); |
|
62 |
} |
|
63 |
} |
|
0 | 64 |
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/view/BaseZoomView.java | ||
---|---|---|
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.raster.georeferencing.swing.impl.view; |
|
20 |
|
|
21 |
import java.awt.Graphics2D; |
|
22 |
import java.awt.geom.Point2D; |
|
23 |
import java.awt.geom.Rectangle2D; |
|
24 |
import java.awt.image.BufferedImage; |
|
25 |
|
|
26 |
import javax.swing.JPanel; |
|
27 |
|
|
28 |
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer; |
|
29 |
|
|
30 |
/** |
|
31 |
* Clase de la que heredan los dialogos de vista y zoom que utilizan |
|
32 |
* el ViewControl. Contiene m?todos generales para el acceso a informaci?n |
|
33 |
* del control. |
|
34 |
* |
|
35 |
* 20/01/2008 |
|
36 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
37 |
*/ |
|
38 |
public abstract class BaseZoomView extends JPanel { |
|
39 |
private static final long serialVersionUID = -8041978144045965049L; |
|
40 |
|
|
41 |
protected ViewControl zoomPixelControl = null; |
|
42 |
|
|
43 |
/** |
|
44 |
* Obtiene el graphics del canvas |
|
45 |
* @return |
|
46 |
*/ |
|
47 |
public Graphics2D getCanvasGraphic() { |
|
48 |
return (Graphics2D) zoomPixelControl.getCanvas().getGraphics(); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Obtiene el ancho del canvas |
|
53 |
* @return |
|
54 |
*/ |
|
55 |
public int getCanvasWidth() { |
|
56 |
return zoomPixelControl.getCanvasWith(); |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Obtiene el alto del canvas |
|
61 |
* @return |
|
62 |
*/ |
|
63 |
public int getCanvasHeight() { |
|
64 |
return zoomPixelControl.getCanvasHeight(); |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Asigna los par?metros de dibujado para el raster |
|
69 |
* @param img Buffer con un ?rea de datos |
|
70 |
* @param ext Rectangle2D del ?rea de datos dada |
|
71 |
* @param pixelSize Tama?o de pixel |
|
72 |
* @param center Punto del ?rea de datos donde se quiere centrar el dibujado del buffer |
|
73 |
*/ |
|
74 |
public abstract void setDrawParams(BufferedImage img, Rectangle2D ext, double pixelSize, Point2D center); |
|
75 |
|
|
76 |
/** |
|
77 |
* A?ade una capa gr?fica al canvas. Esta debe heredar de IGraphicLayer |
|
78 |
* para que pueda ser dibujada sobre el mismo. |
|
79 |
* @param gl |
|
80 |
*/ |
|
81 |
public abstract void addGraphicLayer(IGraphicLayer graphicLayer); |
|
82 |
|
|
83 |
/** |
|
84 |
* Obtiene el panel de control de la vista |
|
85 |
* @return |
|
86 |
*/ |
|
87 |
public abstract ViewControl getControl(); |
|
88 |
|
|
89 |
/** |
|
90 |
* Obtiene el canvas asociado |
|
91 |
* @return CanvasZone |
|
92 |
*/ |
|
93 |
public CanvasZone getCanvas() { |
|
94 |
return zoomPixelControl.getCanvas(); |
|
95 |
} |
|
96 |
} |
|
0 | 97 |
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/view/ViewControl.java | ||
---|---|---|
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.raster.georeferencing.swing.impl.view; |
|
20 |
|
|
21 |
import java.awt.BorderLayout; |
|
22 |
import java.awt.GridBagConstraints; |
|
23 |
import java.awt.GridBagLayout; |
|
24 |
import java.awt.Insets; |
|
25 |
import java.awt.event.ActionEvent; |
|
26 |
import java.awt.event.ActionListener; |
|
27 |
import java.awt.geom.Point2D; |
|
28 |
import java.awt.geom.Rectangle2D; |
|
29 |
import java.awt.image.BufferedImage; |
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.List; |
|
32 |
|
|
33 |
import javax.swing.JButton; |
|
34 |
import javax.swing.JPanel; |
|
35 |
import javax.swing.JToggleButton; |
|
36 |
|
|
37 |
import org.gvsig.andami.IconThemeHelper; |
|
38 |
import org.gvsig.i18n.Messages; |
|
39 |
import org.gvsig.raster.georeferencing.swing.exception.InvalidRequestException; |
|
40 |
import org.gvsig.raster.georeferencing.swing.impl.GeoreferencingSwingImplLibrary; |
|
41 |
import org.gvsig.raster.georeferencing.swing.impl.layer.ZoomCursorGraphicLayer; |
|
42 |
import org.gvsig.raster.georeferencing.swing.impl.tool.BaseViewTool; |
|
43 |
import org.gvsig.raster.georeferencing.swing.impl.tool.PanTool; |
|
44 |
import org.gvsig.raster.georeferencing.swing.impl.tool.ZoomRectangleTool; |
|
45 |
import org.gvsig.raster.georeferencing.swing.view.IExtensionRequest; |
|
46 |
import org.gvsig.raster.georeferencing.swing.view.IGraphicLayer; |
|
47 |
import org.gvsig.raster.georeferencing.swing.view.ToolEvent; |
|
48 |
import org.gvsig.raster.georeferencing.swing.view.ToolListener; |
|
49 |
import org.gvsig.raster.georeferencing.swing.view.ViewEvent; |
|
50 |
import org.gvsig.raster.georeferencing.swing.view.ViewListener; |
|
51 |
|
|
52 |
/** |
|
53 |
* <P> |
|
54 |
* Control de zoom. Muestra una imagen contenida en un buffer con controles de aumento |
|
55 |
* y disminuci?n del zoom de la misma. |
|
56 |
* </P><P> |
|
57 |
* El visualizador mostrar? el buffer de datos pasado en <code>setDrawParams</code>. |
|
58 |
* En esta llamada asignamos tambi?n el Extent del ?rea de datos, tama?o de pixel y punto |
|
59 |
* del buffer que coincidir? con el centro del control de zoom. De esta forma la imagen |
|
60 |
* visualizada se centra sobre este punto. En caso de no tener informaci?n geogr?fica del |
|
61 |
* buffer de datos a visualizar el Extent coincidir? con su tama?o en p?xeles y el tama?o |
|
62 |
* de pixel ser? de 1. El punto donde queramos centrar en este caso se dar? tambi?n en |
|
63 |
* coordenadas pixel. |
|
64 |
* </P><P> |
|
65 |
* Hay dos usos posibles en este control al accionar el bot?n de zoom: el primero de ellos, en |
|
66 |
* caso de que no haya ning?n listener del control de zoom registrado. En este caso se |
|
67 |
* aplicar? un escalado sobre los datos del buffer pasado por par?metro. En el caso de que |
|
68 |
* haya un listener registrado (IExtensionBuffer) no se aplicar? un escalado sobre el buffer |
|
69 |
* sino que se aplicar? una escala de 1.0 y se ejecutar? el m?todo request del interfaz |
|
70 |
* registrado para que el cliente reciba notificaci?n del nuevo extent. |
|
71 |
* </P> |
|
72 |
* <P> |
|
73 |
* En la inicializaci?n podemos solicitar que los controles de zoom est?n a la izquierda, |
|
74 |
* a la derecha o no esten. |
|
75 |
* </p> |
|
76 |
* |
|
77 |
* 21/12/2007 |
|
78 |
* @author Nacho Brodin nachobrodin@gmail.com |
|
79 |
*/ |
|
80 |
public class ViewControl extends JPanel implements ActionListener, ToolListener { |
|
81 |
private static final long serialVersionUID = 1L; |
|
82 |
//Lista de controles |
|
83 |
public static final int ZOOM_INCREASE = 0; |
|
84 |
public static final int ZOOM_DECREASE = 1; |
|
85 |
public static final int SELECT_ZOOM_AREA = 2; |
|
86 |
public static final int FULL_VIEW = 3; |
|
87 |
public static final int PREV_ZOOM = 4; |
|
88 |
public static final int PAN = 5; |
|
89 |
public static final int LOCATOR = 6; |
|
90 |
|
|
91 |
public static final int RIGHT_CONTROL = 1; |
|
92 |
public static final int LEFT_CONTROL = 2; |
|
93 |
public static final int NOCONTROL = 0; |
|
94 |
|
|
95 |
private final double SCALE = 0.5; |
|
96 |
|
|
97 |
private int control; |
|
98 |
private CanvasZone canvas = null; |
|
99 |
private JPanel buttons = null; |
|
100 |
private JButton bZoomMas = null; |
|
101 |
private JButton bZoomMenos = null; |
|
102 |
private JToggleButton bSelectZoomArea = null; |
|
103 |
private JToggleButton bMove = null; |
|
104 |
private JButton bFullView = null; |
|
105 |
private JButton bPrevZoom = null; |
|
106 |
private JButton bCenterLocator = null; |
|
107 |
private double stepScale = 2; |
|
108 |
private IExtensionRequest extReq = null; |
|
109 |
private ViewRecord zoomRecord = null; |
|
110 |
private List<BaseViewTool> toolList = null; |
|
111 |
private ViewListener viewListener = null; |
|
112 |
private ViewEvent viewEvent = null; |
|
113 |
|
|
114 |
/** |
|
115 |
* Constructor. Crea el panel y asigna el lado donde se crear?n los botones |
|
116 |
* @param right true para los botones a derecha y false para la izquierda |
|
117 |
*/ |
|
118 |
public ViewControl(int control) { |
|
119 |
this.control = control; |
|
120 |
toolList = new ArrayList<BaseViewTool>(); |
|
121 |
|
|
122 |
init(); |
|
123 |
|
|
124 |
//Tools predefinidas |
|
125 |
addTool(new ZoomRectangleTool(canvas, this)); |
|
126 |
addTool(new PanTool(canvas, this)); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Asigna el listener de eventos de la vista |
|
131 |
* @param listener |
|
132 |
*/ |
|
133 |
public void setViewListener(ViewListener listener) { |
|
134 |
this.viewListener = listener; |
|
135 |
viewEvent = new ViewEvent(this); |
|
136 |
getCanvas().setViewListener(viewListener); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* A?ade una tool a la vista de zoom |
|
141 |
* @param tool |
|
142 |
*/ |
|
143 |
public void addTool(BaseViewTool tool) { |
|
144 |
if(!toolList.contains(tool)) |
|
145 |
toolList.add(tool); |
|
146 |
} |
|
147 |
|
|
148 |
/** |
|
149 |
* A?ade una tool a la vista de zoom |
|
150 |
* @param tool |
|
151 |
*/ |
|
152 |
public void replaceTool(BaseViewTool tool) { |
|
153 |
for (int i = 0; i < toolList.size(); i++) { |
|
154 |
if(toolList.get(i).getClass().isInstance(tool)) { |
|
155 |
toolList.remove(i); |
|
156 |
break; |
|
157 |
} |
|
158 |
toolList.add(tool); |
|
159 |
} |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* Obtiene la tool seleccionada |
|
164 |
* @return |
|
165 |
*/ |
|
166 |
public BaseViewTool getToolSelected() { |
|
167 |
return canvas.getSelectedTool(); |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* Selecciona una herramienta a partir de su clase. Si el argumento es null |
|
172 |
* elimina cualquier herramienta seleccionada. |
|
173 |
* @param tool |
|
174 |
*/ |
|
175 |
public void selectTool(Class<?> tool, boolean select) { |
|
176 |
//Si tool es null se desactivan todas y ponemos la seleccionada en el canvas a null |
|
177 |
if(tool == null) { |
|
178 |
for (int i = 0; i < toolList.size(); i++) |
|
179 |
((BaseViewTool)toolList.get(i)).setActive(false); |
|
180 |
canvas.setSelectedTool(null); |
|
181 |
return; |
|
182 |
} |
|
183 |
|
|
184 |
//Si seleccionamos una esta se activa, se asigna al canvas y las otras se desactivan |
|
185 |
if(select) { |
|
186 |
for (int i = 0; i < toolList.size(); i++) { |
|
187 |
if(tool.isInstance(toolList.get(i))) { |
|
188 |
((BaseViewTool)toolList.get(i)).setActive(true); |
|
189 |
canvas.setSelectedTool((BaseViewTool)toolList.get(i)); |
|
190 |
if(viewListener != null) |
|
191 |
viewListener.addingTool(viewEvent); |
|
192 |
} else |
|
193 |
((BaseViewTool)toolList.get(i)).setActive(false); |
|
194 |
} |
|
195 |
return; |
|
196 |
} |
|
197 |
//Si quitamos una esta se desactiva y si es la del canvas se quita tambi?n |
|
198 |
for (int i = 0; i < toolList.size(); i++) { |
|
199 |
if(tool.isInstance(toolList.get(i))) { |
|
200 |
((BaseViewTool)toolList.get(i)).setActive(false); |
|
201 |
if(tool.isInstance(canvas.getSelectedTool())) |
|
202 |
canvas.setSelectedTool(null); |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
/** |
|
208 |
* Selecciona una herramienta a partir de su clase deseleccionando cualquier |
|
209 |
* otra que estuviera activa. Si el argumento es null |
|
210 |
* elimina cualquier herramienta seleccionada. |
|
211 |
* @param tool |
|
212 |
*/ |
|
213 |
public void selectUniqueTool(Class<?> tool) { |
|
214 |
if(tool == null) { |
|
215 |
for (int i = 0; i < toolList.size(); i++) |
|
216 |
((BaseViewTool)toolList.get(i)).setActive(false); |
|
217 |
canvas.setSelectedTool(null); |
|
218 |
return; |
|
219 |
} |
|
220 |
for (int i = 0; i < toolList.size(); i++) { |
|
221 |
if(tool.isInstance(toolList.get(i))) { |
|
222 |
((BaseViewTool)toolList.get(i)).setActive(true); |
|
223 |
canvas.setSelectedTool((BaseViewTool)toolList.get(i)); |
|
224 |
if(viewListener != null) |
|
225 |
viewListener.addingTool(viewEvent); |
|
226 |
} else |
|
227 |
((BaseViewTool)toolList.get(i)).setActive(false); |
|
228 |
} |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* Registra un listener a la tool especificada por el par?metro de la llamada. |
|
233 |
* @param tool Clase de la tool a la que queremos registrar el listener |
|
234 |
* @return true si se ha registrado con ?xito y false si no se ha hecho |
|
235 |
*/ |
|
236 |
public boolean registerToolListener(Class<?> tool, ToolListener listener) { |
|
237 |
if(tool == null) |
|
238 |
return false; |
|
239 |
for (int i = 0; i < toolList.size(); i++) { |
|
240 |
if(tool.isInstance(toolList.get(i))) { |
|
241 |
((BaseViewTool)toolList.get(i)).addToolListener(listener); |
|
242 |
return true; |
|
243 |
} |
|
244 |
} |
|
245 |
return false; |
|
246 |
} |
|
247 |
|
|
248 |
/** |
|
249 |
* Desactiva las herramientas temporalmente. Guarda el estado en el que estaban |
|
250 |
* para restaurarlo cuando se invoque a awake |
|
251 |
*/ |
|
252 |
public void sleepTools() { |
|
253 |
for (int i = 0; i < toolList.size(); i++) |
|
254 |
((BaseViewTool)toolList.get(i)).sleep(); |
|
255 |
} |
|
256 |
|
|
257 |
/** |
|
258 |
* Recupera el estado de activaci?n que ten?a antes de la ?ltima invocaci?n |
|
259 |
* de sleep |
|
260 |
*/ |
|
261 |
public void awakeTools() { |
|
262 |
for (int i = 0; i < toolList.size(); i++) |
|
263 |
((BaseViewTool)toolList.get(i)).awake(); |
|
264 |
} |
|
265 |
|
|
266 |
/** |
|
267 |
* Inicializaci?n de los componetes |
|
268 |
*/ |
|
269 |
private void init() { |
|
270 |
BorderLayout layout = new BorderLayout(); |
|
271 |
setLayout(layout); |
|
272 |
|
|
273 |
if(control == LEFT_CONTROL) |
|
274 |
add(getButtonsPanel(), BorderLayout.WEST); |
|
275 |
if(control == RIGHT_CONTROL) |
|
276 |
add(getButtonsPanel(), BorderLayout.EAST); |
|
277 |
|
|
278 |
add(getCanvas(), BorderLayout.CENTER); |
|
279 |
} |
|
280 |
|
|
281 |
/** |
|
282 |
* Oculta el bot?n indicado en el par?metro |
|
283 |
* @param b Constante definida en ZoomControl |
|
284 |
*/ |
|
285 |
public void hideButton(int b) { |
|
286 |
switch (b) { |
|
287 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(false); |
|
288 |
break; |
|
289 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(false); |
|
290 |
break; |
|
291 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(false); |
|
292 |
break; |
|
293 |
case FULL_VIEW: this.getBFullView().setVisible(false); |
|
294 |
break; |
|
295 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(false); |
|
296 |
break; |
|
297 |
case PAN: this.getBMove().setVisible(false); |
|
298 |
break; |
|
299 |
case LOCATOR: this.getBCenterLocator().setVisible(false); |
|
300 |
break; |
|
301 |
default: |
|
302 |
break; |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
/** |
|
307 |
* Muestra el bot?n indicado en el par?metro |
|
308 |
* @param b Constante definida en ZoomControl |
|
309 |
*/ |
|
310 |
public void showButton(int b) { |
|
311 |
switch (b) { |
|
312 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(true); |
|
313 |
break; |
|
314 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(true); |
|
315 |
break; |
|
316 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(true); |
|
317 |
break; |
|
318 |
case FULL_VIEW: this.getBFullView().setVisible(true); |
|
319 |
break; |
|
320 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(true); |
|
321 |
break; |
|
322 |
case PAN: this.getBMove().setVisible(true); |
|
323 |
break; |
|
324 |
case LOCATOR: this.getBCenterLocator().setVisible(true); |
|
325 |
break; |
|
326 |
default: |
|
327 |
break; |
|
328 |
} |
|
329 |
} |
|
330 |
|
|
331 |
/** |
|
332 |
* Asigna una capa gr?fica |
|
333 |
* @param layer IGraphicLayer |
|
334 |
*/ |
|
335 |
public void setGraphicLayer(IGraphicLayer layer) { |
|
336 |
canvas.setGraphicLayer(layer); |
|
337 |
} |
|
338 |
|
|
339 |
/** |
|
340 |
* Obtiene el panel de dibujado del zoom |
|
341 |
* @return Canvas Zone |
|
342 |
*/ |
|
343 |
public CanvasZone getCanvas() { |
|
344 |
if(canvas == null) { |
|
345 |
canvas = new CanvasZone(); |
|
346 |
canvas.setViewListener(viewListener); |
|
347 |
canvas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
|
348 |
} |
|
349 |
return canvas; |
|
350 |
} |
|
351 |
|
|
352 |
/** |
|
353 |
* Obtiene el panel con los botones de zoom |
|
354 |
* @return |
|
355 |
*/ |
|
356 |
public JPanel getButtonsPanel() { |
|
357 |
if(buttons == null) { |
|
358 |
buttons = new JPanel(); |
|
359 |
GridBagLayout l = new GridBagLayout(); |
|
360 |
GridBagConstraints gbc = new GridBagConstraints(); |
|
361 |
gbc.insets = new Insets(5, 0, 3, 3); |
|
362 |
buttons.setLayout(l); |
|
363 |
|
|
364 |
buttons.add(getBZoomMas(), gbc); |
|
365 |
gbc.gridy = 1; |
|
366 |
buttons.add(getBZoomMenos(), gbc); |
|
367 |
gbc.gridy = 2; |
|
368 |
buttons.add(getBSelectZoomArea(), gbc); |
|
369 |
gbc.gridy = 3; |
|
370 |
buttons.add(getBFullView(), gbc); |
|
371 |
gbc.gridy = 4; |
|
372 |
buttons.add(getBPrevZoom(), gbc); |
|
373 |
gbc.gridy = 5; |
|
374 |
buttons.add(getBMove(), gbc); |
|
375 |
gbc.gridy = 6; |
|
376 |
buttons.add(getBCenterLocator(), gbc); |
|
377 |
} |
|
378 |
return buttons; |
|
379 |
} |
|
380 |
|
|
381 |
/** |
|
382 |
* Crea el bot?n de zoom m?s |
|
383 |
* @return JButton |
|
384 |
*/ |
|
385 |
public JButton getBZoomMas() { |
|
386 |
if (bZoomMas == null) { |
|
387 |
bZoomMas = new JButton(); |
|
388 |
bZoomMas.setToolTipText(Messages.getText("zoom_mas")); |
|
389 |
bZoomMas.setPreferredSize(new java.awt.Dimension(25,25)); |
|
390 |
try{ |
|
391 |
bZoomMas.setIcon(IconThemeHelper.getImageIcon("increase-icon")); |
|
392 |
}catch(NullPointerException e){ |
|
393 |
//Sin icono |
|
394 |
} |
|
395 |
bZoomMas.addActionListener(this); |
|
396 |
} |
|
397 |
return bZoomMas; |
|
398 |
} |
|
399 |
|
|
400 |
/** |
|
401 |
* Crea el bot?n de centrar el localizador |
|
402 |
* @return JButton |
|
403 |
*/ |
|
404 |
public JButton getBCenterLocator() { |
|
405 |
if (bCenterLocator == null) { |
|
406 |
bCenterLocator = new JButton(); |
|
407 |
bCenterLocator.setToolTipText(Messages.getText("center_locator")); |
|
408 |
bCenterLocator.setPreferredSize(new java.awt.Dimension(25,25)); |
|
409 |
try{ |
|
410 |
bCenterLocator.setIcon(IconThemeHelper.getImageIcon("centerlocator-icon")); |
|
411 |
}catch(NullPointerException e){ |
|
412 |
//Sin icono |
|
413 |
} |
|
414 |
bCenterLocator.addActionListener(this); |
|
415 |
} |
|
416 |
return bCenterLocator; |
|
417 |
} |
|
418 |
|
|
419 |
/** |
|
420 |
* Crea el bot?n de zoom menos |
|
421 |
* @return JButton |
|
422 |
*/ |
|
423 |
public JButton getBZoomMenos() { |
|
424 |
if (bZoomMenos == null) { |
|
425 |
bZoomMenos = new JButton(); |
|
426 |
bZoomMenos.setToolTipText(Messages.getText("zoom_menos")); |
|
427 |
bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25)); |
|
428 |
try{ |
|
429 |
bZoomMenos.setIcon(IconThemeHelper.getImageIcon("decrease-icon")); |
|
430 |
}catch(NullPointerException e){ |
|
431 |
//Sin icono |
|
432 |
} |
|
433 |
bZoomMenos.addActionListener(this); |
|
434 |
} |
|
435 |
return bZoomMenos; |
|
436 |
} |
|
437 |
|
|
438 |
/** |
|
439 |
* Crea el bot?n de selecci?n de zoom por ?rea |
|
440 |
* @return JButton |
|
441 |
*/ |
|
442 |
public JToggleButton getBSelectZoomArea() { |
|
443 |
if (bSelectZoomArea == null) { |
|
444 |
bSelectZoomArea = new JToggleButton(); |
|
445 |
bSelectZoomArea.setToolTipText(Messages.getText("select_zoom_area")); |
|
446 |
bSelectZoomArea.setPreferredSize(new java.awt.Dimension(25,25)); |
|
447 |
try{ |
|
448 |
bSelectZoomArea.setIcon(IconThemeHelper.getImageIcon("selectzoomarea-icon")); |
|
449 |
}catch(NullPointerException e){ |
|
450 |
//Sin icono |
|
451 |
} |
|
452 |
bSelectZoomArea.addActionListener(this); |
|
453 |
} |
|
454 |
return bSelectZoomArea; |
|
455 |
} |
|
456 |
|
|
457 |
/** |
|
458 |
* Crea el bot?n de desplazamiento de imagen |
|
459 |
* @return JToggleButton |
|
460 |
*/ |
|
461 |
public JToggleButton getBMove() { |
|
462 |
if (bMove == null) { |
|
463 |
bMove = new JToggleButton(); |
|
464 |
bMove.setToolTipText(Messages.getText("move_image")); |
|
465 |
bMove.setPreferredSize(new java.awt.Dimension(25,25)); |
|
466 |
try{ |
|
467 |
bMove.setIcon(IconThemeHelper.getImageIcon("hand-icon")); |
|
468 |
}catch(NullPointerException e){ |
|
469 |
//Sin icono |
|
470 |
} |
|
471 |
bMove.addActionListener(this); |
|
472 |
} |
|
473 |
return bMove; |
|
474 |
} |
|
475 |
|
|
476 |
/** |
|
477 |
* Crea el bot?n de zoom m?s |
|
478 |
* @return |
|
479 |
*/ |
|
480 |
public JButton getBPrevZoom() { |
|
481 |
if (bPrevZoom == null) { |
|
482 |
bPrevZoom = new JButton(); |
|
483 |
bPrevZoom.setToolTipText(Messages.getText("prev_zoom")); |
|
484 |
bPrevZoom.setPreferredSize(new java.awt.Dimension(25,25)); |
|
485 |
try{ |
|
486 |
bPrevZoom.setIcon(IconThemeHelper.getImageIcon("prevzoom-icon")); |
|
487 |
}catch(NullPointerException e){ |
|
488 |
//Sin icono |
|
489 |
} |
|
490 |
bPrevZoom.addActionListener(this); |
|
491 |
} |
|
492 |
return bPrevZoom; |
|
493 |
} |
|
494 |
|
|
495 |
/** |
|
496 |
* Crea el bot?n de zoom completo. El zoom completo inicializa al primer zoom |
|
497 |
* que recibi? en caso de que no se est? usando un IExtensionRequest, ya que no tendr?a conocimiento |
|
498 |
* del tama?o total de capa, sino solo del buffer que tiene en ese momento. En caso de usar un |
|
499 |
* IExtensionRequest entonces se har? la petici?n correspondiente al interfaz. |
|
500 |
* @return JButton |
|
501 |
*/ |
|
502 |
public JButton getBFullView() { |
|
503 |
if (bFullView == null) { |
|
504 |
bFullView = new JButton(); |
|
505 |
bFullView.setToolTipText(Messages.getText("full_view")); |
|
506 |
bFullView.setPreferredSize(new java.awt.Dimension(25,25)); |
|
507 |
try{ |
|
508 |
bFullView.setIcon(IconThemeHelper.getImageIcon("fullview-icon")); |
|
509 |
}catch(NullPointerException e){ |
|
510 |
//Sin icono |
|
511 |
} |
|
512 |
bFullView.addActionListener(this); |
|
513 |
} |
|
514 |
return bFullView; |
|
515 |
} |
|
516 |
|
|
517 |
/** |
|
518 |
* Asigna los par?metros de dibujado |
|
519 |
* @param img Buffer con un ?rea de datos |
|
520 |
* @param ext Rectangle2D del ?rea de datos dada |
|
521 |
* @param pixelSize Tama?o de pixel |
|
522 |
* @param center Punto del ?rea de datos donde se quiere centrar el dibujado del buffer |
|
523 |
*/ |
|
524 |
public void setDrawParams(BufferedImage img, Rectangle2D ext, double pixelSize, Point2D center) { |
|
525 |
getZoomRecord().setRequest(ext); |
|
526 |
canvas.setDrawParams(img, ext, pixelSize, center) ; |
|
527 |
} |
|
528 |
|
|
529 |
/** |
|
530 |
* Asigna un nuevo centro de visualizaci?n. Vuelve a realizar la petici?n |
|
531 |
* @param center |
|
532 |
*/ |
|
533 |
public void setCenter(Point2D center) { |
|
534 |
Rectangle2D ext = canvas.getExtent(); |
|
535 |
double diffX = center.getX() - ext.getCenterX(); |
|
536 |
double diffY = center.getY() - ext.getCenterY(); |
|
537 |
ext.setRect(ext.getX() + diffX, ext.getY() + diffY, ext.getWidth(), ext.getHeight()); |
|
538 |
try { |
|
539 |
canvas.setForceRequest(true); |
|
540 |
extReq.request(ext); |
|
541 |
} catch (InvalidRequestException e) { |
|
542 |
GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", this); |
|
543 |
} |
|
544 |
} |
|
545 |
|
|
546 |
/** |
|
547 |
* Registra un objeto IExtensionRequest para que no se aplique un escalado sobre |
|
548 |
* el buffer pasado por par?metro. Alternativamente a la aplicaci?n de este escalado |
|
549 |
* se ejecutar? el m?todo request del interfaz para que el cliente pueda pasar un |
|
550 |
* nuevo buffer con escala 1:1 y con la extensi?n correspondiente al zoom, Es decir, se |
|
551 |
* deja en manos del cliente la aplicaci?n del zoom. |
|
552 |
* @param er |
|
553 |
*/ |
|
554 |
public void setExtensionRequest(IExtensionRequest er) { |
|
555 |
this.extReq = er; |
|
556 |
} |
|
557 |
|
|
558 |
/** |
|
559 |
* Obtiene el objeto IExtensionRequest |
|
560 |
* @param er |
|
561 |
*/ |
|
562 |
public IExtensionRequest getExtensionRequest() { |
|
563 |
return extReq; |
|
564 |
} |
|
565 |
|
|
566 |
/** |
|
567 |
* Obtiene el historico de zooms |
|
568 |
* @return ZoomRecord |
|
569 |
*/ |
|
570 |
public ViewRecord getZoomRecord() { |
|
571 |
if(zoomRecord == null) { |
|
572 |
zoomRecord = new ViewRecord(); |
|
573 |
//zoomRecord.setRequest(new Integer(FULL_VIEW)); |
|
574 |
} |
|
575 |
return zoomRecord; |
|
576 |
} |
|
577 |
|
|
578 |
/** |
|
579 |
* Eventos de los botones zoom m?s y zoom menos. |
|
580 |
*/ |
|
581 |
public void actionPerformed(ActionEvent e) { |
|
582 |
//Centrar el localizador |
|
583 |
if(e.getSource() == bCenterLocator) { |
|
584 |
List<IGraphicLayer> graphicLayers = canvas.getGraphicLayers(); |
|
585 |
for (int i = 0; i < graphicLayers.size(); i++) { |
|
586 |
if(graphicLayers.get(i) instanceof ZoomCursorGraphicLayer) { |
|
587 |
((ZoomCursorGraphicLayer)graphicLayers.get(i)).setCursorPosition(canvas.getWidth() / 2, canvas.getHeight() / 2); |
|
588 |
((ZoomCursorGraphicLayer)graphicLayers.get(i)).resetCursorSize(); |
|
589 |
} |
|
590 |
} |
|
591 |
} |
|
592 |
|
|
593 |
if(extReq == null) { |
|
594 |
if(e.getSource() == bZoomMas) |
|
595 |
canvas.setZoom(canvas.getZoom() * stepScale); |
|
596 |
|
|
597 |
if(e.getSource() == bZoomMenos) |
|
598 |
canvas.setZoom(canvas.getZoom() / stepScale); |
|
599 |
|
|
600 |
if(e.getSource() == bFullView) |
|
601 |
canvas.setZoom(1); |
|
602 |
|
|
603 |
} else { |
|
604 |
double width = 0, height = 0; |
|
605 |
canvas.setZoom(1); |
|
606 |
|
|
607 |
//Zoom Todo |
|
608 |
if(e.getSource() == bFullView) { |
|
609 |
try { |
|
610 |
extReq.fullExtent(); |
|
611 |
} catch (InvalidRequestException e1) { |
|
612 |
GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", this); |
|
613 |
} |
|
614 |
return; |
|
615 |
} |
|
616 |
|
|
617 |
//Zoom Anterior |
|
618 |
if(e.getSource() == bPrevZoom) { |
|
619 |
Object request = getZoomRecord().getRequest(); |
|
620 |
if(request != null) { |
|
621 |
if(request instanceof Rectangle2D) { |
|
622 |
try { |
|
623 |
extReq.request((Rectangle2D)request); |
|
624 |
} catch (InvalidRequestException e1) { |
|
625 |
GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", this); |
|
626 |
} |
|
627 |
} |
|
628 |
if(request instanceof Integer && ((Integer)request).intValue() == FULL_VIEW) { |
|
629 |
try { |
|
630 |
extReq.fullExtent(); |
|
631 |
} catch (InvalidRequestException e1) { |
|
632 |
GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", this); |
|
633 |
} |
|
634 |
} |
|
635 |
getZoomRecord().getRequest(); //El que hemos metido al hacer zoom previo no sirve por lo que hay que sacarlo |
|
636 |
} |
|
637 |
return; |
|
638 |
} |
|
639 |
|
|
640 |
width = canvas.getCanvasExtent().getWidth(); |
|
641 |
height = canvas.getCanvasExtent().getHeight(); |
|
642 |
|
|
643 |
//Desplazamiento |
|
644 |
if(e.getSource() == bMove) { |
|
645 |
if(getBMove().isSelected()) |
|
646 |
selectTool(PanTool.class, true); |
|
647 |
else |
|
648 |
selectTool(PanTool.class, false); |
|
649 |
} |
|
650 |
|
|
651 |
//Zoom por selecci?n de ?rea |
|
652 |
if(e.getSource() == bSelectZoomArea) { |
|
653 |
if(getBSelectZoomArea().isSelected()) |
|
654 |
selectTool(ZoomRectangleTool.class, true); |
|
655 |
else |
|
656 |
selectTool(ZoomRectangleTool.class, false); |
|
657 |
} |
|
658 |
|
|
659 |
//Zoom m?s |
|
660 |
if(e.getSource() == bZoomMas) { |
|
661 |
width = canvas.getCanvasExtent().getWidth() * SCALE; |
|
662 |
height = canvas.getCanvasExtent().getHeight() * SCALE; |
|
663 |
} |
|
664 |
|
|
665 |
//Zoom menos |
|
666 |
if(e.getSource() == bZoomMenos) { |
|
667 |
width = canvas.getCanvasExtent().getWidth() / SCALE; |
|
668 |
height = canvas.getCanvasExtent().getHeight() / SCALE; |
|
669 |
} |
|
670 |
//C?digo com?n a bZoomMas y bZoomMenos |
|
671 |
double x = canvas.getCenter().getX() - (width / 2); |
|
672 |
double y = canvas.getCenter().getY() - (height / 2); |
|
673 |
Rectangle2D request = new Rectangle2D.Double(x, y, width, height); |
|
674 |
try { |
|
675 |
extReq.request(request); |
|
676 |
} catch (InvalidRequestException e1) { |
|
677 |
GeoreferencingSwingImplLibrary.messageBoxError("error_setview_preview", this); |
|
678 |
} |
|
679 |
} |
|
680 |
} |
|
681 |
|
|
682 |
/** |
|
683 |
* Recarga la vista con los par?metros de tama?o y |
|
684 |
* extensi?n actuales |
|
685 |
*/ |
|
686 |
public void reload() { |
Also available in: Unified diff