Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / mosaic / gui / MosaicDialog.java @ 21869

History | View | Annotate | Download (17.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
         *
3
         * Copyright (C) 2006 Instituto de Desarrollo Regional 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 Iba?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
         *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
         *   Campus Universitario s/n
35
         *   02071 Alabacete
36
         *   Spain
37
         *
38
         *   +34 967 599 200
39
         */
40

    
41
package org.gvsig.remotesensing.mosaic.gui;
42

    
43

    
44
/**
45
 * Di?logo para la funcionalidad de mosaicos. 
46
 * 
47
 * @author aMu?oz (alejandro.munoz@uclm.es)
48
 * @version 24/4/2008
49
 * 
50
 * */
51

    
52
import java.awt.BorderLayout;
53
import java.awt.Dimension;
54
import java.awt.GridBagConstraints;
55
import java.awt.GridBagLayout;
56
import java.awt.Insets;
57
import java.awt.event.ActionEvent;
58
import java.awt.event.ActionListener;
59
import java.awt.event.FocusEvent;
60
import java.awt.event.FocusListener;
61
import java.beans.PropertyChangeEvent;
62
import java.beans.PropertyChangeListener;
63
import java.io.File;
64

    
65
import javax.swing.BorderFactory;
66
import javax.swing.ButtonGroup;
67
import javax.swing.JFileChooser;
68
import javax.swing.JLabel;
69
import javax.swing.JPanel;
70
import javax.swing.JRadioButton;
71
import javax.swing.JTabbedPane;
72
import javax.swing.JTextField;
73
import javax.swing.SwingConstants;
74
import javax.swing.border.TitledBorder;
75

    
76
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
77
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
78
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
79
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
80
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
81
import org.gvsig.raster.IProcessActions;
82
import org.gvsig.raster.RasterLibrary;
83
import org.gvsig.raster.dataset.GeoRasterWriter;
84
import org.gvsig.raster.datastruct.Histogram;
85
import org.gvsig.raster.hierarchy.IHistogramable;
86
import org.gvsig.raster.util.ExtendedFileFilter;
87
import org.gvsig.raster.util.PropertyEvent;
88
import org.gvsig.raster.util.PropertyListener;
89
import org.gvsig.raster.util.RasterNotLoadException;
90
import org.gvsig.raster.util.RasterToolsUtil;
91
import org.gvsig.rastertools.histogram.HistogramProcess;
92
import org.gvsig.remotesensing.mosaic.process.FeatherProcess;
93
import org.gvsig.remotesensing.mosaic.process.HistogramMatchProcess;
94
import org.gvsig.remotesensing.mosaic.process.MosaicProcess;
95

    
96
import com.iver.andami.PluginServices;
97
import com.iver.andami.Utilities;
98
import com.iver.andami.ui.mdiManager.IWindow;
99
import com.iver.andami.ui.mdiManager.IWindowListener;
100
import com.iver.andami.ui.mdiManager.WindowInfo;
101
import com.iver.cit.gvsig.addlayer.fileopen.FileOpenWizard;
102
import com.iver.cit.gvsig.fmap.MapControl;
103
import com.iver.cit.gvsig.project.documents.view.gui.View;
104

    
105
public class MosaicDialog extends DefaultButtonsPanel implements PropertyListener, IWindow, IWindowListener, 
106
        ButtonsPanelListener,ActionListener,FocusListener,PropertyChangeListener,IProcessActions{
107

    
108
        private static final long serialVersionUID = 2847035927527203595L;
109
        private SelectLayersFromViewPanel northPanel = null;
110
        private MethodMosaicPanel southPanel = null;
111
        private MapControl mapControl = null;
112
        private View view=null;
113
        private JPanel                                         panelTab1=null;
114
        private JPanel                                         panelTab2=null;
115
        private JPanel                                         nombreCapa=null;
116
        private JRadioButton                         rButtonMemory=null;
117
        private JRadioButton                         rButtonFile=null;
118
        private JTextField                                 jTextNombreCapa=null;
119
        
120
        /**
121
         * Constructor
122
         * @param width Ancho del panel
123
         * @param height Alto del panel
124
         */
125
        public MosaicDialog(int width, int height, View view) {
126
                super();
127
                this.view= view;
128
                this.setSize(width, height);
129
                this.setLayout(new BorderLayout());
130
                JTabbedPane tabbedPane = new JTabbedPane();
131
                 tabbedPane.addTab(PluginServices.getText(this, "operacion"), getPanelTab1());
132
                tabbedPane.addTab(PluginServices.getText(this, "opciones"), getPanelTab2());
133
                this.add(tabbedPane, BorderLayout.CENTER);
134
                this.addButtonPressedListener(this);
135
        }
136
        
137
        
138
        public WindowInfo getWindowInfo() {
139
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.PALETTE);
140
                m_viewinfo.setTitle(PluginServices.getText(this, "mosaicos"));
141
                m_viewinfo.setHeight(this.getHeight());
142
                m_viewinfo.setWidth(this.getWidth());
143
                return m_viewinfo;
144
        }
145

    
146

    
147
        /**
148
         * Acciones a ejecutar cuando se cancela
149
         */
150
        public void close(){        
151
                try {
152
                        PluginServices.getMDIManager().closeWindow(this);
153
                } catch (ArrayIndexOutOfBoundsException ex) {
154
                        // Si la ventana no se puede eliminar no hacemos nada
155
                }
156
        }
157
                
158
        /**
159
         *  @return northPanel. Panel principal
160
         * */
161
        public SelectLayersFromViewPanel getPanelNorth() {
162
                if (northPanel == null){
163
                        northPanel = new SelectLayersFromViewPanel(view);
164
                        northPanel.getTableImages().addPropertyChangeListener(this);
165
                }
166
                return northPanel;
167
        }
168

    
169
        /**
170
         *  @return northPanel. Panel principal
171
         * */
172
        public MethodMosaicPanel getPanelSouth() {
173
                if (southPanel == null){
174
                        southPanel = new MethodMosaicPanel(northPanel.getSelectedLayers(),view);
175
                }
176
                return southPanel;
177
        }
178

    
179
        
180

    
181
        public MapControl getMapControl() {
182
                return mapControl;
183
        }
184

    
185
        
186
        /**
187
         * @return panel para el tab operacion.
188
         */
189
        private JPanel getPanelTab1(){         
190
                if (panelTab1==null){
191
                        panelTab1 = new JPanel();        
192
                        panelTab1.setLayout(new GridBagLayout());
193
                        JPanel selectionImagenPanel= new JPanel();
194
                        JPanel aux= new JPanel();
195
                        BorderLayout bd=new BorderLayout();
196
                        aux.setLayout(bd);
197
                        aux.add(getPanelNorth(),BorderLayout.NORTH);
198
                        aux.add(getPanelSouth(),BorderLayout.SOUTH);
199
                        selectionImagenPanel.add(aux);
200
                        GridBagConstraints c = new GridBagConstraints();
201
                        c.fill = GridBagConstraints.BOTH;
202
                        c.insets=new Insets(2,2,2,2);
203
                        c.weightx = 1;
204
                        c.weighty = 0.1;
205
                        c.gridx = 0;
206
                        c.gridy = 0;
207
                        panelTab1.add(selectionImagenPanel, c);
208
                 }
209
                 return panelTab1;
210
         }
211

    
212
         
213
         /**
214
          * @return Panel para el tab de opciones.
215
          */
216
        private JPanel getPanelTab2(){         
217
                if (panelTab2==null){
218
                        panelTab2 = new JPanel();        
219
                        panelTab2.setLayout(new GridBagLayout());
220
                                
221
                        JPanel parameterPanel= new JPanel();
222
                        TitledBorder topBorder = BorderFactory.createTitledBorder(PluginServices.getText(this, "parametros"));
223
                        topBorder.setTitlePosition(TitledBorder.TOP);
224
                        JPanel aux= new JPanel();
225
                        aux.setPreferredSize(new Dimension(300,110));
226
                                
227
                        BorderLayout bd=new BorderLayout();
228
                        aux.setLayout(bd);
229
                        parameterPanel.add(aux);
230
                                
231
                        parameterPanel.setBorder(topBorder);
232
                        BorderLayout bordlayout= new BorderLayout();
233
                        parameterPanel.setLayout(bordlayout);
234
                        parameterPanel.add(new JPanel(),BorderLayout.NORTH);
235
                        GridBagConstraints c = new GridBagConstraints();
236
                        c.fill = GridBagConstraints.BOTH;
237
                        c.insets=new Insets(2,2,2,2);
238
                        c.weightx = 1;
239
                        c.weighty = 1;
240
                        c.gridx = 0;
241
                        c.gridy = 0;
242
                        panelTab2.add(parameterPanel, c);
243
                                
244
        
245
                        // Insercion del panel de las clases
246
                        c.fill = GridBagConstraints.BOTH;
247
                        c.insets=new Insets(2,2,2,2);
248
                        c.weightx = 0.05;
249
                        c.weighty = 0.05;
250
                        c.gridx = 0;
251
                        c.gridy = 1; 
252
                                
253
                        panelTab2.add(getOutputOptionsPanel(), c);                        
254
                }
255
                return panelTab2; 
256
         }
257
         
258
        
259
         /**
260
         * @return panel con las opciones de salida.
261
         */
262
        public JPanel getOutputOptionsPanel() {
263
                        
264
                if (nombreCapa==null){ 
265
                        nombreCapa=new JPanel();
266
                        nombreCapa.setPreferredSize(new Dimension(400,130));
267
                        TitledBorder topBorder = BorderFactory.createTitledBorder(PluginServices.getText(this, "salida"));
268
                        topBorder.setTitlePosition(TitledBorder.TOP);
269
                        nombreCapa.setBorder(topBorder);
270
                        GridBagConstraints gridBagConstraints;
271
                        JPanel radioPanel = new JPanel();
272
                        radioPanel.setLayout(new GridBagLayout());
273
                        radioPanel.setBorder(BorderFactory.createTitledBorder(""));
274
                        ButtonGroup buttonGroup = new ButtonGroup();
275
                        buttonGroup.add(getRadioMemory());
276
                        gridBagConstraints = new java.awt.GridBagConstraints();
277
                        gridBagConstraints.gridx = 0;
278
                        gridBagConstraints.gridy = 1;
279
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
280
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
281
                        radioPanel.add(getRadioMemory(),gridBagConstraints);
282
                        buttonGroup.add(getRadioFile());
283
                        gridBagConstraints = new java.awt.GridBagConstraints();
284
                        gridBagConstraints.gridx = 0;
285
                        gridBagConstraints.gridy = 2;
286
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
287
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
288
                        radioPanel.add(getRadioFile(),gridBagConstraints);
289
                        //Establece la separacion entre los elementos
290
                        nombreCapa.setLayout(new GridBagLayout());
291
                        gridBagConstraints = new java.awt.GridBagConstraints();
292
                        gridBagConstraints.gridx = 0;
293
                        gridBagConstraints.gridy = 0;
294
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
295
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
296
                        nombreCapa.add(new JLabel(("Nombre:"),SwingConstants.RIGHT ),gridBagConstraints);
297
                                
298
                        gridBagConstraints = new java.awt.GridBagConstraints();
299
                        gridBagConstraints.gridx = 1;
300
                        gridBagConstraints.gridy = 0;
301
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
302
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
303
                        nombreCapa.add(getJTextNombreCapa(),gridBagConstraints);
304
                        
305
                        gridBagConstraints = new java.awt.GridBagConstraints();
306
                        gridBagConstraints.gridx = 2;
307
                        gridBagConstraints.gridy = 0;
308
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
309
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
310
                        nombreCapa.add(radioPanel,gridBagConstraints);        
311
                }
312
                        return nombreCapa;
313
                }
314
                
315
                
316
                
317
        /**
318
         * @return JRadioButton de generar en memoria
319
         */
320
        public JRadioButton getRadioMemory(){
321
                if (rButtonMemory == null){
322
                        rButtonMemory = new JRadioButton(PluginServices.getText(this,"a_memoria"),true);
323
                        rButtonMemory.addActionListener(this);
324
                }
325
                return rButtonMemory;
326
        }
327
                
328
                
329
                
330
        /**
331
        * @return JRadioButton de generar fichero
332
        */        
333
        public JRadioButton getRadioFile(){
334
                        if (rButtonFile == null){
335
                                rButtonFile = new JRadioButton(PluginServices.getText(this,"a_fichero"));
336
                                rButtonFile.addActionListener(this);
337
                        }
338
                        return rButtonFile;
339
        }
340

    
341
        /**
342
         * @return javax.swing.JTextField
343
         */        
344
        public JTextField getJTextNombreCapa() {
345
                if (jTextNombreCapa==null) {
346
                        jTextNombreCapa=new JTextField(10);
347
                        jTextNombreCapa.setText(RasterLibrary.getOnlyLayerName());
348
                        jTextNombreCapa.addFocusListener(this);
349
                }
350
                        return jTextNombreCapa;        
351
        }
352
                 
353
                 
354
        /**
355
         * Especificar el nombre de la nueva capa para el recuadro de texto asign?ndo
356
         * en cada llamada un nombre consecutivo.
357
         */
358
         public void updateNewLayerText() {
359
                         getJTextNombreCapa().setText(RasterLibrary.getOnlyLayerName());
360
         }
361
        
362
         
363

    
364
        /**
365
         * Acciones ante la modificaci?n de las capas seleccionadas en panelNorth 
366
         * */
367
         public void propertyChange(PropertyChangeEvent evt) {
368
                getPanelSouth().setNameLayers(getPanelNorth().getSelectedLayers());
369
                getPanelSouth().updateCombo();
370
                getPanelSouth().updateRoisTable();
371
         }        
372
         
373
        
374
         /**
375
         * Devuelve la ruta del fichero donde se va a guardar, en caso de guardarse
376
         * en memoria, calcula el nombre sin preguntar y devuelve la ruta.
377
         * @return string con la ruta de salida
378
         */
379
        public String getFileSelected() {
380
                String path = "";
381
                if (getRadioFile().isSelected()) {
382
                        JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath());
383
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
384
                        //A?adimos las extensiones que hayan sido registradas en el driver
385
                        String[] extList = GeoRasterWriter.getDriversExtensions();
386
                        for(int i=0;i<extList.length;i++)
387
                                chooser.addChoosableFileFilter(new ExtendedFileFilter(extList[i]));
388

    
389
                        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
390
                                return null;
391

    
392
                        FileOpenWizard.setLastPath(chooser.getSelectedFile().getPath().substring(0, chooser.getSelectedFile().getPath().lastIndexOf(File.separator)));
393
                        ExtendedFileFilter fileFilter = (ExtendedFileFilter) chooser.getFileFilter();
394
                        path = fileFilter.getNormalizedFilename(chooser.getSelectedFile());
395
                        
396
                } else {
397
                        String file = getJTextNombreCapa().getText();
398
                        path = Utilities.createTempDirectory() + File.separator + getJTextNombreCapa().getText() + ".tif";
399
                        if(file.compareTo(RasterLibrary.getOnlyLayerName()) == 0) 
400
                                RasterLibrary.usesOnlyLayerName();
401
                        updateNewLayerText();
402
                }
403
                return path;        
404
        }
