Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / extensions / extGeoreferencing / src / org / gvsig / georeferencing / gui / panels / ZoomControlPanel.java @ 8745

History | View | Annotate | Download (31.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.georeferencing.gui.panels;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Color;
23
import java.awt.Component;
24
import java.awt.Cursor;
25
import java.awt.Graphics;
26
import java.awt.Graphics2D;
27
import java.awt.GridBagConstraints;
28
import java.awt.GridBagLayout;
29
import java.awt.Image;
30
import java.awt.Point;
31
import java.awt.Toolkit;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.awt.event.MouseEvent;
35
import java.awt.event.MouseListener;
36
import java.awt.event.MouseMotionListener;
37
import java.awt.geom.Point2D;
38
import java.awt.geom.Rectangle2D;
39
import java.awt.image.BufferedImage;
40

    
41
import javax.swing.ImageIcon;
42
import javax.swing.JButton;
43
import javax.swing.JOptionPane;
44
import javax.swing.JPanel;
45

    
46
import org.cresques.px.Extent;
47
import org.gvsig.georeferencing.gui.dialog.GeoreferencingDialog;
48
import org.gvsig.georeferencing.utils.GeoUtils;
49

    
50
import com.iver.andami.PluginServices;
51
import com.iver.cit.gvsig.fmap.DriverException;
52
import com.iver.cit.gvsig.fmap.ViewPort;
53
import com.iver.cit.gvsig.fmap.layers.FLayer;
54
import com.iver.cit.gvsig.fmap.layers.FLayers;
55
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
56
import com.iver.cit.gvsig.fmap.layers.FLyrPoints;
57
import com.iver.cit.gvsig.gui.View;
58
import com.iver.utiles.swing.threads.Cancellable;
59

    
60
/**
61
 * Panel que contiene los controles de zoom de la vista para
62
 * afinar en la georreferenciaci?n.
63
 *
64
 * @author Nacho Brodin (brodin_ign@gva.es)
65
 *
66
 */
67
public class ZoomControlPanel extends JPanel implements ActionListener{
68

    
69
        //**********************Vars**********************************
70
        public static final int                        WIDTH_MINIMG = 140;
71
        public static final int                        HEIGHT_MINIMG = 140;
72
        private JPanel                                         pImage = null;
73
        private JPanel                                         pControls = null;
74
        private JButton                                 bZoomMas = null;
75
        private JButton                                 bZoomMenos = null;
76
        private CanvasZone                                 canvas = null;
77
        private ViewPort                                 viewPort = null;
78
    public FLyrPoints                                 lyrPoints = null;
79
    public FLyrGeoRaster                         lyrGeoRaster = null;
80
    public GeoreferencingDialog         dialog = null;
81
    private boolean                                 rightControl = false;
82
    /**
83
         * Estado de los botones cuando se ejecuta disableAllControls
84
         */
85
        private boolean[]                                 buttonsState = new boolean[2];
86
        /**
87
         * DisableAllControls ha sido ejecutada si est? a true esta variabled
88
         */
89
        private boolean                                 disableAllControls = false;
90
        /**
91
         * Factor multiplicador cuando hacemos zoom mas
92
         */
93
        private double                                        zoomMas = 0.6;
94
        /**
95
         * Factor multiplicador cuando hacemos zoom menos
96
         */
97
        private double                                        zoomMenos = 1.8;
98
        //**********************End Vars******************************
99

    
100
        //**********************Methods*******************************
101
        /**
102
         * This is the default constructor
103
         */
104
        public ZoomControlPanel(boolean rightControl, GeoreferencingDialog dialog) {
105
                super();
106
                this.rightControl = rightControl;
107
                this.dialog = dialog;
108
                initialize(rightControl);
109
        }
110

    
111
        /**
112
         * This method initializes this
113
         *
114
         * @return void
115
         */
116
        private void initialize(boolean rightControl) {
117

    
118
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
119
        if(rightControl)
120
                gridBagConstraints1.gridx = 1;
121
        else
122
                gridBagConstraints1.gridx = 0;
123
        gridBagConstraints1.gridy = 0;
124
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
125
        if(rightControl)
126
                gridBagConstraints.gridx = 0;
127
        else
128
                gridBagConstraints.gridx = 1;
129
        gridBagConstraints.gridy = 0;
130
        this.setLayout(new GridBagLayout());
131
        this.setPreferredSize(new java.awt.Dimension(175,142));
132
        this.setSize(new java.awt.Dimension(175,142));
133
        this.setLocation(new java.awt.Point(0,0));
134
        this.add(getPImage(), gridBagConstraints);
135
        this.add(getPControls(), gridBagConstraints1);
136
        //Desactivamos el control en la inicializaci?n
137
        this.setEnabled(false);
138
        }
139

    
140
        /**
141
         * Carga las capas de puntos y de raster georreferenciado
142
         *
143
         */
144
        public void loadLayer() throws InstantiationException{
145
                //Cargamos las capas
146
                this.lyrGeoRaster = dialog.getLyrGeoRaster();
147
                this.lyrPoints = dialog.getLyrPoints();
148

    
149
                if(lyrGeoRaster == null || lyrPoints == null)
150
                        throw new InstantiationException("No se ha podido instanciar la capa de puntos o georreferenciada");
151

    
152
        }
153

    
154
        /**
155
         *Llama a la funci?n de pintado del canvas con los par?metros que tenga en ese momento
156
         */
157
        public void draw(){
158
                if(!getDialog().isEnlarge())
159
                        getCanvas().loadBuffer();
160
                canvas.draw = true;
161
                getCanvas().repaint();
162
        }
163

    
164
        /**
165
         * Manejo de los controles zoom m?s y zoom menos
166
         */
167
        public void actionPerformed(ActionEvent e) {
168
                double zoom = zoomMas;
169

    
170
                if(e.getSource() == bZoomMas)
171
                        canvas.calcZoom(zoomMas);
172

    
173
                if(e.getSource() == bZoomMenos){
174
                        canvas.calcZoom(zoomMenos);
175
                        zoom = zoomMenos;
176
                }
177

    
178
                canvas.draw = true;
179
                canvas.repaint();
180
                if(isRightControl())
181
                        lyrPoints.getPoint(dialog.getSelectedPoint()).zoomLeft *= zoom;
182
                else
183
                        lyrPoints.getPoint(dialog.getSelectedPoint()).zoomRight *= zoom;
184

    
185
        }
186

    
187
    /**
188
     *Limpia el canvas
189
     */
190
    public void clear(){
191
            canvas.setCleanCanvas(true);
192
            canvas.repaint();
193
    }
194

    
195
    /**
196
         * Esta funci?n deshabilita todos los controles y guarda sus valores
197
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
198
         * se vuelvan a quedar como estaba
199
         */
200
        public void disableAllControls(){
201
                if(!disableAllControls){
202
                        disableAllControls = true;
203

    
204
                        //Salvamos los estados
205
                        buttonsState[0] = getBZoomMas().isEnabled();
206
                        buttonsState[1] = getBZoomMenos().isEnabled();
207

    
208
                        //Desactivamos controles
209
                        getBZoomMas().setEnabled(false);
210
                        getBZoomMenos().setEnabled(false);
211
                }
212
        }
213

    
214
        /**
215
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n
216
         * disableAllControls
217
         */
218
        public void restoreControlsValue(){
219
                if(disableAllControls){
220
                        disableAllControls = false;
221
                        getBZoomMas().setEnabled(buttonsState[0]);
222
                        getBZoomMenos().setEnabled(buttonsState[1]);
223
                }
224
        }
225
        //**********************End Methods***************************
226

    
227
    //**********************Setters & Getters*********************
228
    /**
229
         * @param viewPort The viewPort to set.
230
         */
231
        public void setViewPort(ViewPort viewPort, double zoom) {
232
                this.viewPort = viewPort;
233
                getCanvas().setViewPort(this.viewPort, zoom);
234
        }
235

    
236
        /**
237
         * This method initializes jPanel
238
         *
239
         * @return javax.swing.JPanel
240
         */
241
        private JPanel getPImage() {
242
                if (pImage == null) {
243
                        pImage = new JPanel();
244
                        BorderLayout borderLayout = new BorderLayout();
245
                        pImage.setLayout(borderLayout);
246
                        pImage.setPreferredSize(new java.awt.Dimension(140,140));
247
                        pImage.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
248
                        pImage.add(getCanvas(), BorderLayout.CENTER);
249
                }
250
                return pImage;
251
        }
252

    
253
        /**
254
         * This method initilizes canvas
255
         * @return        CanvasZone
256
         */
257
        public CanvasZone getCanvas(){
258
                if(canvas == null){
259
                        canvas = new CanvasZone(ZoomControlPanel.WIDTH_MINIMG, ZoomControlPanel.HEIGHT_MINIMG, this);
260
                        canvas.setVisible(false);
261
                        canvas.addMouseListener(canvas);
262
                        canvas.addMouseMotionListener(canvas);
263
                }
264
                return canvas;
265
        }
266

    
267
        /**
268
         * This method initializes jPanel1
269
         *
270
         * @return javax.swing.JPanel
271
         */
272
        private JPanel getPControls() {
273
                if (pControls == null) {
274
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
275
                        gridBagConstraints3.gridx = 0;
276
                        gridBagConstraints3.insets = new java.awt.Insets(5,0,0,0);
277
                        gridBagConstraints3.gridy = 1;
278
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
279
                        gridBagConstraints2.gridx = 0;
280
                        gridBagConstraints2.gridy = 0;
281
                        pControls = new JPanel();
282
                        pControls.setLayout(new GridBagLayout());
283
                        pControls.setPreferredSize(new java.awt.Dimension(33,140));
284
                        pControls.add(getBZoomMas(), gridBagConstraints2);
285
                        pControls.add(getBZoomMenos(), gridBagConstraints3);
286
                }
287
                return pControls;
288
        }
289

    
290
        /**
291
         * This method initializes jButton
292
         *
293
         * @return javax.swing.JButton
294
         */
295
        public JButton getBZoomMas() {
296
                if (bZoomMas == null) {
297
                        bZoomMas = new JButton();
298
                        bZoomMas.setPreferredSize(new java.awt.Dimension(25,25));
299
                        try{
300
                                bZoomMas.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/aumentar.png")));
301
                        }catch(NullPointerException e){
302
                                //Sin icono
303
                        }
304
                        bZoomMas.addActionListener(this);
305
                }
306
                return bZoomMas;
307
        }
308

    
309
        /**
310
         * This method initializes jButton
311
         *
312
         * @return javax.swing.JButton
313
         */
314
        public JButton getBZoomMenos() {
315
                if (bZoomMenos == null) {
316
                        bZoomMenos = new JButton();
317
                        bZoomMenos.setPreferredSize(new java.awt.Dimension(25,25));
318
                        try{
319
                                bZoomMenos.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/disminuir.png")));
320
                        }catch(NullPointerException e){
321
                                //Sin icono
322
                        }
323
                        bZoomMenos.addActionListener(this);
324
                }
325
                return bZoomMenos;
326
        }
327

    
328
        /**
329
         * Obtiene true si se trata de un control con los botones de zoom
330
         * a la derecha y false si los tiene a la izquierda.
331
         * @return Returns the rightControl.
332
         */
333
        public boolean isRightControl() {
334
                return rightControl;
335
        }
336

    
337
        /**
338
         * Obtiene la capa de imagen georreferenciada.
339
         * @return Returns the lyrGeoRaster.
340
         */
341
        public FLyrGeoRaster getLyrGeoRaster() {
342
                return lyrGeoRaster;
343
        }
344

    
345
        /**
346
         * Obtiene la capa de puntos.
347
         * @return Returns the lyrPoints.
348
         */
349
        public FLyrPoints getLyrPoints() {
350
                try{
351
                if(lyrPoints == null)
352
                        this.loadLayer();
353
                }catch(InstantiationException e){
354
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
355
                                        PluginServices.getText(this, "error_capa_puntos"));
356
                }
357
                return lyrPoints;
358
        }
359

    
360
        /**
361
         * @return Returns the dialog.
362
         */
363
        public GeoreferencingDialog getDialog() {
364
                try{
365
                        if(dialog == null)
366
                                this.loadLayer();
367
                }catch(InstantiationException e){
368
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
369
                                        PluginServices.getText(this, "error_capa_puntos"));
370
                }
371
                return dialog;
372
        }
