Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / ui / HistogramPanel.java @ 11393

History | View | Annotate | Download (17.4 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.rastertools.histogram.ui;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.GridBagConstraints;
23
import java.util.ArrayList;
24

    
25
import javax.swing.JButton;
26
import javax.swing.JComboBox;
27
import javax.swing.JLabel;
28
import javax.swing.JPanel;
29
import javax.swing.JScrollPane;
30
import javax.swing.JTable;
31
import javax.swing.table.DefaultTableModel;
32

    
33
import org.cresques.filter.RasterBuf;
34
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
35
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
36
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
37
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
38
import org.gvsig.gui.beans.graphic.GraphicContainer;
39
import org.gvsig.gui.beans.table.TableContainer;
40
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
41
import org.gvsig.raster.RasterLibrary;
42
import org.gvsig.raster.util.Histogram;
43
import org.gvsig.raster.util.IHistogramable;
44
import org.gvsig.rastertools.histogram.HistogramPanelListener;
45

    
46
import com.iver.andami.PluginServices;
47
/**
48
 * <code>HistogramPanel</code>. Interfaz de usuario para la representaci?n de
49
 * histogramas.
50
 * 
51
 * @version 20/03/2007
52
 * @author Diego Guerrero (diego.guerrero@uclm.es)
53
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
54
 */
55
public class HistogramPanel extends DefaultButtonsPanel implements ButtonsPanelListener {
56
        private static final long serialVersionUID = 2772897994667886753L;
57

    
58
        /**
59
         * Objeto que controlara toda la carga pesada del panel
60
         */
61
        private HistogramPanelListener histogramPanelListener = null;
62

    
63
        /**
64
         * Array para la seleccion del origen
65
         */
66
        private ArrayList comboSource = new ArrayList();
67
        
68
        /**
69
         * Variable para controlar si se dispararan los eventos del panel,
70
         * si es la primera vez no tiene porque disparar eventos.
71
         */
72
        public boolean panelInizialited = false;
73

    
74
        /**
75
         * Componentes graficos
76
         */
77
        private JComboBox                                 comboBoxSource= null;
78
        private JComboBox                                 comboBoxType = null;
79
        private JButton                                         bTable = null;
80
        private JPanel                                                 panelNorth = null;
81
        private JPanel                                                 panelSouth = null;
82
        private JButton                                                buttonShowAll = null;
83
        private JButton                                                buttonClean = null;
84
        private JTable                                                jTableBands = null;
85
        private TableContainer                tableContainer = null;
86
        private GraphicContainer        graphicContainer = null;
87

    
88
        /**
89
         * Tipo de dato de la petici?n. Si la petici?n del histograma es de la vista el tipo
90
         * de dato ser? byte aunque la imagen sea de 16 bits. Si la petici?n es de los datos 
91
         * reales de la imagen entonces el tipo de dato de la petici?n coincide con el tipo
92
         * de datos de la imagen.
93
         */
94
        private int                                                        dataType = RasterBuf.TYPE_BYTE;
95
        
96
        /**
97
         * Crea un dialogo para los histogramas.
98
         *
99
         */
100
        public HistogramPanel() {
101
                super(ButtonsPanel.BUTTONS_NONE);
102
                this.getButtonsPanel().addHideDetails();
103
                this.getButtonsPanel().addSeeDetails();
104
                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setVisible(false);
105
                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setText(PluginServices.getText(this, "mostrar_estadisticas"));
106
                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_HIDEDETAILS).setText(PluginServices.getText(this, "ocultar_estadisticas"));
107
                this.getButtonsPanel().addClose();
108
                this.addButtonPressedListener(this);
109
                histogramPanelListener = getHistogramPanelListener();
110
                initialize();
111
        }
112

    
113
        /**
114
         * This method initializes this
115
         * 
116
         */
117
        private void initialize() {
118
                this.setLayout(new BorderLayout(5, 5));
119
    this.add(getPanelNorth(), java.awt.BorderLayout.NORTH);
120
    this.add(getGraphicContainer(), java.awt.BorderLayout.CENTER);
121
    this.add(getPanelSouth(), java.awt.BorderLayout.SOUTH);
122
                this.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
123

    
124
                getHistogramPanelListener().setControlListeners();
125
        }
126

    
127
        /**
128
         * This method initializes jPanel        
129
         *         
130
         * @return javax.swing.JPanel        
131
         */
132
        public GraphicContainer getGraphicContainer() {
133
                if (graphicContainer == null) {
134
                        graphicContainer = new GraphicContainer(true);
135
                }
136
                return graphicContainer;
137
        }
138

    
139
        /**
140
         * This method initializes jPanel        
141
         *         
142
         * @return javax.swing.JPanel        
143
         */
144
        private JPanel getPanelSouth() {
145
                if (panelSouth == null) {
146
                        panelSouth = new JPanel();
147
                        panelSouth.setLayout(new BorderLayout(5,5));
148
                        panelSouth.add(getTableContainer(), BorderLayout.CENTER);
149

    
150
                        JPanel jPanel2 = new JPanel();
151
      jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
152
      jPanel2.add(getBCreateTable());
153
                  jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
154

    
155
                  panelSouth.add(jPanel2, BorderLayout.EAST);
156
                }
157
                return panelSouth;
158
        }
159

    
160
        /**
161
         * Obtiene la tabla de estadisticas
162
         * @return
163
         */
164
        public TableContainer getTableContainer(){
165
                if(tableContainer == null){
166
                        String[] columnNames = {PluginServices.getText(this, "banda"), PluginServices.getText(this, "minimo"), PluginServices.getText(this, "maximo"), PluginServices.getText(this, "media"), PluginServices.getText(this, "mediana"), PluginServices.getText(this, "npixeles")};
167
                        int[] columnWidths = {50, 65, 65, 65, 65, 115};
168
                        tableContainer = new TableContainer(columnNames, columnWidths);
169
                        tableContainer.setControlVisible(true);
170
                        tableContainer.setModel("ListModel");
171
                        tableContainer.initialize();
172
                        tableContainer.setName("tableContainer");
173
                        tableContainer.setControlVisible(false);
174
                        
175
                        try{
176
                                tableContainer.setEditable(false);
177
                        }catch(NotInitializeException ex){
178
                                System.out.println("Tabla no inicializada");
179
                        }        
180
                }
181
                return tableContainer;
182
        }
183
        
184
        /**
185
         * Devuelve el boton de mostrar todas las bandas
186
         * @return JButton
187
         */
188
        public JButton getButtonShowAll() {
189
                if (buttonShowAll == null) {
190
                        buttonShowAll = new JButton(PluginServices.getText(this, "boton_mostrar"));
191
                        buttonShowAll.addActionListener(getHistogramPanelListener());
192
                }
193
                return buttonShowAll;
194
        }
195

    
196
        /**
197
         * Devuelve el boton de limpiar las bandas
198
         * @return JButton
199
         */
200
        public JButton getButtonClean() {
201
                if (buttonClean == null) {
202
                        buttonClean = new JButton(PluginServices.getText(this, "boton_limpiar"));
203
                        buttonClean.addActionListener(getHistogramPanelListener());
204
                }
205
                return buttonClean;
206
        }
207

    
208
        /**
209
         * This method initializes jPanel        
210
         *         
211
         * @return javax.swing.JPanel        
212
         */
213
        private JPanel getPanelNorth() {
214
                if (panelNorth == null) {
215
                        panelNorth = new JPanel();
216
                        JScrollPane jScrollPane1 = new JScrollPane();
217
                        JPanel jPanel4 = new JPanel();
218
                        java.awt.GridBagConstraints gridBagConstraints;
219

    
220
                        jScrollPane1.setPreferredSize(new java.awt.Dimension(120,65));
221
                        jScrollPane1.setViewportView(getJTableBands());
222

    
223
                        panelNorth.setLayout(new BorderLayout());
224
                        panelNorth.add(jPanel4, BorderLayout.CENTER);
225
                        panelNorth.add(jScrollPane1, BorderLayout.EAST);
226
                        
227
      jPanel4.setLayout(new java.awt.GridBagLayout());
228
      JLabel jLabel1 = new JLabel(PluginServices.getText(this, "origen")+":");
229
      gridBagConstraints = new java.awt.GridBagConstraints();
230
      gridBagConstraints.gridx = 0;
231
      gridBagConstraints.gridy = 0;
232
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
233
      gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
234
      jPanel4.add(jLabel1, gridBagConstraints);
235

    
236
      JLabel jLabel2 = new JLabel(PluginServices.getText(this, "tipo")+":");
237
      gridBagConstraints = new java.awt.GridBagConstraints();
238
      gridBagConstraints.gridx = 0;
239
      gridBagConstraints.gridy = 1;
240
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
241
      gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
242
      jPanel4.add(jLabel2, gridBagConstraints);
243

    
244
      gridBagConstraints = new java.awt.GridBagConstraints();
245
      gridBagConstraints.gridx = 1;
246
      gridBagConstraints.gridy = 0;
247
      gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
248
      jPanel4.add(getComboBoxSource(), gridBagConstraints);
249

    
250
      gridBagConstraints = new java.awt.GridBagConstraints();
251
      gridBagConstraints.gridx = 1;
252
      gridBagConstraints.gridy = 1;
253
      gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
254
      jPanel4.add(getComboBoxType(), gridBagConstraints);
255

    
256
      gridBagConstraints = new java.awt.GridBagConstraints();
257
      gridBagConstraints.gridx = 2;
258
      gridBagConstraints.gridy = 0;
259
      gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
260
      gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
261
      jPanel4.add(getButtonShowAll(), gridBagConstraints);                
262

    
263
      gridBagConstraints = new java.awt.GridBagConstraints();
264
      gridBagConstraints.gridx = 2;
265
      gridBagConstraints.gridy = 1;
266
      gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
267
      gridBagConstraints.insets = new java.awt.Insets(0, 15, 0, 0);
268
      jPanel4.add(getButtonClean(), gridBagConstraints);                        
269
                }
270
                return panelNorth;
271
        }
272

    
273
        /**
274
         * Obtiene el combo con la selecci?n de tipo de histograma, acumulado/no acumulado
275
         * @return javax.swing.JComboBox        
276
         */
277
        public JComboBox getComboBoxType() {
278
                if (comboBoxType == null) {
279
                        String lista [] = new String[Histogram.getHistogramTypesCount()];
280
                        for (int i = 0; i < lista.length; i++) {
281
                                lista[i] = PluginServices.getText(this, Histogram.getType(i));
282
                        }
283
                        comboBoxType = new JComboBox(lista);
284
                        comboBoxType.addActionListener(getHistogramPanelListener());
285
                        comboBoxType.setPreferredSize(new java.awt.Dimension(150, 25));
286
                }
287
                return comboBoxType;
288
        }
289

    
290
        /**
291
         * Obtiene el combo con la selecci?n de la fuente de datos en el calculo del histograma,
292
         * datos de la vista, datos reales con el extent de la vista e imagen completa.
293
         * @return javax.swing.JComboBox        
294
         */
295
        public JComboBox getComboBoxSource() {
296
                if (comboBoxSource == null) {
297
                        comboBoxSource = new JComboBox();
298
                        comboBoxSource.addActionListener(getHistogramPanelListener());
299
                        comboBoxSource.setPreferredSize(new java.awt.Dimension(150,25));
300
                }
301
                return comboBoxSource;
302
        }
303

    
304
        /**
305
         *Asigna el combo "Origen" el valor de solo vista para el calculo del histograma. Esa opci?n es 
306
         *utilizada para extensiones que necesitan histograma pero no pueden acceder a la fuente de datos.
307
         */
308
        public void setOnlyViewValue(){
309
                getHistogramPanelListener().eventsEnabled = false;
310
                getComboBoxSource().removeAllItems();
311
                getComboBoxSource().addItem(PluginServices.getText(this, "vista"));
312
                getHistogramPanelListener().eventsEnabled = panelInizialited;
313
        }
314

    
315
        /**
316
         * This method initializes jButton        
317
         *         
318
         * @return javax.swing.JButton        
319
         */
320
        public JButton getBCreateTable() {
321
                if (bTable == null) {
322
                        bTable = new JButton();
323
                        bTable.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
324
                        bTable.setText(PluginServices.getText(this, "crear_tabla"));
325
                        bTable.setName("bTable");
326
                        bTable.addActionListener(getHistogramPanelListener());
327
                        bTable.setPreferredSize(new java.awt.Dimension(150,25));
328
                }
329
                return bTable;
330
        }
331

    
332
        /**
333
         * This method initializes jComboBox
334
         *         
335
         * @return javax.swing.JComboBox        
336
         */
337
        public JTable getJTableBands() {
338
                getHistogramPanelListener().eventsEnabled = false;
339
                if (jTableBands == null) {
340
                        jTableBands = new JTable();
341
                        jTableBands.setModel(new javax.swing.table.DefaultTableModel(
342
          new Object [][] {},
343
          new String [] {
344
              " ", PluginServices.getText(this, "bandas")
345
          }
346
      ) {
347
                                private static final long serialVersionUID = 0L;
348
                                        Class[] types = new Class [] {
349
              java.lang.Boolean.class, java.lang.String.class
350
          };
351
          boolean[] canEdit = new boolean [] {
352
              true, false
353
          };
354

    
355
          public Class getColumnClass(int columnIndex) {
356
              return types [columnIndex];
357
          }
358

    
359
          public boolean isCellEditable(int rowIndex, int columnIndex) {
360
              return canEdit [columnIndex];
361
          }
362
      });
363
                        jTableBands.addPropertyChangeListener(getHistogramPanelListener());
364
                        jTableBands.getColumnModel().getColumn(0).setPreferredWidth(20);
365
                        jTableBands.getColumnModel().getColumn(0).setResizable(false);
366
                }
367
                getHistogramPanelListener().eventsEnabled = panelInizialited;
368
                return jTableBands;
369
        }
370

    
371
        /**
372
         * Asigna el n?mero de bandas al combobox
373
         * @param bands N?mero de bandas de la imagen
374
         */
375
        public void setBands(int bands){
376
                getHistogramPanelListener().eventsEnabled = false;
377
                ((DefaultTableModel) getJTableBands().getModel()).setRowCount(0);
378
                for(int i = 0; i < bands; i++){
379
                        ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), PluginServices.getText(this, "band") + " " + String.valueOf(i)});
