Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / wizards / GeoRasterWizard.java @ 4841

History | View | Annotate | Download (18 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig.gui.wizards;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.FlowLayout;
45
import java.awt.GridBagConstraints;
46
import java.awt.GridBagLayout;
47
import java.awt.event.ActionEvent;
48
import java.awt.geom.Rectangle2D;
49
import java.io.File;
50

    
51
import javax.swing.JButton;
52
import javax.swing.JCheckBox;
53
import javax.swing.JDesktopPane;
54
import javax.swing.JDialog;
55
import javax.swing.JFileChooser;
56
import javax.swing.JInternalFrame;
57
import javax.swing.JLabel;
58
import javax.swing.JLayeredPane;
59
import javax.swing.JPanel;
60
import javax.swing.JTextField;
61
import javax.swing.filechooser.FileFilter;
62

    
63
import org.cresques.cts.IProjection;
64
import org.cresques.io.GeoRasterFile;
65
import org.cresques.px.Extent;
66

    
67
import com.hardcode.driverManager.DriverLoadException;
68
import com.iver.andami.PluginServices;
69
import com.iver.andami.messages.NotificationManager;
70
import com.iver.andami.ui.mdiFrame.MDIFrame;
71
import com.iver.cit.gvsig.fmap.DriverException;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
74
import com.iver.cit.gvsig.fmap.layers.FLayer;
75
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
76
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
77
import com.iver.cit.gvsig.gui.FOpenDialog;
78
import com.iver.cit.gvsig.gui.View;
79
import com.iver.cit.gvsig.gui.WizardPanel;
80
import com.iver.cit.gvsig.gui.Panels.ProjChooserPanel;
81
import com.iver.cit.gvsig.gui.dialogs.GeoreferencingDialog;
82
import com.iver.cit.gvsig.utils.PointManager;
83
 
84
/**
85
 * Clase que implementa el Wizard para la georreferenciaci?n
86
 * @author Nacho Brodin (brodin_ign@gva.es)
87
 */
88
public class GeoRasterWizard extends WizardPanel{
89
        
90
        //**********************Vars**********************************
91
        private JPanel                                         pGeneral = null;
92
        private JPanel                                         pFileSelection = null;
93
        private JPanel                                         pControls = null;
94
        private JTextField                                 tFile = null;
95
        private JButton                                 bSelectFile = null;
96
        private JPanel                                         pProyection = null;
97
        private WizardListenerSupport         listenerSupport = new WizardListenerSupport();
98
        /**
99
         * Lista de formatos soportados
100
         */
101
        private String[]                                 fileFilters = {"ecw", "sid", "tif", "jpg", "png", "jp2"};
102
        /**
103
         * Nombre del fichero seleccionado
104
         */
105
        private String                                         fName = "";
106
        
107
        /**
108
         * Nombre de la capa
109
         */
110
        private String                                         lyrName = "";
111
        /**
112
         * Recuerda la ?ltima ruta seleccionada por el usuario
113
         */
114
        //private String                                         lastPath = "./";
115
        private static String                         lastPath = null;
116
        private FLyrGeoRaster                         lyrGeoRaster = null;
117
        private JPanel                                         pCheckUseGeoref = null;
118
        private JCheckBox                                 cbUseGeoref = null;
119
        private JLabel                                         lUseGeoref = null;
120
        private double                                         widthPxImg, heightPxImg;
121
        /**
122
         * Extent calculado a partir de la vista.
123
         */
124
        private Extent                                         ext = null;
125
        //**********************Vars**********************************
126

    
127
        //**********************Classes*******************************
128
        /**
129
         * @author Nacho Brodin (brodin_ign@gva.es)
130
         */
131
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
132
                
133
                private JFileChooser chooser = null;
134
                private String file = null;
135
                
136
                public SelectFileFilter(JFileChooser ch, String file){
137
                        this.chooser = ch;
138
                        this.file = file;
139
                }
140
                
141
            public boolean accept(File f) {
142

    
143
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
144
            }
145
            
146
            public String getDescription() {
147
                    return file;
148
            }
149
            
150
        }
151
        //**********************End Classes***************************
152
        
153
        //**********************Methods*******************************
154
        /**
155
         * This is the default constructor
156
         */
157
        public GeoRasterWizard() {
158
                super();
159
                initialize();
160
        }
161

    
162
        /**
163
         * This method initializes this
164
         * 
165
         * @return void
166
         */
167
        private void initialize() {
168
        this.setPreferredSize(new java.awt.Dimension(750,320));
169
        this.setSize(new java.awt.Dimension(510,311));
170
        this.setLocation(new java.awt.Point(0,0));
171
        this.add(getPGeneral(), null);
172
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
173
            public void actionPerformed(java.awt.event.ActionEvent evt) {
174
                    acceptButtonActionPerformed(evt);
175
            }
176
        });
