Statistics
| Revision:

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

History | View | Annotate | Download (8.07 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.IncrementableEvent;
31
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
32
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
33
import org.gvsig.raster.util.Histogram;
34
import org.gvsig.raster.util.IHistogramable;
35
import org.gvsig.rastertools.histogram.ui.HistogramPanel;
36

    
37
import com.iver.utiles.swing.threads.Cancellable;
38
/**
39
 * Listener para eventos del panel de histograma
40
 *
41
 * @version 20/03/2007
42
 * @author Nacho Brodin (brodin_ign@gva.es)
43
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
44
 */
45
public class HistogramPanelListener implements ActionListener, KeyListener, IncrementableListener {
46
        private HistogramPanel                histogramPanel = null;
47
        private IncrementableTask        incrementableTask = null;
48
        private HistogramProcess        histogramProcess = null;
49
        private Histogram                                        lastHistogram = null;
50
        private int                                                                bandCount = 0;
51

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

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

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

    
80

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

    
162
        public void showHistogram() {
163
                if (histogramPanel.getJComboBoxOrigen().getSelectedIndex() < 0) return;
164
                closeHistogramProcess();
165
                IHistogramable iaux = (IHistogramable) ((ArrayList) histogramPanel.getComboSource().get(histogramPanel.getJComboBoxOrigen().getSelectedIndex())).get(0);
166
                histogramProcess = new HistogramProcess(iaux);
167
                incrementableTask = new IncrementableTask(histogramProcess);
168
                incrementableTask.addIncrementableListener(this);
169
                incrementableTask.showWindow();
170
        }
171
        
172
        /**
173
         * Limpia la gr?fica
174
         */
175
        public void cleanChart(){
176
                histogramPanel.cleanChart();
177
                for(int i = 0; i < histogramPanel.showBands.length; i++)
178
                        histogramPanel.showBands[i] = false;
179
        }
180

    
181
        public void keyReleased(KeyEvent e) {
182
        }
183

    
184
        public void keyTyped(KeyEvent e) {
185
        }
186

    
187
        private void updateGraphic() {
188
                long[][] auxHistogram = lastHistogram.getHistogramByType(Histogram.getType(histogramPanel.getJComboBoxTipo().getSelectedIndex()));
189
                if (auxHistogram == null) return;
190

    
191
                int first = 0;
192
                int end = auxHistogram[0].length;
193

    
194
                first = (int) ((histogramPanel.getGraphicContainer().getBoxesValues()[1]*auxHistogram[0].length)/100);
195
                end = (int) ((histogramPanel.getGraphicContainer().getBoxesValues()[0]*auxHistogram[0].length)/100);
196

    
197
                int bands = (histogramPanel.getJComboBands().getSelectedIndex()==0?auxHistogram.length:1);
198

    
199
                int[][] newHistogram = new int[bands][end - first];
200
                String[] bandNames = new String[bands];
201
                
202
                bandCount = auxHistogram.length;
203

    
204
                for (int iBand=0; iBand<bands; iBand++) {
205
                        for (int j=first; j<end; j++)
206
                                newHistogram[iBand][j-first] = (int) auxHistogram[(bands>1?iBand:histogramPanel.getJComboBands().getSelectedIndex()-1)][j];
207
                        bandNames[iBand] = iBand + "";
208
                }
209

    
210
                histogramPanel.getGraphicContainer().getPGraphic().cleanChart();
211
                histogramPanel.getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames);
212
        }
213

    
214
        private void closeHistogramProcess() {
215
                histogramProcess = null;
216
                incrementableTask = null;
217
        }
218

    
219
        public void actionCanceled(IncrementableEvent e) {
220
//                if (histogramProcess != null) histogramProcess.suspend();                
221
                closeHistogramProcess();
222
        }
223
        
224
        public void actionClosed(IncrementableEvent e) {
225
                if (histogramProcess == null) return;
226
                lastHistogram = histogramProcess.getLastHistogram();
227
                updateGraphic();
228
                closeHistogramProcess();
229
        }
230

    
231
        public void actionResumed(IncrementableEvent e) {
232
                if (histogramProcess == null) return;
233
                histogramProcess.resume();
234
        }
235

    
236
        public void actionSuspended(IncrementableEvent e) {
237
                if (histogramProcess == null) return;
238
                histogramProcess.suspend();
239
        }
240
}