Statistics
| Revision:

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

History | View | Annotate | Download (15.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.GraphicContainer;
36
import org.gvsig.gui.beans.table.TableContainer;
37
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
38
import org.gvsig.raster.util.Histogram;
39
import org.gvsig.raster.util.IHistogramable;
40
import org.gvsig.raster.util.RasterUtilities;
41
import org.gvsig.rastertools.histogram.HistogramPanelListener;
42
/**
43
 * <code>HistogramPanel</code>. Interfaz de usuario para la representaci?n de
44
 * histogramas.
45
 * 
46
 * @version 20/03/2007
47
 * @author Diego Guerrero (diego.guerrero@uclm.es)
48
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
49
 */
50
public class HistogramPanel extends DefaultButtonsPanel {
51
        private static final long serialVersionUID = 2772897994667886753L;
52

    
53
        private HistogramPanelListener          histogramPanelListener = getHistogramPanelListener();
54
        private ArrayList comboSource = new ArrayList();
55
        private boolean panelInizialited = false;
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(true);
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
                getHistogramPanelListener().comboEventEnable = false;
278
                getJComboBoxOrigen().removeAllItems();
279
                getJComboBoxOrigen().addItem(Messages.getText("vista"));
280
                getHistogramPanelListener().comboEventEnable = panelInizialited;
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
                getHistogramPanelListener().comboEventEnable = false;
323
                if (jComboBands == null) {
324
                        String lista [] = {Messages.getText("todas")};
325
                        jComboBands = new JComboBox(lista);
326
                        jComboBands.addActionListener(getHistogramPanelListener());
327
                        jComboBands.setPreferredSize(new java.awt.Dimension(100,25));
328
                }
329
                getHistogramPanelListener().comboEventEnable = panelInizialited;
330
                return jComboBands;
331
        }
332
                
333
        /**
334
         * Asigna el n?mero de bandas al combobox
335
         * @param bands N?mero de bandas de la imagen
336
         */
337
        public void setBands(int bands){
338
                getHistogramPanelListener().comboEventEnable = false;
339
                getJComboBands().removeAllItems();
340
                getJComboBands().addItem(Messages.getText("todas"));
341
//                showBands = new boolean[bands];
342
                for(int i = 0; i < bands; i++){
343
                        getJComboBands().addItem("Band "+String.valueOf(i));
344
//                        showBands[i] = true;
345
                }
346
                getHistogramPanelListener().comboEventEnable = panelInizialited;
347
        }
348
        
349
        
350
        /**
351
         * Asigna la estadistica a la tabla
352
         * @param stat
353
         */
354
        public void setStatistic(long[][] stat){
355
                if(stat == null)
356
                        return;
357
                try {
358
                        getTableContainer().removeAllRows();
359
                        for(int iBand = 0; iBand < stat[0].length; iBand++){
360
                                Object[] list = new Object[stat.length + 1];
361
                                list[0] = new Integer(iBand);
362
                                for(int iStat = 1; iStat <= stat.length; iStat++)
363
                                        list[iStat] = new Long(stat[iStat - 1][iBand]);                                
364
                                        
365
                                getTableContainer().addRow(list);
366
                                list = null;
367
                        }
368
                } catch (NotInitializeException e) {
369
                        // TODO Auto-generated catch block
370
                        e.printStackTrace();
371
                }
372
        }
373
        
374
        /**
375
         * Resetea el control de bandas del panel con los valores RGB para 
376
         * cuando se est? haciendo el histograma de la visualizaci?n en
377
         * vez del histograma con los datos
378
         *
379
         */
380
        public void setRGBInBandList(){
381
                getHistogramPanelListener().comboEventEnable = false;
382
//                boolean[] list = {true, true, true};
383
//                showBands = list;
384
                getJComboBands().removeAllItems();
385
                getJComboBands().addItem(Messages.getText("todas"));
386
                getJComboBands().addItem("R");
387
                getJComboBands().addItem("G");
388
                getJComboBands().addItem("B");
389
                getHistogramPanelListener().comboEventEnable = panelInizialited;
390
        }
391
        
392
        /**
393
         * Obtiene el tipo de datos de la fuente de la imagen
394
         * @return tipo de datos de la fuente de la imagen
395
         */
396
        public int getHistogramDataSource() {
397
                return histogramDataSource;
398
        }
399

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

    
408
        /**
409
         * Obtiene el tipo de histograma a mostrar
410
         * @return acumulado/no acumulado
411
         */
412
        public int getType() {
413
                return type;
414
        }
415

    
416
        /**
417
         * Asigna el tipo de histograma a mostrar
418
         * @param type acumulado/no acumulado 
419
         */
420
        public void setType(int type) {
421
                this.type = type;
422
        }
423

    
424
        /**
425
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
426
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
427
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
428
         * y el segundo el de la izquierda.  
429
         */
430
        public double[] getBoxesValues(){
431
                double[] v = new double[2];
432
                double[] currentValues = new double[2];
433
                currentValues[0] = getGraphicContainer().getX2();
434
                currentValues[1] = getGraphicContainer().getX1();
435
                switch (requestDataType){
436
                        case RasterBuf.TYPE_BYTE:         v[0] = (currentValues[0] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100; 
437
                                v[1] = (currentValues[1] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100;
438
                                break;
439
                        case RasterBuf.TYPE_SHORT:         v[0] = (currentValues[0] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
440
                                v[1] = (currentValues[1] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
441
                                break;
442
                }
443
                return v;
444
        }
445
        
446
        public HistogramPanelListener getHistogramPanelListener() {
447
                if (histogramPanelListener == null) {
448
                        histogramPanelListener = new HistogramPanelListener(this);
449
                }
450
                return histogramPanelListener;
451
        }
452
        
453
        public void setHistogramableSource(IHistogramable lyr, String name) {
454
                getHistogramPanelListener().comboEventEnable = false;
455
                ArrayList aux = new ArrayList();
456
                aux.add(lyr);
457
                aux.add(name);
458
                comboSource.add(aux);
459
                updateComboBoxSource();
460
                getHistogramPanelListener().comboEventEnable = panelInizialited;
461
        }
462

    
463
        public ArrayList getComboSource() {
464
                return comboSource;
465
        }
466

    
467
        private void updateComboBoxSource() {
468
                getHistogramPanelListener().comboEventEnable = false;
469
                getJComboBoxOrigen().removeAllItems();
470
                for (int i = 0; i < comboSource.size(); i++) {
471
                        getJComboBoxOrigen().addItem(((ArrayList) comboSource.get(i)).get(1));
472
                }
473
                getHistogramPanelListener().comboEventEnable = panelInizialited;
474
        }
475

    
476
        public void clearSources() {
477
                comboSource = new ArrayList();
478
                updateComboBoxSource();
479
        }
480
        
481
        public void firstRun() {
482
                panelInizialited = true;
483
                getHistogramPanelListener().comboEventEnable = true;
484
                getHistogramPanelListener().showHistogram();
485
        }
486
}