Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / com / iver / cit / gvsig / gui / Panels / SelectFilePanel.java @ 2894

History | View | Annotate | Download (12.6 KB)

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

    
3
import java.awt.FlowLayout;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.geom.Rectangle2D;
8
import java.io.File;
9

    
10
import javax.swing.JButton;
11
import javax.swing.JDialog;
12
import javax.swing.JFileChooser;
13
import javax.swing.JPanel;
14
import javax.swing.JTextField;
15
import javax.swing.filechooser.FileFilter;
16

    
17
import org.cresques.cts.IProjection;
18
import org.cresques.io.GeoRasterFile;
19
import org.cresques.px.Extent;
20

    
21
import com.hardcode.driverManager.DriverLoadException;
22
import com.iver.andami.PluginServices;
23
import com.iver.andami.messages.NotificationManager;
24
import com.iver.cit.gvsig.fmap.DriverException;
25
import com.iver.cit.gvsig.fmap.ViewPort;
26
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
27
import com.iver.cit.gvsig.fmap.drivers.RasterDriver;
28
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
29
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
30
import com.iver.cit.gvsig.fmap.layers.RasterAdapter;
31
import com.iver.cit.gvsig.fmap.layers.RasterFileAdapter;
32
import com.iver.cit.gvsig.gui.FOpenDialog;
33
import com.iver.cit.gvsig.gui.View;
34
import com.iver.cit.gvsig.gui.Dialogs.GeoreferencingDialog;
35

    
36
public class SelectFilePanel extends JPanel {
37

    
38
        private JPanel pGeneral = null;
39
        private JPanel pFileSelection = null;
40
        private JPanel pControls = null;
41
        private JTextField tFile = null;
42
        private JButton bSelectFile = null;
43
        //private ProjChooserPanel jPanelProj = null;
44
        private JPanel pProyection = null;
45
        /**
46
         * Lista de formatos soportados
47
         */
48
        private String[] fileFilters = {"ecw", "sid", "tif", "jpg", "png"};
49
        /**
50
         * Nombre del fichero seleccionado
51
         */
52
        private String fName = "";
53
        
54
        /**
55
         * Nombre de la capa
56
         */
57
        private String lyrName = "";
58
        /**
59
         * Recuerda la ?ltima ruta seleccionada por el usuario
60
         */
61
        private String lastPath = "./";
62
        private GeoreferencingDialog parent = null;
63
        private FLyrRaster lyrRaster = null;
64
        
65

    
66
        /**
67
         * This is the default constructor
68
         */
69
        public SelectFilePanel(GeoreferencingDialog georef) {
70
                super();
71
                initialize();
72
                this.parent = georef;
73
        }
74

    
75
        /**
76
         * This method initializes this
77
         * 
78
         * @return void
79
         */
80
        private void initialize() {
81
        this.setPreferredSize(new java.awt.Dimension(400,240));
82
        this.setSize(new java.awt.Dimension(400,240));
83
        this.setLocation(new java.awt.Point(0,0));
84
        this.add(getPGeneral(), null);
85
        this.getBSelectFile().addActionListener(new java.awt.event.ActionListener() {
86
            public void actionPerformed(java.awt.event.ActionEvent evt) {
87
                    acceptButtonActionPerformed(evt);
88
            }
89
        });
90
         }
91

    
92
        /**
93
         * This method initializes jPanel        
94
         *         
95
         * @return javax.swing.JPanel        
96
         */
97
        private JPanel getPGeneral() {
98
                if (pGeneral == null) {
99
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
100
                        gridBagConstraints1.insets = new java.awt.Insets(5,0,0,0);
101
                        gridBagConstraints1.gridy = 1;
102
                        gridBagConstraints1.gridx = 0;
103
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
104
                        gridBagConstraints.insets = new java.awt.Insets(0,0,2,0);
105
                        gridBagConstraints.gridy = 0;
106
                        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
107
                        gridBagConstraints.gridx = 0;
108
                        pGeneral = new JPanel();
109
                        pGeneral.setLayout(new GridBagLayout());
110
                        pGeneral.setPreferredSize(new java.awt.Dimension(400,240));
111
                        pGeneral.add(getPFileSelection(), gridBagConstraints);
112
                        pGeneral.add(getPControls(), gridBagConstraints1);
113
                }
114
                return pGeneral;
115
        }
116

    
117
        /**
118
         * This method initializes jPanel        
119
         *         
120
         * @return javax.swing.JPanel        
121
         */
122
        private JPanel getPFileSelection() {
123
                if (pFileSelection == null) {
124
                        FlowLayout flowLayout = new FlowLayout();
125
                        flowLayout.setVgap(10);
126
                        pFileSelection = new JPanel();
127
                        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));
128
                        pFileSelection.setLayout(flowLayout);
129
                        pFileSelection.setPreferredSize(new java.awt.Dimension(380,70));
130
                        pFileSelection.add(getTFile(), null);
131
                        pFileSelection.add(getBSelectFile(), null);
132
                }
