Statistics
| Revision:

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

History | View | Annotate | Download (14 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.FlowLayout;
23
import java.util.ArrayList;
24

    
25
import javax.swing.JButton;
26
import javax.swing.JComboBox;
27
import javax.swing.JLabel;
28
import javax.swing.JPanel;
29
import javax.swing.JScrollPane;
30
import javax.swing.JTable;
31
import javax.swing.table.DefaultTableModel;
32

    
33
import org.cresques.filter.RasterBuf;
34
import org.cresques.i18n.Messages;
35
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
36
import org.gvsig.gui.beans.defaultbuttonspanel.DefaultButtonsPanel;
37
import org.gvsig.gui.beans.graphic.GraphicContainer;
38
import org.gvsig.gui.beans.table.TableContainer;
39
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
40
import org.gvsig.raster.util.Histogram;
41
import org.gvsig.raster.util.IHistogramable;
42
import org.gvsig.raster.util.RasterUtilities;
43
import org.gvsig.rastertools.histogram.HistogramPanelListener;
44
/**
45
 * <code>HistogramPanel</code>. Interfaz de usuario para la representaci?n de
46
 * histogramas.
47
 * 
48
 * @version 20/03/2007
49
 * @author Diego Guerrero (diego.guerrero@uclm.es)
50
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
51
 */
52
public class HistogramPanel extends DefaultButtonsPanel {
53
        private static final long serialVersionUID = 2772897994667886753L;
54

    
55
        /**
56
         * Objeto que controlara toda la carga pesada del panel
57
         */
58
        private HistogramPanelListener histogramPanelListener = null;
59

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

    
71
        /**
72
         * Componentes graficos
73
         */
74
        private GraphicContainer        graphicContainer = null;
75
        private JComboBox                                 jComboBoxOrigen = null;
76
        private JComboBox                                 jComboBoxTipo = null;
77
        private JButton                                         bTable = null;
78
        private TableContainer                tableContainer = null;
79
        private JPanel                                                 panelNorth = null;
80
        private JPanel                                                 panelSouth = null;
81

    
82
        private JTable                                                jTableBands = null;
83

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

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

    
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 getPanelSouth() {
134
                if (panelSouth == null) {
135
                        panelSouth = new JPanel();
136
                        panelSouth.setLayout(new BorderLayout(5,5));
137
                        panelSouth.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
                  panelSouth.add(jPanel2, BorderLayout.EAST);
145
                }
146
                return panelSouth;
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 getPanelNorth() {
179
                if (panelNorth == null) {
180
                        panelNorth = new JPanel();
181
                        JPanel jPanel3 = new JPanel();
182
                        JScrollPane jScrollPane1 = new JScrollPane();
183
                        JPanel jPanel4 = new JPanel();
184
                        JPanel jPanel5 = new JPanel();
185

    
186
                        jScrollPane1.setPreferredSize(new java.awt.Dimension(120,65));
187
                        jScrollPane1.setViewportView(getJTableBands());
188

    
189
                        panelNorth.setLayout(new BorderLayout());
190
                        panelNorth.add(jPanel3, BorderLayout.CENTER);
191
                        panelNorth.add(jScrollPane1, BorderLayout.EAST);
192
                        
193
                        jPanel3.setLayout(new BorderLayout());
194
                        jPanel3.add(jPanel4, BorderLayout.NORTH);
195
                        jPanel3.add(jPanel5, BorderLayout.SOUTH);
196
                        
197
                        jPanel4.setLayout(new FlowLayout());
198
                        jPanel4.add(new JLabel(Messages.getText("origen")+":"));
199
                        jPanel4.add(getJComboBoxOrigen());
200

    
201
                        jPanel5.setLayout(new FlowLayout());
202
                        jPanel5.add(new JLabel(Messages.getText("tipo")+":"));
203
                        jPanel5.add(getJComboBoxTipo());
204
                }
205
                return panelNorth;
206
        }
207

    
208
        /**
209
         * Obtiene el combo con la selecci?n de tipo de histograma, acumulado/no acumulado
210
         * @return javax.swing.JComboBox        
211
         */
212
        public JComboBox getJComboBoxTipo() {
213
                if (jComboBoxTipo == null) {
214
                        String lista [] = new String[Histogram.getHistogramTypesCount()];
215
                        for (int i = 0; i < lista.length; i++) {
216
                                lista[i] = Messages.getText(Histogram.getType(i));
217
                        }
218
                        jComboBoxTipo = new JComboBox(lista);
219
                        jComboBoxTipo.addActionListener(getHistogramPanelListener());
220
                        jComboBoxTipo.setPreferredSize(new java.awt.Dimension(150, 25));
221
                }
222
                return jComboBoxTipo;
223
        }
224

    
225
        /**
226
         * Obtiene el combo con la selecci?n de la fuente de datos en el calculo del histograma,
227
         * datos de la vista, datos reales con el extent de la vista e imagen completa.
228
         * @return javax.swing.JComboBox        
229
         */
230
        public JComboBox getJComboBoxOrigen() {
231
                if (jComboBoxOrigen == null) {
232
                        jComboBoxOrigen = new JComboBox();
233
                        jComboBoxOrigen.addActionListener(getHistogramPanelListener());
234
                        jComboBoxOrigen.setPreferredSize(new java.awt.Dimension(150,25));
235
                }
236
                return jComboBoxOrigen;
237
        }
238

    
239
        /**
240
         *Asigna el combo "Origen" el valor de solo vista para el calculo del histograma. Esa opci?n es 
241
         *utilizada para extensiones que necesitan histograma pero no pueden acceder a la fuente de datos.
242
         */
243
        public void setOnlyViewValue(){
244
                getHistogramPanelListener().eventsEnabled = false;
245
                getJComboBoxOrigen().removeAllItems();
246
                getJComboBoxOrigen().addItem(Messages.getText("vista"));
247
                getHistogramPanelListener().eventsEnabled = panelInizialited;
248
        }
249

    
250
        /**
251
         * This method initializes jButton        
252
         *         
253
         * @return javax.swing.JButton        
254
         */
255
        public JButton getBCreateTable() {
256
                if (bTable == null) {
257
                        bTable = new JButton();
258
                        bTable.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
259
                        bTable.setText(Messages.getText("crear_tabla"));
260
                        bTable.setName("bTable");
261
                        bTable.addActionListener(getHistogramPanelListener());
262
                        bTable.setPreferredSize(new java.awt.Dimension(150,25));
263
                }
264
                return bTable;
265
        }
266

    
267
        /**
268
         * This method initializes jComboBox
269
         *         
270
         * @return javax.swing.JComboBox        
271
         */
272
        public JTable getJTableBands() {
273
                getHistogramPanelListener().eventsEnabled = false;
274
                if (jTableBands == null) {
275
                        jTableBands = new JTable();
276
                        jTableBands.setModel(new javax.swing.table.DefaultTableModel(
277
          new Object [][] {},
278
          new String [] {
279
              " ", Messages.getText("bandas")
280
          }
281
      ) {
282
                                private static final long serialVersionUID = 0L;
283
                                        Class[] types = new Class [] {
284
              java.lang.Boolean.class, java.lang.String.class
285
          };
286
          boolean[] canEdit = new boolean [] {
287
              true, false
288
          };
289

    
290
          public Class getColumnClass(int columnIndex) {
291
              return types [columnIndex];
292
          }
293

    
294
          public boolean isCellEditable(int rowIndex, int columnIndex) {
295
              return canEdit [columnIndex];
296
          }
297
      });
298
                        jTableBands.addPropertyChangeListener(getHistogramPanelListener());
299
                        jTableBands.getColumnModel().getColumn(0).setPreferredWidth(20);
300
                        jTableBands.getColumnModel().getColumn(0).setResizable(false);
301
                }
302
                getHistogramPanelListener().eventsEnabled = panelInizialited;
303
                return jTableBands;
304
        }
305

    
306
        /**
307
         * Asigna el n?mero de bandas al combobox
308
         * @param bands N?mero de bandas de la imagen
309
         */
310
        public void setBands(int bands){
311
                getHistogramPanelListener().eventsEnabled = false;
312
                ((DefaultTableModel) getJTableBands().getModel()).setRowCount(0);
313
                for(int i = 0; i < bands; i++){
314
                        ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "Band "+String.valueOf(i)});
315
                }
316
                getHistogramPanelListener().eventsEnabled = panelInizialited;
317
        }
318

    
319
        /**
320
         * Asigna la estadistica a la tabla
321
         * @param stat
322
         */
323
        public void setStatistic(long[][] stat){
324
                if(stat == null)
325
                        return;
326
                try {
327
                        getTableContainer().removeAllRows();
328
                        for(int iBand = 0; iBand < stat[0].length; iBand++){
329
                                Object[] list = new Object[stat.length + 1];
330
                                list[0] = new Integer(iBand);
331
                                for(int iStat = 1; iStat <= stat.length; iStat++)
332
                                        list[iStat] = new Long(stat[iStat - 1][iBand]);                                
333
                                        
334
                                getTableContainer().addRow(list);
335
                                list = null;
336
                        }
337
                } catch (NotInitializeException e) {
338
                        // TODO Auto-generated catch block
339
                        e.printStackTrace();
340
                }
341
        }
342

    
343
        /**
344
         * Resetea el control de bandas del panel con los valores RGB para 
345
         * cuando se est? haciendo el histograma de la visualizaci?n en
346
         * vez del histograma con los datos
347
         *
348
         */
349
        public void setRGBInBandList(){
350
                getHistogramPanelListener().eventsEnabled = false;
351

    
352
                ((DefaultTableModel) getJTableBands().getModel()).setRowCount(0);
353
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "R"});
354
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "G"});
355
                ((DefaultTableModel) getJTableBands().getModel()).addRow(new Object[] { new Boolean(true), "B"});
