gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.mapcontext.raster.swing / org.gvsig.fmap.mapcontext.raster.swing.impl / src / main / java / org / gvsig / fmap / mapcontext / raster / swing / impl / bands / BandsPanelController.java @ 6900
History | View | Annotate | Download (16.7 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.mapcontext.raster.swing.impl.bands; |
24 |
|
25 |
import java.awt.event.ActionEvent; |
26 |
import java.awt.event.ActionListener; |
27 |
import java.io.File; |
28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
30 |
import java.util.Locale; |
31 |
|
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.JFileChooser; |
34 |
import javax.swing.JOptionPane; |
35 |
import javax.swing.ListSelectionModel; |
36 |
import javax.swing.event.ListSelectionEvent; |
37 |
import javax.swing.event.ListSelectionListener; |
38 |
import javax.swing.filechooser.FileFilter; |
39 |
import javax.swing.table.DefaultTableColumnModel; |
40 |
import javax.swing.table.TableColumn; |
41 |
import javax.swing.table.TableColumnModel; |
42 |
|
43 |
import org.slf4j.Logger; |
44 |
import org.slf4j.LoggerFactory; |
45 |
|
46 |
import org.gvsig.fmap.dal.DALLocator; |
47 |
import org.gvsig.fmap.dal.DataManager; |
48 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.exception.DataException; |
50 |
import org.gvsig.fmap.dal.exception.InitializeException; |
51 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
52 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
53 |
import org.gvsig.fmap.dal.raster.api.BandDescriptor; |
54 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
55 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter; |
56 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
57 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
58 |
import org.gvsig.fmap.mapcontext.exceptions.LegendLayerException; |
59 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
60 |
import org.gvsig.fmap.mapcontext.raster.api.RasterLayer; |
61 |
import org.gvsig.fmap.mapcontext.raster.swing.bands.BandsPanel; |
62 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
63 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
64 |
import org.gvsig.raster.lib.buffer.api.NoData; |
65 |
import org.gvsig.raster.lib.legend.api.RasterLegend; |
66 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
67 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
68 |
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
69 |
import org.gvsig.tools.ToolsLocator; |
70 |
import org.gvsig.tools.dynobject.DynObject; |
71 |
import org.gvsig.tools.i18n.I18nManager; |
72 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
73 |
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager; |
74 |
|
75 |
/**
|
76 |
* @author fdiaz
|
77 |
*
|
78 |
*/
|
79 |
public class BandsPanelController extends BandsPanelView implements BandsPanel, ActionListener, ListSelectionListener { |
80 |
|
81 |
/**
|
82 |
*
|
83 |
*/
|
84 |
private static final long serialVersionUID = 8255630092178065352L; |
85 |
@SuppressWarnings("unused") |
86 |
private static final Logger LOG = LoggerFactory.getLogger(BandsPanelController.class); |
87 |
private RasterLayer layer;
|
88 |
|
89 |
private static final String RASTER_FILTER = "All_raster_supported"; |
90 |
|
91 |
// FLayer layer;
|
92 |
|
93 |
/**
|
94 |
* @param layer
|
95 |
*/
|
96 |
@SuppressWarnings("unchecked") |
97 |
public BandsPanelController() {
|
98 |
translate(); |
99 |
initializeComponents(); |
100 |
} |
101 |
|
102 |
private void initializeComponents() { |
103 |
this.btnAdd.addActionListener(this); |
104 |
this.btnRemove.addActionListener(this); |
105 |
tblBands.setRowSelectionAllowed(true);
|
106 |
tblBands.setColumnSelectionAllowed(false);
|
107 |
tblBands.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
108 |
tblBands.getSelectionModel().addListSelectionListener(this);
|
109 |
BandsTableModel model = new BandsTableModel();
|
110 |
tblBands.setModel(model); |
111 |
|
112 |
tblBands.setColumnModel(new DefaultTableColumnModel()); |
113 |
TableColumnModel columnModel = tblBands.getColumnModel();
|
114 |
columnModel.setColumnSelectionAllowed(false);
|
115 |
for (int i = 0; i < model.getColumnCount(); i++) { |
116 |
TableColumn tableColumn = new TableColumn(i); |
117 |
tableColumn.setIdentifier(model.getColumnName(i)); |
118 |
tableColumn.setHeaderValue(model.getColumnName(i)); |
119 |
columnModel.addColumn(tableColumn); |
120 |
} |
121 |
|
122 |
// FIXME: esto ser?a lo que habr?a que cambiar si se desea implementar
|
123 |
// otros espacios de color
|
124 |
columnModel.getColumn(0).setCellEditor(new ColorInterpretationCellEditor(BandsTableModel.RGBColorSpace)); |
125 |
columnModel.getColumn(0).setCellRenderer(new ColorInterpretationCellRenderer()); |
126 |
|
127 |
columnModel.getColumn(1).setCellRenderer(new NoDataCellRenderer()); |
128 |
// columnModel.getColumn(1).setCellEditor(new NoDataCellEditor());
|
129 |
|
130 |
columnModel.getColumn(2).setCellRenderer(new DataTypeCellRenderer()); |
131 |
|
132 |
} |
133 |
|
134 |
private void translate() { |
135 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
136 |
lblBands.setText(i18nManager.getTranslation(lblBands.getText())); |
137 |
btnAdd.setText(i18nManager.getTranslation(btnAdd.getText())); |
138 |
btnAdd.setToolTipText(i18nManager.getTranslation(btnAdd.getToolTipText())); |
139 |
btnRemove.setText(i18nManager.getTranslation(btnRemove.getText())); |
140 |
btnRemove.setToolTipText(i18nManager.getTranslation(btnRemove.getToolTipText())); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* @param locale
|
145 |
*
|
146 |
*/
|
147 |
public void setLocate(Locale locale) { |
148 |
Locale l = super.getLocale(); |
149 |
if (!l.equals(locale)) {
|
150 |
translate(); |
151 |
} |
152 |
super.setLocale(locale);
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* @param layer
|
157 |
*/
|
158 |
@SuppressWarnings("unchecked") |
159 |
public void set(FLayer layer) { |
160 |
this.layer = (RasterLayer) layer;
|
161 |
RasterLegend legend = this.layer.getLegend();
|
162 |
ColorInterpretation colorInterpretation = null;
|
163 |
if (legend != null) { |
164 |
colorInterpretation = legend.getColorInterpretation(); |
165 |
} |
166 |
RasterStore store = this.layer.getRasterStore();
|
167 |
addBands(store, colorInterpretation); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* @param colorInterpretation
|
172 |
* @param store
|
173 |
* @param model
|
174 |
*/
|
175 |
private void addBands(RasterStore store, ColorInterpretation colorInterpretation) { |
176 |
BandsTableModel model = ((BandsTableModel) tblBands.getModel()); |
177 |
for (int i = 0; i < store.getBands(); i++) { |
178 |
BandDescriptor bandDescriptor = store.getBandDescriptor(i); |
179 |
|
180 |
RasterStoreBand rasterStoreBand = new RasterStoreBand(bandDescriptor.getStore(), bandDescriptor.getBand());
|
181 |
NoData noData = bandDescriptor.getNoData(); |
182 |
if (noData != null) { |
183 |
rasterStoreBand.setNoDataNumber(noData.getValue()); |
184 |
} |
185 |
rasterStoreBand.setBandName(bandDescriptor.getName()); |
186 |
rasterStoreBand.setDataType(bandDescriptor.getDataType()); |
187 |
|
188 |
if (colorInterpretation != null) { |
189 |
rasterStoreBand.setBandColorInterpretation(colorInterpretation.get(i)); |
190 |
} else {
|
191 |
rasterStoreBand.setBandColorInterpretation(ColorInterpretation.UNDEFINED_BAND); |
192 |
} |
193 |
model.add(rasterStoreBand); |
194 |
model.fireTableDataChanged(); |
195 |
} |
196 |
} |
197 |
|
198 |
/**
|
199 |
* @param layer
|
200 |
*/
|
201 |
public boolean fetch(FLayer layer) { |
202 |
RasterLayer rasterLayer = (RasterLayer) layer; |
203 |
RasterStore store = rasterLayer.getRasterStore(); |
204 |
RasterLegendManager rasterLegendManager = RasterLegendLocator.getRasterLegendManager(); |
205 |
if (willBeChangedBands(store)) {
|
206 |
store.clearAdditionalBands(); |
207 |
BandsTableModel model = (BandsTableModel) tblBands.getModel(); |
208 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
209 |
|
210 |
for (int index = 0; index < model.getRowCount(); index++) { |
211 |
RasterStoreBand rasterStoreBand = model.getElementAt(index); |
212 |
RasterStore subStore = rasterStoreBand.getStore(); |
213 |
int band = rasterStoreBand.getBand();
|
214 |
if (subStore != store) {
|
215 |
try {
|
216 |
store.addBand(subStore, band); |
217 |
} catch (DataException e) {
|
218 |
ThreadSafeDialogsManager dlgManager = ToolsSwingLocator.getThreadSafeDialogsManager(); |
219 |
String message = "_cant_add_band_XbandX_from_store_XsourceX_to_store_XtargetX"; |
220 |
String title = "_error_adding_bands"; |
221 |
dlgManager.messageDialog(message, new String[] { String.valueOf(band), subStore.getName(), |
222 |
store.getName() }, title, JOptionPane.WARNING_MESSAGE);
|
223 |
LOG.warn(message, e); |
224 |
return false; |
225 |
} |
226 |
} |
227 |
BandDescriptor bandDescriptor = store.getBandDescriptor(index); |
228 |
NoData noData = bandDescriptor.getNoData(); |
229 |
Number value = null; |
230 |
value = rasterStoreBand.getNoDataNumber(); |
231 |
|
232 |
if (noData == null) { |
233 |
noData = bufferManager.createNoData(value, null);
|
234 |
} else {
|
235 |
noData.setValue((Number) value);
|
236 |
} |
237 |
bandDescriptor.setNoData(noData); |
238 |
} |
239 |
rasterLayer.recalculateStatistics(); |
240 |
|
241 |
List<String> colorInterpretations = getColorInterpretations(); |
242 |
ColorInterpretation colorInterpretation = |
243 |
rasterLegendManager.createColorInterpretation(colorInterpretations.toArray(new String[0])); |
244 |
|
245 |
RasterLegend legend = rasterLegendManager.createLegend(colorInterpretation); |
246 |
rasterLayer.setLegend(legend); |
247 |
rasterLayer.getMapContext().invalidate(); |
248 |
} |
249 |
|
250 |
return true; |
251 |
} |
252 |
|
253 |
private List<String> getColorInterpretations(){ |
254 |
List<String> colorInterpretations = new ArrayList(); |
255 |
BandsTableModel model = (BandsTableModel) tblBands.getModel(); |
256 |
|
257 |
for (int index = 0; index < model.getRowCount(); index++) { |
258 |
RasterStoreBand rasterStoreBand = model.getElementAt(index); |
259 |
RasterStore subStore = rasterStoreBand.getStore(); |
260 |
int band = rasterStoreBand.getBand();
|
261 |
colorInterpretations.add(rasterStoreBand.getBandColorInterpretation()); |
262 |
} |
263 |
return colorInterpretations;
|
264 |
} |
265 |
|
266 |
private boolean willBeChangedBands(RasterStore store){ |
267 |
|
268 |
BandsTableModel model = (BandsTableModel) tblBands.getModel(); |
269 |
|
270 |
if(store.getBands()!=model.getRowCount()){
|
271 |
return true; |
272 |
} |
273 |
ColorInterpretation colorInterpretation = this.layer.getLegend().getColorInterpretation();
|
274 |
for (int index = 0; index < model.getRowCount(); index++) { |
275 |
RasterStoreBand rasterStoreBand = model.getElementAt(index); |
276 |
RasterStore subStore = rasterStoreBand.getStore(); |
277 |
int band = rasterStoreBand.getBand();
|
278 |
BandDescriptor bandDescriptor = store.getBandDescriptor(index); |
279 |
if (bandDescriptor.getStore() != subStore || bandDescriptor.getBand() != band
|
280 |
|| isChangedNoData(bandDescriptor.getNoData(), rasterStoreBand.getNoDataNumber()) |
281 |
|| !(rasterStoreBand.getBandColorInterpretation().equals(colorInterpretation.get(index))) |
282 |
){ |
283 |
return true; |
284 |
} |
285 |
} |
286 |
return false; |
287 |
} |
288 |
|
289 |
private boolean isChangedNoData(NoData noData, Number number){ |
290 |
if(noData!=null){ |
291 |
return noData.getValue()!=number;
|
292 |
} else {
|
293 |
return number!=null; |
294 |
} |
295 |
} |
296 |
|
297 |
@Override
|
298 |
public JComponent asJComponent() { |
299 |
return this; |
300 |
} |
301 |
|
302 |
@SuppressWarnings("unchecked") |
303 |
@Override
|
304 |
public void actionPerformed(ActionEvent e) { |
305 |
if (e.getSource() == btnRemove) {
|
306 |
BandsTableModel model = (BandsTableModel) tblBands.getModel(); |
307 |
model.removeElementAt(tblBands.getSelectedRow()); |
308 |
model.fireTableDataChanged(); |
309 |
|
310 |
} else if (e.getSource() == btnAdd) { |
311 |
DataManager manager = DALLocator.getDataManager(); |
312 |
|
313 |
JFileChooser fileChooser = createJFileChooser();
|
314 |
int result = fileChooser.showOpenDialog(this); |
315 |
|
316 |
if (result == JFileChooser.APPROVE_OPTION) { |
317 |
File[] files = fileChooser.getSelectedFiles(); |
318 |
for (File file : files) { |
319 |
|
320 |
DataServerExplorerParameters fileExplorerParams; |
321 |
try {
|
322 |
fileExplorerParams = manager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
323 |
FilesystemServerExplorer explorer = |
324 |
(FilesystemServerExplorer) manager.openServerExplorer(FilesystemServerExplorer.NAME, |
325 |
fileExplorerParams); |
326 |
String type = explorer.getProviderName(file);
|
327 |
DynObject params = manager.createStoreParameters(type); |
328 |
if (params.getDynClass().getDynField("file") != null) { |
329 |
params.setDynValue("file", file);
|
330 |
} |
331 |
// FIXME: ?se le pone la proyecci?n del raster principal
|
332 |
// o se busca la que tenga el archivo si la tiene?
|
333 |
if (params.getDynClass().getDynField("crs") != null) { |
334 |
params.setDynValue("crs", this.layer.getProjection()); |
335 |
} |
336 |
RasterStore store = (RasterStore) manager.openStore(type, params); |
337 |
|
338 |
addBands(store, null);
|
339 |
|
340 |
} catch (InitializeException | ProviderNotRegisteredException | ValidateDataParametersException e1) {
|
341 |
LOG.warn("Can't add bands from the file '" + file.getName() + "'", e1); |
342 |
// e1.printStackTrace();
|
343 |
} |
344 |
} |
345 |
} |
346 |
} |
347 |
} |
348 |
|
349 |
@Override
|
350 |
public void valueChanged(ListSelectionEvent e) { |
351 |
if (e.getSource() == tblBands.getSelectionModel()) {
|
352 |
if (tblBands.getSelectedRow() >= 0) { |
353 |
if (this.layer.getRasterStore() != ((BandsTableModel) tblBands.getModel()).getElementAt( |
354 |
tblBands.getSelectedRow()).getStore()) { |
355 |
btnRemove.setEnabled(true);
|
356 |
} else {
|
357 |
btnRemove.setEnabled(false);
|
358 |
} |
359 |
} else {
|
360 |
btnRemove.setEnabled(false);
|
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
private JFileChooser createJFileChooser() { |
366 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
367 |
JFileChooser fileChooser = new JFileChooser(); |
368 |
|
369 |
int mode = FilesystemServerExplorer.MODE_RASTER;
|
370 |
FilesystemServerExplorer explorer; |
371 |
|
372 |
DataManager dm = DALLocator.getDataManager(); |
373 |
FilesystemServerExplorerParameters param; |
374 |
try {
|
375 |
param = |
376 |
(FilesystemServerExplorerParameters) dm.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
377 |
explorer = (FilesystemServerExplorer) dm.openServerExplorer(FilesystemServerExplorer.NAME, param); |
378 |
} catch (Exception e) { |
379 |
throw new RuntimeException(e); |
380 |
} |
381 |
|
382 |
FilesystemFileFilter filter = explorer.getFilter(mode, i18nManager.getTranslation(RASTER_FILTER)); |
383 |
|
384 |
fileChooser.setMultiSelectionEnabled(true);
|
385 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
386 |
fileChooser.setFileFilter(new MyFileFilter(filter));
|
387 |
|
388 |
return fileChooser;
|
389 |
} |
390 |
|
391 |
/**
|
392 |
* @author fdiaz
|
393 |
*
|
394 |
*/
|
395 |
public class MyFileFilter extends FileFilter { |
396 |
|
397 |
public FilesystemFileFilter filter = null; |
398 |
|
399 |
/**
|
400 |
* @param params
|
401 |
*/
|
402 |
public MyFileFilter(FilesystemFileFilter params) {
|
403 |
this.filter = params;
|
404 |
} |
405 |
|
406 |
/**
|
407 |
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
|
408 |
*/
|
409 |
public boolean accept(File f) { |
410 |
if (f.isDirectory()) {
|
411 |
return true; |
412 |
} |
413 |
return filter.accept(f);
|
414 |
|
415 |
} |
416 |
|
417 |
/**
|
418 |
* @see javax.swing.filechooser.FileFilter#getDescription()
|
419 |
*/
|
420 |
public String getDescription() { |
421 |
return filter.getDescription();
|
422 |
} |
423 |
|
424 |
/**
|
425 |
* @return
|
426 |
*/
|
427 |
public String getName() { |
428 |
return filter.getDataStoreProviderName();
|
429 |
} |
430 |
} |
431 |
} |