133
                return pFileSelection;
134
        }
135

    
136
        /**
137
         * This method initializes jPanel1        
138
         *         
139
         * @return javax.swing.JPanel        
140
         */
141
        private JPanel getPControls() {
142
                if (pControls == null) {
143
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
144
                        gridBagConstraints2.gridx = 0;
145
                        gridBagConstraints2.gridy = 0;
146
                        pControls = new JPanel();
147
                        pControls.setLayout(new GridBagLayout());
148
                        pControls.setPreferredSize(new java.awt.Dimension(380,135));
149
                        pControls.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
150
                        pControls.add(getPProyection(), gridBagConstraints2);
151
                }
152
                return pControls;
153
        }
154
        
155
        /**
156
         * This method initializes jTextField        
157
         *         
158
         * @return javax.swing.JTextField        
159
         */
160
        private JTextField getTFile() {
161
                if (tFile == null) {
162
                        tFile = new JTextField();
163
                        tFile.setPreferredSize(new java.awt.Dimension(280,25));
164
                }
165
                return tFile;
166
        }
167

    
168
        /**
169
         * This method initializes jButton        
170
         *         
171
         * @return javax.swing.JButton        
172
         */
173
        private JButton getBSelectFile() {
174
                if (bSelectFile == null) {
175
                        bSelectFile = new JButton();
176
                        bSelectFile.setText(PluginServices.getText(this, "cargar"));
177
                }
178
                return bSelectFile;
179
        }
180
        
181
        /**
182
         * This method initializes jPanel        
183
         *         
184
         * @return javax.swing.JPanel        
185
         */    
186
        private ProjChooserPanel getPProyection() {
187
                if (pProyection == null) {
188
                        pProyection = new ProjChooserPanel(GeoreferencingDialog.getLastProjection());
189
                        ((ProjChooserPanel)pProyection).addActionListener(new java.awt.event.ActionListener() { 
190
                                public void actionPerformed(java.awt.event.ActionEvent e) {
191
                                if (((ProjChooserPanel)pProyection).isOkPressed()) {
192
                                        FOpenDialog.setLastProjection(((ProjChooserPanel)pProyection).getCurProj());
193
                                }
194
                                }
195
                        });
196
                }
197
                return ((ProjChooserPanel)pProyection);
198
        }
199

    
200
        /**
201
         * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n y 
202
         * coordenadas de georeferenciaci?n. Esta funci?n es para georeferenciar 
203
         * capas raster. Para imagenes que no tienen georeferenciaci?n hay que asignarle
204
         * una temporal, normalmente a partir de la vista activa.
205
         *
206
         * @param layerName Nombre de la capa.
207
         * @param d RasterDriver.
208
         * @param f Fichero.
209
         * @param proj Proyecci?n.
210
         * @param extent Extent de la vista activa
211
         *
212
         * @return Nueva capa de tipo raster.
213
         *
214
         * @throws DriverIOException
215
         */
