Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / ZoomControlPanel.java @ 3058

History | View | Annotate | Download (17.6 KB)

1
package com.iver.cit.gvsig.gui.Panels;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Canvas;
5
import java.awt.Color;
6
import java.awt.Component;
7
import java.awt.Graphics;
8
import java.awt.Graphics2D;
9
import java.awt.GridBagConstraints;
10
import java.awt.GridBagLayout;
11
import java.awt.Image;
12
import java.awt.event.ActionEvent;
13
import java.awt.event.ActionListener;
14
import java.awt.event.MouseEvent;
15
import java.awt.event.MouseListener;
16
import java.awt.event.MouseMotionListener;
17
import java.awt.geom.Point2D;
18
import java.awt.geom.Rectangle2D;
19
import java.awt.image.BufferedImage;
20

    
21
import javax.swing.ImageIcon;
22
import javax.swing.JButton;
23
import javax.swing.JOptionPane;
24
import javax.swing.JPanel;
25

    
26
import com.iver.andami.PluginServices;
27
import com.iver.cit.gvsig.fmap.DriverException;
28
import com.iver.cit.gvsig.fmap.ViewPort;
29
import com.iver.cit.gvsig.fmap.layers.FLayer;
30
import com.iver.cit.gvsig.fmap.layers.FLayers;
31
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
32
import com.iver.cit.gvsig.fmap.layers.FLyrPoints;
33
import com.iver.cit.gvsig.fmap.operations.Cancellable;
34
import com.iver.cit.gvsig.gui.View;
35
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
36

    
37
/**
38
 * Panel que contiene los controles de zoom de la vista para 
39
 * afinar en la georreferenciaci?n.
40
 * 
41
 * @author Nacho Brodin (brodin_ign@gva.es)
42
 *
43
 */
