gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.legend / org.gvsig.raster.swing.legend.impl / src / main / java / org / gvsig / raster / swing / legend / impl / colortable / selector / ColorTableSelectorPanelController.java @ 6899
History | View | Annotate | Download (18.6 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.raster.swing.legend.impl.colortable.selector; |
24 |
|
25 |
import java.awt.Color; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.io.File; |
30 |
import java.util.ArrayList; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Locale; |
34 |
|
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JFileChooser; |
37 |
import javax.swing.ListSelectionModel; |
38 |
import javax.swing.event.ListSelectionEvent; |
39 |
import javax.swing.event.ListSelectionListener; |
40 |
import javax.swing.table.DefaultTableColumnModel; |
41 |
import javax.swing.table.TableColumn; |
42 |
import javax.swing.table.TableColumnModel; |
43 |
|
44 |
import org.apache.commons.io.FilenameUtils; |
45 |
import org.apache.commons.lang3.tuple.MutablePair; |
46 |
import org.apache.commons.lang3.tuple.Pair; |
47 |
import org.slf4j.Logger; |
48 |
import org.slf4j.LoggerFactory; |
49 |
|
50 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
51 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
52 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
53 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIO; |
54 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIOFactory; |
55 |
import org.gvsig.raster.lib.legend.api.colortable.MakeColorTable; |
56 |
import org.gvsig.raster.lib.legend.api.exceptions.ColorTableIOException; |
57 |
import org.gvsig.raster.swing.legend.RasterSwingLegendLocator; |
58 |
import org.gvsig.raster.swing.legend.RasterSwingLegendManager; |
59 |
import org.gvsig.raster.swing.legend.colortable.create.CreateColorTablePanel; |
60 |
import org.gvsig.raster.swing.legend.colortable.editor.ColorTableEditorPanel; |
61 |
import org.gvsig.raster.swing.legend.colortable.selector.ColorTableSelectorPanel; |
62 |
import org.gvsig.raster.swing.legend.colortable.selector.ColorTablesTableModel; |
63 |
import org.gvsig.raster.swing.legend.impl.colortable.editor.ColorTableEditorPanelController; |
64 |
import org.gvsig.tools.ToolsLocator; |
65 |
import org.gvsig.tools.i18n.I18nManager; |
66 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
67 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
68 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
69 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
70 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
71 |
|
72 |
|
73 |
/**
|
74 |
* @author fdiaz
|
75 |
*
|
76 |
*/
|
77 |
public class ColorTableSelectorPanelController extends ColorTableSelectorPanelView implements ColorTableSelectorPanel { |
78 |
|
79 |
/**
|
80 |
*
|
81 |
*/
|
82 |
private static final long serialVersionUID = -2427828721121167549L; |
83 |
private static final Logger LOG = LoggerFactory.getLogger(ColorTableSelectorPanelController.class); |
84 |
|
85 |
private List<Pair<File,ColorTable>> colorTables; |
86 |
private List<ColorTable> addedColorTables; |
87 |
private List<Pair<File,ColorTable>> modifiedColorTables; |
88 |
private List<Pair<File,ColorTable>> removedColorTables; |
89 |
private boolean editable; |
90 |
|
91 |
public ColorTableSelectorPanelController() {
|
92 |
translate(); |
93 |
this.editable = true; |
94 |
initializeComponents(); |
95 |
} |
96 |
|
97 |
/* (non-Javadoc)
|
98 |
* @see org.gvsig.tools.swing.api.Component#asJComponent()
|
99 |
*/
|
100 |
@Override
|
101 |
public JComponent asJComponent() { |
102 |
return this; |
103 |
} |
104 |
|
105 |
private void initializeComponents() { |
106 |
IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
107 |
btnNew.setIcon(iconTheme.get("new-color-table-icon"));
|
108 |
btnEdit.setIcon(iconTheme.get("edit-color-table-icon"));
|
109 |
btnExport.setIcon(iconTheme.get("export-color-table-icon"));
|
110 |
btnImport.setIcon(iconTheme.get("import-color-table-icon"));
|
111 |
btnRemove.setIcon(iconTheme.get("remove-color-table-icon"));
|
112 |
|
113 |
btnNew.addActionListener(new ActionListener() { |
114 |
@Override
|
115 |
public void actionPerformed(ActionEvent e) { |
116 |
doNewColorTable(); |
117 |
} |
118 |
}); |
119 |
|
120 |
btnEdit.addActionListener(new ActionListener() { |
121 |
@Override
|
122 |
public void actionPerformed(ActionEvent e) { |
123 |
doEditColorTable(); |
124 |
} |
125 |
}); |
126 |
|
127 |
btnExport.addActionListener(new ActionListener() { |
128 |
@Override
|
129 |
public void actionPerformed(ActionEvent e) { |
130 |
doExportColorTable(); |
131 |
} |
132 |
}); |
133 |
|
134 |
btnImport.addActionListener(new ActionListener() { |
135 |
@Override
|
136 |
public void actionPerformed(ActionEvent e) { |
137 |
doImportColorTable(); |
138 |
} |
139 |
}); |
140 |
|
141 |
btnRemove.addActionListener(new ActionListener() { |
142 |
@Override
|
143 |
public void actionPerformed(ActionEvent e) { |
144 |
doRemoveColorTable(); |
145 |
} |
146 |
}); |
147 |
|
148 |
tblColorTables.setRowSelectionAllowed(true);
|
149 |
tblColorTables.setColumnSelectionAllowed(false);
|
150 |
tblColorTables.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
151 |
tblColorTables.getSelectionModel().addListSelectionListener(new ListSelectionListener() { |
152 |
@Override
|
153 |
public void valueChanged(ListSelectionEvent e) { |
154 |
updateComponents(); |
155 |
} |
156 |
}); |
157 |
|
158 |
updateComponents(); |
159 |
|
160 |
} |
161 |
protected void doEditColorTable() { |
162 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
163 |
|
164 |
int index = this.tblColorTables.getSelectedRow(); |
165 |
ColorTable colorTable = ((ColorTablesTableModel)this.tblColorTables.getModel()).getColorTable(index).getValue();
|
166 |
//FIXME: ?Al manager?
|
167 |
final ColorTableEditorPanelController panel = new ColorTableEditorPanelController(); |
168 |
|
169 |
panel.set(colorTable); |
170 |
panel.asJComponent().setPreferredSize(new Dimension(600, 400)); |
171 |
|
172 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
173 |
|
174 |
final Dialog dialog = |
175 |
winManager.createDialog(panel.asJComponent(), i18nManager.getTranslation("color-table-edit"),
|
176 |
null, WindowManager_v2.BUTTONS_APPLY_OK_CANCEL);
|
177 |
|
178 |
dialog.addActionListener(new ActionListener() { |
179 |
|
180 |
@Override
|
181 |
public void actionPerformed(ActionEvent e) { |
182 |
switch (dialog.getAction()) {
|
183 |
case WindowManager_v2.BUTTON_APPLY:
|
184 |
doApplyEditColorTable(panel); |
185 |
break;
|
186 |
|
187 |
case WindowManager_v2.BUTTON_OK:
|
188 |
if (doApplyEditColorTable(panel)) {
|
189 |
panel.asJComponent().setVisible(false);
|
190 |
} |
191 |
break;
|
192 |
|
193 |
case WindowManager_v2.BUTTON_CANCEL:
|
194 |
panel.asJComponent().setVisible(false);
|
195 |
break;
|
196 |
} |
197 |
|
198 |
} |
199 |
|
200 |
}); |
201 |
dialog.show(WindowManager.MODE.DIALOG); |
202 |
|
203 |
} |
204 |
|
205 |
protected boolean doApplyEditColorTable(ColorTableEditorPanel panel) { |
206 |
int selectedRow = tblColorTables.getSelectedRow();
|
207 |
ColorTable colorTableEdited = panel.getColorTable(); |
208 |
if(panel.isModified()){
|
209 |
ColorTablesTableModel colorTablesTableModel = (ColorTablesTableModel)this.tblColorTables.getModel();
|
210 |
colorTablesTableModel.setColorTable(selectedRow, colorTableEdited); |
211 |
if(!modifiedColorTables.contains(colorTablesTableModel.getColorTable(selectedRow))){
|
212 |
modifiedColorTables.add(colorTablesTableModel.getColorTable(selectedRow)); |
213 |
} |
214 |
|
215 |
tblColorTables.setRowSelectionInterval(selectedRow, selectedRow); |
216 |
} |
217 |
return true; |
218 |
} |
219 |
|
220 |
protected void doRemoveColorTable() { |
221 |
int row = tblColorTables.getSelectedRow();
|
222 |
ColorTablesTableModel colorTablesTableModel = (ColorTablesTableModel)tblColorTables.getModel(); |
223 |
Pair<File, ColorTable> pair = colorTablesTableModel.getColorTable(row);
|
224 |
removedColorTables.add(pair); |
225 |
colorTablesTableModel.removeColorTable(tblColorTables.getSelectedRow()); |
226 |
} |
227 |
|
228 |
protected void doImportColorTable() { |
229 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
230 |
JFileChooser fileChooser = new JFileChooser(); |
231 |
List<ColorTableIOFactory> factories = legendManager.getColorTableIOFactories();
|
232 |
for (Iterator iterator = factories.iterator(); iterator.hasNext();) { |
233 |
ColorTableIOFactory colorTableIOFactory = (ColorTableIOFactory) iterator.next(); |
234 |
fileChooser.addChoosableFileFilter(new MyFileFilter(colorTableIOFactory));
|
235 |
} |
236 |
|
237 |
fileChooser.setMultiSelectionEnabled(true);
|
238 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
239 |
|
240 |
int result = fileChooser.showOpenDialog(this); |
241 |
|
242 |
if (result == JFileChooser.APPROVE_OPTION) { |
243 |
File[] files = fileChooser.getSelectedFiles(); |
244 |
for (File file : files) { |
245 |
for (Iterator iterator = factories.iterator(); iterator.hasNext();) { |
246 |
ColorTableIOFactory colorTableIOFactory = (ColorTableIOFactory) iterator.next(); |
247 |
if(colorTableIOFactory.accept(file)){
|
248 |
ColorTableIO colorTableIO = colorTableIOFactory.create(); |
249 |
ColorTable colorTable; |
250 |
try {
|
251 |
colorTable = colorTableIO.read(file); |
252 |
} catch (ColorTableIOException e) {
|
253 |
LOG.warn("Can't read color table '"+file.getName()+"'.", e); |
254 |
//FIXME: mostrar aviso al usuario
|
255 |
throw new RuntimeException("Can't read color table '"+file.getName()+"'.", e); |
256 |
} |
257 |
addedColorTables.add(colorTable); |
258 |
((ColorTablesTableModel)tblColorTables.getModel()).addColorTable(new MutablePair<File, ColorTable>(null,colorTable)); |
259 |
continue;
|
260 |
} |
261 |
} |
262 |
} |
263 |
} |
264 |
} |
265 |
|
266 |
protected void doExportColorTable() { |
267 |
ColorTable colorTable = |
268 |
((ColorTablesTableModel) tblColorTables.getModel()).getColorTable(tblColorTables.getSelectedRow()) |
269 |
.getValue(); |
270 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
271 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
272 |
JFileChooser fileChooser = new JFileChooser(); |
273 |
List<ColorTableIOFactory> factories = legendManager.getColorTableIOFactories();
|
274 |
for (Iterator iterator = factories.iterator(); iterator.hasNext();) { |
275 |
ColorTableIOFactory colorTableIOFactory = (ColorTableIOFactory) iterator.next(); |
276 |
fileChooser.addChoosableFileFilter(new MyFileFilter(colorTableIOFactory));
|
277 |
} |
278 |
|
279 |
fileChooser.setMultiSelectionEnabled(false);
|
280 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
281 |
|
282 |
int result = fileChooser.showSaveDialog(this); |
283 |
|
284 |
if (result == JFileChooser.APPROVE_OPTION) { |
285 |
File file = fileChooser.getSelectedFile();
|
286 |
MyFileFilter fileFilter = (MyFileFilter) fileChooser.getFileFilter(); |
287 |
File parent = file.getParentFile();
|
288 |
String filename = file.getName();
|
289 |
ColorTableIOFactory colorTableIOFactory = fileFilter.getColorTableIOFactory(); |
290 |
if (FilenameUtils.getExtension(filename).isEmpty()) {
|
291 |
file = new File(parent, filename + "." + colorTableIOFactory.getDefaultExtension()); |
292 |
} |
293 |
; |
294 |
try {
|
295 |
colorTableIOFactory.create().write(colorTable, file); |
296 |
} catch (ColorTableIOException e) {
|
297 |
LOG.warn("Can't write color table '" + file.getName() + "'.", e); |
298 |
// FIXME: mostrar aviso al usuario
|
299 |
throw new RuntimeException("Can't write color table '" + file.getName() + "'.", e); |
300 |
} |
301 |
} |
302 |
} |
303 |
|
304 |
protected void doNewColorTable() { |
305 |
RasterSwingLegendManager rasterSwingLegendManager = RasterSwingLegendLocator.getSwingManager(); |
306 |
final CreateColorTablePanel panel = rasterSwingLegendManager.createCreateColorTablePanel();
|
307 |
panel.setFromColor(new Color(0,0,0,255)); |
308 |
panel.setToColor(new Color(255,255,255,255)); |
309 |
panel.setMinimum(0);
|
310 |
panel.setMaximum(255);
|
311 |
panel.setIntervals(1);
|
312 |
panel.setMode(MakeColorTable.MODE_NUMBER_OF_INTERVALS); |
313 |
panel.setName("");
|
314 |
panel.asJComponent().setPreferredSize(new Dimension(400, 300)); |
315 |
|
316 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
317 |
|
318 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
319 |
|
320 |
final Dialog dialog = |
321 |
winManager.createDialog(panel.asJComponent(), i18nManager.getTranslation("color-tables-library"),
|
322 |
null, WindowManager_v2.BUTTONS_OK_CANCEL);
|
323 |
|
324 |
dialog.addActionListener(new ActionListener() { |
325 |
|
326 |
@Override
|
327 |
public void actionPerformed(ActionEvent e) { |
328 |
switch (dialog.getAction()) {
|
329 |
case WindowManager_v2.BUTTON_OK:
|
330 |
if (doMakeColorTable(panel)) {
|
331 |
panel.asJComponent().setVisible(false);
|
332 |
} |
333 |
break;
|
334 |
|
335 |
case WindowManager_v2.BUTTON_CANCEL:
|
336 |
panel.asJComponent().setVisible(false);
|
337 |
break;
|
338 |
} |
339 |
|
340 |
} |
341 |
|
342 |
}); |
343 |
dialog.show(WindowManager.MODE.WINDOW); |
344 |
} |
345 |
|
346 |
protected boolean doMakeColorTable(CreateColorTablePanel panel) { |
347 |
MakeColorTable makeColorTable = RasterLegendLocator.getRasterLegendManager().createMakeColorTable(); |
348 |
panel.fetch(makeColorTable); |
349 |
ColorTable colorTable = makeColorTable.getColorTable(); |
350 |
((ColorTablesTableModel)tblColorTables.getModel()).addColorTable(new MutablePair<File, ColorTable>(null, colorTable)); |
351 |
addedColorTables.add(colorTable); |
352 |
return true; |
353 |
} |
354 |
|
355 |
private void translate() { |
356 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
357 |
|
358 |
btnNew.setToolTipText(i18nManager.getTranslation(btnNew.getToolTipText())); |
359 |
btnImport.setToolTipText(i18nManager.getTranslation(btnImport.getToolTipText())); |
360 |
btnExport.setToolTipText(i18nManager.getTranslation(btnExport.getToolTipText())); |
361 |
btnRemove.setToolTipText(i18nManager.getTranslation(btnRemove.getToolTipText())); |
362 |
} |
363 |
|
364 |
/**
|
365 |
* @param locale
|
366 |
*
|
367 |
*/
|
368 |
public void setLocate(Locale locale) { |
369 |
Locale l = super.getLocale(); |
370 |
if (!l.equals(locale)) {
|
371 |
translate(); |
372 |
} |
373 |
super.setLocale(locale);
|
374 |
} |
375 |
|
376 |
|
377 |
/* (non-Javadoc)
|
378 |
* @see org.gvsig.raster.tools.swing.api.colortable.ColorTableSelector#fetch(org.gvsig.raster.lib.legend.api.ColorTable)
|
379 |
*/
|
380 |
@Override
|
381 |
public ColorTable getSelected() {
|
382 |
int row = tblColorTables.getSelectedRow();
|
383 |
return (ColorTable)((ColorTablesTableModel)tblColorTables.getModel()).getValueAt(row, 0); |
384 |
} |
385 |
|
386 |
/* (non-Javadoc)
|
387 |
* @see org.gvsig.raster.tools.swing.api.colortable.ColorTableSelector#set(java.util.List)
|
388 |
*/
|
389 |
@Override
|
390 |
public void set(List<Pair<File,ColorTable>> colorTables) { |
391 |
if(colorTables!=null){ |
392 |
this.colorTables = colorTables;
|
393 |
} else {
|
394 |
this.colorTables = new ArrayList<Pair<File,ColorTable>>(); |
395 |
} |
396 |
this.modifiedColorTables = new ArrayList<Pair<File,ColorTable>>(); |
397 |
this.addedColorTables = new ArrayList<ColorTable>(); |
398 |
this.removedColorTables = new ArrayList<Pair<File,ColorTable>>(); |
399 |
this.init();
|
400 |
} |
401 |
|
402 |
private void init() { |
403 |
|
404 |
ColorTablesTableModel tableModel = new DefaultColorTablesTableModel(this.colorTables); |
405 |
tblColorTables.setModel(tableModel); |
406 |
|
407 |
tblColorTables.setColumnModel(new DefaultTableColumnModel()); |
408 |
TableColumnModel columnModel = tblColorTables.getColumnModel();
|
409 |
columnModel.setColumnSelectionAllowed(false);
|
410 |
for (int i = 0; i < tableModel.getColumnCount(); i++) { |
411 |
TableColumn tableColumn = new TableColumn(i); |
412 |
tableColumn.setIdentifier(tableModel.getColumnName(i)); |
413 |
tableColumn.setHeaderValue(tableModel.getColumnName(i)); |
414 |
if(i==DefaultColorTablesTableModel.COLUMN_PREVIEW){
|
415 |
tableColumn.setCellRenderer(new PreviewerColorTableCellRenderer());
|
416 |
} |
417 |
columnModel.addColumn(tableColumn); |
418 |
} |
419 |
|
420 |
} |
421 |
|
422 |
private void updateComponents() { |
423 |
boolean selected = tblColorTables.getSelectedRowCount() >= 1; |
424 |
btnExport.setEnabled(selected); |
425 |
btnRemove.setEnabled(selected && this.editable);
|
426 |
btnEdit.setEnabled(selected && this.editable);
|
427 |
|
428 |
btnEdit.setVisible(this.editable);
|
429 |
btnImport.setVisible(this.editable);
|
430 |
btnNew.setVisible(this.editable);
|
431 |
btnRemove.setVisible(this.editable);
|
432 |
btnExport.setVisible(this.editable);
|
433 |
} |
434 |
|
435 |
@Override
|
436 |
public List<Pair<File, ColorTable>> getModifiedColorTables() { |
437 |
return modifiedColorTables;
|
438 |
} |
439 |
|
440 |
@Override
|
441 |
public List<Pair<File, ColorTable>> getRemovedColorTables() { |
442 |
return removedColorTables;
|
443 |
} |
444 |
|
445 |
@Override
|
446 |
public List<ColorTable> getAddedColorTables() { |
447 |
return addedColorTables;
|
448 |
} |
449 |
|
450 |
@Override
|
451 |
public void setEditable(boolean editable) { |
452 |
this.editable = editable;
|
453 |
updateComponents(); |
454 |
} |
455 |
|
456 |
/**
|
457 |
* @author fdiaz
|
458 |
*
|
459 |
*/
|
460 |
private static class MyFileFilter extends javax.swing.filechooser.FileFilter { |
461 |
|
462 |
private ColorTableIOFactory factory;
|
463 |
|
464 |
/**
|
465 |
*/
|
466 |
public MyFileFilter(ColorTableIOFactory factory) {
|
467 |
this.factory = factory;
|
468 |
} |
469 |
|
470 |
@Override
|
471 |
public boolean accept(File f) { |
472 |
if (f.isDirectory()) {
|
473 |
return true; |
474 |
} |
475 |
return this.getColorTableIOFactory().accept(f); |
476 |
} |
477 |
|
478 |
@Override
|
479 |
public String getDescription() { |
480 |
return this.getColorTableIOFactory().getName(); |
481 |
} |
482 |
|
483 |
/**
|
484 |
* @return the fileFilter
|
485 |
*/
|
486 |
public ColorTableIOFactory getColorTableIOFactory() {
|
487 |
return this.factory; |
488 |
} |
489 |
} |
490 |
|
491 |
|
492 |
} |