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