44
public class ZoomControlPanel extends JPanel implements ActionListener{
45
        private JPanel pImage = null;
46
        private JPanel pControls = null;
47
        private JButton bZoomMas = null;
48
        private JButton bZoomMenos = null;
49

    
50
        private int sX = 140, sY = 140; 
51
        private CanvasZone canvas = null;
52
        private ViewPort viewPort = null;
53
        
54
    private int numberPoint = -1;
55
    
56
    public FLyrPoints lyrPoints = null;
57
    public FLyrGeoRaster lyrGeoRaster = null;
58
    public GeoreferencingDialog dialog = null;
59
    private boolean rightControl = false;
60
    
61
   
62
    
63
        /**
64
         * This is the default constructor
65
         */
66
        public ZoomControlPanel(boolean rightControl) {
67
                super();
68
                this.rightControl = rightControl;
69
                initialize(rightControl);
70
        }
71

    
72
        /**
73
         * This method initializes this
74
         * 
75
         * @return void
76
         */
77
        private void initialize(boolean rightControl) {
78
                
79
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
80
        if(rightControl)
81
                gridBagConstraints1.gridx = 1;
82
        else
83
                gridBagConstraints1.gridx = 0;
84
        gridBagConstraints1.gridy = 0;
85
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
86
        if(rightControl)
87
                gridBagConstraints.gridx = 0;
88
        else
89
                gridBagConstraints.gridx = 1;
90
        gridBagConstraints.gridy = 0;
91
        this.setLayout(new GridBagLayout());
92
        this.setPreferredSize(new java.awt.Dimension(175,142));
93
        this.setSize(new java.awt.Dimension(175,142));
94
        this.setLocation(new java.awt.Point(0,0));
95
        this.add(getPImage(), gridBagConstraints);
96
        this.add(getPControls(), gridBagConstraints1);        
97
        }
98

    
99
        /**
100
         * Carga las capas de puntos y de raster georreferenciado
101
         *
102
         */
103
        public void loadLayer(){
104
                //Cargamos la capa
105
                View theView = (View) PluginServices.getMDIManager().getActiveView();
106
                for(int i=0;i<theView.getMapControl().getMapContext().getLayers().getLayersCount();i++){
107
                        FLayer lyr = theView.getMapControl().getMapContext().getLayers().getLayer(i);
108
                        if(lyr instanceof FLyrPoints)
109
                                lyrPoints = (FLyrPoints)lyr;
110
                        if(        lyr instanceof FLyrGeoRaster && 
111
                                lyr.getName().startsWith("*") &&
112
                                lyr.isActive())
113
                                lyrGeoRaster = (FLyrGeoRaster)lyr;
114
                }
115
                dialog = lyrGeoRaster.getGeoDialog();
116
        }
117
        
118
        /**
119
         * This method initializes jPanel        
120
         *         
121
         * @return javax.swing.JPanel        
122
         */
123
        private JPanel getPImage() {
124
                if (pImage == null) {
125
                        pImage = new JPanel();
126
                        BorderLayout borderLayout = new BorderLayout();
127
                        pImage.setLayout(borderLayout);
128
                        pImage.setPreferredSize(new java.awt.Dimension(140,140));
129
                        pImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
130
                        if(canvas == null){
131
                                canvas = new CanvasZone(sX, sY, this);
132
                                canvas.setVisible(false);
133
                                canvas.addMouseListener(canvas);
134
                                canvas.addMouseMotionListener(canvas);
135
                        }
136
                        pImage.add(canvas, BorderLayout.CENTER);
137
                        
138
                }
139
                return pImage;
140
        }
141

    
142
        /**
143
         * This method initializes jPanel1        
144
         *         
145
         * @return javax.swing.JPanel        
146
         */
147
        private JPanel getPControls() {
148
                if (pControls == null) {
149
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
150
                        gridBagConstraints3.gridx = 0;
151
                        gridBagConstraints3.insets = new java.awt.Insets(5,0,0,0);
152
                        gridBagConstraints3.gridy = 1;
153
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
154
                        gridBagConstraints2.gridx = 0;
155
                        gridBagConstraints2.gridy = 0;
156
                        pControls = new JPanel();
157
                        pControls.setLayout(new GridBagLayout());
158
                        pControls.setPreferredSize(new java.awt.Dimension(33,140));
159
                        pControls.add(getBZoomMas(), gridBagConstraints2);
160
                        pControls.add(getBZoomMenos(), gridBagConstraints3);
161
                }
162
                return pControls;
163
        }
164

    
165
        /**
166
         * This method initializes jButton        
167
         *         
168
         * @return javax.swing.JButton        
169
         */
170
        private JButton getBZoomMas() {
171
                if (bZoomMas == null) {
172
                        bZoomMas = new JButton();
173
                        bZoomMas.setPreferredSize(new java.awt.Dimension(25,25));
174
                        bZoomMas.setIcon(new ImageIcon(getClass().getResource("/com/iver/cit/gvsig/gui/Panels/images/aumentar.png")));
175
                        bZoomMas.addActionListener(this);
176
                }
177
                return bZoomMas;
178
        }
179

    
180
        /**
181
         * This method initializes jButton        
182
         *         
183
         * @return javax.swing.JButton        
184
         */
185
        private JButton getBZoomMenos() {
186
                if (bZoomMenos == null) {
187
                        bZoomMenos = new JButton();
188
                        bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25));
189
                        bZoomMenos.setIcon(new ImageIcon(getClass().getResource("/com/iver/cit/gvsig/gui/Panels/images/disminuir.png")));
190
                        bZoomMenos.addActionListener(this);
191
                }
192
                return bZoomMenos;
193
        }
194

    
195
        /**
196
         * @param viewPort The viewPort to set.
197
         */
198
        public void setViewPort(ViewPort viewPort) {
199
                this.viewPort = viewPort;
200
                canvas.setViewPort(this.viewPort);
201
        }
202
        
203
        /**
204
         *Llama a la funci?n de pintado del canvas con los par?metros que tenga en ese momento
205
         */
206
        public void draw(){
207
                if(canvas != null){
208
                        canvas.repaint();
209
                }
210
        }
211
        
212
        /**
213
         * Asigna al canvas la propiedad de visibilidad a verdadero o falso.
214
         * @param visible true para mostrar el canvas y false para ocultarlo
215
         */
216
        public void setVisible(boolean visible){
217
                canvas.setVisible(visible);
218
                if(!visible)
219
                        canvas.setSize(0,0);
220
                else{
221
                        canvas.setSize(sX, sY);
222
                }
223
                canvas.repaint();
224
        }
225
        
226
        /**
227
         * Asigna el punto sobre el que se centrar? el zoom de la minimagen
228
         * @param pto
229
         */
230
        public void setCenterPoint(Point2D pto){
231
                canvas.setCenterPoint(pto);
232
        }
233
        
234
        /**
235
         * Asigna el n?mero de punto que se est? tratando
236
         * @param n
237
         */
238
        public void setNumberPoint(int n){
239
                this.numberPoint = n;
240
        }
241
        
242
        /**
243
         * Recupera el n?mero de punto que se est? tratando
244
         * @param n
245
         */
