Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRemoteSensing / src / org / gvsig / remotesensing / principalcomponents / gui / PrincipalComponentCalculusPanel.java @ 18828

History | View | Annotate | Download (24.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.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.gui.beans.buttonspanel.ButtonsPanel;
79
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
80
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
81
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
82
import org.gvsig.raster.dataset.GeoRasterWriter;
83
import org.gvsig.raster.grid.Grid;
84
import org.gvsig.raster.grid.GridException;
85
import org.gvsig.raster.util.ExtendedFileFilter;
86
import org.gvsig.rastertools.PropertyEvent;
87
import org.gvsig.rastertools.PropertyListener;
88
import org.gvsig.rastertools.RasterModule;
89
import org.gvsig.remotesensing.principalcomponents.PCImageProcess;
90
import org.gvsig.remotesensing.principalcomponents.PCStatisticsProcess;
91

    
92
import Jama.Matrix;
93

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

    
102

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

    
113
public class PrincipalComponentCalculusPanel extends DefaultButtonsPanel implements PropertyListener, IWindow, IWindowListener, 
114
ButtonsPanelListener,ActionListener,FocusListener {        
115
        
116
        private static final long serialVersionUID = 1L;
117
        private JPanel statisticsPanel;
118
        private JPanel panelTodos;
119
        private JPanel nombreCapaPanel;
120
        private JButton jbExportar;
121
        private JButton jexportVarMatrix;
122
        private JButton jexportAutovectorMatrix;
123
        private ComponentTableFormat tableFormatComponent;
124
        private JScrollPane scrollComponents;
125
        private JTable tableComponent;
126
        private JTextField jTextNombreCapa;
127
        private JButton jbTodos;
128
        private JButton jbNinguno;
129
        private int wPanel=460;
130
        private int hPanel=470;
131
        private double[] autovalores;
132
        private PCStatisticsProcess pc;
133
        private JPanel panelSuperior;        
134
        private JRadioButton rButtonFile = null;
135
        private JRadioButton rButtonMemory = null;
136
        private JCheckBox bandasStatistics;
137
        private JCheckBox varianceStatistics;
138
        private JCheckBox autovectorStatistics;
139
        private View view= null;
140
        
141
        /**
142
         * Constructor
143
         * @param vista vista de la aplicacion
144
         * @param pC proceso de estadisticas 
145
         */
146
        public PrincipalComponentCalculusPanel(View vista,PCStatisticsProcess pC){
147
                autovalores = (double[]) pC.getResult();
148
                pc=pC;
149
                view= vista;
150
                this.addButtonPressedListener(this);
151
                Inicializar();
152
        }
153

    
154
        /**
155
         * @see com.iver.mdiApp.ui.MDIManager.View#getViewInfo()
156
         */
157
        public WindowInfo getWindowInfo() {
158
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODALDIALOG);
159
                //Establecer el tama?o del formulario
160
                m_viewinfo.setWidth(wPanel);
161
                m_viewinfo.setHeight(hPanel);
162
                //Establecer el t?tulo del formulario
163
                m_viewinfo.setTitle(PluginServices.getText(this,"principal_components_calculus"));
164
                //punto X de la pantalla donde se situa
165
                m_viewinfo.setX(300);
166
                return m_viewinfo;
167
        }
168

    
169

    
170
        /**
171
         * @return panel con las opciones de exportar estadisticas
172
         */
173
         public JPanel getStatisticsPanel(){
174

    
175
                 if(statisticsPanel==null){
176
                         statisticsPanel=new JPanel();
177
                         TableLayout thisLayout = new TableLayout(new double[][] {
178
                                                {20,250,120},         
179
                                                {TableLayout.PREFERRED}}); 
180
                                        
181
                         statisticsPanel.setLayout(thisLayout);
182
                         JPanel p= new JPanel();
183
                         TitledBorder topBorder = BorderFactory.createTitledBorder((PluginServices.getText(this,"estadisticas")));
184
                         topBorder.setTitlePosition(TitledBorder.TOP);
185
                         statisticsPanel.setBorder(new CompoundBorder(topBorder,new EmptyBorder(5,5,5,5)));
186
                         BorderLayout bd=new BorderLayout();
187
                         p.setPreferredSize(new Dimension(170,70));
188
                         bd.setVgap(5);
189
                         bd.setHgap(5);
190
                         p.setLayout(bd);
191
                         p.add(getBandasStatistics(),BorderLayout.NORTH);
192
                         p.add(getVarianceStatistics(),BorderLayout.CENTER);
193
                         p.add(getAutovectorStatistics(),BorderLayout.SOUTH);
194
                         TableLayout thisLayout2 = new TableLayout(new double[][] {
195
                                                {5 ,85},         
196
                                                {40,TableLayout.PREFERRED}}); 
197
                         JPanel panelButton= new JPanel();
198
                         panelButton.setLayout(thisLayout2);
199
                         panelButton.add(getExportar(),"1,1");
200
                         statisticsPanel.add(p,"1,0");
201
                         statisticsPanel.add(panelButton,"2,0");
202
                 }
203
                 return statisticsPanel;
204
         }
205
         
206
        
207
         /**
208
          * inicializaci?n del di?logo
209
          */
210
         private void Inicializar(){
211
                        BorderLayout bd=new BorderLayout();
212
                        this.setLayout(bd);
213
                        TitledBorder topBorder = BorderFactory.createTitledBorder(" ");
214
                        topBorder.setTitlePosition(TitledBorder.TOP);
215
                        this.setBorder(new CompoundBorder(topBorder,new EmptyBorder(5,10,10,10)));
216
                        this.add(getPanelSuperior(),BorderLayout.NORTH);
217
                    this.add(getStatisticsPanel(),BorderLayout.CENTER);
218
                        completarTabla();
219
         }
220

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

    
290

    
291
        /**
292
         * @return JButton jbTodos
293
         */
294
        public JButton getTodos() {
295
                if (jbTodos==null){
296
                        ImageIcon icono = new ImageIcon(PrincipalComponentCalculusPanel.class.getClassLoader().getResource("images/table.png"));
297
                        jbTodos = new JButton(icono);
298
                        jbTodos.addActionListener(this);
299
                }
300
                return jbTodos;
301
        }
302
        
303

    
304
        /**
305
         * @return JButton jexportVarMatrix
306
         */
307
        public JButton getJexportVarMatrix() {
308
                if(jexportVarMatrix==null){
309
                        jexportVarMatrix=new JButton((PluginServices.getText(this,"var_matrix")));
310
                        jexportVarMatrix.addActionListener(this);
311
                }
312
                return jexportVarMatrix;
313
        }
314

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

    
327
        
328
        /**
329
         * @see ComponentTableFormat
330
         * @return modelo de tabla de componentes
331
         */
332
        public ComponentTableFormat getTableFormatComponent() {
333
                if(tableFormatComponent==null){
334
                        tableFormatComponent=new ComponentTableFormat();
335
                }
336
                return tableFormatComponent;
337
        }
338

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

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

    
440
        /**
441
         * @return javax.swing.JTextField
442
         */        
443
         /**
444
          * @return javax.swing.JTextField
445
          */        
446
         public JTextField getJTextNombreCapa() {
447
                 if (jTextNombreCapa==null){
448
                         jTextNombreCapa=new JTextField(10);
449
                         jTextNombreCapa.setText(RasterModule.getOnlyLayerName());
450
                         jTextNombreCapa.addFocusListener(this);
451
                         RasterModule.addOnlyLayerNameListener(this);
452
                 }
453
                
454
                 return jTextNombreCapa;
455
         }
456

    
457
        
458
        /**
459
         * Especificar el nombre de la nueva capa para el recuadro de texto asign?ndo
460
         * en cada llamada un nombre consecutivo.
461
         */
462
         public void updateNewLayerText() {
463
                 getJTextNombreCapa().setText(RasterModule.getOnlyLayerName());
464
         }
465
         
466
         
467
        /**
468
        * Cuando alguien ha cambiado la propiedad del nombre de la 
469
        * capa se actualiza autom?ticamente
470
        */
471
        public void actionValueChanged(PropertyEvent e) {
472
                        updateNewLayerText();
473
        }
474
        
475
        /**
476
         * @return JRadioButton de generar fichero
477
         */
478
        public JRadioButton getRadioFile(){
479
                if (rButtonFile == null){
480
                        rButtonFile = new JRadioButton(PluginServices.getText(this,"a_fichero"));
481
                        rButtonFile.addActionListener(this);
482
                }
483
                return rButtonFile;
484
        }
485
        
486
        /**
487
         * @return JRadioButton de generar en memoria
488
         */
489
        public JRadioButton getRadioMemory(){
490
                if (rButtonMemory == null){
491
                        rButtonMemory = new JRadioButton(PluginServices.getText(this,"a_memoria"),true);
492
                        rButtonMemory.addActionListener(this);
493
                }
494
                return rButtonMemory;
495
        }
496

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

    
660
        /**
661
         * @return checkbox autovectores
662
         */
663
        public JCheckBox getAutovectorStatistics() {
664
                if(autovectorStatistics==null){
665
                        autovectorStatistics=new JCheckBox(PluginServices.getText(this, "autovector_statistics"),true);
666
                }
667
                return autovectorStatistics;
668
        }
669

    
670

    
671
        /**
672
         * @return checkbox estadisticas por banda
673
         */
674
        public JCheckBox getBandasStatistics() {
675
                if(bandasStatistics==null){
676
                        bandasStatistics=new JCheckBox(PluginServices.getText(this, "bandas_statistics"),true);
677
                }
678
                return bandasStatistics;
679
        }
680

    
681

    
682
        /**
683
         * @return checkbox matriz varianza-covarianza 
684
         */
685
        public JCheckBox getVarianceStatistics() {
686
                if(varianceStatistics==null){
687
                        varianceStatistics=new JCheckBox(PluginServices.getText(this, "variance_statistics"),true);
688
                }
689
                return varianceStatistics;
690
        }
691

    
692

    
693
        /*
694
         * (non-Javadoc)
695
         * @see org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener#actionButtonPressed(org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent)
696
         */
697
        public void actionButtonPressed(ButtonsPanelEvent e) {
698
//                 Bot?n de Aceptar
699
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
700
                        aplicar();
701
                        close();
702
                }
703

    
704
                // Bot?n de Aplicar
705
                if (e.getButton() == ButtonsPanel.BUTTON_APPLY) {
706
                        aplicar();
707
                }
708

    
709
                // Bot?n de Cerrar
710
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
711
                        close();
712
                }        