216
        private FLyrRaster createLayer(String layerName, RasterDriver d,
217
                File f, IProjection proj, Extent ext) throws DriverException {
218
                RasterAdapter adapter = new RasterFileAdapter(f);
219
                adapter.setDriver(d);
220
                FLyrRaster capa = new FLyrRaster();
221
                if (capa != null) {
222
                        capa.setName(layerName);
223
                        capa.setSource(adapter);
224
                        capa.setProjection(proj);
225
                        capa.setTempExtent(ext);
226
                        capa.setExtentFlag(GeoRasterFile.ORDER);
227
                        try {
228
                                capa.load();
229
                        } catch (DriverIOException e) {
230
                                throw new DriverException(e);
231
                        }
232
                
233
                capa.setVisible(true);
234
                }else 
235
                        return null;
236
                return capa;
237
        }
238
        
239
        /**
240
         * Calcula un extent posible para la imagen a partir del extent de la vista.
241
         * En este caso centra la imagen en la vista.
242
         * @param w        Ancho de la imagen
243
         * @param h Alto de la imagen
244
         * @return        Extent para la imagen
245
         */
246
        protected Extent calcTempExtent(String file, ViewPort vp, IProjection proj){
247
                
248
                //Obtenemos el ancho y alto de la imagen y obtenemos un extent a partir
249
                //del viewport.
250
                Extent tempExtent = null;
251
                GeoRasterFile grf = GeoRasterFile.openFile(proj, file);
252
                double w = grf.getWidth();
253
                double h = grf.getHeight();
254
                if(vp == null || vp.getAdjustedExtent() == null){
255
                        vp = new ViewPort(proj);
256
                        Rectangle2D r2d = new Rectangle2D.Double(0, 0, w, h);
257
                        vp.setExtent(r2d);
258
                        tempExtent = new Extent(0, 0, w, h);
259
                }else
260
                        tempExtent = new Extent(vp.getAdjustedExtent());
261
                grf.close();
262
                
263
                
264
                double ulX = 0D, ulY = 0D, lrX = 0D, lrY = 0D;
265
                if(w > h){
266
                        double widthView = tempExtent.maxX() - tempExtent.minX();
267
                        ulX = tempExtent.minX() + (widthView / 4);
268
                        lrX = ulX + (widthView / 2);
269
                        double newAlto = ((h * (widthView / 2)) / w);
270
                        double centroY = tempExtent.minY()+((tempExtent.maxY() - tempExtent.minY())/2);
271
                        ulY = centroY - (newAlto / 2);
272
                        lrY = centroY + (newAlto / 2);
273
                }else{
274
                        double heightView = tempExtent.maxY() - tempExtent.minY();
275
                        ulY = tempExtent.minY() + (heightView / 4);
276
                        lrY = ulY + (heightView / 2);
277
                        double newAncho = ((w * (heightView / 2)) / h);
278
                        double centroX = tempExtent.minX()+((tempExtent.maxX() - tempExtent.minX())/2);
279
                        ulX = centroX - (newAncho / 2);
280
                        lrX = centroX + (newAncho / 2);
281
                }
282
                return new Extent(ulX, ulY, lrX, lrY);
283
        }
284
        
285
        /**
286
         * Evento de pulsado del bot?n de seleccionar fichero
287
         * @param e
288
         */
