Statistics
| Revision:

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

History | View | Annotate | Download (16.4 KB)

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

    
21
import java.awt.BorderLayout;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.util.ArrayList;
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 = getHistogramPanelListener();
55
        private ArrayList comboSource = new ArrayList();
56
        private boolean panelInizialited = false;
57

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

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

    
114
        /**
115
         * This method initializes this
116
         * 
117
         */
118
        private void initialize() {
119
                this.setLayout(new BorderLayout(5, 5));
120
    this.add(getCbSup(), java.awt.BorderLayout.NORTH);
121
    this.add(getGraphicContainer(), java.awt.BorderLayout.CENTER);
122
    this.add(getPTable(), java.awt.BorderLayout.SOUTH);
123
                this.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
124
                getHistogramPanelListener().setControlListeners();
125
        }
126

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

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

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

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

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

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

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

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

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

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

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

    
501
        public ArrayList getComboSource() {
502
                return comboSource;
503
        }
504

    
505
        private void updateComboBoxSource() {
506
                getHistogramPanelListener().comboEventEnable = false;
507
                getJComboBoxOrigen().removeAllItems();
508
                for (int i = 0; i < comboSource.size(); i++) {
509
                        getJComboBoxOrigen().addItem(((ArrayList) comboSource.get(i)).get(1));
510
                }
511
                getHistogramPanelListener().comboEventEnable = panelInizialited;
512
        }
513

    
514
        public void clearSources() {
515
                comboSource = new ArrayList();
516
                updateComboBoxSource();
517
        }
518
        
519
        public void firstRun() {
520
                panelInizialited = true;
521
                getHistogramPanelListener().comboEventEnable = true;
522
                getHistogramPanelListener().showHistogram();
523
        }
524
}