Statistics
| Revision:

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

History | View | Annotate | Download (14.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
        /**
54
         * Objeto que controlara toda la carga pesada del panel
55
         */
56
        private HistogramPanelListener histogramPanelListener = null;
57

    
58
        /**
59
         * Array para la seleccion del origen
60
         */
61
        private ArrayList comboSource = new ArrayList();
62
        
63
        /**
64
         * Variable para controlar si se dispararan los eventos del panel,
65
         * si es la primera vez no tiene porque disparar eventos.
66
         */
67
        public boolean panelInizialited = false;
68

    
69
        /**
70
         * Componentes graficos
71
         */
72
        private GraphicContainer        graphicContainer = null;
73
        private JPanel                                                 pTable = null;
74
        private JComboBox                                 jComboBoxOrigen = null;
75
        private JComboBox                                 jComboBoxTipo = null;
76
        private JComboBox                                 jComboBands = null;
77
        private JButton                                         jButtonClear = null;
78
        private JButton                                         bTable = null;
79
        private TableContainer                tableContainer = null;
80
        private JPanel                                                 cbSup = null;
81
        private JLabel                                                 lOrigin = null;
82
        private JLabel                                                 lBands = null;
83
        private JLabel                                                 lType = null;
84

    
85
        /**
86
         * Tipo de dato de la petici?n. Si la petici?n del histograma es de la vista el tipo
87
         * de dato ser? byte aunque la imagen sea de 16 bits. Si la petici?n es de los datos 
88
         * reales de la imagen entonces el tipo de dato de la petici?n coincide con el tipo
89
         * de datos de la imagen.
90
         */
91
        private int                                                        requestDataType = RasterBuf.TYPE_BYTE;
92
        
93
        /**
94
         * Crea un dialogo para los histogramas.
95
         *
96
         */
97
        public HistogramPanel() {
98
                super(ButtonsPanel.BUTTONS_CLOSE);
99
                histogramPanelListener = getHistogramPanelListener();
100
                initialize();
101
        }
102

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

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

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

    
139
                        JPanel jPanel2 = new JPanel();
140
      jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.Y_AXIS));
141
      jPanel2.add(getBCreateTable());
142
                  jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
143

    
144
                        pTable.add(jPanel2, BorderLayout.EAST);
145
                }
146
                return pTable;
147
        }
148

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

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

    
378
        /**
379
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
380
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
381
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
382
         * y el segundo el de la izquierda.  
383
         */
384
        public double[] getBoxesValues(){
385
                double[] v = new double[2];
386
                double[] currentValues = new double[2];
387
                currentValues[0] = getGraphicContainer().getX2();
388
                currentValues[1] = getGraphicContainer().getX1();
389
                switch (requestDataType){
390
                        case RasterBuf.TYPE_BYTE:
391
                                v[0] = (currentValues[0] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100; 
392
                                v[1] = (currentValues[1] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100;
393
                                break;
394
                        case RasterBuf.TYPE_SHORT:
395
                                v[0] = (currentValues[0] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
396
                                v[1] = (currentValues[1] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
397
                                break;
398
                }
399
                return v;
400
        }
401
        
402
        public HistogramPanelListener getHistogramPanelListener() {
403
                if (histogramPanelListener == null) {
404
                        histogramPanelListener = new HistogramPanelListener(this);
405
                }
406
                return histogramPanelListener;
407
        }
408
        
409
        public void setHistogramableSource(IHistogramable lyr, String name) {
410
                getHistogramPanelListener().eventsEnabled = false;
411
                ArrayList aux = new ArrayList();
412
                aux.add(lyr);
413
                aux.add(name);
414
                comboSource.add(aux);
415
                updateComboBoxSource();
416
                getHistogramPanelListener().eventsEnabled = panelInizialited;
417
        }
418

    
419
        public ArrayList getComboSource() {
420
                return comboSource;
421
        }
422

    
423
        private void updateComboBoxSource() {
424
                getHistogramPanelListener().eventsEnabled = false;
425
                getJComboBoxOrigen().removeAllItems();
426
                for (int i = 0; i < comboSource.size(); i++) {
427
                        getJComboBoxOrigen().addItem(((ArrayList) comboSource.get(i)).get(1));
428
                }
429
                getHistogramPanelListener().eventsEnabled = panelInizialited;
430
        }
431

    
432
        public void clearSources() {
433
                comboSource = new ArrayList();
434
                updateComboBoxSource();
435
        }
436
        
437
        public void refreshBands() {
438
                //En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
439
                int bandCount = getHistogramPanelListener().getLastHistogram().getNumBands();
440
                if ((getJComboBoxOrigen().getSelectedIndex() == 0) && (bandCount == 3)) {
441
                        setRGBInBandList();
442
                } else {
443
                        setBands(bandCount);
444
                }
445
        }
446
        
447
        public void firstRun() {
448
                getHistogramPanelListener().showHistogram();
449
        }
450
}