405
         
406
        
407
        /**
408
         * Acciones al presionar los botones aceptar, aplicar o cancelar
409
         * Cancelar se cierra el dialogo
410
         * Aceptar se comprueba las opciones seleccionadas. si la opcion de histogramMatching se encuentra seleccionada, si no se dispone del hostograma
411
         * de referencia.Obtenido el histograma (con un proceso histogramProcess), se lanza el proceso de HistogramMatching para cada una de las capas seleccionadas.
412
         * Una vez finalizado, se comprueba que la opci?n de mosaicos se encuentra activa. En caso afirmativo se lanza el proceso de mosaico con los par?metros 
413
         * correspondientes.
414
         * */
415
        public void actionButtonPressed(ButtonsPanelEvent e) {
416
                // Al pulsar Cancelar la ventana se cierra y se refresca la vista
417
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
418
                        close();
419
                }
420
                
421
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {        
422
                        // Opci?n de HistogramMatching seleccionada
423
                        if(getPanelSouth().getCheckHistogramMatching().isSelected()){        
424
                                try{
425
                                        FLyrRasterSE rasterReferencia= (FLyrRasterSE) getPanelSouth().getLayers().getLayer((String)getPanelSouth().getMasterImageCombo().getSelectedItem());
426
                                        // Si el histograma no esta calculado se calcula
427
                                        HistogramProcess histoCalculus= new HistogramProcess();
428
                                        histoCalculus.addParam("histogramable", (IHistogramable)rasterReferencia.getDataSource());
429
                                        histoCalculus.setActions(this);
430
                                        histoCalculus.start();
431
                                
432
                                        // cuando el proceso termina se lanzara e HistogramMatching;
433
                                                
434
                                }catch (Exception e2) {                
435
                                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_getting_referenceHistogram"), this, e2);                
436
                                }        
437
                        }
438
                        
439
                        if(getPanelSouth().getCheckFeathering().isSelected()){
440
                                
441
                                // Caso de degradado de borde seleccionado
442
                                if(getPanelSouth().getRButtomEdge().isSelected()){
443
                                        
444
                                        FeatherProcess featherProcess= new FeatherProcess();
445
                                        FLyrRasterSE layers[]= new FLyrRasterSE[getPanelSouth().getLayers().getLayersCount()];
446
                                        for(int i=0; i<layers.length;i++)
447
                                                layers[i]=(FLyrRasterSE) getPanelSouth().getLayers().getLayer(i);
448
                                        
449
                                        featherProcess.addParam("inputRasterLayers",layers);
450
                                        featherProcess.addParam("filename", getFileSelected());
451
                                        featherProcess.addParam("view", view);
452
                                        featherProcess.start();
453
                                }
454
                                
455
                                // Caso sin degradado
456
                                else {
457
                                
458
                                        MosaicProcess proceso= new MosaicProcess ();
459
                                        try {
460
                                                proceso.addParam("layers",getPanelSouth().getLayers().cloneLayer());
461
                                        } catch (Exception e1) {
462
                                                e1.printStackTrace();
463
                                        }
464
                                        proceso.addParam("numbands",new Integer(3));
465
                                        proceso.addParam("codOp",new Integer(getPanelSouth().getComboOverlapFunction().getSelectedIndex()));
466
                                        proceso.addParam("filename",getFileSelected());
467
                                        proceso.addParam("vista",view);
468
                                        proceso.start();
469
                                }
470
                        }
471
                }
