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 10950 bsanchez
/* 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 10963 bsanchez
import java.util.ArrayList;
25 10950 bsanchez
26
import javax.swing.JButton;
27
import javax.swing.JComboBox;
28
29 11004 bsanchez
import org.gvsig.gui.beans.graphic.GraphicEvent;
30
import org.gvsig.gui.beans.graphic.GraphicListener;
31 10970 bsanchez
import org.gvsig.gui.beans.incrementabletask.IncrementableEvent;
32
import org.gvsig.gui.beans.incrementabletask.IncrementableListener;
33 10950 bsanchez
import org.gvsig.gui.beans.incrementabletask.IncrementableTask;
34 10981 bsanchez
import org.gvsig.raster.util.Histogram;
35 10950 bsanchez
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 11004 bsanchez
public class HistogramPanelListener implements IncrementableListener, GraphicListener, ActionListener {
45 10981 bsanchez
        private HistogramPanel                histogramPanel = null;
46
        private IncrementableTask        incrementableTask = null;
47
        private HistogramProcess        histogramProcess = null;
48 10997 bsanchez
        private Histogram                                        lastHistogram = null;
49
        private int                                                                bandCount = 0;
50 10950 bsanchez
51 10981 bsanchez
        public boolean                                                comboEventEnable = false;
52 11004 bsanchez
53
        private boolean[]                                        showBands = {true, true, true};
54
55 10950 bsanchez
56
        public HistogramPanelListener(HistogramPanel p){
57 10981 bsanchez
                histogramPanel = p;
58 10950 bsanchez
        }
59
60
        public void setControlListeners(){
61 11004 bsanchez
                histogramPanel.getGraphicContainer().addValueChangedListener(this);
62 10950 bsanchez
        }
63
64
65
        private void updateStatistic() {
66 10981 bsanchez
//                TODO Actualizar tabla de los histogramas.
67
//                histogramPanel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
68 10950 bsanchez
        }
69
70
        public void actionPerformed(ActionEvent e) {
71
                //--------------------------------------
72
                //Cambiar las bandas en el combo
73 10981 bsanchez
                JComboBox cbb = histogramPanel.getJComboBands();
74
                if (comboEventEnable && (e.getSource() == cbb)) {
75
                        if (cbb.getSelectedItem() == null) return;
76 11004 bsanchez
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 10981 bsanchez
                                if (cbb.getSelectedItem().equals("Band "+i))
85 11004 bsanchez
                                        addOrRemoveGraphicBand(i);
86
87
                        updateStatistic();
88
                        updateGraphic();
89 10950 bsanchez
                        return;
90
                }
91
92
                //--------------------------------------
93
                //Limpiar
94 10981 bsanchez
                JButton clean = histogramPanel.getJButtonClear();
95 10950 bsanchez
                if (e.getSource() == clean) {
96 10981 bsanchez
                        cleanChart();
97 10950 bsanchez
                        return;
98
                }
99
100
                //--------------------------------------
101
                //Selecci?n de fuente de datos del histograma
102 10981 bsanchez
                JComboBox cbo = histogramPanel.getJComboBoxOrigen();
103 10950 bsanchez
                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 10997 bsanchez
                        if(cbo.getSelectedIndex() == 0) {
106
                                histogramPanel.setRGBInBandList();
107
                        }
108 10950 bsanchez
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 10997 bsanchez
                        if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2) {
111 11004 bsanchez
                                // TODO: Rellenar BandCount con el valor que toca
112 10997 bsanchez
                                histogramPanel.setBands(bandCount);
113 11004 bsanchez
                                showBands = new boolean[bandCount];
114
                                for (int i = 0; i < showBands.length; i++)
115
                                        showBands[i] = true;
116 10997 bsanchez
                        }
117
                        showHistogram();
118 10981 bsanchez
                        return;
119 10950 bsanchez
                }
120
121
                //--------------------------------------
122
                //Selecci?n de histograma acumulado y no acumulado
123 10981 bsanchez
                JComboBox cbt = histogramPanel.getJComboBoxTipo();
124 10950 bsanchez
                if (comboEventEnable && e.getSource() == cbt) {
125 10981 bsanchez
                        histogramPanel.setType(cbt.getSelectedIndex());
126 10997 bsanchez
                        updateStatistic();
127
                        updateGraphic();
128 10981 bsanchez
                        return;
129 10950 bsanchez
                }
130
        }
131
132
        public void showHistogram() {
133 10981 bsanchez
                if (histogramPanel.getJComboBoxOrigen().getSelectedIndex() < 0) return;
134 10997 bsanchez
                closeHistogramProcess();
135 10981 bsanchez
                IHistogramable iaux = (IHistogramable) ((ArrayList) histogramPanel.getComboSource().get(histogramPanel.getJComboBoxOrigen().getSelectedIndex())).get(0);
136 10997 bsanchez
                histogramProcess = new HistogramProcess(iaux);
137 10950 bsanchez
                incrementableTask = new IncrementableTask(histogramProcess);
138 10970 bsanchez
                incrementableTask.addIncrementableListener(this);
139 10950 bsanchez
                incrementableTask.showWindow();
140
        }
141 10981 bsanchez
142 10950 bsanchez
        /**
143 11004 bsanchez
         * 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 10981 bsanchez
         * Limpia la gr?fica
155 10950 bsanchez
         */