246
        public int getNumberPoint(){
247
                return this.numberPoint;
248
        }
249
        
250
        /**
251
         * Manejo de los controles zoom m?s y zoom menos
252
         */
253
        public void actionPerformed(ActionEvent e) {
254
                if(e.getSource() == bZoomMas){
255
                        canvas.calcZoom(0.6);
256
                        canvas.repaint();
257
                }
258
                
259
                if(e.getSource() == bZoomMenos){
260
                        canvas.calcZoom(1.8);
261
                        canvas.repaint();
262
                }
263
                
264
        }
265
        
266

    
267
        /**
268
         * @return Returns the rightControl.
269
         */
270
        public boolean isRightControl() {
271
                return rightControl;
272
        }
273
        
274
        /**
275
         * @return Returns the lyrGeoRaster.
276
         */
277
        public FLyrGeoRaster getLyrGeoRaster() {
278
                return lyrGeoRaster;
279
        }
280

    
281
        
282
        /**
283
         * @return Returns the lyrPoints.
284
         */
285
        public FLyrPoints getLyrPoints() {
286
                return lyrPoints;
287
        }
288
        
289
        /**
290
         * @return Returns the dialog.
291
         */
292
        public GeoreferencingDialog getDialog() {
293
                if(dialog == null)
294
                        this.loadLayer();
295
                return dialog;
296
        }        
297
        
298
    /**
299
     * Asigna el viewPort recalculado
300
     * @param vp ViewPort
301
     */
302
    public void setNewViewPort(ViewPort vp){
303
            canvas.setNewViewPort(vp);
304
    }
305
    
306
        /**
307
         * Control que representa la zona de dibujado de la ventana. Este se encarga de
308
         * repintar el contenido sobre el Graphics2D a partir del viewPort que se le pase
309
         * @author Nacho Brodin (brodin_ign@gva.es)
310
         */