177
         }
178
        
179
        /**
180
         * Calcula un extent posible para la imagen a partir del extent de la vista.
181
         * En este caso centra la imagen en la vista para que cuando se cargue la imagen
182
         * a georreferenciar nos quede centrada si elegimos la opci?n de no utilizar el
183
         * extent de la imagen.
184
         * @param w        Ancho de la imagen
185
         * @param h Alto de la imagen
186
         * @return        Extent para la imagen
187
         */
188
        protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){
189
                
190
                //Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
191
                //del viewport.
192
                Extent tempExtent = null;
193
                GeoRasterFile grf = GeoRasterFile.openFile(proj, file);
194
                widthPxImg = grf.getWidth();
195
                heightPxImg = grf.getHeight();
196
                if(vp == null || vp.getAdjustedExtent() == null){
197
                        vp = new ViewPort(proj);
198
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, widthPxImg, heightPxImg);
199
                        vp.setExtent(r2d);
200
                        tempExtent = new Extent(0, 0, widthPxImg, heightPxImg);
201
                }else
202
                        tempExtent = new Extent(vp.getAdjustedExtent());
203
                //grf.close();                
204
                
205
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
206
                if(widthPxImg > heightPxImg){
207
                        double widthView = tempExtent.maxX() - tempExtent.minX();
208
                        ulX = tempExtent.minX() + (widthView / 4);
209
                        lrX = ulX + (widthView / 2);
210
                        double newAlto = ((heightPxImg * (widthView / 2)) / widthPxImg);
211
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
212
                        ulY = centroY - (newAlto / 2);
213
                        lrY = centroY + (newAlto / 2);
214
                }else{
215
                        double heightView = tempExtent.maxY() - tempExtent.minY();
216
                        ulY = tempExtent.minY() + (heightView / 4);
217
                        lrY = ulY + (heightView / 2);
218
                        double newAncho = ((widthPxImg * (heightView / 2)) / heightPxImg);
219
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
220
                        ulX = centroX - (newAncho / 2);
221
                        lrX = centroX + (newAncho / 2);
222
                }
223
                return new Extent(ulX, ulY, lrX, lrY);
224
        }
225
        
226
        /**
227
         * Evento de pulsado del bot?n de seleccionar fichero
228
         * @param e
229
         */
