Statistics
| Revision:

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

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

    
49
        private HistogramPanelListener          histogramPanelListener = null;
50
        
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
         * Crea un dialogo para los histogramas.
93
         *
94
         */
95
        public HistogramPanel() {
96
                super(ButtonsPanel.BUTTONS_CLOSE);
97
                initialize();
98
        }
99

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

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

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

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

    
141
                        pTable.add(jPanel2, BorderLayout.EAST);
142
                }
143
                return pTable;
144
        }
145

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

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

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

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

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

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

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