Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / gui / panels / ZoomControlPanel.java @ 5563

History | View | Annotate | Download (30.4 KB)

1
package org.gvsig.georeferencing.gui.panels;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Component;
6
import java.awt.Cursor;
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.Point;
13
import java.awt.Toolkit;
14
import java.awt.event.ActionEvent;
15
import java.awt.event.ActionListener;
16
import java.awt.event.MouseEvent;
17
import java.awt.event.MouseListener;
18
import java.awt.event.MouseMotionListener;
19
import java.awt.geom.Point2D;
20
import java.awt.geom.Rectangle2D;
21
import java.awt.image.BufferedImage;
22

    
23
import javax.swing.ImageIcon;
24
import javax.swing.JButton;
25
import javax.swing.JOptionPane;
26
import javax.swing.JPanel;
27

    
28
import org.gvsig.georeferencing.gui.dialogs.GeoreferencingDialog;
29
import org.gvsig.georeferencing.utils.MathUtils;
30

    
31
import com.iver.andami.PluginServices;
32
import com.iver.cit.gvsig.fmap.DriverException;
33
import com.iver.cit.gvsig.fmap.ViewPort;
34
import com.iver.cit.gvsig.fmap.layers.FLayer;
35
import com.iver.cit.gvsig.fmap.layers.FLayers;
36
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
37
import com.iver.cit.gvsig.fmap.layers.FLyrPoints;
38
import com.iver.cit.gvsig.gui.View;
39
import com.iver.utiles.swing.threads.Cancellable;
40

    
41
/**
42
 * Panel que contiene los controles de zoom de la vista para 
43
 * afinar en la georreferenciaci?n.
44
 * 
45
 * @author Nacho Brodin (brodin_ign@gva.es)
46
 *
47
 */