373

    
374
    /**
375
     * Asigna el viewPort recalculado
376
     * @param vp ViewPort
377
     */
378
    public void setNewViewPort(ViewPort vp){
379
            canvas.setNewViewPort(vp);
380
    }
381

    
382
    /**
383
     * Obtiene el viewPort recalculado
384
     * @return vp ViewPort
385
     */
386
    public ViewPort getNewViewPort(){
387
            return canvas.getNewViewPort();
388
    }
389

    
390
        /**
391
         * Asigna al canvas la propiedad de visibilidad a verdadero o falso.
392
         * @param visible true para mostrar el canvas y false para ocultarlo
393
         */
394
        public void setVisible(boolean visible){
395
                canvas.setVisible(visible);
396
                canvas.draw = true;
397
                canvas.repaint();
398
        }
399

    
400
        /**
401
         * Asigna el punto sobre el que se centrar? el zoom de la minimagen
402
         * @param pto
403
         */
404
        public void setCenterPoint(Point2D pto){
405
                getCanvas().setCenterPoint(pto);
406
        }
407

    
408
        /**
409
         * Recupera el n?mero de punto que se est? tratando
410
         * @param n
411
         */
412
        public int getNumberPoint(){
413
                try{
414
                        if(getDialog().getConectorPanel().getDataPointsTabPanel().getTbPoints().getSelectedIndex() == 0)
415
                                return Integer.valueOf(getDialog().getConectorPanel().getDataPointsTabPanel().getSelectPointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
416
                        else
417
                                return Integer.valueOf(getDialog().getConectorPanel().getDataPointsTabPanel().getTablePointsPanel().getTableControlerPanel().getCPoint().getSelectedItem().toString()).intValue() - 1;
418
                }catch(NullPointerException exc){
419
                        return -1;
420
                }
421
        }
422

    
423
        /**
424
     * Activa o desactiva este panel y todos los que lo componen
425
     * @param enabled variable booleana para la activaci?n y/o desactivaci?n
426
     */
427
    public void setEnabled(boolean enabled){
428
            this.getBZoomMas().setEnabled(enabled);
429
            this.getBZoomMenos().setEnabled(enabled);
430
            this.getCanvas().setEnableEvent(enabled);
431
    }
432

    
433
    /**
434
     * Obtiene el modificador de escala aplicado a zoom m?s
435
     * @return
436
     */
437
    public double getZoomMas() {
438
                return zoomMas;
439
        }
440

    
441
    /**
442
     * Obtiene el modificador de escala aplicado a zoom m?s
443
     * @return
444
     */
445
    public double getZoomMenos() {
446
                return zoomMenos;
447
        }
448

    
449
        //**********************End Setters & Getters*****************
450
        /**
451
         * Control que representa la zona de dibujado de la ventana. Este se encarga de
452
         * repintar el contenido sobre el Graphics2D a partir del viewPort que se le pase
453
         * @author Nacho Brodin (brodin_ign@gva.es)
454
         */
455
        public class CanvasZone extends JPanel implements MouseListener, MouseMotionListener{
456

    
457
                //**********************Params********************************
458
                /**
459
                 * Zoom inicial de la miniimagen
460
                 */
461
                private int                                 initZoom = 20;
462
                //**********************End Params****************************
463

    
464
                //**********************Vars**********************************
465
                private ZoomControlPanel         zoomControl = null;
466
            private int                                 ancho;
467
            private int                                 alto;
468
            private FLayers                         flayers = null;
469
            private ViewPort                         newViewPort = null;
470
            private Point2D                         centerPoint = null;
471
            private Image                                 dobleBuffer = null;
472
            private BufferedImage                 imgTmp = null;
473
            private Graphics                         dbGraphics = null;
474

    
475
            /**
476
             * Variable que estar? a true cuando queremos limpiar el canvas y a false cuando queremos
477
             * dibujar las capas.
478
             */
479
            private boolean                         cleanCanvas = false;
480
            /**
481
             * Cuando el control est? desactivado necesitamos controlar con esta variable que
482
             * aunque se pinche sobre el canvas no se capture el evento
483
             */
484
            private boolean                                enableEvent = true;
485

    
486
            private Point2D                         initCrux = null;
487
            /**
488
             * Variable que esta a true si estamos arrastrando la cruz por el canvas.
489
             */
490
            private boolean                         dragged = false;
491
            private Image                                 redCursor = null;
492
            
493
            private Image                                 blueCursor = null;
494
            /**
495
             * Esta variable se usa para informar cuando debe recargarse el buffer de las miniimagenes. Si siempre
496
             * est? a true no para de llamar al draw de FLyers para que pinte sobre el minicanvas.
497
             */
498
            public boolean                                draw = true;
499
            /**
500
             * Esta variable estar? a false si no queremos que no se pinte la capa de puntos rojos sobre
501
             * la miniimagenes
502
             */
503
            private boolean                         drawRedCrux = true;
504
            /**
505
             * Esta variable estar? a false si no queremos que no se pinte la capa de puntos azules sobre
506
             * la miniimagenes
507
             */
508
            private boolean                         drawBlueCrux = true;
509

    
510
                //**********************End Vars******************************
511

    
512
            //**********************Classes*******************************
513
            class CancellableClass implements Cancellable{
514
                    private boolean cancel = false;
515
                       public void setCanceled(boolean canceled) {
516
                               this.cancel = canceled;
517
                       }
518

    
519
                        public boolean isCanceled() {
520
                                return this.cancel;
521
                        }
522
               }
523
            //**********************End Classes***************************
524

    
525
            //**********************Methods*******************************
526
            /**
527
             *
528
             */
529
            public CanvasZone( int anc,int alt, ZoomControlPanel zc) {
530
                    zoomControl = zc;
531
                ancho = anc;
532
                alto = alt;
533
                
534
                if(redCursor == null){
535
                        try{
536
                                redCursor = new ImageIcon(getClass().getClassLoader().getResource("images/icoRed.png")).getImage();
537
                        }catch(NullPointerException e){
538
                                        //Sin icono
539
                                }
540
                }
541
                
542
                if(blueCursor == null){
543
                        try{
544
                                blueCursor = new ImageIcon(getClass().getClassLoader().getResource("images/icoBlue.png")).getImage();
545
                        }catch(NullPointerException e){
546
                                        //Sin icono
547
                                }
548
                }
549

    
550
                this.setSize(ancho, alto);
551
                this.setBackground(Color.WHITE);
552

    
553
                imgTmp = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
554
                dobleBuffer = new BufferedImage(this.ancho, this.alto, BufferedImage.TYPE_INT_ARGB);
555
                dbGraphics = dobleBuffer.getGraphics();
556
            }
557

    
558
            /**
559
             * Calcula el zoom dependiendo del factor de escala pasado por
560
             * par?metro.
561
             */
562
            public void calcZoom(double factor){
563
                    Rectangle2D.Double r = new Rectangle2D.Double();
564
                        double nuevoX = centerPoint.getX() -
565
                                ((newViewPort.getExtent().getWidth() * factor) / 2.0);
566
                        double nuevoY = centerPoint.getY() -
567
                                ((newViewPort.getExtent().getHeight() * factor) / 2.0);
568
                        r.x = nuevoX;
569
                        r.y = nuevoY;
570
                        r.width = newViewPort.getExtent().getWidth() * factor;
571
                        r.height = newViewPort.getExtent().getHeight() * factor;
572
                        newViewPort.setExtent(r);
573
                        newViewPort.refreshExtent();
574
                }
575

    
576
            /**
577
             * Calculamos el viewPort para el zoom de la minimagen
578
             * @param vp                ViewPort inicial
579
             * @param center        Centro del ViewPort a calcular.
580
             * @param vpDest        ViewPort de destino. Si este es null se crear? un nuevo ViewPort sino se asignar?n los cambios a este.
581
             * @return
582
             */
583
            public ViewPort initViewPort(ViewPort vp, Point2D center, ViewPort vpDest, double zoom){
584
                    if(center != null){
585
                        //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
586
                                double relacionPixelWcWidth =  ((vp.getExtent().getMaxX() - vp.getExtent().getMinX()) / initZoom) / ancho;
587
                                double relacionPixelWcHeight = ((vp.getExtent().getMaxY() - vp.getExtent().getMinY()) / initZoom) / alto;
588

    
589
                                double wcOriginX = center.getX() - ((vp.getImageWidth() * relacionPixelWcWidth) / 2);
590
                                double wcOriginY = center.getY() - ((vp.getImageHeight() * relacionPixelWcHeight) / 2);
591

    
592
                                double wcDstMinX = wcOriginX;
593
                                double wcDstMinY = wcOriginY;
594
                                double wcDstMaxX = wcDstMinX + (vp.getImageWidth()*relacionPixelWcWidth);
595
                                double wcDstMaxY = wcDstMinY + (vp.getImageHeight()*relacionPixelWcHeight);
596

    
597
                                double wcDstWidth = wcDstMaxX - wcDstMinX;
598
                                double wcDstHeight = wcDstMaxY - wcDstMinY;
599

    
600
                        Rectangle2D ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
601

    
602
                        if(vpDest == null){
603
                                    newViewPort = vp.cloneViewPort();
604
                                newViewPort.setExtent(ext);
605
                                newViewPort.setImageSize(new java.awt.Dimension(ancho, alto));
606
                                newViewPort.refreshExtent();
607
                                if(zoom != 1 && zoom != 0)
608
                                        newViewPort = GeoUtils.calcZoom(newViewPort, zoom);
609
                                return newViewPort;
610
                            }else{
611
                                    vpDest.setExtent(ext);
612
                                    vpDest.refreshExtent();
613
                                    if(zoom != 1 && zoom != 0)
614
                                        vpDest = GeoUtils.calcZoom(vpDest, zoom);
615
                                    return vpDest;
616
                            }
617

    
618
                    }
619
                    return null;
620
            }
621

    
622
            /**
623
             * Dibuja sobre el graphics de la mini imagen la imagen
624
             * definida en el newViewPort.
625
             */
626
            public void loadBuffer(){
627
                    Graphics2D gTmp = (Graphics2D)imgTmp.getGraphics();
628

    
629
                    dbGraphics.setColor( Color.WHITE );
630
                    dbGraphics.fillRect( 0,0,ancho,alto );
631
                    gTmp.setColor( Color.WHITE );
632
                        gTmp.fillRect( 0,0,ancho,alto );
633

    
634
                    try{
635
                             if(newViewPort != null){
636
                                       //Si no tenemos las capas las cargamos
637
                                       if(flayers == null){
638
                                               View  theView = null;
639
                                    try{
640
                                            theView = (View)PluginServices.getMDIManager().getActiveWindow();
641
                                    }catch(ClassCastException exc){
642
                                            return;
643
                                    }
644
                                         flayers = theView.getMapControl().getMapContext().getLayers();
645
                                }
646

    
647
                                /*Dibujamos sobre las miniimagenes
648
                                       Forzamos un draw de las capas para dibujar la vista sobre el control de las miniimagenes.
649
                                       Como la capa gr?fica est? en FMap esta no aparece en el dibujado de FLayers por los que
650
                                       forzaremos el pintado tambi?n de la capa de puntos sobre nuestro control*/
651

    
652
                                       Cancellable cancellable = new CancellableClass();
653
                                if(zoomControl.getDialog().isEnlarge()){
654
                                        if(zoomControl.getNumberPoint() != -1){
655
                                                       draw(new BufferedImage(newViewPort.getImageWidth(), newViewPort.getImageHeight(), BufferedImage.TYPE_INT_ARGB),
656
                                                                (Graphics2D) dbGraphics,
657
                                                                newViewPort,
658
                                                                cancellable ,
659
                                                                flayers.getMapContext().getScaleView());
660
                                                       if(zoomControl.isRightControl()){
661
                                                               if(drawRedCrux)
662
                                                                       zoomControl.getLyrPoints().drawPixelCrux((Graphics2D) dbGraphics, newViewPort, cancellable);
663
                                                       }else{
664
                                                            if(drawBlueCrux)
665
                                                                       zoomControl.getLyrPoints().drawMapCrux((Graphics2D) dbGraphics, newViewPort, cancellable);
666
                                                       }
667

    
668
                                        }
669
                                }
670

    
671
                                draw(new BufferedImage(newViewPort.getImageWidth(), newViewPort.getImageHeight(), BufferedImage.TYPE_INT_ARGB),
672
                                         gTmp,
673
                                         newViewPort,
674
                                         cancellable,
675
                                         flayers.getMapContext().getScaleView());
676
                                            if(zoomControl.isRightControl()){
677
                                                    if(drawRedCrux)
678
                                                            zoomControl.getLyrPoints().drawPixelCrux(gTmp, newViewPort, cancellable);
679
                                            }else{
680
                                                    if(drawBlueCrux)
681
                                                            zoomControl.getLyrPoints().drawMapCrux(gTmp, newViewPort, cancellable);
682
                                            }
683
                               }
684
                     }catch (DriverException e) {
685
                                e.printStackTrace();
686
                     }
687
            }
688

    
689
            /**
690
             * Esta funci?n es una copia de la de Flyers modificada para para dibujar sobre las miniimagenes. En
691
             * la miniimagen con los controles a la derecha solo dibujar? el raster a georreferenciar. En la miniimagen
692
             * con los controles a la izquierda dibujar? la cartograf?a de base.
693
             * @param image
694
             * @param g
695
             * @param viewPort
696
             * @param cancel
697
             * @param scale
698
             * @throws DriverException
699
             */
700
            public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
701
                            Cancellable cancel,double scale) throws DriverException {
702
                    for(int i=0;i<flayers.getLayersCount();i++){
703
                            if (cancel.isCanceled())
704
                                    break;
705
                            FLayer lyr = flayers.getLayer(i);
706

    
707
                                    try{
708
                                            if(zoomControl.isRightControl()){
709
                                                    if(lyr instanceof FLyrGeoRaster)
710
                                                            lyr.draw(image, g, viewPort, cancel,scale);
711
                                            }else{
712
                                                    if (lyr.isVisible()){
713
                                                            if(!(lyr instanceof FLyrGeoRaster))
714
                                                                    lyr.draw(image, g, viewPort, cancel,scale);
715
                                                    }
716
                                            }
717

    
718
                                    } catch (DriverException e){
719
                                    }
720
                    }
721
            }
722

    
723
            /**
724
             *
725
             */
726
            public void update(Graphics g){
727
                    draw = true;
728
                    this.paint(g);
729
            }
730

    
731
            /**
732
             * Dibuja sobre el Graphics2D pasado el viewPort solicitado a
733
             * la lista de capas.
734
             * @param g
735
             */
736
            public void paint( Graphics g ) {
737
                    if(!cleanCanvas){
738
                            if(draw){
739
                                    loadBuffer();
740
                                    draw = false;
741
                            }
742
                            g.drawImage(dobleBuffer, 0, 0, null);
743
                            this.invalidate();
744
                            AdjustGeorefPanel p = zoomControl.getDialog().getConectorPanel().getAdjustGeorefPanel();
745
                            try{
746
                                    if(!this.zoomControl.isRightControl())
747
                                            p.setLZoomLeft("1:"+((int)newViewPort.getScale()));
748
                                    else
749
                                            p.setLZoomRight("1:"+((int)newViewPort.getScale()));
750
                            }catch(NullPointerException exc){
751
                                    //No ponemos la escala
752
                            }
753
                    }else{
754
                            dbGraphics.setColor( Color.WHITE );
755
                            dbGraphics.fillRect( 0,0,ancho,alto );
756
                            g.setColor( Color.WHITE );
757
                            g.fillRect( 0,0,ancho,alto );
758
                            cleanCanvas = false;
759
                    }
760
            }
761

    
762
                /* (non-Javadoc)
763
                 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
764
                 */
765
                public void mouseClicked(MouseEvent ev) {
766

    
767
                }
768

    
769
                /**
770
                 * Cuando el rat?n entra dentro del control fijamos el valor de las coordenadas
771
                 * que tiene el punto seleccionado. De esta forma cuando salga del control podr?
772
                 * volver a restaurarlo en caso de que no haya sido cambiado.
773
                 */
774
                public void mouseEntered(MouseEvent arg0) {
775
                        if(isEnableEvent()){
776
                                if(!zoomControl.rightControl)
777
                                        zoomControl.getDialog().getPointManager().setCurrentWC();
778
                                else
779
                                        zoomControl.getDialog().getPointManager().setCurrentPixelCoord();
780
                        }
781
                }
782

    
783
                /**
784
                 * Al sacar el rat?n de la zona de dibujado de la miniimagen restauramos los valores
785
                 * de los JTextField.
786
                 *
787
                 * Se actualizaran los correspondientes a las coordenadas del mundo real
788
                 * en el caso de que el control tenga botones a la izquierda.
789
                 *
790
                 * Se actualizaran los correspondientes a las coordenadas en pixels cuando se trata del control con
791
                 * botones a la derecha.
792
                 */
793
                public void mouseExited(MouseEvent arg0) {
794
                        if(isEnableEvent()){
795
                                if(!zoomControl.rightControl)
796
                                        zoomControl.getDialog().getPointManager().replaceWC();
797
                                else
798
                                        zoomControl.getDialog().getPointManager().replacePixelCoord();
799
                                drawRedCrux = drawBlueCrux = true;
800
                                repaint();
801
                        }
802
                }
803

    
804
                /**
805
                 * Controla la actualizaci?n del valor de un punto en los JTextDialog. Cuando se mueve sobre
806
                 * el panel con controles a la izquierda actualizamos las coordenadas del mapa. Cuando se mueve sobre
807
                 * el panel con controles a la derecha actualizamos las coordenadas de la imagen.
808
                 */
809
                public void mouseMoved(MouseEvent arg0) {
810
                        if(        isEnableEvent() &&
811
                                zoomControl.getLyrPoints() != null &&
812
                                zoomControl.getLyrPoints().getCountPoints() > 0 &&
813
                                this.newViewPort != null &&
814
                                this.newViewPort.getExtent() != null){
815

    
816
                                double newWCPointX = this.newViewPort.getExtent().getMinX() + ((arg0.getX() * this.newViewPort.getExtent().getWidth()) / this.getWidth());
817
                                int ptoSelectY = (int)(this.getHeight() - arg0.getY());
818
                                double newWCPointY = this.newViewPort.getExtent().getMinY() + ((ptoSelectY * this.newViewPort.getExtent().getHeight()) / this.getHeight());
819
                                if(!zoomControl.rightControl){
820
                                        zoomControl.getDialog().getPointManager().setTempWC(newWCPointX, newWCPointY);
821
                                }else{
822
                                        Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(newWCPointX, newWCPointY);
823
                                        if(pixelImg != null)
824
                                                zoomControl.getDialog().getPointManager().setTempPixelCoord(pixelImg.getX(), pixelImg.getY());
825
                                        else
826
                                                //Si pixelImg es null es que nos hemos salido de la imagen por lo que reponemos los valores originales hasta que vuelva a entrar.
827
                                                zoomControl.getDialog().getPointManager().replacePixelCoord();
828

    
829
                                }
830
                        }
831
                }
832

    
833
                /**
834
                 * Al pulsar el rat?n sobre la minimagen permitimos que se pueda desplazar la cruz
835
                 *
836
                 */
837
                public void mousePressed(MouseEvent ev) {
838
                        int cte = 10;
839
                        if( (ev.getX() > ((getWidth() >> 1) - cte)) && (ev.getX() < ((getWidth() >> 1) + cte)) &&
840
                                (ev.getY() > ((getHeight() >> 1) - cte)) && (ev.getY() < ((getHeight() >> 1) + cte))){
841
                                this.dragged = true;
842

    
843
                                try{
844
                                        if(zoomControl.isRightControl()){
845
                                                drawRedCrux = false;
846
                                                setCursor(Toolkit.getDefaultToolkit().createCustomCursor(redCursor, new Point(16, 16), ""));
847
                                        }else{
848
                                                drawBlueCrux = false;
849
                                                setCursor(Toolkit.getDefaultToolkit().createCustomCursor(blueCursor, new Point(16, 16), ""));
850
                                        }
851

    
852
                                        zoomControl.loadLayer();
853
                                        initCrux = new Point2D.Double();
854
                                        initCrux.setLocation(this.getWidth() / 2, this.getHeight() / 2);
855
                                    draw = true;
856
                                        repaint();
857
                                }catch(InstantiationException e){
858
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
859
                                                        PluginServices.getText(this, "error_capa_puntos"));
860
                                }
861
                        }
862
                }