230
        private void acceptButtonActionPerformed(ActionEvent e) {
231
                //Selector de Fichero que se quiere georeferenciar
232
                                
233
                if(        e.getSource().equals(this.getBSelectFile())){
234
                        JFileChooser chooser = new JFileChooser(lastPath);
235
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
236
                        FileFilter f = null;
237
                        for(int i=0; i<this.fileFilters.length;i++){
238
                                f = new SelectFileFilter(chooser, this.fileFilters[i]);
239
                                chooser.addChoosableFileFilter(f);
240
                        }
241
                        int returnVal = chooser.showOpenDialog(this);
242
                        if(returnVal == JFileChooser.APPROVE_OPTION){
243
                                 this.fName = chooser.getSelectedFile().toString();
244
                                 FileFilter filter = chooser.getFileFilter();
245
                                 this.getTFile().setText(fName);
246
                                 lastPath = chooser.getCurrentDirectory().getAbsolutePath();
247
                                 
248
                                 if (PluginServices.getMainFrame() == null) {
249
                    ((JDialog) (getParent().getParent().getParent()
250
                            .getParent())).dispose();
251
                } else {
252
                        //Creamos la capa y la cargamos
253
                        IProjection proj = this.getPProyection().getCurProj();
254
                        try{
255
                                View  theView = null;
256
                                    try{
257
                                            theView = (View)PluginServices.getMDIManager().getActiveView();
258
                                    }catch(ClassCastException exc){
259
                                            return;
260
                                    }
261
                                                                
262
                                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
263
                                                        
264
                                File fich = new File(fName);
265
                                
266
                                                ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
267
                                                
268
                                                //Calcula el extent para que la imagen que se carga quede centrada.
269
                                                ext = calcTempExtent( fName, viewPort, proj);
270
                                                
271
                                                //Asigna el nombre de la capa con el marcador *
272
                                this.setLyrName("*"+fName.substring(fName.lastIndexOf(File.separator)+1));
273
                                
274
                                //Crea la capa
275
                                lyrGeoRaster = FLyrGeoRaster.createLayer(getLyrName(), 
276
                                                                                                                (RasterDriver) driver, 
277
                                                                                                                fich, 
278
                                                                                                                proj,
279
                                                                                                                widthPxImg,
280
                                                                                                                heightPxImg);
281
                                //Pone activa la capa                              
282
                                if(lyrGeoRaster != null)
283
                                    lyrGeoRaster.setActive(true);
284
                            
285
                                
286
                                if(        ext != null && fich != null && 
287
                                        this.lyrGeoRaster != null && !getTFile().getText().equals(""))
288
                                        listenerSupport.callStateChanged(true);
289
                                
290
                        }catch(DriverLoadException exc){
291
                                NotificationManager.addError("No se pudo acceder a los drivers", exc);
292
                        }catch(DriverException exc){
293
                                NotificationManager.addError("El driver ha producido un error", exc);
294
                        }
295
                   
296
                }
297
                         }
298
            }        
299
                
300
        }
301
        
302
        /**
303
         * M?todo que se ejecuta al pulsar el bot?n de aceptar. En este caso asignamos el
304
         * extent calculado desde la vista si el checkbox no est? marcado y lanzaremos el
305
         * dialogo de georeferenciaci?n. El resto del trabajo lo realiza la clase AddLayer.
306
         */
307
        public void execute() {
308
                if(!this.getCbUseGeoref().isSelected())
309
                        lyrGeoRaster.setAssignExtent(ext);  
310
                
311
                PointManager.initWindow(lyrGeoRaster, true, this);
312
                
313
                /*lyrGeoRaster.getStackZoom().setInitExtent(lyrGeoRaster.getAssignExtent());
314
                GeoreferencingDialog geoDialog = null;
315
                
316
                JInternalFrame panel = PointManager.getInternalFrame(this);
317
        geoDialog = new GeoreferencingDialog(panel, lyrGeoRaster);             
318
            lyrGeoRaster.getFLyrPoints().saveState();
319
        lyrGeoRaster.setGeoDialog(geoDialog);
320
        panel.setSize(geoDialog.getWidth() + 12, geoDialog.getHeight() + 10);
321
        panel.getContentPane().add(geoDialog);
322
       
323
        MDIFrame mainFrame = (MDIFrame) PluginServices.getMainFrame();
324
        
325
        JDesktopPane desk = new JDesktopPane();
326
        desk.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
327
              
328
        JLayeredPane lyrPane = mainFrame.getLayeredPane();
329
        lyrPane.add(panel, JDesktopPane.PALETTE_LAYER);    
330
        lyrPane.add(desk, JDesktopPane.OUTLINE_DRAG_MODE);
331
        panel.show();*/
332
        }
333
                           
334
        public void initWizard() {
335
        
336
        }
