root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / georeferencing / ui / zoom / ViewControl.java @ 18230
History | View | Annotate | Download (15.1 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 |
import java.util.ArrayList; |
32 |
|
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JPanel; |
35 |
|
36 |
import org.gvsig.raster.util.RasterToolsUtil; |
37 |
|
38 |
/**
|
39 |
* <P>
|
40 |
* Control de zoom. Muestra una imagen contenida en un buffer con controles de aumento
|
41 |
* y disminuci?n del zoom de la misma.
|
42 |
* </P><P>
|
43 |
* El visualizador mostrar? el buffer de datos pasado en <code>setDrawParams</code>.
|
44 |
* En esta llamada asignamos tambi?n el Extent del ?rea de datos, tama?o de pixel y punto
|
45 |
* del buffer que coincidir? con el centro del control de zoom. De esta forma la imagen
|
46 |
* visualizada se centra sobre este punto. En caso de no tener informaci?n geogr?fica del
|
47 |
* buffer de datos a visualizar el Extent coincidir? con su tama?o en p?xeles y el tama?o
|
48 |
* de pixel ser? de 1. El punto donde queramos centrar en este caso se dar? tambi?n en
|
49 |
* coordenadas pixel.
|
50 |
* </P><P>
|
51 |
* Hay dos usos posibles en este control al accionar el bot?n de zoom: el primero de ellos, en
|
52 |
* caso de que no haya ning?n listener del control de zoom registrado. En este caso se
|
53 |
* aplicar? un escalado sobre los datos del buffer pasado por par?metro. En el caso de que
|
54 |
* haya un listener registrado (IExtensionBuffer) no se aplicar? un escalado sobre el buffer
|
55 |
* sino que se aplicar? una escala de 1.0 y se ejecutar? el m?todo request del interfaz
|
56 |
* registrado para que el cliente reciba notificaci?n del nuevo extent.
|
57 |
* </P>
|
58 |
* <P>
|
59 |
* En la inicializaci?n podemos solicitar que los controles de zoom est?n a la izquierda,
|
60 |
* a la derecha o no esten.
|
61 |
* </p>
|
62 |
*
|
63 |
* 21/12/2007
|
64 |
* @author Nacho Brodin nachobrodin@gmail.com
|
65 |
*/
|
66 |
public class ViewControl extends JPanel implements ActionListener, ToolListener { |
67 |
private static final long serialVersionUID = 1L; |
68 |
//Lista de controles
|
69 |
public static final int ZOOM_INCREASE = 0; |
70 |
public static final int ZOOM_DECREASE = 1; |
71 |
public static final int SELECT_ZOOM_AREA = 2; |
72 |
public static final int FULL_VIEW = 3; |
73 |
public static final int PREV_ZOOM = 4; |
74 |
|
75 |
public static final int RIGHT_CONTROL = 1; |
76 |
public static final int LEFT_CONTROL = 2; |
77 |
public static final int NOCONTROL = 0; |
78 |
|
79 |
private final double SCALE = 0.5; |
80 |
|
81 |
private int control; |
82 |
private CanvasZone canvas = null; |
83 |
private JPanel buttons = null; |
84 |
private JButton bZoomMas = null; |
85 |
private JButton bZoomMenos = null; |
86 |
private JButton bSelectZoomArea = null; |
87 |
private JButton bFullView = null; |
88 |
private JButton bPrevZoom = null; |
89 |
private double stepScale = 2; |
90 |
private IExtensionRequest extReq = null; |
91 |
private ViewRecord zoomRecord = null; |
92 |
private ArrayList toolList = null; |
93 |
|
94 |
/**
|
95 |
* Constructor. Crea el panel y asigna el lado donde se crear?n los botones
|
96 |
* @param right true para los botones a derecha y false para la izquierda
|
97 |
*/
|
98 |
public ViewControl(int control) { |
99 |
this.control = control;
|
100 |
toolList = new ArrayList(); |
101 |
|
102 |
init(); |
103 |
|
104 |
//Tools predefinidas
|
105 |
addTool(new ZoomRectangleTool(canvas, this)); |
106 |
} |
107 |
|
108 |
/**
|
109 |
* A?ade una tool a la vista de zoom
|
110 |
* @param tool
|
111 |
*/
|
112 |
public void addTool(BaseViewTool tool) { |
113 |
if(!toolList.contains(tool))
|
114 |
toolList.add(tool); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* A?ade una tool a la vista de zoom
|
119 |
* @param tool
|
120 |
*/
|
121 |
public void replaceTool(BaseViewTool tool) { |
122 |
for (int i = 0; i < toolList.size(); i++) { |
123 |
if(toolList.get(i).getClass().isInstance(tool)) {
|
124 |
toolList.remove(i); |
125 |
break;
|
126 |
} |
127 |
toolList.add(tool); |
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Selecciona una herramienta a partir de su clase. Si el argumento es null
|
133 |
* elimina cualquier herramienta seleccionada.
|
134 |
* @param tool
|
135 |
*/
|
136 |
public void selectTool(Class tool) { |
137 |
if(tool == null) { |
138 |
for (int i = 0; i < toolList.size(); i++) |
139 |
((BaseViewTool)toolList.get(i)).setSelected(false);
|
140 |
canvas.setSelectedTool(null);
|
141 |
return;
|
142 |
} |
143 |
|
144 |
for (int i = 0; i < toolList.size(); i++) { |
145 |
if(tool.isInstance(toolList.get(i))) {
|
146 |
((BaseViewTool)toolList.get(i)).setSelected(true);
|
147 |
canvas.setSelectedTool((BaseViewTool)toolList.get(i)); |
148 |
} else
|
149 |
((BaseViewTool)toolList.get(i)).setSelected(false);
|
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Inicializaci?n de los componetes
|
155 |
*/
|
156 |
private void init() { |
157 |
BorderLayout layout = new BorderLayout(); |
158 |
setLayout(layout); |
159 |
|
160 |
if(control == LEFT_CONTROL)
|
161 |
add(getButtonsPanel(), BorderLayout.WEST);
|
162 |
if(control == RIGHT_CONTROL)
|
163 |
add(getButtonsPanel(), BorderLayout.EAST);
|
164 |
|
165 |
add(getCanvas(), BorderLayout.CENTER);
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Oculta el bot?n indicado en el par?metro
|
170 |
* @param b Constante definida en ZoomControl
|
171 |
*/
|
172 |
public void hideButton(int b) { |
173 |
switch (b) {
|
174 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(false); |
175 |
break;
|
176 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(false); |
177 |
break;
|
178 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(false); |
179 |
break;
|
180 |
case FULL_VIEW: this.getBFullView().setVisible(false); |
181 |
break;
|
182 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(false); |
183 |
break;
|
184 |
default:
|
185 |
break;
|
186 |
} |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Muestra el bot?n indicado en el par?metro
|
191 |
* @param b Constante definida en ZoomControl
|
192 |
*/
|
193 |
public void showButton(int b) { |
194 |
switch (b) {
|
195 |
case ZOOM_INCREASE: this.getBZoomMas().setVisible(true); |
196 |
break;
|
197 |
case ZOOM_DECREASE: this.getBZoomMenos().setVisible(true); |
198 |
break;
|
199 |
case SELECT_ZOOM_AREA: this.getBSelectZoomArea().setVisible(true); |
200 |
break;
|
201 |
case FULL_VIEW: this.getBFullView().setVisible(true); |
202 |
break;
|
203 |
case PREV_ZOOM: this.getBPrevZoom().setVisible(true); |
204 |
break;
|
205 |
default:
|
206 |
break;
|
207 |
} |
208 |
} |
209 |
|
210 |
/**
|
211 |
* Asigna una capa gr?fica
|
212 |
* @param layer IGraphicLayer
|
213 |
*/
|
214 |
public void setGraphicLayer(IGraphicLayer layer) { |
215 |
canvas.setGraphicLayer(layer); |
216 |
} |
217 |
|
218 |
/**
|
219 |
* Obtiene el panel de dibujado del zoom
|
220 |
* @return Canvas Zone
|
221 |
*/
|
222 |
public CanvasZone getCanvas() {
|
223 |
if(canvas == null) { |
224 |
canvas = new CanvasZone();
|
225 |
canvas.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED)); |
226 |
} |
227 |
return canvas;
|
228 |
} |
229 |
|
230 |
/**
|
231 |
* Obtiene el panel con los botones de zoom
|
232 |
* @return
|
233 |
*/
|
234 |
public JPanel getButtonsPanel() { |
235 |
if(buttons == null) { |
236 |
buttons = new JPanel(); |
237 |
GridBagLayout l = new GridBagLayout(); |
238 |
GridBagConstraints gbc = new GridBagConstraints(); |
239 |
gbc.insets = new Insets(5, 0, 3, 3); |
240 |
buttons.setLayout(l); |
241 |
|
242 |
buttons.add(getBZoomMas(), gbc); |
243 |
gbc.gridy = 1;
|
244 |
buttons.add(getBZoomMenos(), gbc); |
245 |
gbc.gridy = 2;
|
246 |
buttons.add(getBSelectZoomArea(), gbc); |
247 |
gbc.gridy = 3;
|
248 |
buttons.add(getBFullView(), gbc); |
249 |
gbc.gridy = 4;
|
250 |
buttons.add(getBPrevZoom(), gbc); |
251 |
} |
252 |
return buttons;
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* Crea el bot?n de zoom m?s
|
257 |
* @return JButton
|
258 |
*/
|
259 |
public JButton getBZoomMas() { |
260 |
if (bZoomMas == null) { |
261 |
bZoomMas = new JButton(); |
262 |
bZoomMas.setToolTipText(RasterToolsUtil.getText(this, "zoom_mas")); |
263 |
bZoomMas.setPreferredSize(new java.awt.Dimension(25,25)); |
264 |
try{
|
265 |
bZoomMas.setIcon(RasterToolsUtil.getIcon("increase-icon"));
|
266 |
}catch(NullPointerException e){ |
267 |
//Sin icono
|
268 |
} |
269 |
bZoomMas.addActionListener(this);
|
270 |
} |
271 |
return bZoomMas;
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* Crea el bot?n de zoom menos
|
276 |
* @return JButton
|
277 |
*/
|
278 |
public JButton getBZoomMenos() { |
279 |
if (bZoomMenos == null) { |
280 |
bZoomMenos = new JButton(); |
281 |
bZoomMenos.setToolTipText(RasterToolsUtil.getText(this, "zoom_menos")); |
282 |
bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25)); |
283 |
try{
|
284 |
bZoomMenos.setIcon(RasterToolsUtil.getIcon("decrease-icon"));
|
285 |
}catch(NullPointerException e){ |
286 |
//Sin icono
|
287 |
} |
288 |
bZoomMenos.addActionListener(this);
|
289 |
} |
290 |
return bZoomMenos;
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* Crea el bot?n de selecci?n de zoom por ?rea
|
295 |
* @return JButton
|
296 |
*/
|
297 |
public JButton getBSelectZoomArea() { |
298 |
if (bSelectZoomArea == null) { |
299 |
bSelectZoomArea = new JButton(); |
300 |
bSelectZoomArea.setToolTipText(RasterToolsUtil.getText(this, "select_zoom_area")); |
301 |
bSelectZoomArea.setPreferredSize(new java.awt.Dimension(25,25)); |
302 |
try{
|
303 |
bSelectZoomArea.setIcon(RasterToolsUtil.getIcon("selectzoomarea-icon"));
|
304 |
}catch(NullPointerException e){ |
305 |
//Sin icono
|
306 |
} |
307 |
bSelectZoomArea.addActionListener(this);
|
308 |
} |
309 |
return bSelectZoomArea;
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Crea el bot?n de zoom m?s
|
314 |
* @return
|
315 |
*/
|
316 |
public JButton getBPrevZoom() { |
317 |
if (bPrevZoom == null) { |
318 |
bPrevZoom = new JButton(); |
319 |
bPrevZoom.setToolTipText(RasterToolsUtil.getText(this, "prev_zoom")); |
320 |
bPrevZoom.setPreferredSize(new java.awt.Dimension(25,25)); |
321 |
try{
|
322 |
bPrevZoom.setIcon(RasterToolsUtil.getIcon("prevzoom-icon"));
|
323 |
}catch(NullPointerException e){ |
324 |
//Sin icono
|
325 |
} |
326 |
bPrevZoom.addActionListener(this);
|
327 |
} |
328 |
return bPrevZoom;
|
329 |
} |
330 |
|
331 |
/**
|
332 |
* Crea el bot?n de zoom completo. El zoom completo inicializa al primer zoom
|
333 |
* que recibi? en caso de que no se est? usando un IExtensionRequest, ya que no tendr?a conocimiento
|
334 |
* del tama?o total de capa, sino solo del buffer que tiene en ese momento. En caso de usar un
|
335 |
* IExtensionRequest entonces se har? la petici?n correspondiente al interfaz.
|
336 |
* @return JButton
|
337 |
*/
|
338 |
public JButton getBFullView() { |
339 |
if (bFullView == null) { |
340 |
bFullView = new JButton(); |
341 |
bFullView.setToolTipText(RasterToolsUtil.getText(this, "full_view")); |
342 |
bFullView.setPreferredSize(new java.awt.Dimension(25,25)); |
343 |
try{
|
344 |
bFullView.setIcon(RasterToolsUtil.getIcon("fullview-icon"));
|
345 |
}catch(NullPointerException e){ |
346 |
//Sin icono
|
347 |
} |
348 |
bFullView.addActionListener(this);
|
349 |
} |
350 |
return bFullView;
|
351 |
} |
352 |
|
353 |
/**
|
354 |
* Asigna los par?metros de dibujado
|
355 |
* @param img Buffer con un ?rea de datos
|
356 |
* @param ext Rectangle2D del ?rea de datos dada
|
357 |
* @param pixelSize Tama?o de pixel
|
358 |
* @param center Punto del ?rea de datos donde se quiere centrar el dibujado del buffer
|
359 |
*/
|
360 |
public void setDrawParams(BufferedImage img, Rectangle2D ext, double pixelSize, Point2D center) { |
361 |
getZoomRecord().setRequest(ext); |
362 |
canvas.setDrawParams(img, ext, pixelSize, center) ; |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Asigna un nuevo centro de visualizaci?n
|
367 |
* @param center
|
368 |
*/
|
369 |
public void setCenter(Point2D center) { |
370 |
canvas.setCenter(center); |
371 |
} |
372 |
|
373 |
/**
|
374 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel
|
375 |
* del ?rea de dibujado (canvas)
|
376 |
* @param center
|
377 |
*/
|
378 |
public void setPixelCenter(Point2D center) { |
379 |
if(extReq == null) |
380 |
canvas.setPixelCenter(center); |
381 |
else {
|
382 |
|
383 |
} |
384 |
} |
385 |
|
386 |
/**
|
387 |
* Asigna un nuevo centro de visualizaci?n en coordenadas pixel. Esta llamada tiene
|
388 |
* en cuenta solo p?xeles completos. No centra sobre porciones de pixel cuando el zoom es
|
389 |
* mayor de 1:1.
|
390 |
* @param center
|
391 |
*/
|
392 |
public void setPixelCenter(int x, int y) { |
393 |
if(extReq == null) |
394 |
canvas.setPixelCenter(x, y); |
395 |
else {
|
396 |
//System.out.println(new Point2D.Double(x, y));
|
397 |
Point2D p1 = canvas.viewCoordsToWorld(new Point2D.Double(x, y)); |
398 |
//System.out.println(p1);
|
399 |
//System.out.println(canvas.viewCoordsFromWorld(p1));
|
400 |
/*canvas.setZoom(1);
|
401 |
canvas.setViewCenter(new Point2D.Double(x, y));
|
402 |
double cx = canvas.getCenter().getX() - (canvas.getCanvasExtent().getWidth() / 2);
|
403 |
double cy = canvas.getCenter().getY() - (canvas.getCanvasExtent().getHeight() / 2);
|
404 |
extReq.request(new Rectangle2D.Double(cx, cy, canvas.getCanvasExtent().getWidth(), canvas.getCanvasExtent().getHeight()));*/
|
405 |
} |
406 |
} |
407 |
|
408 |
/**
|
409 |
* Registra un objeto IExtensionRequest para que no se aplique un escalado sobre
|
410 |
* el buffer pasado por par?metro. Alternativamente a la aplicaci?n de este escalado
|
411 |
* se ejecutar? el m?todo request del interfaz para que el cliente pueda pasar un
|
412 |
* nuevo buffer con escala 1:1 y con la extensi?n correspondiente al zoom, Es decir, se
|
413 |
* deja en manos del cliente la aplicaci?n del zoom.
|
414 |
* @param er
|
415 |
*/
|
416 |
public void setExtensionRequest(IExtensionRequest er) { |
417 |
this.extReq = er;
|
418 |
} |
419 |
|
420 |
/**
|
421 |
* Obtiene el historico de zooms
|
422 |
* @return ZoomRecord
|
423 |
*/
|
424 |
public ViewRecord getZoomRecord() {
|
425 |
if(zoomRecord == null) { |
426 |
zoomRecord = new ViewRecord();
|
427 |
//zoomRecord.setRequest(new Integer(FULL_VIEW));
|
428 |
} |
429 |
return zoomRecord;
|
430 |
} |
431 |
|
432 |
/**
|
433 |
* Eventos de los botones zoom m?s y zoom menos.
|
434 |
*/
|
435 |
public void actionPerformed(ActionEvent e) { |
436 |
if(extReq == null) { |
437 |
if(e.getSource() == bZoomMas)
|
438 |
canvas.setZoom(canvas.getZoom() * stepScale); |
439 |
|
440 |
if(e.getSource() == bZoomMenos)
|
441 |
canvas.setZoom(canvas.getZoom() / stepScale); |
442 |
|
443 |
if(e.getSource() == bFullView)
|
444 |
canvas.setZoom(1);
|
445 |
|
446 |
} else {
|
447 |
double width = 0, height = 0; |
448 |
canvas.setZoom(1);
|
449 |
if(e.getSource() == bFullView) {
|
450 |
extReq.fullExtent(); |
451 |
return;
|
452 |
} |
453 |
if(e.getSource() == bPrevZoom) {
|
454 |
Object request = getZoomRecord().getRequest();
|
455 |
if(request != null) { |
456 |
if(request instanceof Rectangle2D) |
457 |
extReq.request((Rectangle2D)request);
|
458 |
if(request instanceof Integer && ((Integer)request).intValue() == FULL_VIEW) |
459 |
extReq.fullExtent(); |
460 |
getZoomRecord().getRequest(); //El que hemos metido al hacer zoom previo no sirve por lo que hay que sacarlo
|
461 |
} |
462 |
return;
|
463 |
} |
464 |
if(e.getSource() == bSelectZoomArea) {
|
465 |
selectTool(ZoomRectangleTool.class); |
466 |
return;
|
467 |
} |
468 |
if(e.getSource() == bZoomMas) {
|
469 |
width = canvas.getCanvasExtent().getWidth() * SCALE; |
470 |
height = canvas.getCanvasExtent().getHeight() * SCALE; |
471 |
} |
472 |
if(e.getSource() == bZoomMenos) {
|
473 |
width = canvas.getCanvasExtent().getWidth() / SCALE; |
474 |
height = canvas.getCanvasExtent().getHeight() / SCALE; |
475 |
} |
476 |
//C?digo com?n a bZoomMas y bZoomMenos
|
477 |
double x = canvas.getCenter().getX() - (width / 2); |
478 |
double y = canvas.getCenter().getY() - (height / 2); |
479 |
Rectangle2D request = new Rectangle2D.Double(x, y, width, height); |
480 |
extReq.request(request); |
481 |
} |
482 |
} |
483 |
|
484 |
/**
|
485 |
* Obtiene el ancho del canvas
|
486 |
* @return
|
487 |
*/
|
488 |
public int getCanvasWith() { |
489 |
return canvas.getVisibleRect().width;
|
490 |
} |
491 |
|
492 |
/**
|
493 |
* Obtiene el alto del canvas
|
494 |
* @return
|
495 |
*/
|
496 |
public int getCanvasHeight() { |
497 |
return canvas.getVisibleRect().height;
|
498 |
} |
499 |
|
500 |
/**
|
501 |
* Evento de finalizaci?n de la tool que maneja el viewControl
|
502 |
*/
|
503 |
public void endAction(ToolEvent ev) { |
504 |
if(ev.getSource() instanceof ZoomRectangleTool) { |
505 |
Rectangle2D extent = (Rectangle2D)canvas.getSelectedTool().getResult(); |
506 |
extReq.request(extent); |
507 |
this.selectTool(null); |
508 |
} |
509 |
} |
510 |
|
511 |
} |