863

    
864
                /* (non-Javadoc)
865
                 * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
866
                 */
867
                public void mouseReleased(MouseEvent ev) {
868
                        if(!dragged)
869
                                return;
870
                        dragged = false;
871
                        View  theView = null;
872
                        try{
873
                                theView = (View) PluginServices.getMDIManager().getActiveWindow();
874
                        }catch(ClassCastException exc){
875
                                return;
876
                        }
877
                        ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
878
                        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
879

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

    
883
                        FLyrPoints lp = zoomControl.getLyrPoints();//(FLyrPoints)lyr;
884

    
885
                if(zoomControl.isRightControl()){ //Control de actualizaci?n de pixel de imagen
886
                        Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(point);
887
                        //Point2D pixelImg = zoomControl.getLyrGeoRaster().world2Img(newWCPointX, newWCPointY);
888

    
889
                        if(pixelImg == null){
890
                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
891
                                                                     PluginServices.getText(this, "fuera_de_extent"));
892
                                     return;
893
                             }
894

    
895
                        zoomControl.getDialog().getPointManager().updateData(        nPoint, pixelImg, null,
896
                                                                                                                                         zoomControl.getDialog(), null);
897
                        setCenterPoint(point);
898
                        newViewPort = GeoUtils.shiftExtent(lyrPoints.getCenterPoint(dialog.getSelectedPoint(), false), point, lyrPoints.getMiniExtent(dialog.getSelectedPoint(), false));
899
                        lyrPoints.setMiniExtent(nPoint - 1, point, newViewPort, false);
900
                        calcZoom(lyrPoints.getZoomLeft(dialog.getSelectedPoint()));
901
                        zoomControl.getDialog().getPointManager().setCurrentPixelCoord();
902
                }else{ //Control de actualizaci?n de coordenadas del mundo
903
                                zoomControl.getDialog().getPointManager().updateData(        nPoint, null, point,
904
                                                                                                                                                        zoomControl.getDialog(), null);
905
                                setCenterPoint(point);
906
                                newViewPort = GeoUtils.shiftExtent(lyrPoints.getCenterPoint(dialog.getSelectedPoint(), true), point, lyrPoints.getMiniExtent(dialog.getSelectedPoint(), true));
907
                                lyrPoints.setMiniExtent(nPoint - 1, point, newViewPort, true);
908
                                calcZoom(lyrPoints.getZoomRight(dialog.getSelectedPoint()));
909
                                zoomControl.getDialog().getPointManager().setCurrentWC();
910
                 }