713
        }
714

    
715

    
716
        /**
717
         * acciones al aplicar
718
         */
719
        public void aplicar(){
720
                
721
                if(getTableFormatComponent().getNumSelected()>0){
722
                        PCImageProcess process = new PCImageProcess();
723
                        process.addParam("layer",pc.getRasterLayer());
724
                        process.addParam("grid",pc.getInputGrid());
725
                        process.addParam("autovalores",pc.getResult());
726
                        process.addParam("autovectores",pc.getAutoVectorMatrix());
727
                        process.addParam("compselected",getTableFormatComponent().getSeleccionadas());
728
                        process.addParam("filename",getFileSelected());
729
                        process.addParam("view", view);
730
                        process.start();
731
                        
732
                        
733
                }
734
                else{                        
735
                        JOptionPane.showMessageDialog(null,
736
                        PluginServices.getText(this,"no_components"), PluginServices.getText(this,"principal_components"),
737
                        JOptionPane.WARNING_MESSAGE);
738
                }
739
        }
740
        
741
        /**
742
         * acciones al cerrar
743
         */
744
        public void close(){
745
                try {
746
                        RasterModule.removeOnlyLayerNameListener(this);
747
                        PluginServices.getMDIManager().closeWindow(this);
748
                } catch (ArrayIndexOutOfBoundsException ex) {
749
                // Si la ventana no se puede eliminar no hacemos nada
750
                }
751
        }
