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