Statistics
| Revision:

root / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / principalcomponents / gui / PrincipalComponentCalculusPanel.java @ 24986

History | View | Annotate | Download (26 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.principalcomponents.gui;
42

    
43
import info.clearthought.layout.TableLayout;
44

    
45
import java.awt.BorderLayout;
46
import java.awt.Dimension;
47
import java.awt.FlowLayout;
48
import java.awt.GridBagConstraints;
49
import java.awt.GridBagLayout;
50
import java.awt.event.ActionEvent;
51
import java.awt.event.ActionListener;
52
import java.awt.event.FocusEvent;
53
import java.awt.event.FocusListener;
54
import java.io.File;
55
import java.io.FileNotFoundException;
56
import java.io.FileOutputStream;
57
import java.io.IOException;
58
import java.io.OutputStream;
59

    
60
import javax.swing.BorderFactory;
61
import javax.swing.ButtonGroup;
62
import javax.swing.ImageIcon;
63
import javax.swing.JButton;
64
import javax.swing.JCheckBox;
65
import javax.swing.JFileChooser;
66
import javax.swing.JLabel;
67
import javax.swing.JOptionPane;
68
import javax.swing.JPanel;
69
import javax.swing.JRadioButton;
70
import javax.swing.JScrollPane;
71
import javax.swing.JTable;
72
import javax.swing.JTextField;
73
import javax.swing.SwingConstants;
74
import javax.swing.border.CompoundBorder;
75
import javax.swing.border.EmptyBorder;
76
import javax.swing.border.TitledBorder;
77

    
78
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
79
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
80
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
81
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
82
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
83
import org.gvsig.raster.IProcessActions;
84
import org.gvsig.raster.RasterLibrary;
85
import org.gvsig.raster.dataset.GeoRasterWriter;
86
import org.gvsig.raster.grid.Grid;
87
import org.gvsig.raster.grid.GridException;
88
import org.gvsig.raster.util.ExtendedFileFilter;
89
import org.gvsig.raster.util.PropertyEvent;
90
import org.gvsig.raster.util.PropertyListener;
91
import org.gvsig.remotesensing.principalcomponents.PCImageProcess;
92
import org.gvsig.remotesensing.principalcomponents.PCStatistics;
93
import org.gvsig.remotesensing.principalcomponents.PCStatisticsProcess;
94

    
95
import Jama.Matrix;
96

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

    
105

    
106
/**
107
 * Clase que define la interfaz para la selecci?n de componentes que interviene la construcci?n de la
108
 * imagen resultante del analisis de componentes principales.
109
 *  
110
 * @see PCStatisticsProcess
111
 * @author Alejandro Mu?oz Sanchez (alejandro.munoz@uclm.es)
112
 * @author Diego Guerrero Sevilla (diego.guerrero@uclm.es) 
113
 * @version 22/10/2007  
114
 */
115

    
116
public class PrincipalComponentCalculusPanel extends DefaultButtonsPanel implements PropertyListener, IWindow, IWindowListener, 
117
ButtonsPanelListener,ActionListener,FocusListener, IProcessActions {        
118
        
119
        private static final long serialVersionUID = 1L;
120
        
121
        private JPanel                                         statisticsPanel;
122
        private JPanel                                         panelTodos;
123
        private JPanel                                         nombreCapaPanel;
124
        private JButton                                 jbExportar;
125
        private JButton                                 jexportVarMatrix;
126
        private JButton                                 jexportAutovectorMatrix;
127
        private ComponentTableFormat         tableFormatComponent;
128
        private JScrollPane                         scrollComponents;
129
        private JTable                                         tableComponent;
130
        private JTextField                                 jTextNombreCapa;
131
        private JButton                                 jbTodos;
132
        private JButton                                 jbNinguno;
133
        private int                                         wPanel=420;
134
        private int                                                hPanel=410;
135
        private JPanel                                         panelSuperior;        
136
        private JRadioButton                         rButtonFile                                 = null;
137
        private JRadioButton                         rButtonMemory                                 = null;
138
        private JCheckBox                                 bandasStatistics;
139
        private JCheckBox                                 varianceStatistics;
140
        private JCheckBox                                 autovectorStatistics;
141
        
142
        private PCStatisticsProcess         pc                                                        = null;
143
        private FLyrRasterSE                         resultLayer                                 = null;
144
        private View                                         view                                                 = null;
145
        private String                                         filename                                         = null;
146
        private boolean                                 selectedBands[]                         = null;
147
        private PCStatistics                        pcStatistics                                = null;
148
        
149
        
150
        /**
151
         * Constructor
152
         * @param vista vista de la aplicacion
153
         * @param statistics Estad?sticas necesarias para crear el raster transformado.
154
         * @param pC proceso de estadisticas 
155
         * @param selectecBands bandas de raster de entrada que intervienen en la transformaci?n
156
         */
157
        public PrincipalComponentCalculusPanel(View vista,PCStatistics pcStatistics, PCStatisticsProcess pC, boolean[] selectecBands){
158
                pc=pC;
159
                view= vista;
160
                this.selectedBands = selectecBands;
161
                this.pcStatistics = pcStatistics;
162
                this.addButtonPressedListener(this);
163
                Inicializar();
164
        }
165

    
166
        /**
167
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
168
         */
169
        public WindowInfo getWindowInfo() {
170
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODALDIALOG);
171
                //Establecer el tama?o del formulario
172
                m_viewinfo.setWidth(wPanel);
173
                m_viewinfo.setHeight(hPanel);
174
                //Establecer el t?tulo del formulario
175
                m_viewinfo.setTitle(PluginServices.getText(this,"principal_components_calculus"));
176
                //punto X de la pantalla donde se situa
177
                m_viewinfo.setX(300);
178
                return m_viewinfo;
179
        }
180

    
181

    
182
        /**
183
         * @return panel con las opciones de exportar estadisticas
184
         */
185
         public JPanel getStatisticsPanel(){
186

    
187
                 if(statisticsPanel==null){
188
                         statisticsPanel=new JPanel();
189
                         TableLayout thisLayout = new TableLayout(new double[][] {
190
                                                {20,245,100},         
191
                                                {TableLayout.PREFERRED}}); 
192
                                        
193
                         statisticsPanel.setLayout(thisLayout);
194
                         JPanel p= new JPanel();
195
                         TitledBorder topBorder = BorderFactory.createTitledBorder((PluginServices.getText(this,"estadisticas")));
196
                         topBorder.setTitlePosition(TitledBorder.TOP);
197
                         statisticsPanel.setBorder(new CompoundBorder(topBorder,new EmptyBorder(5,5,5,5)));
198
                         BorderLayout bd=new BorderLayout();
199
                         p.setPreferredSize(new Dimension(160,70));
200
                         bd.setVgap(5);
201
                         bd.setHgap(5);
202
                         p.setLayout(bd);
203
                         p.add(getBandasStatistics(),BorderLayout.NORTH);
204
                         p.add(getVarianceStatistics(),BorderLayout.CENTER);
205
                         p.add(getAutovectorStatistics(),BorderLayout.SOUTH);
206
                         TableLayout thisLayout2 = new TableLayout(new double[][] {
207
                                                {5 ,65},         
208
                                                {40,TableLayout.PREFERRED}}); 
209
                         JPanel panelButton= new JPanel();
210
                         panelButton.setLayout(thisLayout2);
211
                         panelButton.add(getExportar(),"1,1");
212
                         statisticsPanel.add(p,"1,0");
213
                         statisticsPanel.add(panelButton,"2,0");
214
                 }
215
                 return statisticsPanel;
216
         }
217
         
218
        
219
         /**
220
          * inicializaci?n del di?logo
221
          */
222
         private void Inicializar(){
223
                        BorderLayout bd=new BorderLayout();
224
                        this.setLayout(bd);
225
                        TitledBorder topBorder = BorderFactory.createTitledBorder(" ");
226
                        topBorder.setTitlePosition(TitledBorder.TOP);
227
                        this.setBorder(new CompoundBorder(topBorder,new EmptyBorder(5,10,10,10)));
228
                        this.add(getPanelSuperior(),BorderLayout.NORTH);
229
                    this.add(getStatisticsPanel(),BorderLayout.CENTER);
230
                        completarTabla();
231
         }
232

    
233
         
234
         /**
235
          * @return panel con opciones de salida, tabla de componentes y botones de selecci?n todos/ninguno
236
          */
237
         private JPanel getPanelSuperior(){         
238
                 if (panelSuperior==null){
239
                         panelSuperior = new JPanel();
240
                         TitledBorder topBorder = BorderFactory.createTitledBorder((PluginServices.getText(this,"salida_raster")));
241
                         topBorder.setTitlePosition(TitledBorder.TOP);
242
                         panelSuperior.setBorder(new CompoundBorder(topBorder,new EmptyBorder(5,5,5,5)));
243
                         JPanel p=new JPanel();
244
                         TableLayout thisLayout = new TableLayout(new double[][] {
245
                                         {350},         
246
                                        {5,135,40}}); 
247
                                thisLayout.setHGap(5);
248
                                thisLayout.setVGap(3);
249
                                p.setLayout(thisLayout);
250
                        //        p.add(getNombreCapaPanel(),"0,0");
251
                                p.add(getScrollComponents(),"0,1");
252
                                p.add(getPanelTodos(),"0,2");
253
                            panelSuperior.add(p);         
254
                 }
255
                 return panelSuperior; 
256
         }
257
         
258
         
259
         /**
260
         * A?ade las filas a la tabla de selecci?n de componentes
261
         */
262
        public void completarTabla(){
263
                double acumulado=0;
264
                for (int i=0;i<pcStatistics.getAutovalues().length;i++)
265
                        acumulado+=pcStatistics.getAutovalues()[i];
266
                int autova[]=new int[ pcStatistics.getAutovalues().length];
267
                int cont= pcStatistics.getAutovalues().length-1;
268
                for (int i=0; i<pcStatistics.getAutovalues().length;i++){
269
                        autova[i]=cont;
270
                        cont--;
271
                }
272
                
273
                for (int i=pcStatistics.getAutovalues().length-1;i>=0;i--)
274
                        getTableFormatComponent().addRow(autova[i],pcStatistics.getAutovalues()[i],pcStatistics.getAutovalues()[i]/acumulado,true);         
275
         }
276
         
277
        
278
        /**
279
         * @return JButton jbExportar
280
         */
281
        public JButton getExportar() {        
282
                if (jbExportar==null){
283
                        jbExportar=new JButton((PluginServices.getText(this,"exportar")));
284
                        jbExportar.addActionListener(this);
285
                }        
286
                return jbExportar;
287
        }
288
        
289
        
290
        /**
291
         * @return JButton jbNinguno
292
         */
293
        public JButton getNinguno() {
294
                if (jbNinguno==null){
295
                        ImageIcon icono = new ImageIcon(PrincipalComponentCalculusPanel.class.getClassLoader().getResource("images/table_delete.png"));
296
                        jbNinguno = new JButton(icono);
297
                        jbNinguno.addActionListener(this);
298
                }
299
                return jbNinguno;
300
        }
301

    
302

    
303
        /**
304
         * @return JButton jbTodos
305
         */
306
        public JButton getTodos() {
307
                if (jbTodos==null){
308
                        ImageIcon icono = new ImageIcon(PrincipalComponentCalculusPanel.class.getClassLoader().getResource("images/table.png"));
309
                        jbTodos = new JButton(icono);
310
                        jbTodos.addActionListener(this);
311
                }
312
                return jbTodos;
313
        }
314
        
315

    
316
        /**
317
         * @return JButton jexportVarMatrix
318
         */
319
        public JButton getJexportVarMatrix() {
320
                if(jexportVarMatrix==null){
321
                        jexportVarMatrix=new JButton((PluginServices.getText(this,"var_matrix")));
322
                        jexportVarMatrix.addActionListener(this);
323
                }
324
                return jexportVarMatrix;
325
        }
326

    
327
        
328
        /**
329
         * @return JButton jexportAutovectorMatrix
330
         */
331
        public JButton getJexportAutovectorMatrix() {
332
                if (jexportAutovectorMatrix==null){
333
                        jexportAutovectorMatrix=new JButton((PluginServices.getText(this,"autovector_matrix")));
334
                        jexportAutovectorMatrix.addActionListener(this);
335
                }
336
                return jexportAutovectorMatrix;
337
        }
338

    
339
        
340
        /**
341
         * @see ComponentTableFormat
342
         * @return modelo de tabla de componentes
343
         */
344
        public ComponentTableFormat getTableFormatComponent() {
345
                if(tableFormatComponent==null){
346
                        tableFormatComponent=new ComponentTableFormat();
347
                }
348
                return tableFormatComponent;
349
        }
350

    
351
        
352
        /**
353
         * @return tabla de componentes
354
         */
355
        public JTable getTableComponents(){
356
                if (tableComponent==null){
357
                        tableComponent=new JTable(getTableFormatComponent());
358
                }
359
                return tableComponent;
360
        }
361
        
362
        
363
        /**
364
         * @return scroll con la tabla de componentes
365
         */
366
        public JScrollPane getScrollComponents() {
367
                
368
                if(scrollComponents==null){
369
                        scrollComponents=new JScrollPane();
370
                }
371
                 getTableComponents().getColumn((PluginServices.getText(this,"componente"))).setPreferredWidth(110);        
372
                 getTableComponents().getColumn((PluginServices.getText(this,"autovalor"))).setPreferredWidth(80);
373
                 getTableComponents().getColumn((PluginServices.getText(this,"%"))).setPreferredWidth(80);
374
        
375
                 scrollComponents.setViewportView(getTableComponents());
376
                 scrollComponents.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
377
                        
378
                return scrollComponents;
379
        }
380
        
381
         
382
        /**
383
         * @return panel con los botones de selecci?n todo/ninguno
384
         */
385
        public JPanel getPanelTodos(){
386
                if (panelTodos==null){ 
387
                        panelTodos= new JPanel();
388
                        JPanel p = new JPanel();
389
                        p.setLayout(new FlowLayout(FlowLayout.RIGHT,1,1));
390
                        p.add(getTodos());
391
                        p.add(getNinguno());
392
                        p.setPreferredSize(new Dimension(320,30));
393
                        panelTodos.add(p);
394
                 }
395
                 return panelTodos;        
396
        }
397

    
398
         
399
        /**
400
        * @return panel que incluye el nombre de la capa y las opciones de almacenamieto de la capa de salida 
401
        */
402
        public JPanel getNombreCapaPanel() {
403
                
404
                if (nombreCapaPanel==null){
405
                        nombreCapaPanel=new JPanel();
406
                        GridBagConstraints gridBagConstraints;
407
                        JPanel radioPanel = new JPanel();
408
                        radioPanel.setLayout(new GridBagLayout());
409
                        radioPanel.setBorder(BorderFactory.createTitledBorder(""));
410
                        ButtonGroup buttonGroup = new ButtonGroup();
411
                        buttonGroup.add(getRadioMemory());
412
                        gridBagConstraints = new java.awt.GridBagConstraints();
413
                        gridBagConstraints.gridx = 0;
414
                        gridBagConstraints.gridy = 1;
415
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
416
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
417
                        radioPanel.add(getRadioMemory(),gridBagConstraints);
418
                        buttonGroup.add(getRadioFile());
419
                        gridBagConstraints = new java.awt.GridBagConstraints();
420
                        gridBagConstraints.gridx = 0;
421
                        gridBagConstraints.gridy = 2;
422
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
423
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
424
                        radioPanel.add(getRadioFile(),gridBagConstraints);
425
                        //Establece la separacion entre los elementos
426
                        nombreCapaPanel.setLayout(new GridBagLayout());
427
                        
428
                        gridBagConstraints = new java.awt.GridBagConstraints();
429
                        gridBagConstraints.gridx = 0;
430
                        gridBagConstraints.gridy = 0;
431
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
432
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
433
                        nombreCapaPanel.add(new JLabel( (PluginServices.getText(this,"nombre_capa")),SwingConstants.RIGHT ),gridBagConstraints);
434
                        
435
                        gridBagConstraints = new java.awt.GridBagConstraints();
436
                        gridBagConstraints.gridx = 1;
437
                        gridBagConstraints.gridy = 0;
438
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
439
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
440
                        nombreCapaPanel.add(getJTextNombreCapa(),gridBagConstraints);
441
                        
442
                        gridBagConstraints = new java.awt.GridBagConstraints();
443
                        gridBagConstraints.gridx = 2;
444
                        gridBagConstraints.gridy = 0;
445
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
446
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
447
                        nombreCapaPanel.add(radioPanel,gridBagConstraints);
448
                }
449
                return nombreCapaPanel;
450
        }
451

    
452
        /**
453
         * @return javax.swing.JTextField
454
         */        
455
         /**
456
          * @return javax.swing.JTextField
457
          */        
458
         public JTextField getJTextNombreCapa() {
459
                 if (jTextNombreCapa==null){
460
                         jTextNombreCapa=new JTextField(10);
461
                         jTextNombreCapa.setText(RasterLibrary.getOnlyLayerName());
462
                         jTextNombreCapa.addFocusListener(this);
463
                         RasterLibrary.addOnlyLayerNameListener(this);
464
                 }
465
                
466
                 return jTextNombreCapa;
467
         }
468

    
469
        
470
        /**
471
         * Especificar el nombre de la nueva capa para el recuadro de texto asign?ndo
472
         * en cada llamada un nombre consecutivo.
473
         */
474
         public void updateNewLayerText() {
475
                 getJTextNombreCapa().setText(RasterLibrary.getOnlyLayerName());
476
         }
477
         
478
         
479
        /**
480
        * Cuando alguien ha cambiado la propiedad del nombre de la 
481
        * capa se actualiza autom?ticamente
482
        */
483
        public void actionValueChanged(PropertyEvent e) {
484
                        updateNewLayerText();
485
        }
486
        
487
        /**
488
         * @return JRadioButton de generar fichero
489
         */
490
        public JRadioButton getRadioFile(){
491
                if (rButtonFile == null){
492
                        rButtonFile = new JRadioButton(PluginServices.getText(this,"a_fichero"));
493
                        rButtonFile.addActionListener(this);
494
                }
495
                return rButtonFile;
496
        }
497
        
498
        /**
499
         * @return JRadioButton de generar en memoria
500
         */
501
        public JRadioButton getRadioMemory(){
502
                if (rButtonMemory == null){
503
                        rButtonMemory = new JRadioButton(PluginServices.getText(this,"a_memoria"),true);
504
                        rButtonMemory.addActionListener(this);
505
                }
506
                return rButtonMemory;
507
        }
508

    
509
        
510
        /**
511
         * M?todo para exportar las estadisticas seleccionadas a un fichero de texto
512
         */
513
        public void exportStatistics(){
514
                
515
                JFileChooser dlgExportar = new JFileChooser(FileOpenWizard.getLastPath());
516
                dlgExportar.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
517
                String[] extList = new String[]{"txt"};
518
                for(int i=0;i<extList.length;i++)
519
                        dlgExportar.addChoosableFileFilter(new ExtendedFileFilter(extList[i]));
520
                int op= dlgExportar.showSaveDialog(this);
521
                    // Se comprueban las estadisticas a exportar
522
                        if (op == JFileChooser.APPROVE_OPTION){
523
                                File outFile = new File(dlgExportar.getSelectedFile().getAbsolutePath());                                
524
                                OutputStream outputStream=null;
525
                                try {
526
                                         outputStream = new FileOutputStream(outFile);
527
                                } catch (FileNotFoundException e) {
528
                                        e.printStackTrace();
529
                                }
530
                                
531
                                if(getBandasStatistics().isSelected()){
532
                                        ExportGridStatistic(pc.getInputGrid(), outputStream);
533
                                }
534
                                if(getVarianceStatistics().isSelected()){
535
                                        ExportarMatriz(pcStatistics.getCoVarMatrix(),true,outputStream);
536
                                }
537
                
538
                                if (getAutovectorStatistics().isSelected()){
539
                                        ExportarMatriz(pcStatistics.getAutoVectorsMatrix(),true,outputStream);
540
                                }
541
                
542
                                try {
543
                                        outputStream.close();
544
                                } catch (IOException e) {
545
                                        e.printStackTrace();
546
                                }
547
                }
548
        }
549
        
550
        
551
        /**
552
         * M?todo para exportar a disco las estadisticas del grid que se pasa como argumento.
553
         * @param grid        grid con las estadisticas a exportar
554
         * @param descriptor de  fichero salida
555
         */
556
        private void ExportGridStatistic(Grid grid,OutputStream outputStream){
557
                
558
                try {
559
                        outputStream.write("\t\t".getBytes());
560
                        outputStream.write("Max Value\t".getBytes());
561
                        outputStream.write("Min Value\t".getBytes());
562
                        outputStream.write("Mean Value\t".getBytes());
563
                        outputStream.write("\n".getBytes());
564
                        String aux="";
565
                        
566
                        for(int i=0; i<grid.getBandCount();i++)
567
                                {
568
                                        grid.setBandToOperate(i);
569
                                        outputStream.write("\n".getBytes());
570
                                        outputStream.write(("Band"+(i+1)).getBytes());
571
                                        // Se escribe los valores con un numero de caracteres inferior a 8.
572
                                        outputStream.write(("\t\t").getBytes());
573
                                        Double var= new Double(grid.getMaxValue());
574
                                        if (var.toString().getBytes().length>7)
575
                                                outputStream.write(var.toString().getBytes(),0,7);// Elementos de la matriz;
576
                                        else{
577
                                                aux = var.toString();
578
                                                for (int k=var.toString().getBytes().length; k<8; k++)
579
                                                        aux=aux+" ";
580
                                                outputStream.write(aux.getBytes(),0,7);
581
                                        }
582
                                        outputStream.write(("\t").getBytes());
583
                                        var= new Double(grid.getMinValue());
584
                                        if (var.toString().getBytes().length>7)
585
                                                outputStream.write(var.toString().getBytes(),0,7);// Elementos de la matriz;
586
                                        else{
587
                                                aux = var.toString();
588
                                                for (int k=var.toString().getBytes().length; k<8; k++)
589
                                                        aux=aux+" ";
590
                                                outputStream.write(aux.getBytes(),0,7);
591
                                        }
592
                                        outputStream.write(("\t").getBytes());
593
                                        var= new Double(grid.getMeanValue());
594
                                        if (var.toString().getBytes().length>7)
595
                                                outputStream.write(var.toString().getBytes(),0,7);// Elementos de la matriz;
596
                                        else{
597
                                                aux = var.toString();
598
                                                for (int k=var.toString().getBytes().length; k<8; k++)
599
                                                        aux=aux+" ";
600
                                                outputStream.write(aux.getBytes(),0,7);
601
                                        }
602
                                
603
                                }
604
                        outputStream.write("\n\n".getBytes());
605
                
606
                        } catch (IOException e) {
607
                                e.printStackTrace();
608
                        } catch (GridException e) {
609
                                
610
                        }
611
                
612
                
613
        }
614
        
615
        /**
616
         * M?todo para exportar a disco la matriz que se le pasa por argumento.
617
         * Si es una matriz de autovectores ordena las columnas en orden descendente segun autovalores.
618
         * @param matriz matriz a exportar
619
         * @param isAutovectorMatrix true si es una matrix de autovectores
620
         * @param descriptor de  fichero de salida
621
         */
622
        private void ExportarMatriz(Matrix matriz, boolean isAutovectorMatrix,OutputStream outputStream ){        
623
                
624
                // Orden correcto de las columnas de autovectores
625
                int resultOrden[]= new int[matriz.getRowDimension()];
626
                int cont = matriz.getRowDimension()-1;
627
                for(int i=0;i<matriz.getRowDimension();i++){
628
                                if (isAutovectorMatrix)
629
                                        resultOrden[i]=cont;
630
                                else
631
                                        resultOrden[i]=i;
632
                                
633
                                cont--;
634
                }
635
                String aux=null;
636
                try{        
637
                        outputStream.write("\t\t\t".getBytes());
638
                                        for (int j=0; j<matriz.getColumnDimension();j++)
639
                                                {        
640
                                                        outputStream.write(("Band"+(j+1)).getBytes());
641
                                                        outputStream.write("\t\t\t".getBytes());
642
                                                }
643
                                        outputStream.write("\n".getBytes());
644
                                        // Volcado de la Matriz a disco
645
                                        for (int i=0 ; i<matriz.getRowDimension(); i++){
646
                                                outputStream.write(("Band"+(i+1)).getBytes());
647
                                                outputStream.write("\t\t".getBytes());
648
                                                for (int j=0; j<matriz.getColumnDimension();j++){                                        
649
                                                        Double var =new Double(matriz.get(i,resultOrden[j]));
650
                                                        if (var.toString().getBytes().length>15)
651
                                                                outputStream.write(var.toString().getBytes(),0,15);// Elementos de la matriz;
652
                                                        else{
653
                                                                aux = var.toString();
654
                                                                for (int k=var.toString().getBytes().length; k<16; k++)
655
                                                                        aux=aux+" ";
656
                                                                outputStream.write(aux.getBytes(),0,15);
657
                                                        }
658
                                                        outputStream.write("\t".getBytes());
659
                                                }
660
                                                outputStream.write("\n".getBytes());
661
                                        }
662
                                        outputStream.write("\n".getBytes());
663
                        }catch (FileNotFoundException e) {
664
                                e.printStackTrace();
665
                        } catch (IOException e) {
666
                                // TODO Auto-generated catch block
667
                                e.printStackTrace();
668
                        }
669
        
670
        }
671

    
672
        /**
673
         * @return checkbox autovectores
674
         */
675
        public JCheckBox getAutovectorStatistics() {
676
                if(autovectorStatistics==null){
677
                        autovectorStatistics=new JCheckBox(PluginServices.getText(this, "autovector_statistics"),true);
678
                }
679
                return autovectorStatistics;
680
        }
681

    
682

    
683
        /**
684
         * @return checkbox estadisticas por banda
685
         */
686
        public JCheckBox getBandasStatistics() {
687
                if(bandasStatistics==null){
688
                        bandasStatistics=new JCheckBox(PluginServices.getText(this, "bandas_statistics"),true);
689
                }
690
                return bandasStatistics;
691
        }
692

    
693

    
694
        /**
695
         * @return checkbox matriz varianza-covarianza 
696
         */
697
        public JCheckBox getVarianceStatistics() {
698
                if(varianceStatistics==null){
699
                        varianceStatistics=new JCheckBox(PluginServices.getText(this, "variance_statistics"),true);
700
                }
701
                return varianceStatistics;
702
        }
703

    
704

    
705
        /*
706
         * (non-Javadoc)
707
         * @see org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener#actionButtonPressed(org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent)
708
         */
709
        public void actionButtonPressed(ButtonsPanelEvent e) {
710
//                 Bot?n de Aceptar
711
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
712
                        aplicar();
713
                        close();
714
                }
715

    
716
                // Bot?n de Aplicar
717
                if (e.getButton() == ButtonsPanel.BUTTON_APPLY) {
718
                        aplicar();
719
                }
720

    
721
                // Bot?n de Cerrar
722
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
723
                        close();
724
                }        
