Statistics
| Revision:

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

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
        }
201
        
202
        /**
203
         *Llama a la funci?n de pintado del canvas con los par?metros que tenga en ese momento
204
         */
205
        public void draw(){
206
                if(canvas != null){
207
                        canvas.setViewPort(this.viewPort);
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
                    System.out.println("===>"+newViewPort.getExtent());
361
            }
362
            
363
            /**
364
             * Calcula el zoom dependiendo del factor de escala pasado por
365
             * par?metro.
366
             */
367
            public void calcZoom(double factor){
368
                    Rectangle2D.Double r = new Rectangle2D.Double();
369
                        double nuevoX = centerPoint.getX() -
370
                                ((newViewPort.getExtent().getWidth() * factor) / 2.0);
371
                        double nuevoY = centerPoint.getY() -
372
                                ((newViewPort.getExtent().getHeight() * factor) / 2.0);
373
                        r.x = nuevoX;
374
                        r.y = nuevoY;
375
                        r.width = newViewPort.getExtent().getWidth() * factor;
376
                        r.height = newViewPort.getExtent().getHeight() * factor;
377
                        newViewPort.setExtent(r);
378
            }
379
            
380
            /**
381
             *Calculamos el viewPort para el zoom de la minimagen
382
             */
383
            public void initViewPort(){
384
                    if(centerPoint != null){
385
                            newViewPort = this.viewPort.cloneViewPort();
386
                        //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
387
                                double relacionPixelWcWidth =  ((viewPort.getExtent().getMaxX() - viewPort.getExtent().getMinX()) / zoom)/this.getWidth();
388
                                double relacionPixelWcHeight = ((viewPort.getExtent().getMaxY() - viewPort.getExtent().getMinY()) / zoom)/this.getHeight();
389
                                
390
                                double wcOriginX = centerPoint.getX() - ((viewPort.getImageWidth()*relacionPixelWcWidth)/2);
391
                                double wcOriginY = centerPoint.getY() - ((viewPort.getImageHeight()*relacionPixelWcHeight)/2);
392
                                
393
                                double wcDstMinX = wcOriginX; 
394
                                double wcDstMinY = wcOriginY; 
395
                                double wcDstMaxX = wcDstMinX + (viewPort.getImageWidth()*relacionPixelWcWidth);
396
                                double wcDstMaxY = wcDstMinY + (viewPort.getImageHeight()*relacionPixelWcHeight);
397
                                
398
                                double wcDstWidth = wcDstMaxX - wcDstMinX;
399
                                double wcDstHeight = wcDstMaxY - wcDstMinY;
400
                                                                
401
                        Rectangle2D ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
402
                        
403
                        newViewPort.setExtent(ext);
404
                            newViewPort.setImageSize(new java.awt.Dimension(this.getWidth(), this.getHeight()));        
405
                            newViewPort.setScale();
406
                    }
407
            }
408
            
409
            public void loadBuffer(){
410
                                        
411
                    Graphics2D gTmp = (Graphics2D)imgTmp.getGraphics();
412
            
413
                    if(!dragged){
414
                            dbGraphics.setColor( Color.WHITE );
415
                            dbGraphics.fillRect( 0,0,ancho,alto );
416
                            gTmp.setColor( Color.WHITE );
417
                                gTmp.fillRect( 0,0,ancho,alto );
418
                        
419
                        try{                            
420
                                if(newViewPort != null){
421
                                        int pos = Integer.valueOf(zoomControl.getDialog().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
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
                                        //Si no tenemos las capas las cargamos
433
                                        if(flayers == null){
434
                                                 View theView = (View) PluginServices.getMDIManager().getActiveView();
435
                                                 flayers = theView.getMapControl().getMapContext().getLayers();
436
                                        }
437
                                        //dibujamos
438
                                        flayers.draw(null, (Graphics2D) dbGraphics, newViewPort,new Cancellable() {
439
                                                public boolean isCanceled() {
440
                                                                return false;
441
                                                        }
442
                                        },flayers.getFMap().getScaleView());
443
                                        flayers.draw(null, gTmp, newViewPort,new Cancellable() {
444
                                                public boolean isCanceled() {
445
                                                                return false;
446
                                                        }
447
                                        },flayers.getFMap().getScaleView());
448
                                }
449
                        }catch (DriverException e) {
450
                                 e.printStackTrace();
451
                        }
452
                }else{
453
                                double x, y;
454
                                if(movDragged != null){
455
                                        x = (movDragged.getX() - ptoIni.getX()) + initCrux.getX();
456
                                        y = (movDragged.getY() - ptoIni.getY()) + initCrux.getY();
457
                                }else{
458
                                        x = ptoIni.getX() + initCrux.getX();
459
                                        y = ptoIni.getY() + initCrux.getY();
460
                                }
461

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

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

    
594

    
595

    
596
        
597

    
598
 }