48
public class ZoomControlPanel extends JPanel implements ActionListener{
49
        
50
        //**********************Vars**********************************
51
        private JPanel                                         pImage = null;
52
        private JPanel                                         pControls = null;
53
        private JButton                                 bZoomMas = null;
54
        private JButton                                 bZoomMenos = null;
55
        private int                                         sX = 140, sY = 140; 
56
        private CanvasZone                                 canvas = null;
57
        private ViewPort                                 viewPort = null;
58
    public FLyrPoints                                 lyrPoints = null;
59
    public FLyrGeoRaster                         lyrGeoRaster = null;
60
    public GeoreferencingDialog         dialog = null;
61
    private boolean                                 rightControl = false;
62
    /**
63
         * Estado de los botones cuando se ejecuta disableAllControls
64
         */
65
        private boolean[]                                 buttonsState = new boolean[2];
66
        /**
67
         * DisableAllControls ha sido ejecutada si est? a true esta variabled
68
         */
69
        private boolean                                 disableAllControls = false;
70
        /**
71
         * Factor multiplicador cuando hacemos zoom mas
72
         */
73
        private double                                        zoomMas = 0.6;
74
        /**
75
         * Factor multiplicador cuando hacemos zoom menos
76
         */
77
        private double                                        zoomMenos = 1.8;
78
        //**********************End Vars******************************
79
    
80
        //**********************Methods*******************************
81
        /**
82
         * This is the default constructor
83
         */
84
        public ZoomControlPanel(boolean rightControl, GeoreferencingDialog dialog) {
85
                super();
86
                this.rightControl = rightControl;
87
                this.dialog = dialog;
88
                initialize(rightControl);
89
        }
90

    
91
        /**
92
         * This method initializes this
93
         * 
94
         * @return void
95
         */
96
        private void initialize(boolean rightControl) {
97
                
98
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
99
        if(rightControl)
100
                gridBagConstraints1.gridx = 1;
101
        else
102
                gridBagConstraints1.gridx = 0;
103
        gridBagConstraints1.gridy = 0;
104
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
105
        if(rightControl)
106
                gridBagConstraints.gridx = 0;
107
        else
108
                gridBagConstraints.gridx = 1;
109
        gridBagConstraints.gridy = 0;
110
        this.setLayout(new GridBagLayout());
111
        this.setPreferredSize(new java.awt.Dimension(175,142));
112
        this.setSize(new java.awt.Dimension(175,142));
113
        this.setLocation(new java.awt.Point(0,0));
114
        this.add(getPImage(), gridBagConstraints);
115
        this.add(getPControls(), gridBagConstraints1);
116
        //Desactivamos el control en la inicializaci?n
117
        this.setEnabled(false);
118
        }
119

    
120
        /**
121
         * Carga las capas de puntos y de raster georreferenciado
122
         *
123
         */
124
        public void loadLayer() throws InstantiationException{
125
                //Cargamos las capas
126
                this.lyrGeoRaster = dialog.getLyrGeoRaster();
127
                this.lyrPoints = dialog.getLyrPoints();
128
                
129
                if(lyrGeoRaster == null || lyrPoints == null)
130
                        throw new InstantiationException("No se ha podido instanciar la capa de puntos o georreferenciada");
131
                
132
        }
133
                
134
        /**
135
         *Llama a la funci?n de pintado del canvas con los par?metros que tenga en ese momento
136
         */
137
        public void draw(){
138
                if(!getDialog().isEnlarge())
139
                        getCanvas().loadBuffer();
140
                canvas.draw = true;
141
                getCanvas().repaint();
142
        }
143
                
144
        /**
145
         * Manejo de los controles zoom m?s y zoom menos
146
         */
147
        public void actionPerformed(ActionEvent e) {
148
                double zoom = zoomMas;
149
                
150
                if(e.getSource() == bZoomMas)
151
                        canvas.calcZoom(zoomMas);
152
                
153
                if(e.getSource() == bZoomMenos){
154
                        canvas.calcZoom(zoomMenos);
155
                        zoom = zoomMenos;
156
                }
157
            
158
                canvas.draw = true;
159
                canvas.repaint();
160
                if(isRightControl())
161
                        lyrPoints.getPoint(dialog.getSelectedPoint()).zoomLeft *= zoom;
162
                else
163
                        lyrPoints.getPoint(dialog.getSelectedPoint()).zoomRight *= zoom;
164
                
165
                lyrPoints.showPoints();
166
        }
167
            
168
    /**
169
     *Limpia el canvas
170
     */
171
    public void clear(){
172
            canvas.setCleanCanvas(true);
173
            canvas.repaint();
174
    }
175
    
176
    /**
177
         * Esta funci?n deshabilita todos los controles y guarda sus valores
178
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
179
         * se vuelvan a quedar como estaba
180
         */
181
        public void disableAllControls(){
182
                if(!disableAllControls){
183
                        disableAllControls = true;
184
                        
185
                        //Salvamos los estados
186
                        buttonsState[0] = getBZoomMas().isEnabled();
187
                        buttonsState[1] = getBZoomMenos().isEnabled();
188
                        
189
                        //Desactivamos controles
190
                        getBZoomMas().setEnabled(false);
191
                        getBZoomMenos().setEnabled(false);
192
                }
193
        }
194
        
195
        /**
196
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n 
197
         * disableAllControls
198
         */
199
        public void restoreControlsValue(){
200
                if(disableAllControls){
201
                        disableAllControls = false;
202
                        getBZoomMas().setEnabled(buttonsState[0]);
203
                        getBZoomMenos().setEnabled(buttonsState[1]);
204
                }
205
        }
206
        //**********************End Methods***************************
207
    
208
    //**********************Setters & Getters*********************
209
    /**
210
         * @param viewPort The viewPort to set.
211
         */
212
        public void setViewPort(ViewPort viewPort, double zoom) {
213
                this.viewPort = viewPort;
214
                getCanvas().setViewPort(this.viewPort, zoom);
215
        }
216
        
217
        /**
218
         * This method initializes jPanel        
219
         *         
220
         * @return javax.swing.JPanel        
221
         */
222
        private JPanel getPImage() {
223
                if (pImage == null) {
224
                        pImage = new JPanel();
225
                        BorderLayout borderLayout = new BorderLayout();
226
                        pImage.setLayout(borderLayout);
227
                        pImage.setPreferredSize(new java.awt.Dimension(140,140));
228
                        pImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
229
                        pImage.add(getCanvas(), BorderLayout.CENTER);
230
                }
231
                return pImage;
232
        }
233

    
234
        /**
235
         * This method initilizes canvas
236
         * @return        CanvasZone
237
         */
238
        public CanvasZone getCanvas(){
239
                if(canvas == null){
240
                        canvas = new CanvasZone(sX, sY, this);
241
                        canvas.setVisible(false);
242
                        canvas.addMouseListener(canvas);
243
                        canvas.addMouseMotionListener(canvas);
244
                }
245
                return canvas;
246
        }
247
        
248
        /**
249
         * This method initializes jPanel1        
250
         *         
251
         * @return javax.swing.JPanel        
252
         */
253
        private JPanel getPControls() {
254
                if (pControls == null) {
255
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
256
                        gridBagConstraints3.gridx = 0;
257
                        gridBagConstraints3.insets = new java.awt.Insets(5,0,0,0);
258
                        gridBagConstraints3.gridy = 1;
259
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
260
                        gridBagConstraints2.gridx = 0;
261
                        gridBagConstraints2.gridy = 0;
262
                        pControls = new JPanel();
263
                        pControls.setLayout(new GridBagLayout());
264
                        pControls.setPreferredSize(new java.awt.Dimension(33,140));
265
                        pControls.add(getBZoomMas(), gridBagConstraints2);
266
                        pControls.add(getBZoomMenos(), gridBagConstraints3);
267
                }
268
                return pControls;
269
        }
270

    
271
        /**
272
         * This method initializes jButton        
273
         *         
274
         * @return javax.swing.JButton        
275
         */
276
        public JButton getBZoomMas() {
277
                if (bZoomMas == null) {
278
                        bZoomMas = new JButton();
279
                        bZoomMas.setPreferredSize(new java.awt.Dimension(25,25));
280
                        bZoomMas.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/aumentar.png")));
281
                        bZoomMas.addActionListener(this);
282
                }
283
                return bZoomMas;
284
        }
285

    
286
        /**
287
         * This method initializes jButton        
288
         *         
289
         * @return javax.swing.JButton        
290
         */
291
        public JButton getBZoomMenos() {
292
                if (bZoomMenos == null) {
293
                        bZoomMenos = new JButton();
294
                        bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25));
295
                        bZoomMenos.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/disminuir.png")));
296
                        bZoomMenos.addActionListener(this);
297
                }
