Statistics
| Revision:

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

History | View | Annotate | Download (15.1 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.awt.GridBagLayout;
24

    
25
import javax.swing.JButton;
26
import javax.swing.JComboBox;
27
import javax.swing.JLabel;
28
import javax.swing.JPanel;
29

    
30
import org.cresques.filter.RasterBuf;
31
import org.cresques.i18n.Messages;
32
import org.gvsig.gui.beans.buttonsPanel.ButtonsPanel;
33
import org.gvsig.gui.beans.dialogPanel.DialogPanel;
34
import org.gvsig.gui.beans.graphic.GraphicChartPanel;
35
import org.gvsig.gui.beans.graphic.GraphicContainer;
36
import org.gvsig.gui.beans.table.TableContainer;
37
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
38
import org.gvsig.raster.util.RasterUtilities;
39
/**
40
 * <code>HistogramPanel</code>. Interfaz de usuario para la representaci?n de
41
 * histogramas.
42
 * 
43
 * @version 20/03/2007
44
 * @author Diego Guerrero (diego.guerrero@uclm.es)
45
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
46
 */
47
public class HistogramPanel extends DialogPanel {
48
        private static final long serialVersionUID = 2772897994667886753L;
49

    
50
        private HistogramPanelListener          histogramPanelListener = null;
51

    
52
        public static int                                        MAXBANDS = 10;  
53
        private int                                                 HSUP = 60;
54
        
55
        private GraphicContainer        graphicContainer = null;
56
        private JPanel                                                 pTable = null;
57
        private JComboBox                                 jComboBoxOrigen = null;
58
        private JComboBox                                 jComboBoxTipo = null;
59
        private JComboBox                                 jComboBands = null;
60
        private JButton                                         jButtonClear = null;
61
        private JButton                                         bTable = null;
62
        private TableContainer                tableContainer = null;
63
                
64
        private JPanel                                                 cbSup = null;
65
        private JLabel                                                 lOrigin = null;
66
        private JLabel                                                 lBands = null;
67
        private JLabel                                                 lType = null;
68
        /**
69
         * Bandas que se est?n mostrando en el gr?fico. Se inicializa con las 3 bandas
70
         * RGB de la visualizaci?n. Este array puede tener m?s elementos ya que si las 
71
         * bandas no son de visualizaci?n (bandas de la imagen en disco) tendr? un elemento
72
         * por cada una. 
73
         */
74
        public boolean[]                                        showBands = {true, true, true};
75
        
76
        /**
77
         * Tipo de histograma 
78
         * 0 = acumulado 
79
         * 1 = No acumulado
80
         */
81
        private int                                                        type = 0;
82
        
83
        /**
84
         * Tipo de fuente de datos para el histograma
85
         * 0 = Datos de la vista
86
         * 1 = extent de la vista y datos leidos desde la imagen
87
         * 2 = histograma de la imagen completa
88
         */
89
        private int                                                 histogramDataSource = 0;
90

    
91
        /**
92
         * Tipo de dato de la petici?n. Si la petici?n del histograma es de la vista el tipo
93
         * de dato ser? byte aunque la imagen sea de 16 bits. Si la petici?n es de los datos 
94
         * reales de la imagen entonces el tipo de dato de la petici?n coincide con el tipo
95
         * de datos de la imagen.
96
         */
97
        private int                                                        requestDataType = RasterBuf.TYPE_BYTE;
98
        
99
        /**
100
         * Crea un dialogo para los histogramas.
101
         *
102
         */
103
        public HistogramPanel() {
104
                super(ButtonsPanel.BUTTONS_CLOSE);
105
                initialize();
106
        }
107

    
108
        /**
109
         * This method initializes this
110
         * 
111
         */
112
        private void initialize() {
113
                this.setLayout(new BorderLayout(5, 5));
114
    this.add(getCbSup(), java.awt.BorderLayout.NORTH);
115
    this.add(getGraphicContainer(), java.awt.BorderLayout.CENTER);
116
    this.add(getPTable(), java.awt.BorderLayout.SOUTH);
117
                this.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
118
                getHistogramPanelListener().setControlListeners();
119
        }
120

    
121
        /**
122
         * This method initializes jPanel        
123
         *         
124
         * @return javax.swing.JPanel        
125
         */
126
        public GraphicContainer getGraphicContainer() {
127
                if (graphicContainer == null) {
128
                        graphicContainer = new GraphicContainer( false);
129
                }
130
                return graphicContainer;
131
        }
132

    
133
        /**
134
         * This method initializes jPanel        
135
         *         
136
         * @return javax.swing.JPanel        
137
         */
138
        private JPanel getPTable() {
139
                if (pTable == null) {
140
                        pTable = new JPanel();
141
                        pTable.setLayout(new BorderLayout(5,5));
142
                        pTable.add(getTableContainer(), BorderLayout.CENTER);
143

    
144
                        JPanel jPanel2 = new JPanel();
145
      jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
146
      jPanel2.add(getBCreateTable());
147
                  jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
148

    
149
                        pTable.add(jPanel2, BorderLayout.EAST);
150
                }
151
                return pTable;
152
        }
153

    
154
        /**
155
         * Obtiene la tabla de estadisticas
156
         * @return
157
         */
158
        public TableContainer getTableContainer(){
159
                if(tableContainer == null){
160
                        String[] columnNames = {Messages.getText("banda"), Messages.getText("minimo"), Messages.getText("maximo"), Messages.getText("media"), Messages.getText("mediana"), Messages.getText("npixeles")};
161
                        int[] columnWidths = {50, 65, 65, 65, 65, 115};
162
                        tableContainer = new TableContainer(columnNames, columnWidths);
163
                        tableContainer.setControlVisible(true);
164
                        tableContainer.setModel("ListModel");
165
                        tableContainer.initialize();
166
                        tableContainer.setName("tableContainer");
167
                        tableContainer.setControlVisible(false);
168
                        
169
                        try{
170
                                tableContainer.setEditable(false);
171
                        }catch(NotInitializeException ex){
172
                                System.out.println("Tabla no inicializada");
173
                        }        
174
                }
175
                return tableContainer;
176
        }
177

    
178
        /**
179
         * This method initializes jPanel        
180
         *         
181
         * @return javax.swing.JPanel        
182
         */
183
        private JPanel getCbSup() {
184
                if (cbSup == null) {
185
                        lOrigin = new JLabel();
186
                        lOrigin.setText(Messages.getText("origen")+":");
187
                        lBands = new JLabel();
188
                        lBands.setText(Messages.getText("bandas")+":");
189
                        lType = new JLabel();
190
                        lType.setText(Messages.getText("tipo")+":");
191
                        
192
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
193
                        gridBagConstraints.gridy = 0;
194
                        gridBagConstraints.gridx = 0;
195
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
196
                        gridBagConstraints1.insets = new java.awt.Insets(0,0,0,80);
197
                        gridBagConstraints1.gridy = 0;
198
                        gridBagConstraints1.gridx = 1;
199
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
200
                        gridBagConstraints2.gridy = 0;
201
                        gridBagConstraints2.gridx = 3;
202
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
203
                        gridBagConstraints3.insets = new java.awt.Insets(2,0,0,0);
204
                        gridBagConstraints3.gridy = 1;
205
                        gridBagConstraints3.gridx = 0;
206
                        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
207
                        gridBagConstraints4.insets = new java.awt.Insets(2,0,0,80);
208
                        gridBagConstraints4.gridy = 1;
209
                        gridBagConstraints4.gridx = 1;
210
                        GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
211
                        gridBagConstraints5.insets = new java.awt.Insets(2,0,0,0);
212
                        gridBagConstraints5.gridy = 1;
213
                        gridBagConstraints5.gridx = 3;
214
                        GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
215
                        gridBagConstraints6.gridy = 0;
216
                        gridBagConstraints6.gridx = 2;
217
                        
218
                        cbSup = new JPanel();
219
                        
220
                cbSup.setPreferredSize(new java.awt.Dimension(0, HSUP));
221
                        
222
                        cbSup.setLayout(new GridBagLayout());
223
                        cbSup.add(lOrigin, gridBagConstraints);
224
                        cbSup.add(getJComboBoxOrigen(), gridBagConstraints1);
225
                        
226
                        cbSup.add(lBands, gridBagConstraints6);
227
                        
228
                        cbSup.add(getJComboBands(), gridBagConstraints2);
229
                        cbSup.add(lType, gridBagConstraints3);
230
                        cbSup.add(getJComboBoxTipo(), gridBagConstraints4);
231
                        cbSup.add(getJButtonClear(), gridBagConstraints5);
232
                }
233
                return cbSup;
234
        }
235
        
236
        /**
237
         * Obtiene el combo con la selecci?n de tipo de histograma, acumulado/no acumulado
238
         * @return javax.swing.JComboBox        
239
         */
240
        public JComboBox getJComboBoxTipo() {
241
                if (jComboBoxTipo == null) {
242
                        String lista [] = {Messages.getText("no_acumulado"),Messages.getText("acumulado")};
243
                        jComboBoxTipo = new JComboBox(lista);
244
                        jComboBoxTipo.addActionListener(getHistogramPanelListener());
245
                        jComboBoxTipo.setPreferredSize(new java.awt.Dimension(150,25));
246
                }
247
                return jComboBoxTipo;
248
        }
249
        
250
        /**
251
         * Obtiene el combo con la selecci?n de la fuente de datos en el calculo del histograma,
252
         * datos de la vista, datos reales con el extent de la vista e imagen completa.
253
         * @return javax.swing.JComboBox        
254
         */
255
        public JComboBox getJComboBoxOrigen() {
256
                if (jComboBoxOrigen == null) {
257
                        String lista [] = {Messages.getText("datos_visualizados"),Messages.getText("vista_datasource"),Messages.getText("imagen_completa")};
258
                        jComboBoxOrigen = new JComboBox(lista);
259
                        jComboBoxOrigen.addActionListener(getHistogramPanelListener());
260
                        jComboBoxOrigen.setPreferredSize(new java.awt.Dimension(150,25));
261
                }
262
                return jComboBoxOrigen;
263
        }
264
        
265
        /**
266
         *Asigna el combo "Origen" el valor de solo vista para el calculo del histograma. Esa opci?n es 
267
         *utilizada para extensiones que necesitan histograma pero no pueden acceder a la fuente de datos.
268
         */
269
        public void setOnlyViewValue(){
270
                HistogramPanelListener.comboEventEnable = false;
271
                getJComboBoxOrigen().removeAllItems();
272
                getJComboBoxOrigen().addItem(Messages.getText("vista"));
273
                HistogramPanelListener.comboEventEnable = true;
274
        }
275
        
276
        /**
277
         * This method initializes jButton        
278
         *         
279
         * @return javax.swing.JButton        
280
         */
281
        public JButton getJButtonClear() {
282
                if (jButtonClear == null) {
283
                        jButtonClear = new JButton();
284
                        jButtonClear.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
285
                        jButtonClear.setText(Messages.getText("limpiar"));
286
                        jButtonClear.addActionListener(getHistogramPanelListener());
287
                        jButtonClear.setPreferredSize(new java.awt.Dimension(100,25));
288
                }
289
                return jButtonClear;
290
        }
291
        
292
        /**
293
         * This method initializes jButton        
294
         *         
295
         * @return javax.swing.JButton        
296
         */
297
        public JButton getBCreateTable() {
298
                if (bTable == null) {
299
                        bTable = new JButton();
300
                        bTable.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
301
                        bTable.setText(Messages.getText("crear_tabla"));
302
                        bTable.setName("bTable");
303
                        bTable.addActionListener(getHistogramPanelListener());
304
                        bTable.setPreferredSize(new java.awt.Dimension(150,25));
305
                }
306
                return bTable;
307
        }
308
        
309
        /**
310
         * This method initializes jComboBox        
311
         *         
312
         * @return javax.swing.JComboBox        
313
         */
314
        public JComboBox getJComboBands() {
315
                if (jComboBands == null) {
316
                        String lista [] = {Messages.getText("todas")};
317
                        jComboBands = new JComboBox(lista);
318
                        jComboBands.addActionListener(getHistogramPanelListener());
319
                        jComboBands.setPreferredSize(new java.awt.Dimension(100,25));
320
                }
321
                return jComboBands;
322
        }
323
                
324
        /**
325
         * Asigna el n?mero de bandas al combobox
326
         * @param bands N?mero de bandas de la imagen
327
         */
328
        public void setBands(int bands){
329
                HistogramPanelListener.comboEventEnable = false;
330
                getJComboBands().removeAllItems();
331
                getJComboBands().addItem(Messages.getText("todas"));
332
                showBands = new boolean[bands];
333
                for(int i = 0; i < bands; i++){
334
                        getJComboBands().addItem("Band "+String.valueOf(i));
335
                        showBands[i] = true;
336
                }
337
                HistogramPanelListener.comboEventEnable = true;
338
        }
339
        
340
        
341
        /**
342
         * Asigna la estadistica a la tabla
343
         * @param stat
344
         */
345
        public void setStatistic(long[][] stat){
346
                if(stat == null)
347
                        return;
348
                try {
349
                        getTableContainer().removeAllRows();
350
                        for(int iBand = 0; iBand < stat[0].length; iBand++){
351
                                Object[] list = new Object[stat.length + 1];
352
                                list[0] = new Integer(iBand);
353
                                for(int iStat = 1; iStat <= stat.length; iStat++)
354
                                        list[iStat] = new Long(stat[iStat - 1][iBand]);                                
355
                                        
356
                                getTableContainer().addRow(list);
357
                                list = null;
358
                        }
359
                } catch (NotInitializeException e) {
360
                        // TODO Auto-generated catch block
361
                        e.printStackTrace();
362
                }
363
        }
364
        
365
        /**
366
         * Resetea el control de bandas del panel con los valores RGB para 
367
         * cuando se est? haciendo el histograma de la visualizaci?n en
368
         * vez del histograma con los datos
369
         *
370
         */
371
        public void setRGBInBandList(){
372
                HistogramPanelListener.comboEventEnable = false;
373
                boolean[] list = {true, true, true};
374
                showBands = list;
375
                getJComboBands().removeAllItems();
376
                getJComboBands().addItem(Messages.getText("todas"));
377
                getJComboBands().addItem("R");
378
                getJComboBands().addItem("G");
379
                getJComboBands().addItem("B");
380
                HistogramPanelListener.comboEventEnable = true;
381
        }
382
        
383
        /**
384
         * A?ade o elimina una banda de la visualizaci?n. Si la banda se est? visualizando
385
         * se elimina y si no entonces se muestra
386
         * @param band banda a visualizar o borrar del gr?fico
387
         */
388
        public void addOrRemoveGraphicBand(int band){
389
                if(band > showBands.length)
390
                        return;
391
                showBands[band] = !showBands[band];
392
        }
393
        
394
        /**
395
         * Limpia la gr?fica
396
         */
397
        public void cleanChart(){
398
                GraphicChartPanel gcp = graphicContainer.getPGraphic();
399
                gcp.cleanChart();
400
                for(int i = 0; i < showBands.length; i++)
401
                        showBands[i] = false;
402
        }
403
        
404
        /**
405
         * Obtiene el tipo de datos de la fuente de la imagen
406
         * @return tipo de datos de la fuente de la imagen
407
         */
408
        public int getHistogramDataSource() {
409
                return histogramDataSource;
410
        }
411

    
412
        /**
413
         * Asigna el tipo de datos de la fuente de la imagen
414
         * @param histogramDataSource tipo de datos de la fuente de la imagen
415
         */
416
        public void setHistogramDataSource(int histogramDataSource) {
417
                this.histogramDataSource = histogramDataSource;
418
        }
419

    
420
        /**
421
         * Obtiene la lista de bandas que se muestran en el histograma
422
         * @return Lista de bandas donde cada elemento es un booleano. True si la banda
423
         * se muestra y false si no se muestra.
424
         */
425
        public boolean[] getBandListShowInChart(){
426
                return showBands;
427
        }
428

    
429
        /**
430
         * Asigna la lista de bandas que se muestran en el histograma
431
         * @param Lista de bandas donde cada elemento es un booleano. True si la banda
432
         * se muestra y false si no se muestra.
433
         */
434
        public void setBandListShowInChart(boolean[] showBands) {
435
                this.showBands = showBands;
436
        }
437
        
438
        /**
439
         * Obtiene el tipo de histograma a mostrar
440
         * @return acumulado/no acumulado
441
         */
442
        public int getType() {
443
                return type;
444
        }
445

    
446
        /**
447
         * Asigna el tipo de histograma a mostrar
448
         * @param type acumulado/no acumulado 
449
         */
450
        public void setType(int type) {
451
                this.type = type;
452
        }
453

    
454
        /**
455
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
456
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
457
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
458
         * y el segundo el de la izquierda.  
459
         */
460
        public double[] getBoxesValues(){
461
                double[] v = new double[2];
462
                double[] currentValues = getGraphicContainer().getBoxesValues();
463
                switch (requestDataType){
464
                        case RasterBuf.TYPE_BYTE:         v[0] = (currentValues[0] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100; 
465
                                v[1] = (currentValues[1] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100;
466
                                break;
467
                        case RasterBuf.TYPE_SHORT:         v[0] = (currentValues[0] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
468
                                v[1] = (currentValues[1] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
469
                                break;
470
                }
471
                return v;
472
        }
473
        
474
        public HistogramPanelListener getHistogramPanelListener() {
475
                if (histogramPanelListener == null) {
476
                        histogramPanelListener = new HistogramPanelListener(this);
477
                }
478
                return histogramPanelListener;
479
        }
480
}