337

    
338
        /**
339
         * Adds the gvSIG's wizard listener 
340
         * 
341
         * @param listener
342
         */
343
        public void addWizardListener(WizardListener listener) { 
344
                listenerSupport.addWizardListener(listener);
345
        }
346

    
347
        /* (non-Javadoc)
348
         * @see com.iver.cit.gvsig.gui.WizardPanel#callError(java.lang.Exception)
349
         */
350
        public void callError(Exception descripcion) {
351
                // TODO Auto-generated method stub
352
                super.callError(descripcion);
353
        }
354

    
355
        /* (non-Javadoc)
356
         * @see com.iver.cit.gvsig.gui.WizardPanel#callStateChanged(boolean)
357
         */
358
        public void callStateChanged(boolean finishable) {
359
                // TODO Auto-generated method stub
360
                super.callStateChanged(finishable);
361
        }
362

    
363
        /* (non-Javadoc)
364
         * @see com.iver.cit.gvsig.gui.WizardPanel#removeWizardListener(com.iver.cit.gvsig.gui.wizards.WizardListener)
365
         */
366
        public void removeWizardListener(WizardListener listener) {
367
                // TODO Auto-generated method stub
368
                super.removeWizardListener(listener);
369
        }        
370
        //**********************End Methods***************************
371
        
372
        //**********************Getters & Setters*********************
373
        /* (non-Javadoc)
374
         * @see com.iver.cit.gvsig.gui.WizardPanel#setTabName(java.lang.String)
375
         */
376
        protected void setTabName(String name) {
377
                // TODO Auto-generated method stub
378
                super.setTabName(name);
379
        }
380
        
381
        /* (non-Javadoc)
382
         * @see com.iver.cit.gvsig.gui.WizardPanel#getLayer()
383
         */
384
        public FLayer getLayer() {
385
                if(lyrGeoRaster != null)
386
                        return lyrGeoRaster;
387
                return null;
388
        }
389

    
390
        /* (non-Javadoc)
391
         * @see com.iver.cit.gvsig.gui.WizardPanel#getTabName()
392
         */
393
        public String getTabName() {
394
                return PluginServices.getText(this, "georef");
395
        }
396
        
397
        /**
398
         * @return Returns the fName.
399
         */
400
        public String getLyrName() {
401
                return lyrName;
402
        }
403
        
404
        /**
405
         * @param name The fName to set.
406
         */
407
        public void setLyrName(String name) {
408
                lyrName = name;
409
        }
410
        
411
        /**
412
         * This method initializes jPanel        
413
         *         
414
         * @return javax.swing.JPanel        
415
         */    
416
        private JPanel getPCheckUseGeoref() {
417
                if (pCheckUseGeoref == null) {
418
                        FlowLayout flowLayout1 = new FlowLayout();
419
                        flowLayout1.setAlignment(java.awt.FlowLayout.LEFT);
420
                        lUseGeoref = new JLabel();
421
                        pCheckUseGeoref = new JPanel();
422
                        pCheckUseGeoref.setLayout(flowLayout1);
423
                        pCheckUseGeoref.setPreferredSize(new java.awt.Dimension(370,31));
424
                        lUseGeoref.setText("Usar georeferenciaci?n de la imagen");
425
                        pCheckUseGeoref.add(getCbUseGeoref(), null);
426
                        pCheckUseGeoref.add(lUseGeoref, null);
427
                }
428
                return pCheckUseGeoref;
429
        }
430
        
431
        /**
432
         * This method initializes jCheckBox        
433
         *         
434
         * @return javax.swing.JCheckBox        
435
         */    
436
        private JCheckBox getCbUseGeoref() {
437
                if (cbUseGeoref == null) {
438
                        cbUseGeoref = new JCheckBox();
439
                        cbUseGeoref.setSelected(false);
440
                }
441
                return cbUseGeoref;
442
        }
443
        
444
        /**
445
         * @return Returns the lyrRaster.
446
         */