156 10981 bsanchez
        public void cleanChart(){
157
                histogramPanel.cleanChart();
158 11004 bsanchez
                for(int i = 0; i < showBands.length; i++)
159
                        showBands[i] = false;
160 10950 bsanchez
        }
161 10981 bsanchez
162
        private void updateGraphic() {
163 10997 bsanchez
                long[][] auxHistogram = lastHistogram.getHistogramByType(Histogram.getType(histogramPanel.getJComboBoxTipo().getSelectedIndex()));
164
                if (auxHistogram == null) return;
165 10981 bsanchez
166
                int first = 0;
167 10997 bsanchez
                int end = auxHistogram[0].length;
168 10981 bsanchez
169 11004 bsanchez
                first = (int) ((histogramPanel.getGraphicContainer().getX1()*auxHistogram[0].length)/100);
170
                end = (int) ((histogramPanel.getGraphicContainer().getX2()*auxHistogram[0].length)/100);
171 10981 bsanchez
172 11004 bsanchez
                int bandCount = 0;
173
                for (int i = 0; i < showBands.length; i++) {
174
                        if (showBands[i]) bandCount++;
175
                }
176 10997 bsanchez
177 11004 bsanchez
                int[][] newHistogram = new int[bandCount][end - first];
178
                String[] bandNames = new String[bandCount];
179 10981 bsanchez
180 10997 bsanchez
                bandCount = auxHistogram.length;
181
182 11004 bsanchez
                int numBand = 0;
183
                for (int iBand = 0; iBand < showBands.length; iBand++) {
184
                        if (!showBands[iBand]) continue;
185
186 10981 bsanchez
                        for (int j=first; j<end; j++)
187 11004 bsanchez
                                newHistogram[numBand][j-first] = (int) auxHistogram[iBand][j];
188
                        bandNames[numBand] = iBand + "";
189
190
                        numBand++;
191 10981 bsanchez
                }
192
193
                histogramPanel.getGraphicContainer().getPGraphic().cleanChart();
194
                histogramPanel.getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames);
195
        }
196 10997 bsanchez
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 10981 bsanchez
207 10970 bsanchez
        public void actionClosed(IncrementableEvent e) {
208 10997 bsanchez
                if (histogramProcess == null) return;
209 10981 bsanchez
                lastHistogram = histogramProcess.getLastHistogram();
210
                updateGraphic();
211 10970 bsanchez
                closeHistogramProcess();
212
        }
213
214
        public void actionResumed(IncrementableEvent e) {
215 10997 bsanchez
                if (histogramProcess == null) return;
216 10970 bsanchez
                histogramProcess.resume();
217
        }
218
219
        public void actionSuspended(IncrementableEvent e) {
220 10997 bsanchez
                if (histogramProcess == null) return;
221 10970 bsanchez
                histogramProcess.suspend();
222
        }
223 11004 bsanchez
224
        public void actionValueChanged(GraphicEvent e) {
225
                updateStatistic();
226
                updateGraphic();
227
        }
228 10950 bsanchez
}