298
                return bZoomMenos;
299
        }
300
        
301
        /**
302
         * Obtiene true si se trata de un control con los botones de zoom
303
         * a la derecha y false si los tiene a la izquierda.
304
         * @return Returns the rightControl.
305
         */
306
        public boolean isRightControl() {
307
                return rightControl;
308
        }
309
        
310
        /**
311
         * Obtiene la capa de imagen georreferenciada.
312
         * @return Returns the lyrGeoRaster.
313
         */
314
        public FLyrGeoRaster getLyrGeoRaster() {
315
                return lyrGeoRaster;
316
        }
317

    
318
        /**
319
         * Obtiene la capa de puntos.
320
         * @return Returns the lyrPoints.
321
         */
322
        public FLyrPoints getLyrPoints() {
323
                try{
324
                if(lyrPoints == null)
325
                        this.loadLayer();
326
                }catch(InstantiationException e){
327
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
328
                                        PluginServices.getText(this, "error_capa_puntos"));
329
                }
330
                return lyrPoints;
331
        }
332
        
333
        /**
334
         * @return Returns the dialog.
335
         */
336
        public GeoreferencingDialog getDialog() {
337
                try{
338
                        if(dialog == null)
339
                                this.loadLayer();
340
                }catch(InstantiationException e){
341
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
342
                                        PluginServices.getText(this, "error_capa_puntos"));
343
                }
344
                return dialog;
345
        }        
346
        
347
    /**
348
     * Asigna el viewPort recalculado
349
     * @param vp ViewPort
350
     */
351
    public void setNewViewPort(ViewPort vp){
352
            canvas.setNewViewPort(vp);
353
    }
354
    
355
    /**
356
     * Obtiene el viewPort recalculado
357
     * @return vp ViewPort
358
     */
359
    public ViewPort getNewViewPort(){
360
            return canvas.getNewViewPort();
361
    }
362
    
363
        /**
364
         * Asigna al canvas la propiedad de visibilidad a verdadero o falso.
365
         * @param visible true para mostrar el canvas y false para ocultarlo
366
         */
367
        public void setVisible(boolean visible){
368
                canvas.setVisible(visible);
369
                canvas.draw = true;
370
                canvas.repaint();
371
        }
372
        
373
        /**
374
         * Asigna el punto sobre el que se centrar? el zoom de la minimagen
375
         * @param pto
376
         */
377
        public void setCenterPoint(Point2D pto){
378
                getCanvas().setCenterPoint(pto);
379
        }
380
        
381
        /**
382
         * Recupera el n?mero de punto que se est? tratando
383
         * @param n
384
         */