380
                }
381
                getHistogramPanelListener().eventsEnabled = panelInizialited;
382
        }
383

    
384
        /**
385
         * Asigna la estadistica a la tabla
386
         * @param stat
387
         */
388
        public void setStatistic(long[][] stat){
389
                if(stat == null)
390
                        return;
391
                try {
392
                        getTableContainer().removeAllRows();
393
                        for(int iBand = 0; iBand < stat[0].length; iBand++){
394
                                Object[] list = new Object[stat.length + 1];
395
                                list[0] = new Integer(iBand);
396
                                for(int iStat = 1; iStat <= stat.length; iStat++)
397
                                        list[iStat] = new Long(stat[iStat - 1][iBand]);                                
398
                                        
399
                                getTableContainer().addRow(list);
400
                                list = null;
401
                        }
402
                } catch (NotInitializeException e) {
403
                        e.printStackTrace();
404
                }
405
        }
406

    
407
        /**
408
         * Resetea el control de bandas del panel con los valores RGB para 
409
         * cuando se est? haciendo el histograma de la visualizaci?n en
410
         * vez del histograma con los datos
411
         *
412
         */
413
        public void setRGBInBandList(){
414
                getHistogramPanelListener().eventsEnabled = false;
415

    
416
                ((DefaultTableModel) getJTableBands().getModel()).setRowCount(0);
417
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "R"});
418
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "G"});
419
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "B"});
420
                
