Statistics
| Revision:

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

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

    
48
        private HistogramPanelListener          histogramPanelListener = null;
49

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

    
98
        /**
99
         * This method initializes this
100
         * 
101
         */
102
        private void initialize() {
103
                this.setLayout(new BorderLayout(5, 5));
104
    this.add(getCbSup(), java.awt.BorderLayout.NORTH);
105
    this.add(getGraphicContainer(), java.awt.BorderLayout.CENTER);
106
    this.add(getPTable(), java.awt.BorderLayout.SOUTH);
107
                this.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
108
                getHistogramPanelListener().setControlListeners();
109
        }
110

    
111
        /**
112
         * This method initializes jPanel        
113
         *         
114
         * @return javax.swing.JPanel        
115
         */
116
        public GraphicContainer getGraphicContainer() {
117
                if (graphicContainer == null) {
118
                        graphicContainer = new GraphicContainer( false);
119
                }
120
                return graphicContainer;
121
        }
122

    
123
        /**
124
         * This method initializes jPanel        
125
         *         
126
         * @return javax.swing.JPanel        
127
         */
128
        private JPanel getPTable() {
129
                if (pTable == null) {
130
                        pTable = new JPanel();
131
                        pTable.setLayout(new BorderLayout(5,5));
132
                        pTable.add(getTableContainer(), BorderLayout.CENTER);
133

    
134
                        JPanel jPanel2 = new JPanel();
135
      jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
136
      jPanel2.add(getBCreateTable());
137
                  jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
138

    
139
                        pTable.add(jPanel2, BorderLayout.EAST);
140
                }
141
                return pTable;
142
        }
143

    
144
        /**
145
         * Obtiene la tabla de estadisticas
146
         * @return
147
         */
148
        public TableContainer getTableContainer(){
149
                if(tableContainer == null){
150
                        String[] columnNames = {Messages.getText("banda"), Messages.getText("minimo"), Messages.getText("maximo"), Messages.getText("media"), Messages.getText("mediana"), Messages.getText("npixeles")};
151
                        int[] columnWidths = {50, 65, 65, 65, 65, 115};
152
                        tableContainer = new TableContainer(columnNames, columnWidths);
153
                        tableContainer.setControlVisible(true);
154
                        tableContainer.setModel("ListModel");
155
                        tableContainer.initialize();
156
                        tableContainer.setName("tableContainer");
157
                        tableContainer.setControlVisible(false);
158
                        
159
                        try{
160
                                tableContainer.setEditable(false);
161
                        }catch(NotInitializeException ex){
162
                                System.out.println("Tabla no inicializada");
163
                        }        
164
                }
165
                return tableContainer;
166
        }
167

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

    
402
        /**
403
         * Asigna el tipo de datos de la fuente de la imagen
404
         * @param histogramDataSource tipo de datos de la fuente de la imagen
405
         */
406
        public void setHistogramDataSource(int histogramDataSource) {
407
                this.histogramDataSource = histogramDataSource;
408
        }
409

    
410
        /**
411
         * Obtiene la lista de bandas que se muestran en el histograma
412
         * @return Lista de bandas donde cada elemento es un booleano. True si la banda
413
         * se muestra y false si no se muestra.
414
         */
415
        public boolean[] getBandListShowInChart(){
416
                return showBands;
417
        }
418

    
419
        /**
420
         * Asigna la lista de bandas que se muestran en el histograma
421
         * @param Lista de bandas donde cada elemento es un booleano. True si la banda
422
         * se muestra y false si no se muestra.
423
         */
424
        public void setBandListShowInChart(boolean[] showBands) {
425
                this.showBands = showBands;
426
        }
427
        
428
        /**
429
         * Obtiene el tipo de histograma a mostrar
430
         * @return acumulado/no acumulado
431
         */
432
        public int getType() {
433
                return type;
434
        }
435

    
436
        /**
437
         * Asigna el tipo de histograma a mostrar
438
         * @param type acumulado/no acumulado 
439
         */
440
        public void setType(int type) {
441
                this.type = type;
442
        }
443

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