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