725
        }
726

    
727

    
728
        /**
729
         * acciones al aplicar
730
         */
731
        public void aplicar(){
732
                
733
                if(getTableFormatComponent().getNumSelected()>0){
734
                        PCImageProcess process = new PCImageProcess();
735
                        process.addParam("inputRasterLayer",pc.getRasterLayer());
736
                        process.addParam("statistics",pcStatistics);
737
                        process.addParam("selectedBands", selectedBands);
738
                        process.addParam("selectedComponents",getTableFormatComponent().getSeleccionadas());
739
                        process.addParam("outputPath",filename);
740
                        process.setActions(this);
741
                        process.start();
742
                        
743
                        
744
                }
745
                else{                        
746
                        JOptionPane.showMessageDialog(null,
747
                        PluginServices.getText(this,"no_components"), PluginServices.getText(this,"principal_components"),
748
                        JOptionPane.WARNING_MESSAGE);
749
                }
750
        }
751
        
752
        /**
753
         * acciones al cerrar
754
         */
755
        public void close(){
756
                try {
757
                        RasterLibrary.removeOnlyLayerNameListener(this);
758
                        PluginServices.getMDIManager().closeWindow(this);
759
                } catch (ArrayIndexOutOfBoundsException ex) {
760
                // Si la ventana no se puede eliminar no hacemos nada
761
                }
762
        }
