root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ui / zoom / ZoomControl.java @ 18044
History | View | Annotate | Download (15.3 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.rastertools.georeferencing.ui.zoom; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.GridBagConstraints; |
24 |
import java.awt.GridBagLayout; |
25 |
import java.awt.Insets; |
26 |
import java.awt.event.ActionEvent; |
27 |
import java.awt.event.ActionListener; |
28 |
import java.awt.event.MouseEvent; |
29 |
import java.awt.event.MouseListener; |
30 |
import java.awt.geom.Point2D; |
31 |
import java.awt.geom.Rectangle2D; |
32 |
import java.awt.image.BufferedImage; |
33 |
|
34 |
import javax.swing.JButton; |
35 |
import javax.swing.JPanel; |
36 |
|
37 |
import org.gvsig.raster.util.RasterToolsUtil; |
38 |
|
39 |
/**
|
40 |
* <P>
|
41 |
* Control de zoom. Muestra una imagen contenida en un buffer con controles de aumento
|
42 |
* y disminuci?n del zoom de la misma.
|
43 |
* </P><P>
|
44 |
* El visualizador mostrar? el buffer de datos pasado en <code>setDrawParams</code>.
|
45 |
* En esta llamada asignamos tambi?n el Extent del ?rea de datos, tama?o de pixel y punto
|
46 |
* del buffer que coincidir? con el centro del control de zoom. De esta forma la imagen
|
47 |
* visualizada se centra sobre este punto. En caso de no tener informaci?n geogr?fica del
|
48 |
* buffer de datos a visualizar el Extent coincidir? con su tama?o en p?xeles y el tama?o
|
49 |
* de pixel ser? de 1. El punto donde queramos centrar en este caso se dar? tambi?n en
|
50 |
* coordenadas pixel.
|
51 |
* </P><P>
|
52 |
* Hay dos usos posibles en este control al accionar el bot?n de zoom: el primero de ellos, en
|
53 |
* caso de que no haya ning?n listener del control de zoom registrado. En este caso se
|
54 |
* aplicar? un escalado sobre los datos del buffer pasado por par?metro. En el caso de que
|
55 |
* haya un listener registrado (IExtensionBuffer) no se aplicar? un escalado sobre el buffer
|
56 |
* sino que se aplicar? una escala de 1.0 y se ejecutar? el m?todo request del interfaz
|
57 |
* registrado para que el cliente reciba notificaci?n del nuevo extent.
|
58 |
* </P>
|
59 |
* <P>
|
60 |
* En la inicializaci?n podemos solicitar que los controles de zoom est?n a la izquierda,
|
61 |
* a la derecha o no esten.
|
62 |
* </p>
|
63 |
*
|
64 |
* 21/12/2007
|
65 |
* @author Nacho Brodin nachobrodin@gmail.com
|
66 |
*/
|
67 |
public class ZoomControl extends JPanel implements ActionListener, MouseListener { |
68 |
private static final long serialVersionUID = 1L; |
69 |
//Lista de controles
|
70 |
public static final int ZOOM_INCREASE = 0; |
71 |
public static final int ZOOM_DECREASE = 1; |
72 |
public static final int SELECT_ZOOM_AREA = 2; |
73 |
public static final int FULL_VIEW = 3; |
74 |
public static final int PREV_ZOOM = 4; |
75 |
|
76 |
public static final int RIGHT_CONTROL = 1; |
77 |
public static final int LEFT_CONTROL = 2; |
78 |
public static final int NOCONTROL = 0; |
79 |
|
80 |
private final double SCALE = 0.5; |
81 |
|
82 |
private int control; |
83 |
private CanvasZone canvas = null; |
84 |
private JPanel buttons = null; |
85 |
private JButton bZoomMas = null; |
86 |
private JButton bZoomMenos = null; |
87 |
private JButton bSelectZoomArea = null; |
88 |
private JButton bFullView = null; |
89 |
private JButton bPrevZoom = null; |
90 |
private double stepScale = 2; |
91 |
private IExtensionRequest extReq = null; |
92 |
private ZoomRecord zoomRecord = null; |
93 |
|
94 |
//Variables para la selecci?n de un ?rea pinchando dos puntos sobre el canvas.
|
95 |
private boolean selectArea = false; |
96 |
private Point2D initPoint = null; |
97 |
|
98 |
/**
|
99 |
* Constructor. Crea el panel y asigna el lado donde se crear?n los botones
|
100 |
* @param right true para los botones a derecha y false para la izquierda
|
101 |
*/
|
102 |
public ZoomControl(int control) { |
103 |
this.control = control;
|
104 |
init(); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* Inicializaci?n de los componetes
|
109 |
*/
|
110 |
private void init() { |
111 |
BorderLayout layout = new BorderLayout(); |
112 |
setLayout(layout); |
113 |
|
114 |
if(control == LEFT_CONTROL)
|
115 |
add(getButtonsPanel(), BorderLayout.WEST);
|
116 |
if(control == RIGHT_CONTROL)
|
117 |
add(getButtonsPanel(), BorderLayout.EAST);
|
118 |
|
119 |
add(getCanvas(), BorderLayout.CENTER);
|
120 |
getCanvas().addMouseListener(this);
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Oculta el bot?n indicado en el par?metro
|
125 |
* @param b Constante definida en ZoomControl
|
126 |
*/
|
127 |
public void hideButton(int b) { |
128 |
switch (b) {
|
129 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(false); |
130 |
break;
|
131 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(false); |
132 |
break;
|
133 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(false); |
134 |
break;
|
135 |
case FULL_VIEW: this.getBFullView().setVisible(false); |
136 |
break;
|
137 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(false); |
138 |
break;
|
139 |
default:
|
140 |
break;
|
141 |
} |
142 |
} |
143 |
|
144 |
/**
|
145 |
* Muestra el bot?n indicado en el par?metro
|
146 |
* @param b Constante definida en ZoomControl
|
147 |
*/
|
148 |
public void showButton(int b) { |
149 |
switch (b) {
|
150 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(true); |
151 |
break;
|
152 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(true); |
153 |
break;
|
154 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(true); |
155 |
break;
|
156 |
case FULL_VIEW: this.getBFullView().setVisible(true); |
157 |
break;
|
158 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(true); |
159 |
break;
|
160 |
default:
|
161 |
break;
|
162 |
} |
163 |
} |
164 |
|
165 |
/**
|
166 |
* Asigna una capa gr?fica
|
167 |
* @param layer IGraphicLayer
|
168 |
*/
|
169 |
public void setGraphicLayer(IGraphicLayer layer) { |
170 |
canvas.setGraphicLayer(layer); |
171 |
} |
172 |
|
173 |
/**
|
174 |
* Obtiene el panel de dibujado del zoom
|
175 |
* @return Canvas Zone
|
176 |
*/
|
177 |
public CanvasZone getCanvas() {
|
178 |
if(canvas == null) { |
179 |
canvas = new CanvasZone();
|
180 |
canvas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
181 |
} |
182 |
return canvas;
|
183 |
} |
184 |
|
185 |
/**
|
186 |
* Obtiene el panel con los botones de zoom
|
187 |
* @return
|
188 |
*/
|
189 |
public JPanel getButtonsPanel() { |
190 |
if(buttons == null) { |
191 |
buttons = new JPanel(); |
192 |
GridBagLayout l = new GridBagLayout(); |
193 |
GridBagConstraints gbc = new GridBagConstraints(); |
194 |
gbc.insets = new Insets(5, 0, 3, 3); |
195 |
buttons.setLayout(l); |
196 |
|
197 |
buttons.add(getBZoomMas(), gbc); |
198 |
gbc.gridy = 1;
|
199 |
buttons.add(getBZoomMenos(), gbc); |
200 |
gbc.gridy = 2;
|
201 |
buttons.add(getBSelectZoomArea(), gbc); |
202 |
gbc.gridy = 3;
|
203 |
buttons.add(getBFullView(), gbc); |
204 |
gbc.gridy = 4;
|
205 |
buttons.add(getBPrevZoom(), gbc); |
206 |
} |
207 |
return buttons;
|
208 |
} |
209 |
|
210 |
/**
|
211 |
* Crea el bot?n de zoom m?s
|
212 |
* @return JButton
|
213 |
*/
|
214 |
public JButton getBZoomMas() { |
215 |
if (bZoomMas == null) { |
216 |
bZoomMas = new JButton(); |
217 |
bZoomMas.setToolTipText(RasterToolsUtil.getText(this, "zoom_mas")); |
218 |
bZoomMas.setPreferredSize(new java.awt.Dimension(25,25)); |
219 |
try{
|
220 |
bZoomMas.setIcon(RasterToolsUtil.getIcon("increase-icon"));
|
221 |
}catch(NullPointerException e){ |
222 |
//Sin icono
|
223 |
} |
224 |
bZoomMas.addActionListener(this);
|
225 |
} |
226 |
return bZoomMas;
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Crea el bot?n de zoom menos
|
231 |
* @return JButton
|
232 |
*/
|
233 |
public JButton getBZoomMenos() { |
234 |
if (bZoomMenos == null) { |
235 |
bZoomMenos = new JButton(); |
236 |
bZoomMenos.setToolTipText(RasterToolsUtil.getText(this, "zoom_menos")); |
237 |
bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25)); |
238 |
try{
|
239 |
bZoomMenos.setIcon(RasterToolsUtil.getIcon("decrease-icon"));
|
240 |
}catch(NullPointerException e){ |
241 |
//Sin icono
|
242 |
} |
243 |
bZoomMenos.addActionListener(this);
|
244 |
} |
245 |
return bZoomMenos;
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* Crea el bot?n de selecci?n de zoom por ?rea
|
250 |
* @return JButton
|
251 |
*/
|
252 |
public JButton getBSelectZoomArea() { |
253 |
if (bSelectZoomArea == null) { |
254 |
bSelectZoomArea = new JButton(); |
255 |
bSelectZoomArea.setToolTipText(RasterToolsUtil.getText(this, "select_zoom_area")); |
256 |
bSelectZoomArea.setPreferredSize(new java.awt.Dimension(25,25)); |
257 |
try{
|
258 |
bSelectZoomArea.setIcon(RasterToolsUtil.getIcon("selectzoomarea-icon"));
|
259 |
}catch(NullPointerException e){ |
260 |
//Sin icono
|
261 |
} |
262 |
bSelectZoomArea.addActionListener(this);
|
263 |
} |
264 |
return bSelectZoomArea;
|
265 |
} |
266 |
|
267 |
/**
|
268 |
* Crea el bot?n de zoom m?s
|
269 |
* @return
|
270 |
*/
|
271 |
public JButton getBPrevZoom() { |
272 |
if (bPrevZoom == null) { |
273 |
bPrevZoom = new JButton(); |
274 |
bPrevZoom.setToolTipText(RasterToolsUtil.getText(this, "prev_zoom")); |
275 |
bPrevZoom.setPreferredSize(new java.awt.Dimension(25,25)); |
276 |
try{
|
277 |
bPrevZoom.setIcon(RasterToolsUtil.getIcon("prevzoom-icon"));
|
278 |
}catch(NullPointerException e){ |
279 |
//Sin icono
|
280 |
} |
281 |
bPrevZoom.addActionListener(this);
|
282 |
} |
283 |
return bPrevZoom;
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* Crea el bot?n de zoom completo. El zoom completo inicializa al primer zoom
|
288 |
* que recibi? en caso de que no se est? usando un IExtensionRequest, ya que no tendr?a conocimiento
|
289 |
* del tama?o total de capa, sino solo del buffer que tiene en ese momento. En caso de usar un
|
290 |
* IExtensionRequest entonces se har? la petici?n correspondiente al interfaz.
|
291 |
* @return JButton
|
292 |
*/
|
293 |
public JButton getBFullView() { |
294 |
if (bFullView == null) { |
295 |
bFullView = new JButton(); |
296 |
bFullView.setToolTipText(RasterToolsUtil.getText(this, "full_view")); |
297 |
bFullView.setPreferredSize(new java.awt.Dimension(25,25)); |
298 |
try{
|
299 |
bFullView.setIcon(RasterToolsUtil.getIcon("fullview-icon"));
|
300 |
}catch(NullPointerException e){ |
301 |
//Sin icono
|
302 |
} |
303 |
bFullView.addActionListener(this);
|
304 |
} |
305 |
return bFullView;
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* Asigna los par?metros de dibujado
|
310 |
* @param img Buffer con un ?rea de datos
|
311 |
* @param ext Rectangle2D del ?rea de datos dada
|
312 |
* @param pixelSize Tama?o de pixel
|
313 |
* @param center Punto del ?rea de datos donde se quiere centrar el dibujado del buffer
|
314 |
*/
|
315 |
public void setDrawParams(BufferedImage img, Rectangle2D ext, double pixelSize, Point2D center) { |
316 |
getZoomRecord().setRequest(ext); |
317 |
canvas.setDrawParams(img, ext, pixelSize, center) ; |
318 |
} |
319 |
|
320 |
/**
|
321 |
* Asigna un nuevo centro de visualizaci?n
|
322 |
* @param center
|
323 |
*/
|
324 |
public void setCenter(Point2D center) { |
325 |
canvas.setCenter(center); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel
|
330 |
* del ?rea de dibujado (canvas)
|
331 |
* @param center
|
332 |
*/
|
333 |
public void setPixelCenter(Point2D center) { |
334 |
if(extReq == null) |
335 |
canvas.setPixelCenter(center); |
336 |
else {
|
337 |
|
338 |
} |
339 |
} |
340 |
|
341 |
/**
|
342 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel. Esta llamada tiene
|
343 |
* en cuenta solo p?xeles completos. No centra sobre porciones de pixel cuando el zoom es
|
344 |
* mayor de 1:1.
|
345 |
* @param center
|
346 |
*/
|
347 |
public void setPixelCenter(int x, int y) { |
348 |
if(extReq == null) |
349 |
canvas.setPixelCenter(x, y); |
350 |
else {
|
351 |
System.out.println(new Point2D.Double(x, y)); |
352 |
Point2D p1 = canvas.viewCoordsToWorld(new Point2D.Double(x, y)); |
353 |
System.out.println(p1);
|
354 |
System.out.println(canvas.viewCoordsFromWorld(p1));
|
355 |
/*canvas.setZoom(1);
|
356 |
canvas.setViewCenter(new Point2D.Double(x, y));
|
357 |
double cx = canvas.getCenter().getX() - (canvas.getCanvasExtent().getWidth() / 2);
|
358 |
double cy = canvas.getCenter().getY() - (canvas.getCanvasExtent().getHeight() / 2);
|
359 |
extReq.request(new Rectangle2D.Double(cx, cy, canvas.getCanvasExtent().getWidth(), canvas.getCanvasExtent().getHeight()));*/
|
360 |
} |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Registra un objeto IExtensionRequest para que no se aplique un escalado sobre
|
365 |
* el buffer pasado por par?metro. Alternativamente a la aplicaci?n de este escalado
|
366 |
* se ejecutar? el m?todo request del interfaz para que el cliente pueda pasar un
|
367 |
* nuevo buffer con escala 1:1 y con la extensi?n correspondiente al zoom, Es decir, se
|
368 |
* deja en manos del cliente la aplicaci?n del zoom.
|
369 |
* @param er
|
370 |
*/
|
371 |
public void setExtensionRequest(IExtensionRequest er) { |
372 |
this.extReq = er;
|
373 |
} |
374 |
|
375 |
/**
|
376 |
* Obtiene el historico de zooms
|
377 |
* @return ZoomRecord
|
378 |
*/
|
379 |
public ZoomRecord getZoomRecord() {
|
380 |
if(zoomRecord == null) { |
381 |
zoomRecord = new ZoomRecord();
|
382 |
//zoomRecord.setRequest(new Integer(FULL_VIEW));
|
383 |
} |
384 |
return zoomRecord;
|
385 |
} |
386 |
|
387 |
/**
|
388 |
* Eventos de los botones zoom m?s y zoom menos.
|
389 |
*/
|
390 |
public void actionPerformed(ActionEvent e) { |
391 |
if(extReq == null) { |
392 |
if(e.getSource() == bZoomMas)
|
393 |
canvas.setZoom(canvas.getZoom() * stepScale); |
394 |
|
395 |
if(e.getSource() == bZoomMenos)
|
396 |
canvas.setZoom(canvas.getZoom() / stepScale); |
397 |
|
398 |
if(e.getSource() == bFullView)
|
399 |
canvas.setZoom(1);
|
400 |
|
401 |
} else {
|
402 |
double width = 0, height = 0; |
403 |
canvas.setZoom(1);
|
404 |
if(e.getSource() == bFullView) {
|
405 |
extReq.fullExtent(new Dimension(canvas.getVisibleRect().width, canvas.getVisibleRect().height)); |
406 |
return;
|
407 |
} |
408 |
if(e.getSource() == bPrevZoom) {
|
409 |
Object request = getZoomRecord().getRequest();
|
410 |
if(request != null) { |
411 |
if(request instanceof Rectangle2D) |
412 |
extReq.request((Rectangle2D)request);
|
413 |
if(request instanceof Integer && ((Integer)request).intValue() == FULL_VIEW) |
414 |
extReq.fullExtent(new Dimension(canvas.getVisibleRect().width, canvas.getVisibleRect().height)); |
415 |
getZoomRecord().getRequest(); //El que hemos metido al hacer zoom previo no sirve por lo que hay que sacarlo
|
416 |
} |
417 |
return;
|
418 |
} |
419 |
if(e.getSource() == bSelectZoomArea) {
|
420 |
setSelectArea(true);
|
421 |
return;
|
422 |
} |
423 |
if(e.getSource() == bZoomMas) {
|
424 |
width = canvas.getCanvasExtent().getWidth() * SCALE; |
425 |
height = canvas.getCanvasExtent().getHeight() * SCALE; |
426 |
} |
427 |
if(e.getSource() == bZoomMenos) {
|
428 |
width = canvas.getCanvasExtent().getWidth() / SCALE; |
429 |
height = canvas.getCanvasExtent().getHeight() / SCALE; |
430 |
} |
431 |
//C?digo com?n a bZoomMas y bZoomMenos
|
432 |
double x = canvas.getCenter().getX() - (width / 2); |
433 |
double y = canvas.getCenter().getY() - (height / 2); |
434 |
Rectangle2D request = new Rectangle2D.Double(x, y, width, height); |
435 |
extReq.request(request); |
436 |
} |
437 |
} |
438 |
|
439 |
/**
|
440 |
* <P>
|
441 |
* Asigna el flag de selecci?n de ?rea. Cuando este flag est? activo se esperan
|
442 |
* los eventos de mousePressed y mouseReleased. Cuando ambos eventos se producen
|
443 |
* se da por finalizado el estado de selecci?n de ?rea y el flag selectArea vuelve
|
444 |
* a valer false.
|
445 |
* </P><P>
|
446 |
* Cuando el est?do de selecci?n de ?rea est? activo y se producen los eventos se guarda
|
447 |
* el rectangulo de coordenadas que ha correspondido con la selecci?n. La clase ZoomControl
|
448 |
* podr? consultar estas coordenadas para hacer la petici?n zoom correspondiente.
|
449 |
* </P>
|
450 |
* @param selectArea
|
451 |
*/
|
452 |
public void setSelectArea(boolean selectArea) { |
453 |
this.selectArea = selectArea;
|
454 |
} |
455 |
|
456 |
/*
|
457 |
* (non-Javadoc)
|
458 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
459 |
*/
|
460 |
public void mousePressed(MouseEvent e) { |
461 |
if(selectArea)
|
462 |
initPoint = getCanvas().viewCoordsToWorld(e.getPoint()); |
463 |
} |
464 |
|
465 |
/*
|
466 |
* (non-Javadoc)
|
467 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
468 |
*/
|
469 |
public void mouseReleased(MouseEvent e) { |
470 |
if(selectArea) {
|
471 |
/*double width = canvas.getCanvasExtent().getWidth();
|
472 |
double height = canvas.getCanvasExtent().getHeight();
|
473 |
Point2D endPoint = getCanvas().viewCoordsToWorld(e.getPoint());
|
474 |
double x = (initPoint.getX() + Math.abs(endPoint.getX() - initPoint.getX())) - (width / 2);
|
475 |
double y = (initPoint.getY() + Math.abs(endPoint.getY() - initPoint.getY())) - (height / 2);
|
476 |
|
477 |
extReq.request(new Rectangle2D.Double(x, y, width, height));*/
|
478 |
} |
479 |
} |
480 |
|
481 |
public void mouseClicked(MouseEvent e) { |
482 |
} |
483 |
|
484 |
public void mouseEntered(MouseEvent e) { |
485 |
} |
486 |
|
487 |
public void mouseExited(MouseEvent e) { |
488 |
} |
489 |
|
490 |
} |