752
        
753
        
754
        /**
755
         * Devuelve la ruta del fichero donde se va a guardar, en caso de guardarse
756
         * en memoria, calcula el nombre sin preguntar y devuelve la ruta.
757
         * @return string con la ruta de salida
758
         */
759
        public String getFileSelected() {
760
                String path = "";
761
                if (getRadioFile().isSelected()) {
762
                        JFileChooser chooser = new JFileChooser(FileOpenWizard.getLastPath());
763
                        chooser.setDialogTitle(PluginServices.getText(this, "seleccionar_fichero"));
764
                        //A?adimos las extensiones que hayan sido registradas en el driver
765
                        String[] extList = GeoRasterWriter.getDriversExtensions();
766
                        for(int i=0;i<extList.length;i++)
767
                                chooser.addChoosableFileFilter(new ExtendedFileFilter(extList[i]));
768

    
769
                        if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
770
                                return null;
771

    
772
                        FileOpenWizard.setLastPath(chooser.getSelectedFile().getPath().substring(0, chooser.getSelectedFile().getPath().lastIndexOf(File.separator)));
773
                        ExtendedFileFilter fileFilter = (ExtendedFileFilter) chooser.getFileFilter();
774
                        path = fileFilter.getNormalizedFilename(chooser.getSelectedFile());
775
                } else {
776
                        String file = getJTextNombreCapa().getText();
777
                        path = Utilities.createTempDirectory() + File.separator + getJTextNombreCapa().getText() + ".tif";
778
                        if(file.compareTo(RasterModule.getOnlyLayerName()) == 0) 
779
                                RasterModule.usesOnlyLayerName();
780
                        updateNewLayerText();
781
                }
782
                
783
                return path;
784
        }
785
        
786
        /*
787
         * (non-Javadoc)
788
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
789
         */
790
        public void actionPerformed(ActionEvent e) {
791
                if(e.getSource()==getExportar()){
792
                        exportStatistics();
793
                }
794
                if (e.getSource()==getTodos()){        
795
                        getTableFormatComponent().seleccionarTodas();
796
                        updateUI();
797
                }
798
                if (e.getSource()==getNinguno()){
799
                        getTableFormatComponent().seleccionarNinguna();
800
                        updateUI();
801
                }
802
        }
803
        
804
        
805
        public void focusGained(FocusEvent arg0) {
806
                // TODO Auto-generated method stub
807
        }
808

    
809
        public void focusLost(FocusEvent arg0) {
810
                // TODO Auto-generated method stub
811
        }
812

    
813

    
814

    
815
        public void windowActivated() {
816
                // TODO Auto-generated method stub
817
                
818
        }
819

    
820
        public void windowClosed() {
821
                // TODO Auto-generated method stub
822
                
823
        }
824
        
825

    
826
                        
827
}
828

    
829

    
830

    
831

    
832