356
                
357
                getHistogramPanelListener().eventsEnabled = panelInizialited;
358
        }
359

    
360
        /**
361
         * Obtiene el valor de los controles en el rango 0-255. Los controles dan un rango en tanto por cien 0-100
362
         * pero para el calculo de estadisticas necesitamos un rango de valor de pixel. 
363
         * @return Array con los valores de ambos controles. El primer valor del array es el control de la derecha
364
         * y el segundo el de la izquierda.  
365
         */
366
        public double[] getBoxesValues(){
367
                double[] v = new double[2];
368
                double[] currentValues = new double[2];
369
                currentValues[0] = getGraphicContainer().getX2();
370
                currentValues[1] = getGraphicContainer().getX1();
371
                switch (dataType){
372
                        case RasterBuf.TYPE_BYTE:
373
                                v[0] = (currentValues[0] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100; 
374
                                v[1] = (currentValues[1] * RasterUtilities.MAX_BYTE_BIT_VALUE) / 100;
375
                                break;
376
                        case RasterBuf.TYPE_SHORT:
377
                                v[0] = (currentValues[0] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
378
                                v[1] = (currentValues[1] * RasterUtilities.MAX_SHORT_BIT_VALUE) / 100;
379
                                break;
380
                        case RasterBuf.TYPE_FLOAT:
381
                        case RasterBuf.TYPE_DOUBLE:
382
                                v[0] = (currentValues[0] * 63) / 100;
383
                                v[1] = (currentValues[1] * 63) / 100;
384
                                break;
385
                }
386
                return v;
387
        }
388

    
389
        public HistogramPanelListener getHistogramPanelListener() {
390
                if (histogramPanelListener == null) {
391
                        histogramPanelListener = new HistogramPanelListener(this);
392
                }
393
                return histogramPanelListener;
394
        }
395

    
396
        public void setHistogramableSource(IHistogramable lyr, String name) {
397
                getHistogramPanelListener().eventsEnabled = false;
398
                ArrayList aux = new ArrayList();
399
                aux.add(lyr);
400
                aux.add(name);
401
                comboSource.add(aux);
402
                updateComboBoxSource();
403
                getHistogramPanelListener().eventsEnabled = panelInizialited;
404
        }
405

    
406
        public ArrayList getComboSource() {
407
                return comboSource;
408
        }
409

    
410
        private void updateComboBoxSource() {
411
                getHistogramPanelListener().eventsEnabled = false;
412
                getJComboBoxOrigen().removeAllItems();
413
                for (int i = 0; i < comboSource.size(); i++) {
414
                        getJComboBoxOrigen().addItem(((ArrayList) comboSource.get(i)).get(1));
415
                }
416
                getHistogramPanelListener().eventsEnabled = panelInizialited;
417
        }
418

    
419
        public void clearSources() {
420
                comboSource = new ArrayList();
421
                updateComboBoxSource();
422
        }
423

    
424
        public void refreshBands() {
425
                //En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
426
                int bandCount = getHistogramPanelListener().getLastHistogram().getNumBands();
427
                if ((getJComboBoxOrigen().getSelectedIndex() == 0) && (bandCount == 3)) {
428
                        setRGBInBandList();
429
                } else {
430
                        setBands(bandCount);
431
                }
432
        }
433

    
434
        public void firstRun() {
435
                getHistogramPanelListener().showHistogram();
436
        }
437

    
438
        public void setDataType(int dataType) {
439
                this.dataType = dataType;
440
        }
441

    
442
        public int getDataType() {
443
                return dataType;
444
        }
445
}