385
        public int getNumberPoint(){
386
                try{
387
                        if(getDialog().getConectorPanel().getDataPointsTabPanel().getTbPoints().getSelectedIndex() == 0)
388
                                return Integer.valueOf(getDialog().getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
389
                        else
390
                                return Integer.valueOf(getDialog().getConectorPanel().getDataPointsTabPanel().getTablePointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
391
                }catch(NullPointerException exc){
392
                        return -1;
393
                }
394
        }
395
        
396
        /**
397
     * Activa o desactiva este panel y todos los que lo componen
398
     * @param enabled variable booleana para la activaci?n y/o desactivaci?n
399
     */
400
    public void setEnabled(boolean enabled){
401
            this.getBZoomMas().setEnabled(enabled);
402
            this.getBZoomMenos().setEnabled(enabled);
403
            this.getCanvas().setEnableEvent(enabled);
404
    }
405
        
406
    /**
407
     * Obtiene el modificador de escala aplicado a zoom m?s
408
     * @return
409
     */
410
    public double getZoomMas() {
411
                return zoomMas;
412
        }
413

    
414
    /**
415
     * Obtiene el modificador de escala aplicado a zoom m?s
416
     * @return
417
     */
418
    public double getZoomMenos() {
419
                return zoomMenos;
420
        }
421
    
422
        //**********************End Setters & Getters*****************
423
        /**
424
         * Control que representa la zona de dibujado de la ventana. Este se encarga de
425
         * repintar el contenido sobre el Graphics2D a partir del viewPort que se le pase
426
         * @author Nacho Brodin (brodin_ign@gva.es)
427
         */
428
        public class CanvasZone extends JPanel implements MouseListener, MouseMotionListener{
429
                
430
                //**********************Params********************************
431
                /**
432
                 * Zoom inicial de la miniimagen
433
                 */
434
                private int                                 initZoom = 20;
435
                //**********************End Params****************************
436
                
437
                //**********************Vars**********************************
438
                private ZoomControlPanel         zoomControl = null;
439
            private int                                 ancho;
440
            private int                                 alto;
441
            private FLayers                         flayers = null;
442
            private ViewPort                         newViewPort = null;
443
            private Point2D                         centerPoint = null;
444
            private Image                                 dobleBuffer = null;
445
            private BufferedImage                 imgTmp = null;
446
            private Graphics                         dbGraphics = null;
447
            
448
            /**
449
             * Variable que estar? a true cuando queremos limpiar el canvas y a false cuando queremos
450
             * dibujar las capas.
451
             */
452
            private boolean                         cleanCanvas = false;
453
            /**
454
             * Cuando el control est? desactivado necesitamos controlar con esta variable que
455
             * aunque se pinche sobre el canvas no se capture el evento 
456
             */            
457
            private boolean                                enableEvent = true;
458

    
459
            private Point2D                         initCrux = null;
460
            /**
461
             * Variable que esta a true si estamos arrastrando la cruz por el canvas.
462
             */
463
            private boolean                         dragged = false;
464
            private final Image                 redCursor = new ImageIcon(getClass().getClassLoader().getResource(
465
                                                                                                "images/icoRed.png")).getImage();
466
            private final Image                 blueCursor = new ImageIcon(getClass().getClassLoader().getResource(
467
                                                                                                "images/icoBlue.png")).getImage();
468
            /**
469
             * Esta variable se usa para informar cuando debe recargarse el buffer de las miniimagenes. Si siempre
470
             * est? a true no para de llamar al draw de FLyers para que pinte sobre el minicanvas.
471
             */
472
            public boolean                                draw = true;            
473
            /**
474
             * Esta variable estar? a false si no queremos que no se pinte la capa de puntos rojos sobre
475
             * la miniimagenes
476
             */
477
            private boolean                         drawRedCrux = true;
478
            /**
479
             * Esta variable estar? a false si no queremos que no se pinte la capa de puntos azules sobre
480
             * la miniimagenes
481
             */
482
            private boolean                         drawBlueCrux = true;
483
            
484
                //**********************End Vars******************************
485
            
486
            //**********************Classes*******************************
487
            class CancellableClass implements Cancellable{
488
                    private boolean cancel = false;
489
                       public void setCanceled(boolean canceled) {
490
                               this.cancel = canceled;
491
                       }
492
                       
493
                        public boolean isCanceled() {
494
                                return this.cancel;
495
                        }
496
               }
497
            //**********************End Classes***************************
498
            
499
            //**********************Methods*******************************
500
            /**
501
             * 
502
             */
503
            public CanvasZone( int anc,int alt, ZoomControlPanel zc) {
504
                    zoomControl = zc;
505
                ancho = anc;
506
                alto = alt;
507
              
508
                this.setSize(ancho, alto);            
509
                this.setBackground(Color.WHITE);
510
                
511
                imgTmp = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
512
                dobleBuffer = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
513
                dbGraphics = dobleBuffer.getGraphics();
514
            }
515
                        
516
            /**
517
             * Calcula el zoom dependiendo del factor de escala pasado por
518
             * par?metro.
519
             */
520
            public void calcZoom(double factor){
521
                    Rectangle2D.Double r = new Rectangle2D.Double();
522
                        double nuevoX = centerPoint.getX() -
523
                                ((newViewPort.getExtent().getWidth() * factor) / 2.0);
524
                        double nuevoY = centerPoint.getY() -
525
                                ((newViewPort.getExtent().getHeight() * factor) / 2.0);
526
                        r.x = nuevoX;
527
                        r.y = nuevoY;
528
                        r.width = newViewPort.getExtent().getWidth() * factor;
529
                        r.height = newViewPort.getExtent().getHeight() * factor;
530
                        newViewPort.setExtent(r);
531
                        newViewPort.setScale();
532
                }
533
        
534
            /**
535
             * Calculamos el viewPort para el zoom de la minimagen
536
             * @param vp                ViewPort inicial
537
             * @param center        Centro del ViewPort a calcular.
538
             * @param vpDest        ViewPort de destino. Si este es null se crear? un nuevo ViewPort sino se asignar?n los cambios a este.
539
             * @return
540
             */
541
            public ViewPort initViewPort(ViewPort vp, Point2D center, ViewPort vpDest, double zoom){
542
                    if(center != null){                            
543
                        //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
544
                                double relacionPixelWcWidth =  ((vp.getExtent().getMaxX() - vp.getExtent().getMinX()) / initZoom) / ancho;
545
                                double relacionPixelWcHeight = ((vp.getExtent().getMaxY() - vp.getExtent().getMinY()) / initZoom) / alto;
546
                                
547
                                double wcOriginX = center.getX() - ((vp.getImageWidth() * relacionPixelWcWidth) / 2);
548
                                double wcOriginY = center.getY() - ((vp.getImageHeight() * relacionPixelWcHeight) / 2);
549
                                
550
                                double wcDstMinX = wcOriginX; 
551
                                double wcDstMinY = wcOriginY; 
552
                                double wcDstMaxX = wcDstMinX + (vp.getImageWidth()*relacionPixelWcWidth);
553
                                double wcDstMaxY = wcDstMinY + (vp.getImageHeight()*relacionPixelWcHeight);
554
                                
555
                                double wcDstWidth = wcDstMaxX - wcDstMinX;
556
                                double wcDstHeight = wcDstMaxY - wcDstMinY;
557
                                                                
558
                        Rectangle2D ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
559
                        
560
                        if(vpDest == null){
561
                                    newViewPort = vp.cloneViewPort();
562
                                newViewPort.setExtent(ext);
563
                                newViewPort.setImageSize(new java.awt.Dimension(ancho, alto));        
564
                                newViewPort.setScale();
565
                                if(zoom != 1 && zoom != 0)
566
                                        newViewPort = MathUtils.calcZoom(newViewPort, zoom);
567
                                return newViewPort;
568
                            }else{
569
                                    vpDest.setExtent(ext);
570
                                    vpDest.setScale();
571
                                    if(zoom != 1 && zoom != 0)
572
                                        vpDest = MathUtils.calcZoom(vpDest, zoom);
573
                                    return vpDest;
574
                            }
575
                            
576
                    }
577
                    return null;
578
            }
579
            
580
            /**
581
             * Dibuja sobre el graphics de la mini imagen la imagen
582
             * definida en el newViewPort.
583
             */
584
            public void loadBuffer(){                    
585
                    Graphics2D gTmp = (Graphics2D)imgTmp.getGraphics();
586
                    
587
                    dbGraphics.setColor( Color.WHITE );
588
                    dbGraphics.fillRect( 0,0,ancho,alto );
589
                    gTmp.setColor( Color.WHITE );
590
                        gTmp.fillRect( 0,0,ancho,alto );
591
                        
592
                    try{                            
593
                             if(newViewPort != null){                        
594
                                       //Si no tenemos las capas las cargamos
595
                                       if(flayers == null){
596
                                               View  theView = null;
597
                                    try{
598
                                            theView = (View)PluginServices.getMDIManager().getActiveView();
599
                                    }catch(ClassCastException exc){
600
                                            return;
601
                                    }
602
                                         flayers = theView.getMapControl().getMapContext().getLayers();
603
                                }
604
                                        
605
                                /*Dibujamos sobre las miniimagenes
606
                                       Forzamos un draw de las capas para dibujar la vista sobre el control de las miniimagenes.
607
                                       Como la capa gr?fica est? en FMap esta no aparece en el dibujado de FLayers por los que 
608
                                       forzaremos el pintado tambi?n de la capa de puntos sobre nuestro control*/
609
                                                                      
610
                                       Cancellable cancellable = new CancellableClass();
611
                                if(zoomControl.getDialog().isEnlarge()){
612
                                        if(zoomControl.getNumberPoint() != -1){
613
                                                       draw(null, (Graphics2D) dbGraphics, newViewPort,cancellable ,flayers.getFMap().getScaleView());
614
                                                       if(zoomControl.isRightControl()){
615
                                                               if(drawRedCrux)
616
                                                                       zoomControl.getLyrPoints().drawPixelCrux((Graphics2D) dbGraphics, newViewPort, cancellable);
617
                                                       }else{
618
                                                            if(drawBlueCrux)
619
                                                                       zoomControl.getLyrPoints().drawMapCrux((Graphics2D) dbGraphics, newViewPort, cancellable);
620
                                                       }
621
                                                       
622
                                        }
623
                                }
624
                                        
625
                                draw(null, gTmp, newViewPort, cancellable, flayers.getFMap().getScaleView());
626
                                            if(zoomControl.isRightControl()){
627
                                                    if(drawRedCrux)
628
                                                            zoomControl.getLyrPoints().drawPixelCrux(gTmp, newViewPort, cancellable);
629
                                            }else{
630
                                                    if(drawBlueCrux)
631
                                                            zoomControl.getLyrPoints().drawMapCrux(gTmp, newViewPort, cancellable);
632
                                            }
633
                               }
634
                     }catch (DriverException e) {
635
                                e.printStackTrace();
636
                     }
637
            }
638
            
639
            /**
640
             * Esta funci?n es una copia de la de Flyers modificada para para dibujar sobre las miniimagenes. En
641
             * la miniimagen con los controles a la derecha solo dibujar? el raster a georreferenciar. En la miniimagen
642
             * con los controles a la izquierda dibujar? la cartograf?a de base.
643
             * @param image 
644
             * @param g
645
             * @param viewPort
646
             * @param cancel
647
             * @param scale
648
             * @throws DriverException
649
             */
650
            public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
651
                            Cancellable cancel,double scale) throws DriverException {                    
652
                    for(int i=0;i<flayers.getLayersCount();i++){
653
                            if (cancel.isCanceled())
654
                                    break; 
655
                            FLayer lyr = flayers.getLayer(i);
656
                            
657
                                    try{
658
                                            if(zoomControl.isRightControl()){
659
                                                    if(lyr instanceof FLyrGeoRaster)
660
                                                            lyr.draw(image, g, viewPort, cancel,scale);
661
                                            }else{
662
                                                    if (lyr.isVisible()){
663
                                                            if(!(lyr instanceof FLyrGeoRaster))
664
                                                                    lyr.draw(image, g, viewPort, cancel,scale);
665
                                                    }
666
                                            }
667
                                            
668
                                    } catch (DriverException e){
669
                                    }
670
                    }
671
            }
672
            
673
            /**
674
             * 
675
             */
676
            public void update(Graphics g){
677
                    draw = true;
678
                    this.paint(g);
679
            }
680
            
681
            /**
682
             * Dibuja sobre el Graphics2D pasado el viewPort solicitado a
683
             * la lista de capas.
684
             * @param g
685
             */
686
            public void paint( Graphics g ) {
687
                    if(!cleanCanvas){
688
                            if(draw){
689
                                    loadBuffer();
690
                                    draw = false;
691
                            }
692
                            g.drawImage(dobleBuffer, 0, 0, null);
693
                            this.invalidate();
694
                            AdjustGeorefPanel p = zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel();
695
                            try{
696
                                    if(!this.zoomControl.isRightControl())
697
                                            p.setLZoomLeft("1:"+((int)newViewPort.getScale()));
698
                                    else
699
                                            p.setLZoomRight("1:"+((int)newViewPort.getScale()));
700
                            }catch(NullPointerException exc){
701
                                    //No ponemos la escala
702
                            }
703
                    }else{
704
                            dbGraphics.setColor( Color.WHITE );
705
                            dbGraphics.fillRect( 0,0,ancho,alto );
706
                            g.setColor( Color.WHITE );
707
                            g.fillRect( 0,0,ancho,alto );
708
                            cleanCanvas = false;
709
                    }
710
            }
711
                        
712
            /**
713
             * Desplaza el extent al nuevo centro 
714
             * @param pto
715
             */
716
            public void shiftExtent(Point2D pto){
717
                    this.setCenterPoint(pto);
718
                    this.calcZoom(1);
719
            }
720
            
721
                /* (non-Javadoc)
722
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
723
                 */
724
                public void mouseClicked(MouseEvent ev) {
725
                        
726
                }
727
                
728
                /**
729
                 * Cuando el rat?n entra dentro del control fijamos el valor de las coordenadas
730
                 * que tiene el punto seleccionado. De esta forma cuando salga del control podr?
731
                 * volver a restaurarlo en caso de que no haya sido cambiado.
732
                 */
733
                public void mouseEntered(MouseEvent arg0) {
734
                        if(isEnableEvent()){
735
                                if(!zoomControl.rightControl)
736
                                        zoomControl.getDialog().getPointManager().setCurrentWC();
737
                                else
738
                                        zoomControl.getDialog().getPointManager().setCurrentPixelCoord();
739
                        }
740
                }
741
                
742
                /**
743
                 * Al sacar el rat?n de la zona de dibujado de la miniimagen restauramos los valores
744
                 * de los JTextField. 
745
                 * 
746
                 * Se actualizaran los correspondientes a las coordenadas del mundo real 
747
                 * en el caso de que el control tenga botones a la izquierda.
748
                 * 
749
                 * Se actualizaran los correspondientes a las coordenadas en pixels cuando se trata del control con
750
                 * botones a la derecha.
751
                 */
752
                public void mouseExited(MouseEvent arg0) {
753
                        if(isEnableEvent()){
754
                                if(!zoomControl.rightControl)
755
                                        zoomControl.getDialog().getPointManager().replaceWC();
756
                                else
757
                                        zoomControl.getDialog().getPointManager().replacePixelCoord();
758
                                drawRedCrux = drawBlueCrux = true;
759
                                repaint();
760
                        }
761
                }
762
                
763
                /**
764
                 * Controla la actualizaci?n del valor de un punto en los JTextDialog. Cuando se mueve sobre
765
                 * el panel con controles a la izquierda actualizamos las coordenadas del mapa. Cuando se mueve sobre
766
                 * el panel con controles a la derecha actualizamos las coordenadas de la imagen.  
767
                 */
768
                public void mouseMoved(MouseEvent arg0) {
769
                        if(        isEnableEvent() && 
770
                                zoomControl.getLyrPoints() != null &&
771
                                zoomControl.getLyrPoints().getCountPoints() > 0 &&
772
                                this.newViewPort != null &&
773
                                this.newViewPort.getExtent() != null){
774
                                
775
                                double newWCPointX = this.newViewPort.getExtent().getMinX() + ((arg0.getX() * this.newViewPort.getExtent().getWidth()) / this.getWidth());
776
                                int ptoSelectY = (int)(this.getHeight() - arg0.getY());
777
                                double newWCPointY = this.newViewPort.getExtent().getMinY() + ((ptoSelectY * this.newViewPort.getExtent().getHeight()) / this.getHeight());
778
                                if(!zoomControl.rightControl){
779
                                        zoomControl.getDialog().getPointManager().setTempWC(newWCPointX, newWCPointY);
780
                                }else{
781
                                        Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(newWCPointX, newWCPointY);
782
                                        if(pixelImg != null)
783
                                                zoomControl.getDialog().getPointManager().setTempPixelCoord(pixelImg.getX(), pixelImg.getY());
784
                                        else 
785
                                                //Si pixelImg es null es que nos hemos salido de la imagen por lo que reponemos los valores originales hasta que vuelva a entrar.
786
                                                zoomControl.getDialog().getPointManager().replacePixelCoord();        
787
                                        
788
                                }
789
                        }
790
                }
791
                
792
                /**
793
                 * Al pulsar el rat?n sobre la minimagen permitimos que se pueda desplazar la cruz
794
                 *
795
                 */
796
                public void mousePressed(MouseEvent ev) {
797
                        int cte = 10;
798
                        if( (ev.getX() > ((getWidth() >> 1) - cte)) && (ev.getX() < ((getWidth() >> 1) + cte)) &&
799
                                (ev.getY() > ((getHeight() >> 1) - cte)) && (ev.getY() < ((getHeight() >> 1) + cte))){
800
                                this.dragged = true;
801
                                
802
                                try{
803
                                        if(zoomControl.isRightControl()){
804
                                                drawRedCrux = false;
805
                                                setCursor(Toolkit.getDefaultToolkit().createCustomCursor(redCursor, new Point(16, 16), ""));
806
                                        }else{
807
                                                drawBlueCrux = false;
808
                                                setCursor(Toolkit.getDefaultToolkit().createCustomCursor(blueCursor, new Point(16, 16), ""));
809
                                        }
810
                                        
811
                                        zoomControl.loadLayer();
812
                                        initCrux = new Point2D.Double();
813
                                        initCrux.setLocation(this.getWidth() / 2, this.getHeight() / 2);
814
                                    draw = true;
815
                                        repaint();
816
                                }catch(InstantiationException e){
817
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
818
                                                        PluginServices.getText(this, "error_capa_puntos"));
819
                                }
820
                        }
821
                }
822

    
823
                /* (non-Javadoc)
824
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
825
                 */
826
                public void mouseReleased(MouseEvent ev) {
827
                        if(!dragged)
828
                                return;
829
                        dragged = false;
830
                        View  theView = null;
831
                        try{
832
                                theView = (View) PluginServices.getMDIManager().getActiveView();
833
                        }catch(ClassCastException exc){
834
                                return;
835
                        }
836
                        ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
837
                        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
838

    
839
                        Point2D point = newViewPort.toMapPoint(ev.getX(), ev.getY());                        
840
                        int nPoint = Integer.valueOf(zoomControl.getDialog().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue();
841

    
842
                        FLyrPoints lp = zoomControl.getLyrPoints();//(FLyrPoints)lyr;
843
                                 
844
                if(zoomControl.isRightControl()){ //Control de actualizaci?n de pixel de imagen
845
                        Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(point);
846
                        //Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(newWCPointX, newWCPointY);
847
                        
848
                        if(pixelImg == null){
849
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
850
                                                                     PluginServices.getText(this, "fuera_de_extent"));
851
                                     return;
852
                             }
853
                        zoomControl.getDialog().getPointManager().updateData(        nPoint,
854
                                                                                                                                         pixelImg,
855
                                                                                                                                         null, 
856
                                                                                                                                         zoomControl.getDialog(),
857
                                                                                                                                         null);
858

    
859
                        //Actualizamos la otra miniimagen
860
                        shiftExtent(zoomControl.getLyrGeoRaster().img2World(pixelImg));
861
                        zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel().getZoomLeft().draw();
862
                        zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel().getZoomRight().draw();
863
                        zoomControl.getDialog().getPointManager().setCurrentPixelCoord();
864
                }else{ //Control de actualizaci?n de coordenadas del mundo
865
                                Point2D mapPoint = new Point2D.Double();
866
                                mapPoint.setLocation(point);
867
                                zoomControl.getDialog().getPointManager().updateData(        nPoint,
868
                                                                                                                                                        null, 
869
                                                                                                                                                        mapPoint,
870
                                                                                                                                                        zoomControl.getDialog(),
871
                                                                                                                                                        null);
872
                                          
873
                                //Actualizamos la otra miniimagen
874
                                shiftExtent(mapPoint);
875
                                zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel().getZoomRight().draw();
876
                                zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel().getZoomLeft().draw();
877
                                zoomControl.getDialog().getPointManager().setCurrentWC();
878
                 }
879
                    draw = true;
880
                    drawRedCrux = drawBlueCrux = true;
881
                        repaint();
882
                        theView.getMapControl().getMapContext().invalidate();
883
                }
884
                
885
                /* (non-Javadoc)
886
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
887
                 */
888
                public void mouseDragged(MouseEvent ev) {
889
                        this.mouseMoved(ev);
890
                }
891
            //**********************End Methods***************************
892

    
893
                //**********************Setters & Getters*********************
894
                /**
895
                 * @param cleanCanvas The cleanCanvas to set.
896
                 */
897
                public void setCleanCanvas(boolean cleanCanvas) {
898
                        this.cleanCanvas = cleanCanvas;
899
                }
900
                
901
                 /**
902
             * Asigna el punto donde se centrar? la vista de la minimagen
903
             * @param pto Punto central.
904
             */
905
            public void setCenterPoint(Point2D pto){
906
                    this.centerPoint = pto;
907
            }
908
            
909
            /**
910
             * Asigna el viewPort
911
             * @param vp ViewPort
912
             */
913
            public void setViewPort(ViewPort vp, double zoom){
914
                    this.initViewPort(vp, this.centerPoint, null, zoom);
915
            }
916

    
917
            /**
918
             * Asigna el viewPort recalculado
919
             * @param vp ViewPort
920
             */
921
            public void setNewViewPort(ViewPort vp){
922
                    this.newViewPort = vp;
923
            }
924
            
925
            /**
926
             * Obtiene el viewPort recalculado
927
             * @return vp ViewPort
928
             */
929
            public ViewPort getNewViewPort(){
930
                    return this.newViewPort;
931
            }
932
            
933
            /**
934
             * Obtiene el valor del desactivador de eventos sobre el canvas.
935
             * @return
936
             */
937
            public boolean isEnableEvent() {
938
                        return enableEvent;
939
                }
940

    
941
            /**
942
             * Asigna el desactivador de eventos sobre el canvas
943
             * @param enableEvent
944
             */
945
                public void setEnableEvent(boolean enableEvent) {
946
                        this.enableEvent = enableEvent;
947
                }
948
                
949
                //**********************End Setters & Getters*****************
950
                
951
        }
952
 }