763
        
764
        
765
        /**
766
         * Devuelve la ruta del fichero donde se va a guardar, en caso de guardarse
767
         * en memoria, calcula el nombre sin preguntar y devuelve la ruta.
768
         * @return string con la ruta de salida
769
         */
770
        public String getFileSelected() {
771
                String path = "";
772
                if (getRadioFile().isSelected()) {
773
                        JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath());
774
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
775
                        //A?adimos las extensiones que hayan sido registradas en el driver
776
                        String[] extList = GeoRasterWriter.getDriversExtensions();
777
                        for(int i=0;i<extList.length;i++)
778
                                chooser.addChoosableFileFilter(new ExtendedFileFilter(extList[i]));
779

    
780
                        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
781
                                return null;
782

    
783
                        FileOpenWizard.setLastPath(chooser.getSelectedFile().getPath().substring(0, chooser.getSelectedFile().getPath().lastIndexOf(File.separator)));
784
                        ExtendedFileFilter fileFilter = (ExtendedFileFilter) chooser.getFileFilter();
785
                        path = fileFilter.getNormalizedFilename(chooser.getSelectedFile());
786
                } else {
787
                        String file = getJTextNombreCapa().getText();
788
                        path = Utilities.createTempDirectory() + File.separator + getJTextNombreCapa().getText() + ".tif";
789
                        if(file.compareTo(RasterLibrary.getOnlyLayerName()) == 0) 
790
                                RasterLibrary.usesOnlyLayerName();
791
                        updateNewLayerText();
792
                }
