Statistics
| Revision:

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

History | View | Annotate | Download (10.8 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.Vector;
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
                System.out.println("Botones + o - apretados, actualizar tabla");
79
                panel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
80
        }
81

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

    
153
        }
154

    
155
        /**
156
         * Obtiene el ?ltimo histograma visualizado
157
         * @return
158
         */
159
        public int[][] getLastHistogram() {
160
                return lastHistogram;
161
        }
162

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

184
                this.updateStatistic();
185

186
                //Si el histograma es grande lo recortamos para mostrarlo porque jfreeChart se vuelve
187
                //inestable al poner muchos elementos en las X
188
                
189
/*
190

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

    
296
        public void keyTyped(KeyEvent e) {
297
        }
298

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

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