421
                getHistogramPanelListener().eventsEnabled = panelInizialited;
422
        }
423

    
424
        /**
425
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
426
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
427
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
428
         * y el segundo el de la izquierda.  
429
         */
430
        public double[] getBoxesValues(){
431
                double[] v = new double[2];
432
                double[] currentValues = new double[2];
433
                currentValues[0] = getGraphicContainer().getX2();
434
                currentValues[1] = getGraphicContainer().getX1();
435
                switch (dataType){
436
                        case RasterBuf.TYPE_BYTE:
437
                                v[0] = (currentValues[0] * 255) / 100; 
438
                                v[1] = (currentValues[1] * 255) / 100;
439
                                break;
440
                        default:
441
                                v[0] = (currentValues[0] * RasterLibrary.defaultNumberOfClasses) / 100;
442
                                v[1] = (currentValues[1] * RasterLibrary.defaultNumberOfClasses) / 100;
443
                                break;
444
                }
445
                return v;
446
        }
447

    
448
        public HistogramPanelListener getHistogramPanelListener() {
449
                if (histogramPanelListener == null) {
450
                        histogramPanelListener = new HistogramPanelListener(this);
451
                }
452
                return histogramPanelListener;
453
        }
454

    
455
        public void setHistogramableSource(IHistogramable lyr, String name) {
456
                getHistogramPanelListener().eventsEnabled = false;
457
                ArrayList aux = new ArrayList();
458
                aux.add(lyr);
459
                aux.add(name);
460
                comboSource.add(aux);
461
                updateComboBoxSource();
462
                getHistogramPanelListener().eventsEnabled = panelInizialited;
463
        }
