Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / HistogramPanelListener.java @ 10963

History | View | Annotate | Download (10.6 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 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;
20

    
21
import java.awt.event.ActionEvent;
22
import java.awt.event.ActionListener;
23
import java.awt.event.KeyEvent;
24
import java.awt.event.KeyListener;
25
import java.util.ArrayList;
26

    
27
import javax.swing.JButton;
28
import javax.swing.JComboBox;
29

    
30
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
31
import org.gvsig.raster.dataset.properties.DatasetStatistics;
32
import org.gvsig.raster.util.IHistogramable;
33
import org.gvsig.rastertools.histogram.ui.HistogramPanel;
34
/**
35
 * Listener para eventos del panel de histograma
36
 *
37
 * @version 20/03/2007
38
 * @author Nacho Brodin (brodin_ign@gva.es)
39
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
40
 */
41
public class HistogramPanelListener implements ActionListener, KeyListener {
42

    
43
        private HistogramPanel                        panel = null;
44
        private IncrementableTask incrementableTask = null;
45
        /**
46
         * Objeto histograma para la gesti?n de procesos de histograma
47
         */
48
//        private        Histogram                                histogram = null;
49
        /**
50
         * ?ltimo histograma visualizado en el gr?fico
51
         */
52
        private int[][]                                                lastHistogram = null;
53

    
54
        public static boolean                         comboEventEnable = true;
55
                
56
        public HistogramPanelListener(HistogramPanel p){
57
                panel = p;
58
        }
59
        
60
        public void setControlListeners(){
61
                panel.getGraphicContainer().getPlusButtonControlLeft().addActionListener(this);
62
                panel.getGraphicContainer().getPlusButtonControlRight().addActionListener(this);
63
                panel.getGraphicContainer().getLessButtonControlLeft().addActionListener(this);
64
                panel.getGraphicContainer().getLessButtonControlRight().addActionListener(this);
65
                panel.getGraphicContainer().getTextControlRight().addKeyListener(this);
66
                panel.getGraphicContainer().getTextControlLeft().addKeyListener(this);
67
        }
68

    
69
        
70
        public void keyPressed(KeyEvent e) {
71
                if (e.getKeyCode() == 10) {
72
                        updateStatistic();
73
                }
74
        }
75

    
76
        private void updateStatistic() {
77
                // TODO Actualizar tabla de los histogramas.
78
                panel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
79
        }
80

    
81
        public void actionPerformed(ActionEvent e) {
82
                //Botones de m?s y menos
83
                if (e.getSource() == panel.getGraphicContainer().getPlusButtonControlLeft()  ||
84
                                e.getSource() == panel.getGraphicContainer().getLessButtonControlLeft()  ||
85
                                e.getSource() == panel.getGraphicContainer().getPlusButtonControlRight() ||
86
                                e.getSource() == panel.getGraphicContainer().getLessButtonControlRight() ) {
87
                        updateStatistic();
88
                        return;
89
                }
90
                
91
                //--------------------------------------
92
                //Cambiar las bandas en el combo
93
                JComboBox cbb = panel.getJComboBands();
94
                if (comboEventEnable && e.getSource() == cbb) {
95
                        if(cbb.getSelectedIndex() == 0) {
96
                                boolean[] list = panel.getBandListShowInChart();
97
                                for (int i = 0; i < list.length; i++)
98
                                        list[i] = true;
99
                                panel.setBandListShowInChart(list);
100
                        } else if(cbb.getSelectedItem().equals("R") || cbb.getSelectedItem().equals("Band 0"))
101
                                panel.addOrRemoveGraphicBand(0);
102
                        else if(cbb.getSelectedItem().equals("G") || cbb.getSelectedItem().equals("Band 1"))
103
                                panel.addOrRemoveGraphicBand(1);
104
                        else if(cbb.getSelectedItem().equals("B") || cbb.getSelectedItem().equals("Band 2"))
105
                                panel.addOrRemoveGraphicBand(2);
106
                        else {
107
                                for (int i = 3; i < HistogramPanel.MAXBANDS; i++) {
108
                                        if (cbb.getSelectedItem().equals("Band "+i))
109
                                                panel.addOrRemoveGraphicBand(i);
110
                                }
111
                        }
112
                        return;
113
                }
114
                
115
                //--------------------------------------                
116
                //Limpiar
117
                JButton clean = panel.getJButtonClear();
118
                if (e.getSource() == clean) {
119
                        panel.cleanChart();
120
                        return;
121
                }
122
                
123
                //--------------------------------------
124
                //Selecci?n de fuente de datos del histograma
125
                JComboBox cbo = panel.getJComboBoxOrigen();
126
                if (comboEventEnable && e.getSource() == cbo) {
127
//                        showHistogram();
128
//                        panel.setHistogramDataSource(cbo.getSelectedIndex());
129
                        
130
                        //En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
131
//                        if(cbo.getSelectedIndex() == 0)
132
//                                panel.setRGBInBandList();
133
                        
134
                        //En caso de que el histograma se monte a partir de los datos reales ponemos el n?mero de bandas en el combo
135
//                        if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2)
136
//                                panel.setBands(getHistogram().getGrid().getGrid().getBandCount());                        
137
                }
138
                                
139
                //--------------------------------------
140
                //Selecci?n de histograma acumulado y no acumulado
141
                JComboBox cbt = panel.getJComboBoxTipo();
142
                if (comboEventEnable && e.getSource() == cbt) {
143
                        panel.setType(cbt.getSelectedIndex());
144
                }
145
        
146
//                if(comboEventEnable)
147
//                        panel.showHistogram();
148

    
149
        }
150

    
151
        /**
152
         * Obtiene el ?ltimo histograma visualizado
153
         * @return
154
         */
155
        public int[][] getLastHistogram() {
156
                return lastHistogram;
157
        }
158

    
159
        /**
160
         * Asigna el objeto histograma para la gesti?n de procesos de histograma
161
         * @param histogramObj
162
         */
163
/*
164
        public void setHistogram(Histogram histogram) {
165
                this.histogram = histogram;
166
        }
167
*/
168
        /**
169
         * M?todo que ejecuta el thread que comprueba cuando se ha acabado de generar
170
         * el histograma. Al finalizar, se lee el histograma de la imagen completa 
171
         * calculado y se muestra.
172
         */
173
/*
174
        public void readFullHistogramFromThread(){
175
                if (panel.getType() == 0)
176
                        lastHistogram = getHistogram().getFullHistogram();
177
                else if(panel.getType() == 1)
178
                        lastHistogram = getHistogram().getFullAccumulatedHistogram();
179

180
                this.updateStatistic();
181

182
                //Si el histograma es grande lo recortamos para mostrarlo porque jfreeChart se vuelve
183
                //inestable al poner muchos elementos en las X
184
                
185
/*
186

187
                 int pos = lastHistogram[0].length;
188
                int[][] newHistogram = lastHistogram;
189
                boolean elem = true;
190
                if(lastHistogram[0].length >= Utilities.MAX_SHORT_BIT_VALUE){
191
                        for(int i = (lastHistogram[0].length - 1); i >= 0; i--){
192
                                for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
193
                                        if(lastHistogram[iBand][i] != 0)
194
                                                elem = false;
195
                                }
196
                                if(!elem){
197
                                        pos = i;
198
                                        break;
199
                                }
200
                        }
201
                        newHistogram = new int[lastHistogram.length][pos];
202
                        for(int iBand = 0; iBand < lastHistogram.length; iBand ++){
203
                                for(int i = 0; i < newHistogram[iBand].length; i ++){
204
                                        newHistogram[iBand][i] = lastHistogram[iBand][i];
205
                                }
206
                        }
207
                }
208
*/
209
/*
210
                GraphicChartPanel gcp = pHistogram.getPGraphic();
211
                
212
                String bandNames[] = new String[lastHistogram.length];
213
                for (int iBand = 0; iBand<lastHistogram.length; iBand++)
214
                        bandNames[iBand]=this.getJComboBands().getItemAt(iBand+1).toString();
215
                
216
                gcp.setNewChart(newHistogram, bandNames);
217
*/
218
//        }
219
        
220
        public void showHistogram() {
221
                IHistogramable iaux = (IHistogramable) ((ArrayList) panel.getComboSource().get(panel.getJComboBoxOrigen().getSelectedIndex())).get(0);
222
                HistogramProcess histogramProcess = new HistogramProcess(iaux);
223
                incrementableTask = new IncrementableTask(histogramProcess);
224
                incrementableTask.showWindow();
225
        }
226
/*
227
        public void showHistogram(){
228
                // Tipo noacumulado = 0, acumulado = 1
229
                switch (panel.getType()) {
230
                        case 0:
231
                                // Fte de datos vista = 0, extent = 1, completo = 2
232
                                switch (panel.getHistogramDataSource()) {
233
                                        case 0:
234
                                                requestDataType = RasterBuf.TYPE_BYTE;
235
                                                calcHistogramFromView(panel.showBands); 
236
                                                break;
237
                                        case 1:
238
                                                if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
239
                                                        requestDataType = RasterBuf.TYPE_SHORT;
240
                                                else
241
                                                        requestDataType = RasterBuf.TYPE_BYTE;        
242
                                                calcHistogramFromDataSourceAndExtentView(panel.showBands);
243
                                                break;
244
                                        case 2:
245
                                                if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
246
                                                        requestDataType = RasterBuf.TYPE_SHORT;
247
                                                else
248
                                                        requestDataType = RasterBuf.TYPE_BYTE;
249
                                                calcFullHistogramFromDataSource(panel.showBands);        
250
                                                break;
251
                                }
252
                                break;
253
                        case 1:
254
                                // Fte de datos vista = 0, extent = 1, completo = 2
255
                                switch (panel.getHistogramDataSource()) {
256
                                        case 0:
257
                                                requestDataType = RasterBuf.TYPE_BYTE;
258
                                                calcAccumulatedHistogramFromView(panel.showBands); 
259
                                                break;
260
                                        case 1:
261
                                                if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
262
                                                        requestDataType = RasterBuf.TYPE_SHORT;
263
                                                else
264
                                                        requestDataType = RasterBuf.TYPE_BYTE;
265
                                                calcHistogramFromDataSourceAndExtentView(panel.showBands);
266
                                                break;
267
                                        case 2:
268
                                                if(this.getHistogram().getGrid().getDataType() == RasterBuf.TYPE_SHORT)
269
                                                        requestDataType = RasterBuf.TYPE_SHORT;
270
                                                else
271
                                                        requestDataType = RasterBuf.TYPE_BYTE;
272
                                                calcFullAccumulatedHistogramFromDataSource(panel.showBands);
273
                                                break;
274
                                }
275
                                break;
276
                }
277
        }
278
*/
279
        /**
280
         * Obtiene el objeto histograma para la gesti?n de procesos de histograma
281
         * @return
282
         */
283
/*
284
        public Histogram getHistogram() {
285
                return histogram;
286
        }
287
*/
288
        
289
        public void keyReleased(KeyEvent e) {
290
        }
291

    
292
        public void keyTyped(KeyEvent e) {
293
        }
294

    
295
        /**
296
         * Calcula el histograma completo
297
         * @param bandas que se desean visualizar. Cada elemento del vector es una banda y el boolean, si
298
         * est? a true se visualizar? esa banda y si est? a false no se visualizar?
299
         */
300
/*
301
        private void calcFullHistogramFromDataSource(boolean[] bands){
302
                if (histogram != null){
303
                        histogram.calcFullHistogram(bands);
304
                        incrementPanel = new HistogramIncrement(this);        
305
                        incrementPanel.showWindow();
306
                }
307
        }
308
*/
309
        
310
        /**
311
         * Calcula el histograma de la vista y solo con los datos de visualizaci?n
312
         * de las bandas R, G o B solicitadas
313
         * @param bandas que se desean visualizar. Cada elemento del vector es una banda y el boolean, si
314
         * est? a true se visualizar? esa banda y si est? a false no se visualizar?
315
         */
316
/*
317
        private void calcHistogramFromView(boolean[] bands){
318
                if(histogram != null){
319
                        lastHistogram = histogram.calcHistogramFromView(bands);
320
                        panel.showBands = bands;
321
                        updateStatistic();
322

323
                        GraphicChartPanel gcp = panel.getGraphicContainer().getPGraphic();
324
                        
325
                        String bandNames[] = new String[bands.length];
326
                        for (int iBand = 0; iBand<bands.length; iBand++)
327
                                bandNames[iBand] = panel.getJComboBands().getItemAt(iBand+1).toString();
328
                        
329
                        gcp.setNewChart(lastHistogram, bandNames);
330
                }
331
        }
332
*/
333
}