18 |
18 |
*/
|
19 |
19 |
package org.gvsig.rastertools.histogram;
|
20 |
20 |
|
|
21 |
import java.awt.Color;
|
|
22 |
import java.awt.Component;
|
21 |
23 |
import java.awt.event.ActionEvent;
|
22 |
24 |
import java.awt.event.ActionListener;
|
23 |
|
import java.awt.event.KeyEvent;
|
|
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;
|
24 |
30 |
import java.util.ArrayList;
|
25 |
31 |
|
26 |
32 |
import javax.swing.JButton;
|
27 |
33 |
import javax.swing.JComboBox;
|
|
34 |
import javax.swing.JFileChooser;
|
|
35 |
import javax.swing.JOptionPane;
|
28 |
36 |
|
29 |
37 |
import org.gvsig.gui.beans.graphic.GraphicEvent;
|
30 |
38 |
import org.gvsig.gui.beans.graphic.GraphicListener;
|
... | ... | |
34 |
42 |
import org.gvsig.raster.util.Histogram;
|
35 |
43 |
import org.gvsig.raster.util.IHistogramable;
|
36 |
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;
|
37 |
63 |
/**
|
38 |
64 |
* Listener para eventos del panel de histograma
|
39 |
65 |
*
|
... | ... | |
51 |
77 |
public boolean comboEventEnable = false;
|
52 |
78 |
|
53 |
79 |
private boolean[] showBands = {true, true, true};
|
54 |
|
|
|
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};
|
55 |
91 |
|
56 |
92 |
public HistogramPanelListener(HistogramPanel p){
|
57 |
93 |
histogramPanel = p;
|
... | ... | |
63 |
99 |
|
64 |
100 |
|
65 |
101 |
private void updateStatistic() {
|
66 |
|
// TODO Actualizar tabla de los histogramas.
|
67 |
|
// histogramPanel.setStatistic(DatasetStatistics.getBasicStatsFromHistogram(getLastHistogram(), (int)panel.getBoxesValues()[1], (int)panel.getBoxesValues()[0], panel.showBands));
|
|
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));
|
68 |
116 |
}
|
69 |
117 |
|
70 |
118 |
public void actionPerformed(ActionEvent e) {
|
... | ... | |
80 |
128 |
if (cbb.getSelectedItem().equals("R")) addOrRemoveGraphicBand(0);
|
81 |
129 |
if (cbb.getSelectedItem().equals("G")) addOrRemoveGraphicBand(1);
|
82 |
130 |
if (cbb.getSelectedItem().equals("B")) addOrRemoveGraphicBand(2);
|
83 |
|
for (int i = 0; i < HistogramPanel.MAXBANDS; i++)
|
84 |
|
if (cbb.getSelectedItem().equals("Band "+i))
|
|
131 |
for (int i = 0; i < showBands.length; i++)
|
|
132 |
if (cbb.getSelectedItem().equals("Band " + i))
|
85 |
133 |
addOrRemoveGraphicBand(i);
|
86 |
134 |
|
|
135 |
updateGraphic();
|
87 |
136 |
updateStatistic();
|
88 |
|
updateGraphic();
|
89 |
137 |
return;
|
90 |
138 |
}
|
91 |
139 |
|
92 |
140 |
//--------------------------------------
|
93 |
141 |
//Limpiar
|
94 |
|
JButton clean = histogramPanel.getJButtonClear();
|
95 |
|
if (e.getSource() == clean) {
|
96 |
|
cleanChart();
|
|
142 |
if (e.getSource() == histogramPanel.getJButtonClear()) {
|
|
143 |
for (int i = 0; i < showBands.length; i++)
|
|
144 |
showBands[i] = false;
|
|
145 |
updateGraphic();
|
97 |
146 |
return;
|
98 |
147 |
}
|
99 |
148 |
|
... | ... | |
108 |
157 |
|
109 |
158 |
//En caso de que el histograma se monte a partir de los datos reales ponemos el n?mero de bandas en el combo
|
110 |
159 |
if (cbo.getSelectedIndex() == 1 || cbo.getSelectedIndex() == 2) {
|
111 |
|
// TODO: Rellenar BandCount con el valor que toca
|
|
160 |
bandCount = lastHistogram.getNumBands();
|
112 |
161 |
histogramPanel.setBands(bandCount);
|
113 |
162 |
showBands = new boolean[bandCount];
|
114 |
163 |
for (int i = 0; i < showBands.length; i++)
|
... | ... | |
127 |
176 |
updateGraphic();
|
128 |
177 |
return;
|
129 |
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 |
}
|
130 |
267 |
}
|
131 |
268 |
|
132 |
269 |
public void showHistogram() {
|
... | ... | |
145 |
282 |
* @param band banda a visualizar o borrar del gr?fico
|
146 |
283 |
*/
|
147 |
284 |
public void addOrRemoveGraphicBand(int band){
|
148 |
|
if (band > showBands.length)
|
149 |
|
return;
|
|
285 |
if (band > showBands.length) return;
|
150 |
286 |
showBands[band] = !showBands[band];
|
151 |
287 |
}
|
152 |
288 |
|
153 |
|
/**
|
154 |
|
* Limpia la gr?fica
|
155 |
|
*/
|
156 |
|
public void cleanChart(){
|
157 |
|
histogramPanel.cleanChart();
|
158 |
|
for(int i = 0; i < showBands.length; i++)
|
159 |
|
showBands[i] = false;
|
160 |
|
}
|
161 |
|
|
162 |
289 |
private void updateGraphic() {
|
163 |
290 |
long[][] auxHistogram = lastHistogram.getHistogramByType(Histogram.getType(histogramPanel.getJComboBoxTipo().getSelectedIndex()));
|
164 |
291 |
if (auxHistogram == null) return;
|
165 |
292 |
|
166 |
|
int first = 0;
|
167 |
|
int end = auxHistogram[0].length;
|
|
293 |
int first = (int) ((histogramPanel.getGraphicContainer().getX1()*(auxHistogram[0].length-1))/100);
|
|
294 |
int end = (int) ((histogramPanel.getGraphicContainer().getX2()*(auxHistogram[0].length-1))/100);
|
168 |
295 |
|
169 |
|
first = (int) ((histogramPanel.getGraphicContainer().getX1()*auxHistogram[0].length)/100);
|
170 |
|
end = (int) ((histogramPanel.getGraphicContainer().getX2()*auxHistogram[0].length)/100);
|
171 |
|
|
172 |
296 |
int bandCount = 0;
|
173 |
297 |
for (int i = 0; i < showBands.length; i++) {
|
174 |
298 |
if (showBands[i]) bandCount++;
|
... | ... | |
187 |
311 |
newHistogram[numBand][j-first] = (int) auxHistogram[iBand][j];
|
188 |
312 |
bandNames[numBand] = iBand + "";
|
189 |
313 |
|
|
314 |
histogramPanel.getGraphicContainer().setBandColor(numBand, bandsColor[iBand]);
|
|
315 |
|
190 |
316 |
numBand++;
|
191 |
317 |
}
|
192 |
318 |
|
193 |
|
histogramPanel.getGraphicContainer().getPGraphic().cleanChart();
|
194 |
319 |
histogramPanel.getGraphicContainer().getPGraphic().setNewChart(newHistogram, bandNames);
|
|
320 |
updateStatistic();
|
195 |
321 |
}
|
196 |
|
|
197 |
322 |
private void closeHistogramProcess() {
|
198 |
323 |
histogramProcess = null;
|
199 |
324 |
incrementableTask = null;
|
... | ... | |
222 |
347 |
}
|
223 |
348 |
|
224 |
349 |
public void actionValueChanged(GraphicEvent e) {
|
225 |
|
updateStatistic();
|
226 |
350 |
updateGraphic();
|
227 |
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 |
|
228 |
372 |
}
|