289
        private void acceptButtonActionPerformed(ActionEvent e) {
290
                //Selector de Fichero que se quiere georeferenciar
291
                
292
                if(        e.getSource().equals(this.getBSelectFile())){
293
                        JFileChooser chooser = new JFileChooser(lastPath);
294
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
295
                        FileFilter f = null;
296
                        for(int i=0; i<this.fileFilters.length;i++){
297
                                f = new SelectFileFilter(chooser, this.fileFilters[i]);
298
                                chooser.addChoosableFileFilter(f);
299
                        }
300
                        int returnVal = chooser.showOpenDialog(this);
301
                        if(returnVal == JFileChooser.APPROVE_OPTION){
302
                                 this.fName = chooser.getSelectedFile().toString();
303
                                 FileFilter filter = chooser.getFileFilter();
304
                                 this.getTFile().setText(fName);
305
                                 lastPath = chooser.getCurrentDirectory().getAbsolutePath();
306
                                 
307
                                 if (PluginServices.getMainFrame() == null) {
308
                    ((JDialog) (getParent().getParent().getParent()
309
                            .getParent())).dispose();
310
                } else {
311
                        //Creamos la capa y la cargamos
312
                        IProjection proj = this.getPProyection().getCurProj();
313
                        try{
314
                                com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
315
                                View theView = (View) PluginServices.getMDIManager().getActiveView();
316
                                                                        
317
                                File fich = new File(fName);
318
                                
319
                                                ViewPort viewPort = theView.getMapControl().getMapContext().getViewPort();
320
                                                                                                                                
321
                                //Calculamos el extent de la imagen que ser? cargada
322
                                Extent ext = calcTempExtent(        fName, 
323
                                                                                                viewPort, 
324
                                                                                                proj);
325
                                
326
                                this.setLyrName("*"+fName.substring(fName.lastIndexOf("/")+1));
327
                                lyrRaster = this.createLayer("*"+fName.substring(fName.lastIndexOf("/")+1, fName.length()),
328
                            (RasterDriver) driver, fich, proj, ext);
329
                            
330
                                //Pasamos la capa al behavior
331
                                this.parent.getRedimBehavior().setLyrRaster(lyrRaster);
332
                                this.parent.getRedimBehavior().setPathToFile(fName);
333
                                this.parent.getGeoMoveBehavior().setLyrRaster(lyrRaster);
334
                                this.parent.getGeoMoveBehavior().setPathToFile(fName);
335
                                
336
                                            theView.getMapControl().getMapContext().getLayers()
337
                                               .addLayer(lyrRaster);  
338
                                                                            
339
                        }catch(DriverLoadException exc){
340
                                NotificationManager.addError("No se pudo acceder a los drivers", exc);
341
                        }catch(DriverException exc){
342
                                NotificationManager.addError("El driver ha producido un error", exc);
343
                        }
344
                    parent.getTabPanel().setSelectedIndex(1);
345
                }
346
                         }
347
            }        
348
                
349
        }
350
        
351
        /**
352
         * @return Returns the lyrRaster.
353
         */
354
        public FLyrRaster getFLyrRaster() {
355
                return lyrRaster;
356
        }
357

    
358
        /**
359
         * @param lyrRaster The lyrRaster to set.
360
         */
361
        public void setFLyrRaster(FLyrRaster lyrRaster) {
362
                this.lyrRaster = lyrRaster;
363
        }
364
   
365
        /**
366
         * 
367
         * @author Nacho Brodin (brodin_ign@gva.es)
368
         *
369
         */
370
        class SelectFileFilter extends javax.swing.filechooser.FileFilter {
371
                
372
                private JFileChooser chooser = null;
373
                private String file = null;
374
                
375
                public SelectFileFilter(JFileChooser ch, String file){
376
                        this.chooser = ch;
377
                        this.file = file;
378
                }
379
                
380
            public boolean accept(File f) {
381

    
382
                    return f.isDirectory() || f.getName().toLowerCase().endsWith("."+file);
383
            }
384
            
385
            public String getDescription() {
386
                    return file;
387
            }
388
            
389
        }
390
        
391
        /**
392
         * @return Returns the fName.
393
         */
394
        public String getLyrName() {
395
                return lyrName;
396
        }
397
        /**
398
         * @param name The fName to set.
399
         */
400
        public void setLyrName(String name) {
401
                lyrName = name;
402
        }
403
}