Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / HistogramPanelListener.java @ 11004

History | View | Annotate | Download (7.4 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.util.ArrayList;
25

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

    
29
import org.gvsig.gui.beans.graphic.GraphicEvent;
30
import org.gvsig.gui.beans.graphic.GraphicListener;
31
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
32
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
33
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
34
import org.gvsig.raster.util.Histogram;
35
import org.gvsig.raster.util.IHistogramable;
36
import org.gvsig.rastertools.histogram.ui.HistogramPanel;
37
/**
38
 * Listener para eventos del panel de histograma
39
 *
40
 * @version 20/03/2007
41
 * @author Nacho Brodin (brodin_ign@gva.es)
42
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
43
 */
44
public class HistogramPanelListener implements IncrementableListener, GraphicListener, ActionListener {
45
        private HistogramPanel                histogramPanel = null;
46
        private IncrementableTask        incrementableTask = null;
47
        private HistogramProcess        histogramProcess = null;
48
        private Histogram                                        lastHistogram = null;
49
        private int                                                                bandCount = 0;
50

    
51
        public boolean                                                comboEventEnable = false;
52
        
53
        private boolean[]                                        showBands = {true, true, true};
54

    
55
                
56
        public HistogramPanelListener(HistogramPanel p){
57
                histogramPanel = p;
58
        }
59
        
60
        public void setControlListeners(){
61
                histogramPanel.getGraphicContainer().addValueChangedListener(this);
62
        }
63

    
64
        
65
        private void updateStatistic() {
66
//                TODO Actualizar tabla de los histogramas.
67
//                histogramPanel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
68
        }
69

    
70
        public void actionPerformed(ActionEvent e) {
71
                //--------------------------------------
72
                //Cambiar las bandas en el combo
73
                JComboBox cbb = histogramPanel.getJComboBands();
74
                if (comboEventEnable && (e.getSource() == cbb)) {
75
                        if (cbb.getSelectedItem() == null) return;
76
                        
77
                        if (cbb.getSelectedIndex() == 0)
78
                                for (int i = 0; i < showBands.length; i++)
79
                                        showBands[i] = true;
80
                        if (cbb.getSelectedItem().equals("R")) addOrRemoveGraphicBand(0);
81
                        if (cbb.getSelectedItem().equals("G")) addOrRemoveGraphicBand(1);
82
                        if (cbb.getSelectedItem().equals("B")) addOrRemoveGraphicBand(2);
83
                        for (int i = 0; i < HistogramPanel.MAXBANDS; i++)
84
                                if (cbb.getSelectedItem().equals("Band "+i))
85
                                        addOrRemoveGraphicBand(i);
86

    
87
                        updateStatistic();
88
                        updateGraphic();
89
                        return;
90
                }
91
                
92
                //--------------------------------------                
93
                //Limpiar
94
                JButton clean = histogramPanel.getJButtonClear();
95
                if (e.getSource() == clean) {
96
                        cleanChart();
97
                        return;
98
                }
99
                
100
                //--------------------------------------
101
                //Selecci?n de fuente de datos del histograma
102
                JComboBox cbo = histogramPanel.getJComboBoxOrigen();
103
                if (comboEventEnable && e.getSource() == cbo) {
104
                        //En caso de que el histograma se monte a partir de los datos de la vista ponemos RGB en el combo
105
                        if(cbo.getSelectedIndex() == 0) {
106
                                histogramPanel.setRGBInBandList();
107
                        }
108
                        
109
                        //En caso de que el histograma se monte a partir de los datos reales ponemos el n?mero de bandas en el combo
110
                        if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2) {
111
                                // TODO: Rellenar BandCount con el valor que toca
112
                                histogramPanel.setBands(bandCount);
113
                                showBands = new boolean[bandCount];
114
                                for (int i = 0; i < showBands.length; i++)
115
                                        showBands[i] = true;
116
                        }
117
                        showHistogram();
118
                        return;
119
                }
120
                                
121
                //--------------------------------------
122
                //Selecci?n de histograma acumulado y no acumulado
123
                JComboBox cbt = histogramPanel.getJComboBoxTipo();
124
                if (comboEventEnable && e.getSource() == cbt) {
125
                        histogramPanel.setType(cbt.getSelectedIndex());
126
                        updateStatistic();
127
                        updateGraphic();
128
                        return;
129
                }
130
        }
131

    
132
        public void showHistogram() {
133
                if (histogramPanel.getJComboBoxOrigen().getSelectedIndex() < 0) return;
134
                closeHistogramProcess();
135
                IHistogramable iaux = (IHistogramable) ((ArrayList) histogramPanel.getComboSource().get(histogramPanel.getJComboBoxOrigen().getSelectedIndex())).get(0);
136
                histogramProcess = new HistogramProcess(iaux);
137
                incrementableTask = new IncrementableTask(histogramProcess);
138
                incrementableTask.addIncrementableListener(this);
139
                incrementableTask.showWindow();
140
        }
141
        
142
        /**
143
         * A?ade o elimina una banda de la visualizaci?n. Si la banda se est? visualizando
144
         * se elimina y si no entonces se muestra
145
         * @param band banda a visualizar o borrar del gr?fico
146
         */
147
        public void addOrRemoveGraphicBand(int band){
148
                if (band > showBands.length)
149
                        return;
150
                showBands[band] = !showBands[band];
151
        }
152

    
153
        /**
154
         * Limpia la gr?fica
155
         */
156
        public void cleanChart(){
157
                histogramPanel.cleanChart();
158
                for(int i = 0; i < showBands.length; i++)
159
                        showBands[i] = false;
160
        }
161

    
162
        private void updateGraphic() {
163
                long[][] auxHistogram = lastHistogram.getHistogramByType(Histogram.getType(histogramPanel.getJComboBoxTipo().getSelectedIndex()));
164
                if (auxHistogram == null) return;
165

    
166
                int first = 0;
167
                int end = auxHistogram[0].length;
168

    
169
                first = (int) ((histogramPanel.getGraphicContainer().getX1()*auxHistogram[0].length)/100);
170
                end = (int) ((histogramPanel.getGraphicContainer().getX2()*auxHistogram[0].length)/100);
171

    
172
                int bandCount = 0;
173
                for (int i = 0; i < showBands.length; i++) {
174
                        if (showBands[i]) bandCount++;
175
                }
176

    
177
                int[][] newHistogram = new int[bandCount][end - first];
178
                String[] bandNames = new String[bandCount];
179
                
180
                bandCount = auxHistogram.length;
181

    
182
                int numBand = 0;
183
                for (int iBand = 0; iBand < showBands.length; iBand++) {
184
                        if (!showBands[iBand]) continue;
185
                        
186
                        for (int j=first; j<end; j++)
187
                                newHistogram[numBand][j-first] = (int) auxHistogram[iBand][j];
188
                        bandNames[numBand] = iBand + "";
189

    
190
                        numBand++;
191
                }
192

    
193
                histogramPanel.getGraphicContainer().getPGraphic().cleanChart();
194
                histogramPanel.getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames);
195
        }
196

    
197
        private void closeHistogramProcess() {
198
                histogramProcess = null;
199
                incrementableTask = null;
200
        }
201

    
202
        public void actionCanceled(IncrementableEvent e) {
203
//                if (histogramProcess != null) histogramProcess.suspend();                
204
                closeHistogramProcess();
205
        }
206
        
207
        public void actionClosed(IncrementableEvent e) {
208
                if (histogramProcess == null) return;
209
                lastHistogram = histogramProcess.getLastHistogram();
210
                updateGraphic();
211
                closeHistogramProcess();
212
        }
213

    
214
        public void actionResumed(IncrementableEvent e) {
215
                if (histogramProcess == null) return;
216
                histogramProcess.resume();
217
        }
218

    
219
        public void actionSuspended(IncrementableEvent e) {
220
                if (histogramProcess == null) return;
221
                histogramProcess.suspend();
222
        }
223

    
224
        public void actionValueChanged(GraphicEvent e) {
225
                updateStatistic();
226
                updateGraphic();
227
        }
228
}