311
        class CanvasZone extends Canvas implements MouseListener, MouseMotionListener{
312
                private ZoomControlPanel zoomControl = null;
313
            private int ancho;
314
            private int alto;
315
            private FLayers flayers = null;
316
            private ViewPort viewPort = null;
317
            private ViewPort newViewPort = null;
318
            private Point2D centerPoint = null;
319
            private int zoom = 20;
320
            private boolean dragged = false;
321
            private Image dobleBuffer = null;
322
            private BufferedImage imgTmp = null;
323
            private Graphics dbGraphics = null;
324
            
325
            /**
326
             * Puntos inicial y final para el desplazamiento del punto de control
327
             */
328
            private Point2D ptoIni = null, ptoFin = null;
329
            private Point2D movDragged = null, initCrux = null;
330
              
331

    
332
            public CanvasZone( int anc,int alt, ZoomControlPanel zc) {
333
                    zoomControl = zc;
334
                ancho = anc;
335
                alto = alt;
336
              
337
                this.setSize(ancho, alto);            
338
                this.setBackground(Color.WHITE);
339
                
340
                imgTmp = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
341
                dobleBuffer = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
342
                dbGraphics = dobleBuffer.getGraphics();
343
            }
344
            
345
            /**
346
             * Asigna el viewPort
347
             * @param vp ViewPort
348
             */
349
            public void setViewPort(ViewPort vp){
350
                    this.viewPort = vp;
351
                    this.initViewPort();
352
            }
353

    
354
            /**
355
             * Asigna el viewPort recalculado
356
             * @param vp ViewPort
357
             */
358
            public void setNewViewPort(ViewPort vp){
359
                    this.newViewPort = vp;
360
            }
361
            
362
            /**
363
             * Calcula el zoom dependiendo del factor de escala pasado por
364
             * par?metro.
365
             */
366
            public void calcZoom(double factor){
367
                    Rectangle2D.Double r = new Rectangle2D.Double();
368
                        double nuevoX = centerPoint.getX() -
369
                                ((newViewPort.getExtent().getWidth() * factor) / 2.0);
370
                        double nuevoY = centerPoint.getY() -
371
                                ((newViewPort.getExtent().getHeight() * factor) / 2.0);
372
                        r.x = nuevoX;
373
                        r.y = nuevoY;
374
                        r.width = newViewPort.getExtent().getWidth() * factor;
375
                        r.height = newViewPort.getExtent().getHeight() * factor;
376
                        newViewPort.setExtent(r);
377
            }
378
            
379
            /**
380
             *Calculamos el viewPort para el zoom de la minimagen
381
             */
382
            public void initViewPort(){
383
                    if(centerPoint != null){
384
                            newViewPort = this.viewPort.cloneViewPort();
385
                        //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
386
                                double relacionPixelWcWidth =  ((viewPort.getExtent().getMaxX() - viewPort.getExtent().getMinX()) / zoom)/this.getWidth();
387
                                double relacionPixelWcHeight = ((viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY()) / zoom)/this.getHeight();
388
                                
389
                                double wcOriginX = centerPoint.getX() - ((viewPort.getImageWidth()*relacionPixelWcWidth)/2);
390
                                double wcOriginY = centerPoint.getY() - ((viewPort.getImageHeight()*relacionPixelWcHeight)/2);
391
                                
392
                                double wcDstMinX = wcOriginX; 
393
                                double wcDstMinY = wcOriginY; 
394
                                double wcDstMaxX = wcDstMinX + (viewPort.getImageWidth()*relacionPixelWcWidth);
395
                                double wcDstMaxY = wcDstMinY + (viewPort.getImageHeight()*relacionPixelWcHeight);
396
                                
397
                                double wcDstWidth = wcDstMaxX - wcDstMinX;
398
                                double wcDstHeight = wcDstMaxY - wcDstMinY;
399
                                                                
400
                        Rectangle2D ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
401
                        
402
                        newViewPort.setExtent(ext);
403
                            newViewPort.setImageSize(new java.awt.Dimension(this.getWidth(), this.getHeight()));        
404
                            newViewPort.setScale();
405
                    }
406
            }
407
            
408
            public void loadBuffer(){
409
                                        
410
                    Graphics2D gTmp = (Graphics2D)imgTmp.getGraphics();
411
            
412
                    if(!dragged){
413
                            dbGraphics.setColor( Color.WHITE );
414
                            dbGraphics.fillRect( 0,0,ancho,alto );
415
                            gTmp.setColor( Color.WHITE );
416
                                gTmp.fillRect( 0,0,ancho,alto );
417
                        
418
                        try{                            
419
                                if(newViewPort != null){
420
                                        int pos = Integer.valueOf(zoomControl.getDialog().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
421
                                
422
                                        if(!zoomControl.isRightControl())
423
                                                zoomControl.getLyrPoints().setMiniExtent(pos, 
424
                                                                                                                                centerPoint,
425
                                                                                                                                        newViewPort, 
426
                                                                                                                                        true);
427
                                        else
428
                                                zoomControl.getLyrPoints().setMiniExtent(pos, 
429
                                                                                                                                        centerPoint,
430
                                                                                                                                        newViewPort, 
431
                                                                                                                                        false);
432
                                        
433
                                        //Si no tenemos las capas las cargamos
434
                                        if(flayers == null){
435
                                                 View theView = (View) PluginServices.getMDIManager().getActiveView();
436
                                                 flayers = theView.getMapControl().getMapContext().getLayers();
437
                                        }
438
                                        //dibujamos
439
                                        flayers.draw(null, (Graphics2D) dbGraphics, newViewPort,new Cancellable() {
440
                                                public boolean isCanceled() {
441
                                                                return false;
442
                                                        }
443
                                        },flayers.getFMap().getScaleView());
444
                                        flayers.draw(null, gTmp, newViewPort,new Cancellable() {
445
                                                public boolean isCanceled() {
446
                                                                return false;
447
                                                        }
448
                                        },flayers.getFMap().getScaleView());
449
                                }
450
                        }catch (DriverException e) {
451
                                 e.printStackTrace();
452
                        }
453
                }else{
454
                                double x, y;
455
                                if(movDragged != null){
456
                                        x = (movDragged.getX() - ptoIni.getX()) + initCrux.getX();
457
                                        y = (movDragged.getY() - ptoIni.getY()) + initCrux.getY();
458
                                }else{
459
                                        x = ptoIni.getX() + initCrux.getX();
460
                                        y = ptoIni.getY() + initCrux.getY();
461
                                }
462

    
463
                                dbGraphics.drawImage(imgTmp, 0, 0, null);
464
                        dbGraphics.setColor( Color.RED );
465
                        dbGraphics.drawLine((int)(x - 5), (int)y, (int)(x + 5), (int)y);
466
                        dbGraphics.drawLine((int)x, (int)(y - 5), (int)x, (int)(y + 5));
467
                }
468
            }
469
            
470
            
471
            public void update(Graphics g){
472
                    this.paint(g);
473
            }
474
            
475
            /**
476
             * Dibuja sobre el Graphics2D pasado el viewPort solicitado a
477
             * la lista de capas.
478
             * @param g
479
             */
480
            public void paint( Graphics g ) {
481
                    loadBuffer();
482
                    g.drawImage(dobleBuffer, 0, 0, null);
483
            }
484
            
485
            /**
486
             * Asigna el punto donde se centrar? la vista de la minimagen
487
             * @param pto Punto central.
488
             */
489
            public void setCenterPoint(Point2D pto){
490
                    this.centerPoint = pto;
491
            }
492
                        
493
                /* (non-Javadoc)
494
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
495
                 */
496
                public void mouseClicked(MouseEvent ev) {
497
                        
498
                }
499
                
500
                /* (non-Javadoc)
501
                 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
502
                 */
503
                public void mouseEntered(MouseEvent arg0) {
504
                        
505
                }
506
                
507
                /* (non-Javadoc)
508
                 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
509
                 */
510
                public void mouseExited(MouseEvent arg0) {
511
                        
512
                }
513
                
514
                /**
515
                 * Al pulsar el rat?n sobre la minimagen permitimos que se pueda desplazar la cruz
516
                 * 
517
                 */
518
                public void mousePressed(MouseEvent ev) {
519
                        this.movDragged = ptoIni = ev.getPoint();
520
                        zoomControl.loadLayer();
521
                        initCrux = new Point2D.Double();
522
                        initCrux.setLocation(this.getWidth() / 2, this.getHeight() / 2);
523
                        this.dragged = true;
524
                        repaint();
525
                }
526
                
527
                /* (non-Javadoc)
528
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
529
                 */
530
                public void mouseReleased(MouseEvent ev) {
531
                        View theView = (View) PluginServices.getMDIManager().getActiveView();
532
                        ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
533
                        ptoFin = ev.getPoint();
534
                        this.dragged = false;
535

    
536
                        double destX = newViewPort.getExtent().getMinX() + ((ptoFin.getX() * (centerPoint.getX() - newViewPort.getExtent().getMinX())) / ptoIni.getX()); 
537
                        double destY = newViewPort.getExtent().getMinY() + (((this.getHeight() - ptoFin.getY()) * (centerPoint.getY() - newViewPort.getExtent().getMinY())) / (this.getHeight() - ptoIni.getY()));
538
                                                
539
                        if(flayers == null)
540
                         flayers = theView.getMapControl().getMapContext().getLayers();
541
                 for(int i=0;i<flayers.getLayersCount();i++){
542
                         FLayer lyr = flayers.getLayer(i);
543
                         if(lyr instanceof FLyrPoints){
544
                                 FLyrPoints lp = (FLyrPoints)lyr;
545
                                 
546
                                 if(zoomControl.isRightControl()){ //Control de actualizaci?n de pixel de imagen
547
                                         Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(destX, destY);
548
                                         
549
                                         if(pixelImg == null){
550
                                                     JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
551
                                                                     PluginServices.getText(this, "fuera_de_extent"));
552
                                                     return;
553
                                              }
554
                                         zoomControl.getLyrGeoRaster().updateData(        Integer.valueOf(zoomControl.getDialog().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue(),
555
                                                                                                                                 pixelImg, 
556
                                                                                                                                        null, 
557
                                                                                                                                        zoomControl.getDialog(),
558
                                                                                                                                        theView, 
559
                                                                                                                                        lp);
560
                                         
561
                                 }else{ //Control de actualizaci?n de coordenadas del mundo
562
                                          Point2D mapPoint = new Point2D.Double();
563
                                          mapPoint.setLocation(destX, destY);
564
                                          zoomControl.getLyrGeoRaster().updateData(        Integer.valueOf(zoomControl.getDialog().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue(),
565
                                                                                                                                        null, 
566
                                                                                                                                        mapPoint, 
567
                                                                                                                                        zoomControl.getDialog(),
568
                                                                                                                                        theView, 
569
                                                                                                                                        lp);
570
                                 }
571
                         }
572
                 }
573
                 
574
                        
575
                        repaint();
576
                }
577
                
578
                /* (non-Javadoc)
579
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
580
                 */
581
                public void mouseDragged(MouseEvent ev) {
582
                        this.movDragged = ev.getPoint();
583
                        repaint();
584
                        
585
                }
586
                
587
                /* (non-Javadoc)
588
                 * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
589
                 */
590
                public void mouseMoved(MouseEvent arg0) {
591
                        
592
                }
593
                
594
        }
595

    
596

    
597

    
598
        
599

    
600
 }