Statistics
| Revision:

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

History | View | Annotate | Download (15.8 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
import java.util.Vector;
25

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

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

    
54
        private HistogramPanelListener          histogramPanelListener = null;
55
        private Vector comboSource = new Vector();
56

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

    
96
        /**
97
         * Tipo de dato de la petici?n. Si la petici?n del histograma es de la vista el tipo
98
         * de dato ser? byte aunque la imagen sea de 16 bits. Si la petici?n es de los datos 
99
         * reales de la imagen entonces el tipo de dato de la petici?n coincide con el tipo
100
         * de datos de la imagen.
101
         */
102
        private int                                                        requestDataType = RasterBuf.TYPE_BYTE;
103
        
104
        /**
105
         * Crea un dialogo para los histogramas.
106
         *
107
         */
108
        public HistogramPanel() {
109
                super(ButtonsPanel.BUTTONS_CLOSE);
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(getCbSup(), java.awt.BorderLayout.NORTH);
120
    this.add(getGraphicContainer(), java.awt.BorderLayout.CENTER);
121
    this.add(getPTable(), java.awt.BorderLayout.SOUTH);
122
                this.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
123
                getHistogramPanelListener().setControlListeners();
124
        }
125

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

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

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

    
154
                        pTable.add(jPanel2, BorderLayout.EAST);
155
                }
156
                return pTable;
157
        }
158

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

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

    
419
        /**
420
         * Asigna el tipo de datos de la fuente de la imagen
421
         * @param histogramDataSource tipo de datos de la fuente de la imagen
422
         */
423
        public void setHistogramDataSource(int histogramDataSource) {
424
                this.histogramDataSource = histogramDataSource;
425
        }
426

    
427
        /**
428
         * Obtiene la lista de bandas que se muestran en el histograma
429
         * @return Lista de bandas donde cada elemento es un booleano. True si la banda
430
         * se muestra y false si no se muestra.
431
         */
432
        public boolean[] getBandListShowInChart(){
433
                return showBands;
434
        }
435

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

    
453
        /**
454
         * Asigna el tipo de histograma a mostrar
455
         * @param type acumulado/no acumulado 
456
         */
457
        public void setType(int type) {
458
                this.type = type;
459
        }
460

    
461
        /**
462
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
463
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
464
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
465
         * y el segundo el de la izquierda.  
466
         */
467
        public double[] getBoxesValues(){
468
                double[] v = new double[2];
469
                double[] currentValues = getGraphicContainer().getBoxesValues();
470
                switch (requestDataType){
471
                        case RasterBuf.TYPE_BYTE:         v[0] = (currentValues[0] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100; 
472
                                v[1] = (currentValues[1] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100;
473
                                break;
474
                        case RasterBuf.TYPE_SHORT:         v[0] = (currentValues[0] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
475
                                v[1] = (currentValues[1] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
476
                                break;
477
                }
478
                return v;
479
        }
480
        
481
        public HistogramPanelListener getHistogramPanelListener() {
482
                if (histogramPanelListener == null) {
483
                        histogramPanelListener = new HistogramPanelListener(this);
484
                }
485
                return histogramPanelListener;
486
        }
487
        
488
        public void setHistogramableSource(IHistogramable lyr, String name) {
489
                Vector aux = new Vector();
490
                aux.add(lyr);
491
                aux.add(name);
492
                comboSource.add(aux);
493
                updateComboBoxSource();
494
        }
495

    
496
        public Vector getComboSource() {
497
                return comboSource;
498
        }
499

    
500
        private void updateComboBoxSource() {
501
                getJComboBoxOrigen().removeAllItems();
502
                for (int i = 0; i < comboSource.size(); i++) {
503
                        getJComboBoxOrigen().addItem(((Vector) comboSource.get(i)).get(1));
504
                }
505
                
506
        }
507

    
508
        public void clearSources() {
509
                comboSource = new Vector();
510
                updateComboBoxSource();
511
        }
512
}