464

    
465
        public ArrayList getComboSource() {
466
                return comboSource;
467
        }
468

    
469
        private void updateComboBoxSource() {
470
                getHistogramPanelListener().eventsEnabled = false;
471
                getComboBoxSource().removeAllItems();
472
                for (int i = 0; i < comboSource.size(); i++) {
473
                        getComboBoxSource().addItem(((ArrayList) comboSource.get(i)).get(1));
474
                }
475
                getHistogramPanelListener().eventsEnabled = panelInizialited;
476
        }
477

    
478
        public void clearSources() {
479
                comboSource = new ArrayList();
480
                updateComboBoxSource();
481
        }
482

    
483
        public void refreshBands() {
484
                //En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
485
                int bandCount = getHistogramPanelListener().getLastHistogram().getNumBands();
486
                if ((getComboBoxSource().getSelectedIndex() == 0) && (bandCount == 3)) {
487
                        setRGBInBandList();
488
                } else {
489
                        setBands(bandCount);
490
                }
491
        }
492

    
493
        public void firstRun() {
494
                getHistogramPanelListener().showHistogram();
495
        }
496

    
497
        public void setDataType(int dataType) {
498
                this.dataType = dataType;
499
        }
500

    
501
        public int getDataType() {
502
                return dataType;
503
        }
504

    
505
        public void actionButtonPressed(ButtonsPanelEvent e) {
506
                switch (e.getButton()) {
507
                        case ButtonsPanel.BUTTON_HIDEDETAILS:
508
                                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_HIDEDETAILS).setVisible(false);
509
                                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setVisible(true);
510
                                panelSouth.setVisible(false);
511
                        break;
512
                        case ButtonsPanel.BUTTON_SEEDETAILS:
513
                                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_HIDEDETAILS).setVisible(true);
514
                                this.getButtonsPanel().getButton(ButtonsPanel.BUTTON_SEEDETAILS).setVisible(false);
515
                                panelSouth.setVisible(true);
516
                                break;
517
                }
518
        }
519
}