Statistics
| Revision:

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

History | View | Annotate | Download (7.59 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
 * Listener para eventos del panel de histograma
38
 *
39
 * @version 20/03/2007
40
 * @author Nacho Brodin (brodin_ign@gva.es)
41
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
42
 */
43
public class HistogramPanelListener implements ActionListener, KeyListener, IncrementableListener {
44
        private HistogramPanel                histogramPanel = null;
45
        private IncrementableTask        incrementableTask = null;
46
        private HistogramProcess        histogramProcess = null;
47
        private long[][]                                        lastHistogram = null; 
48

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

    
64
        
65
        public void keyPressed(KeyEvent e) {
66
                if (e.getKeyCode() == 10) {
67
                        updateStatistic();
68
                        updateGraphic();
69
                }
70
        }
71

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

    
77

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

    
155
        public void showHistogram() {
156
                if (histogramPanel.getJComboBoxOrigen().getSelectedIndex() < 0) return;
157
                IHistogramable iaux = (IHistogramable) ((ArrayList) histogramPanel.getComboSource().get(histogramPanel.getJComboBoxOrigen().getSelectedIndex())).get(0);
158
                histogramProcess = new HistogramProcess(iaux, histogramPanel.getJComboBoxTipo().getSelectedIndex());
159
                incrementableTask = new IncrementableTask(histogramProcess);
160
                incrementableTask.addIncrementableListener(this);
161
                incrementableTask.showWindow();
162
        }
163
        
164
        /**
165
         * Limpia la gr?fica
166
         */
167
        public void cleanChart(){
168
                histogramPanel.cleanChart();
169
                for(int i = 0; i < histogramPanel.showBands.length; i++)
170
                        histogramPanel.showBands[i] = false;
171
        }
172

    
173
        public void keyReleased(KeyEvent e) {
174
        }
175

    
176
        public void keyTyped(KeyEvent e) {
177
        }
178

    
179
        private void closeHistogramProcess() {
180
                histogramProcess = null;
181
                incrementableTask = null;
182
        }
183
        public void actionCanceled(IncrementableEvent e) {
184
                closeHistogramProcess();
185
        }
186

    
187
        private void updateGraphic() {
188
                if (lastHistogram == null) return;
189

    
190
                int first = 0;
191
                int end = lastHistogram[0].length;
192

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

    
196
                int[][] newHistogram = new int[lastHistogram.length][end - first];
197
                String[] bandNames = new String[lastHistogram.length];
198
                
199
                for (int iBand=0; iBand<lastHistogram.length; iBand++) {
200
                        for (int j=first; j<end; j++)
201
                                newHistogram[iBand][j-first] = (int) lastHistogram[iBand][j];
202
                        bandNames[iBand] = iBand + "";
203
                }
204

    
205
                histogramPanel.getGraphicContainer().getPGraphic().cleanChart();
206
                histogramPanel.getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames);
207
        }
208
        
209
        public void actionClosed(IncrementableEvent e) {
210
                lastHistogram = histogramProcess.getLastHistogram();
211
                updateGraphic();
212
                closeHistogramProcess();
213
        }
214

    
215
        public void actionResumed(IncrementableEvent e) {
216
                histogramProcess.resume();
217
        }
218

    
219
        public void actionSuspended(IncrementableEvent e) {
220
                histogramProcess.suspend();
221
        }
222
}