472
        }
473
        
474
        
475
        /** M?todo que lanza la ejecuci?n del histogramMatchProcess */
476
        public void applyHistogramMatchProcess(){
477
                try{
478
                        
479
                        FLyrRasterSE capas[]= new FLyrRasterSE[getPanelSouth().getLayers().getLayersCount()];
480
                        FLyrRasterSE rasterReferencia= (FLyrRasterSE) getPanelSouth().getLayers().getLayer((String)getPanelSouth().getMasterImageCombo().getSelectedItem());
481
                        Histogram histogramReference=rasterReferencia.getDataSource().getHistogram();
482
                        for(int i=0; i<getPanelSouth().getLayers().getLayersCount();i++)
483
                                if(!rasterReferencia.getName().equals(getPanelSouth().getLayers().getLayer(i).getName())){
484
                                        capas[i]= (FLyrRasterSE) getPanelSouth().getLayers().getLayer(i).cloneLayer();
485
                                }
486
                
487
                        HistogramMatchProcess proceso = new HistogramMatchProcess();
488
                        proceso.addParam("histogramMaster",histogramReference);
489
                        proceso.addParam("numbands",new Integer(3));
490
                        proceso.addParam("layers",capas);
491
                        proceso.addParam("view",view);
492
                        proceso.start();
493
        
494
                }catch (Exception e2) {                
495
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "apply_histogramMatching_filter_error"), this, e2);                
496
                }