793
                
794
                return path;
795
        }
796
        
797
        /*
798
         * (non-Javadoc)
799
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
800
         */
801
        public void actionPerformed(ActionEvent e) {
802
                if(e.getSource()==getExportar()){
803
                        exportStatistics();
804
                }
805
                if (e.getSource()==getTodos()){        
806
                        getTableFormatComponent().seleccionarTodas();
807
                        updateUI();
808
                }
809
                if (e.getSource()==getNinguno()){
810
                        getTableFormatComponent().seleccionarNinguna();
811
                        updateUI();
812
                }
813
        }
814
        
815
        
816
        public void setFilename(String fileName){
817
                filename=fileName;
818
        }
819
        
820
        public void focusGained(FocusEvent arg0) {
821
                // TODO Auto-generated method stub
822
        }
823

    
824
        public void focusLost(FocusEvent arg0) {
825
                // TODO Auto-generated method stub
826
        }
827

    
828

    
829

    
830
        public void windowActivated() {
831
                // TODO Auto-generated method stub
832
                
833
        }
834

    
835
        public void windowClosed() {
836
                // TODO Auto-generated method stub
837
                
838
        }
839

    
840
        public void end(Object param) {
841
                resultLayer = (FLyrRasterSE)param;
842
                loadLayer();
843
        }
844

    
845
        private void loadLayer() {
846
                view.getMapControl().getMapContext().getLayers().addLayer(resultLayer);
847
                view.getMapControl().getMapContext().endAtomicEvent();
848
                view.getMapControl().getMapContext().invalidate();
849
        }
850

    
851
        public void interrupted() {
852
                // TODO Auto-generated method stub
853
        }
854

    
855
        public Object getWindowProfile() {
856
                return WindowInfo.DIALOG_PROFILE;
857
        }                
858
}
859

    
860

    
861

    
862

    
863