911
                dialog.getConectorPanel().getAdjustGeorefPanel().getZoomLeft().draw();
912
                dialog.getConectorPanel().getAdjustGeorefPanel().getZoomRight().draw();
913

    
914
                    draw = true;
915
                    drawRedCrux = drawBlueCrux = true;
916
                        repaint();
917
                        theView.getMapControl().getMapContext().invalidate();
918
                }
919

    
920
                /* (non-Javadoc)
921
                 * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
922
                 */
923
                public void mouseDragged(MouseEvent ev) {
924
                        this.mouseMoved(ev);
925
                }
926
            //**********************End Methods***************************
927

    
928
                //**********************Setters & Getters*********************
929
                /**
930
                 * @param cleanCanvas The cleanCanvas to set.
931
                 */
932
                public void setCleanCanvas(boolean cleanCanvas) {
933
                        this.cleanCanvas = cleanCanvas;
934
                }
935

    
936
                 /**
937
             * Asigna el punto donde se centrar? la vista de la minimagen
938
             * @param pto Punto central.
939
             */
940
            public void setCenterPoint(Point2D pto){
941
                    this.centerPoint = pto;
942
            }
943

    
944
            /**
945
             * Asigna el viewPort
946
             * @param vp ViewPort
947
             */
948
            public void setViewPort(ViewPort vp, double zoom){
949
                    this.initViewPort(vp, this.centerPoint, null, zoom);
950
            }
951

    
952
            /**
953
             * Asigna el viewPort recalculado
954
             * @param vp ViewPort
955
             */
956
            public void setNewViewPort(ViewPort vp){
957
                    this.newViewPort = vp;
958
            }
959

    
960
            /**
961
             * Obtiene el viewPort recalculado
962
             * @return vp ViewPort
963
             */
964
            public ViewPort getNewViewPort(){
965
                    return this.newViewPort;
966
            }
967

    
968
            /**
969
             * Obtiene el valor del desactivador de eventos sobre el canvas.
970
             * @return
971
             */
972
            public boolean isEnableEvent() {
973
                        return enableEvent;
974
                }
975

    
976
            /**
977
             * Asigna el desactivador de eventos sobre el canvas
978
             * @param enableEvent
979
             */
980
                public void setEnableEvent(boolean enableEvent) {
981
                        this.enableEvent = enableEvent;
982
                }
983

    
984
                //**********************End Setters & Getters*****************
985

    
986
        }
987
 }