447
        public FLyrGeoRaster getFLyrGeoRaster() {
448
                return lyrGeoRaster;
449
        }
450

    
451
        /**
452
         * @param lyrRaster The lyrRaster to set.
453
         */
454
        public void setFLyrGeoRaster(FLyrGeoRaster lyrGeoRaster) {
455
                this.lyrGeoRaster = lyrGeoRaster;
456
        }
457
        
458
        /**
459
         * This method initializes jPanel        
460
         *         
461
         * @return javax.swing.JPanel        
462
         */
463
        private JPanel getPGeneral() {
464
                if (pGeneral == null) {
465
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
466
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
467
                        gridBagConstraints1.gridy = 1;
468
                        gridBagConstraints1.gridx = 0;
469
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
470
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
471
                        gridBagConstraints.gridy = 0;
472
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
473
                        gridBagConstraints.gridx = 0;
474
                        pGeneral = new JPanel();
475
                        pGeneral.setLayout(new GridBagLayout());
476
                        pGeneral.setPreferredSize(new java.awt.Dimension(750,320));
477
                        gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
478
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
479
                        pGeneral.add(getPControls(), gridBagConstraints1);
480
                }
481
                return pGeneral;
482
        }
483

    
484
        /**
485
         * This method initializes jPanel        
486
         *         
487
         * @return javax.swing.JPanel        
488
         */
489
        private JPanel getPFileSelection() {
490
                if (pFileSelection == null) {
491
                        FlowLayout flowLayout = new FlowLayout();
492
                        flowLayout.setVgap(10);
493
                        pFileSelection = new JPanel();
494
                        pFileSelection.setBorder(javax.swing.BorderFactory.createTitledBorder(null, PluginServices.getText(this,"cargar"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
495
                        pFileSelection.setLayout(flowLayout);
496
                        pFileSelection.setPreferredSize(new java.awt.Dimension(475,70));
497
                        pFileSelection.add(getTFile(), null);
498
                        pFileSelection.add(getBSelectFile(), null);
499
                }
500
                return pFileSelection;
501
        }
502

    
503
        private JPanel getPControls() {
504
                if (pControls == null) {
505
                        GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
506
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
507
                        gridBagConstraints2.gridx = 0;
508
                        gridBagConstraints2.gridy = 1;
509
                        pControls = new JPanel();
510
                        pControls.setLayout(new GridBagLayout());
511
                        pControls.setPreferredSize(new java.awt.Dimension(475,200));
512
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
513
                        gridBagConstraints11.gridx = 0;
514
                        gridBagConstraints11.gridy = 0;
515
                        pControls.add(getPCheckUseGeoref(), gridBagConstraints11);
516
                        pControls.add(getPProyection(), gridBagConstraints2);
517
                }
518
                return pControls;
519
        }
520
        
521
        /**
522
         * This method initializes jTextField        
523
         *         
524
         * @return javax.swing.JTextField        
525
         */
526
        private JTextField getTFile() {
527
                if (tFile == null) {
528
                        tFile = new JTextField();
529
                        tFile.setPreferredSize(new java.awt.Dimension(350,25));
530
                }
531
                return tFile;
532
        }
533

    
534
        /**
535
         * This method initializes jButton        
536
         *         
537
         * @return javax.swing.JButton        
538
         */
539
        private JButton getBSelectFile() {
540
                if (bSelectFile == null) {
541
                        bSelectFile = new JButton();
542
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
543
                }
544
                return bSelectFile;
545
        }
546
        
547
        /**
548
         * This method initializes jPanel        
549
         *         
550
         * @return javax.swing.JPanel        
551
         */    
552
        private ProjChooserPanel getPProyection() {
553
                if (pProyection == null) {
554
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
555
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
556
                                public void actionPerformed(java.awt.event.ActionEvent e) {
557
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
558
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
559
                                }
560
                                }
561
                        });
562
                }
563
                return ((ProjChooserPanel)pProyection);
564
        }
565
        //**********************End Getters & Setters*****************
566
}
567