svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / histogram / HistogramPanelListener.java @ 11151
History | View | Annotate | Download (12.1 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.Color; |
22 |
import java.awt.event.ActionEvent; |
23 |
import java.awt.event.ActionListener; |
24 |
import java.beans.PropertyChangeEvent; |
25 |
import java.beans.PropertyChangeListener; |
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.io.RandomAccessFile; |
29 |
import java.nio.channels.WritableByteChannel; |
30 |
import java.util.ArrayList; |
31 |
|
32 |
import javax.swing.JComboBox; |
33 |
import javax.swing.table.DefaultTableModel; |
34 |
|
35 |
import org.gvsig.gui.beans.graphic.GraphicEvent; |
36 |
import org.gvsig.gui.beans.graphic.GraphicListener; |
37 |
import org.gvsig.gui.beans.incrementabletask.IncrementableTask; |
38 |
import org.gvsig.raster.util.Histogram; |
39 |
import org.gvsig.raster.util.HistogramClass; |
40 |
import org.gvsig.raster.util.IHistogramable; |
41 |
import org.gvsig.rastertools.histogram.ui.HistogramPanel; |
42 |
/**
|
43 |
* Listener para eventos del panel de histograma
|
44 |
*
|
45 |
* @version 20/03/2007
|
46 |
* @author Nacho Brodin (brodin_ign@gva.es)
|
47 |
* @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
|
48 |
*/
|
49 |
public class HistogramPanelListener implements GraphicListener, ActionListener, PropertyChangeListener { |
50 |
private HistogramPanel histogramPanel = null; |
51 |
private IncrementableTask incrementableTask = null; |
52 |
private HistogramProcess histogramProcess = null; |
53 |
private Histogram lastHistogram = null; |
54 |
|
55 |
public boolean eventsEnabled = false; |
56 |
|
57 |
/**
|
58 |
* Bandas que se est?n mostrando en el gr?fico. Se inicializa con las 3 bandas
|
59 |
* RGB de la visualizaci?n. Este array puede tener m?s elementos ya que si las
|
60 |
* bandas no son de visualizaci?n (bandas de la imagen en disco) tendr? un elemento
|
61 |
* por cada una.
|
62 |
*/
|
63 |
private boolean[] showBands = null; |
64 |
|
65 |
private Color[] bandsColor = { |
66 |
Color.red,
|
67 |
Color.green,
|
68 |
Color.blue,
|
69 |
Color.cyan,
|
70 |
Color.black,
|
71 |
Color.darkGray,
|
72 |
Color.gray,
|
73 |
Color.magenta,
|
74 |
Color.yellow,
|
75 |
Color.orange};
|
76 |
|
77 |
public HistogramPanelListener(HistogramPanel p){
|
78 |
histogramPanel = p; |
79 |
} |
80 |
|
81 |
public HistogramPanel getHistogramPanel() {
|
82 |
return histogramPanel;
|
83 |
} |
84 |
|
85 |
public void setControlListeners(){ |
86 |
getHistogramPanel().getGraphicContainer().addValueChangedListener(this);
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Actualizar cuadro de estad?sticas
|
91 |
*/
|
92 |
private void updateStatistic() { |
93 |
int first = (int) getHistogramPanel().getBoxesValues()[1]; |
94 |
int end = (int) getHistogramPanel().getBoxesValues()[0]; |
95 |
|
96 |
getHistogramPanel().setStatistic(getLastHistogram().getBasicStats((int) first, (int) end, showBands)); |
97 |
} |
98 |
|
99 |
/**
|
100 |
* Tratamiento de todos los eventos visuales.
|
101 |
*/
|
102 |
public void actionPerformed(ActionEvent e) { |
103 |
if (!eventsEnabled) return; |
104 |
|
105 |
//--------------------------------------
|
106 |
//Selecci?n de fuente de datos del histograma
|
107 |
JComboBox cbo = getHistogramPanel().getJComboBoxOrigen();
|
108 |
if (e.getSource() == cbo) {
|
109 |
showHistogram(); |
110 |
return;
|
111 |
} |
112 |
|
113 |
//--------------------------------------
|
114 |
//Selecci?n de histograma acumulado y no acumulado
|
115 |
JComboBox cbt = getHistogramPanel().getJComboBoxTipo();
|
116 |
if (e.getSource() == cbt) {
|
117 |
updateStatistic(); |
118 |
updateGraphic(); |
119 |
return;
|
120 |
} |
121 |
|
122 |
//--------------------------------------
|
123 |
// Boton Crear Tabla
|
124 |
/*
|
125 |
JButton table = getHistogramPanel().getBCreateTable();
|
126 |
if (e.getSource() == table) {
|
127 |
try {
|
128 |
// -------Mostrar un fileChooser------------------
|
129 |
String fName;
|
130 |
JFileChooser chooser = new JFileChooser();
|
131 |
chooser.setDialogTitle(PluginServices.getText(this, "guardar_tabla"));
|
132 |
|
133 |
int returnVal = chooser.showOpenDialog(getHistogramPanel());
|
134 |
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
135 |
fName = chooser.getSelectedFile().toString();
|
136 |
if (!fName.endsWith(".dbf"))
|
137 |
fName += ".dbf";
|
138 |
|
139 |
//-------------Crear el dbf----------------------
|
140 |
|
141 |
DbaseFileWriterNIO dbfWrite = null;
|
142 |
DbaseFileHeaderNIO myHeader;
|
143 |
Value[] record;
|
144 |
|
145 |
long histogram[][]= getLastHistogram().getHistogram();
|
146 |
int numBands = histogram.length;
|
147 |
int numRecors = histogram[0].length;
|
148 |
|
149 |
File file = new File(fName);
|
150 |
|
151 |
String names[] = new String[numBands+1];
|
152 |
int types[] = new int [numBands+1];
|
153 |
int lengths[] = new int [numBands+1];
|
154 |
|
155 |
names[0]="Value";
|
156 |
types[0]=4;
|
157 |
lengths[0]=15;
|
158 |
for (int band = 0; band < numBands; band++){
|
159 |
names[band+1]="Band"+band;
|
160 |
types[band+1]=4;
|
161 |
lengths[band+1]=15;
|
162 |
}
|
163 |
|
164 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(names,types,lengths);
|
165 |
|
166 |
myHeader.setNumRecords(numRecors);
|
167 |
dbfWrite = new DbaseFileWriterNIO(myHeader, (FileChannel) getWriteChannel(file.getPath()));
|
168 |
record = new Value[numBands+1];
|
169 |
|
170 |
for (int j = 0; j < numRecors; j++) {
|
171 |
record[0] = ValueFactory.createValue(j);
|
172 |
for (int r = 0; r < numBands; r++) {
|
173 |
record[r+1] = ValueFactory.createValue(histogram[r][j]);
|
174 |
}
|
175 |
|
176 |
dbfWrite.write(record);
|
177 |
}
|
178 |
|
179 |
dbfWrite.close();
|
180 |
|
181 |
//------------A?adir el dbf al proyecto--------------
|
182 |
ProjectExtension ext = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class);
|
183 |
String name = file.getName();
|
184 |
LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf driver", name, fName);
|
185 |
DataSource dataSource;
|
186 |
dataSource = LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING);
|
187 |
|
188 |
SelectableDataSource sds = new SelectableDataSource(dataSource);
|
189 |
EditableAdapter auxea=new EditableAdapter();
|
190 |
auxea.setOriginalDataSource(sds);
|
191 |
ProjectTable projectTable = ProjectFactory.createTable(name, auxea);
|
192 |
//ext.getProject().addTable(projectTable);
|
193 |
ext.getProject().addDocument(projectTable);
|
194 |
|
195 |
Table t = new Table();
|
196 |
t.setModel(projectTable);
|
197 |
//projectTable.setAndamiWindow(t);
|
198 |
PluginServices.getMDIManager().addWindow(t);
|
199 |
}
|
200 |
} catch (IOException e1) {
|
201 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),getHistogramPanel().getName() + " " + PluginServices.getText(this,"table_not_create"));
|
202 |
} catch (DriverLoadException e1) {
|
203 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),getHistogramPanel().getName() + " " + PluginServices.getText(this,"table_not_create"));
|
204 |
} catch (NoSuchTableException e1) {
|
205 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),getHistogramPanel().getName() + " " + PluginServices.getText(this,"table_not_create"));
|
206 |
} catch (ReadDriverException e1) {
|
207 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),getHistogramPanel().getName() + " " + PluginServices.getText(this,"table_not_create"));
|
208 |
}
|
209 |
}
|
210 |
*/
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Actualizar la variable de las bandas visibles y su componente visual.
|
215 |
*/
|
216 |
private void refreshBands() { |
217 |
getHistogramPanel().refreshBands(); |
218 |
showBands = new boolean[getLastHistogram().getNumBands()]; |
219 |
for (int i = 0; i < showBands.length; i++) |
220 |
showBands[i] = true;
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* Lanza los dos threads para procesar el histograma y visualizar la
|
225 |
* ventana de incremento
|
226 |
*/
|
227 |
public void showHistogram() { |
228 |
if (getHistogramPanel().getJComboBoxOrigen().getSelectedIndex() < 0) return; |
229 |
|
230 |
IHistogramable iaux = (IHistogramable) ((ArrayList) getHistogramPanel().getComboSource().get(getHistogramPanel().getJComboBoxOrigen().getSelectedIndex())).get(0); |
231 |
histogramProcess = new HistogramProcess(iaux, this); |
232 |
incrementableTask = new IncrementableTask(histogramProcess);
|
233 |
histogramProcess.setIncrementableTask(incrementableTask); |
234 |
incrementableTask.showWindow(); |
235 |
iaux.resetPercent(); |
236 |
histogramProcess.start(); |
237 |
incrementableTask.start(); |
238 |
} |
239 |
|
240 |
/**
|
241 |
* A?ade o elimina una banda de la visualizaci?n. Si la banda se est? visualizando
|
242 |
* se elimina y si no entonces se muestra
|
243 |
* @param band banda a visualizar o borrar del gr?fico
|
244 |
*/
|
245 |
public void addOrRemoveGraphicBand(int band){ |
246 |
if (band > showBands.length) return; |
247 |
showBands[band] = !showBands[band]; |
248 |
} |
249 |
|
250 |
/**
|
251 |
* Actualiza la grafica con los datos que ya teniamos del histograma.
|
252 |
*/
|
253 |
private void updateGraphic() { |
254 |
HistogramClass[][] histogramClass = getLastHistogram().getHistogramByType(Histogram.getType(getHistogramPanel().getJComboBoxTipo().getSelectedIndex())); |
255 |
if (histogramClass == null) return; |
256 |
|
257 |
double[][][] datos = new double[histogramClass.length][histogramClass[0].length][2]; |
258 |
for (int iBand=0; iBand < histogramClass.length; iBand++) { |
259 |
for (int i=0; i<histogramClass[iBand].length; i++) { |
260 |
datos[iBand][i][0] = histogramClass[iBand][i].getMin();
|
261 |
datos[iBand][i][1] = histogramClass[iBand][i].getNPixels();
|
262 |
} |
263 |
} |
264 |
|
265 |
// long[][] auxHistogram = getLastHistogram().getHistogramByType(Histogram.getType(getHistogramPanel().getJComboBoxTipo().getSelectedIndex()));
|
266 |
// if (auxHistogram == null) return;
|
267 |
|
268 |
int first = (int) getHistogramPanel().getBoxesValues()[1]; |
269 |
int end = (int) getHistogramPanel().getBoxesValues()[0]; |
270 |
|
271 |
int bandCount = 0; |
272 |
for (int i = 0; i < showBands.length; i++) { |
273 |
if (showBands[i]) bandCount++;
|
274 |
} |
275 |
|
276 |
double[][][] newHistogram = new double[bandCount][end - first][2]; |
277 |
String[] bandNames = new String[bandCount]; |
278 |
|
279 |
bandCount = datos.length; |
280 |
|
281 |
int numBand = 0; |
282 |
for (int iBand = 0; iBand < datos.length; iBand++) { |
283 |
if (!showBands[iBand]) continue; |
284 |
|
285 |
for (int j=first; j<end; j++) { |
286 |
newHistogram[numBand][j-first][0] = datos[iBand][j][0]; |
287 |
newHistogram[numBand][j-first][1] = datos[iBand][j][1]; |
288 |
} |
289 |
bandNames[numBand] = (String) ((DefaultTableModel) getHistogramPanel().getJTableBands().getModel()).getValueAt(iBand, 1); |
290 |
|
291 |
getHistogramPanel().getGraphicContainer().setBandColor(numBand, bandsColor[iBand % bandsColor.length]); |
292 |
|
293 |
numBand++; |
294 |
} |
295 |
|
296 |
getHistogramPanel().getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames); |
297 |
updateStatistic(); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Definir el nuevo histograma, metodo pu?blico para ser invocado desde
|
302 |
* histogramProcess
|
303 |
* @param histograma nuevo
|
304 |
*/
|
305 |
public void setNewHistogram(Histogram value) { |
306 |
getHistogramPanel().panelInizialited = false;
|
307 |
eventsEnabled = false;
|
308 |
lastHistogram = value; |
309 |
refreshBands(); |
310 |
updateGraphic(); |
311 |
|
312 |
incrementableTask = null;
|
313 |
histogramProcess = null;
|
314 |
|
315 |
// Activo la ejecucion de los eventos porque seguro que ya tenemos un histograma
|
316 |
eventsEnabled = true;
|
317 |
getHistogramPanel().panelInizialited = true;
|
318 |
} |
319 |
|
320 |
/**
|
321 |
* Obtener ?ltimo histograma
|
322 |
* @return Histogram
|
323 |
*/
|
324 |
public Histogram getLastHistogram() {
|
325 |
return lastHistogram;
|
326 |
} |
327 |
|
328 |
/**
|
329 |
* Eventos de los BoxValues
|
330 |
*/
|
331 |
public void actionValueChanged(GraphicEvent e) { |
332 |
updateGraphic(); |
333 |
} |
334 |
|
335 |
/**
|
336 |
* Proceso para guardar una estadistica en un fichero.
|
337 |
* @param path
|
338 |
* @return
|
339 |
* @throws IOException
|
340 |
*/
|
341 |
private WritableByteChannel getWriteChannel(String path) throws IOException { |
342 |
WritableByteChannel channel;
|
343 |
|
344 |
File f = new File(path); |
345 |
|
346 |
if (!f.exists()) {
|
347 |
System.out.println("Creando fichero " + f.getAbsolutePath()); |
348 |
|
349 |
if (!f.createNewFile()) {
|
350 |
throw new IOException("Cannot create file " + f); |
351 |
} |
352 |
} |
353 |
|
354 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
355 |
channel = raf.getChannel(); |
356 |
|
357 |
return channel;
|
358 |
} |
359 |
|
360 |
public void propertyChange(PropertyChangeEvent evt) { |
361 |
if (!eventsEnabled) return; |
362 |
int countRow = ((DefaultTableModel) histogramPanel.getJTableBands().getModel()).getRowCount(); |
363 |
for (int i=0; i<countRow; i++) { |
364 |
showBands[i] = ((Boolean) ((DefaultTableModel) histogramPanel.getJTableBands().getModel()).getValueAt(i, 0)).booleanValue(); |
365 |
} |
366 |
updateGraphic(); |
367 |
updateStatistic(); |
368 |
} |
369 |
} |