497
        }
498
        
499
        
500
         
501
        /**
502
         * Acciones que se realizan al finalizar de crear los recortes de imagen.
503
         * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
504
         */
505
        public void loadLayerInToc(String fileName) {
506
                if(!new File(fileName).exists())
507
                        return;
508
                try {
509
                        RasterToolsUtil.loadLayer(PluginServices.getMDIManager().getWindowInfo(view).getTitle(), fileName, null);
510
                } catch (RasterNotLoadException e) {
511
                        RasterToolsUtil.messageBoxError("error_cargar_capa", this, e);
512
                }
513
        }
514

    
515
        /*
516
         * (non-Javadoc)
517
         * @see org.gvsig.rastertools.IProcessActions#end(java.lang.Object)
518
         */
519
        public void end(Object param) {        
520
                applyHistogramMatchProcess();
521
        }
522
        
523
        public void windowActivated() {        
524
        }
525
        
526
        public void windowClosed() {
527
        }
528
        
529
        public void focusGained(FocusEvent arg0) {
530
        }
531

    
532
        public void focusLost(FocusEvent arg0) {
533
                // TODO Auto-generated method stub        
534
        }
535

    
536
        public void actionPerformed(ActionEvent e) {
537
                // TODO Auto-generated method stub
538
        }
539

    
540

    
541
        public void actionValueChanged(PropertyEvent e) {
542
                updateNewLayerText();
543
        }
544

    
545

    
546
        public void interrupted() {
547
                // TODO Auto-generated method stub
548
                
549
        }
550
}