root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / ui / library / ColorTableLibraryPanel.java @ 20815
History | View | Annotate | Download (17.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.colortable.ui.library; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.FlowLayout; |
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.io.File; |
27 |
import java.io.IOException; |
28 |
import java.util.ArrayList; |
29 |
import java.util.EventObject; |
30 |
import java.util.Iterator; |
31 |
|
32 |
import javax.swing.ImageIcon; |
33 |
import javax.swing.JButton; |
34 |
import javax.swing.JFileChooser; |
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.JPanel; |
37 |
import javax.swing.JScrollPane; |
38 |
|
39 |
import org.gvsig.gui.beans.listview.ListViewComponent; |
40 |
import org.gvsig.gui.beans.listview.ListViewItem; |
41 |
import org.gvsig.gui.beans.listview.ListViewListener; |
42 |
import org.gvsig.raster.datastruct.ColorTable; |
43 |
import org.gvsig.raster.datastruct.io.RasterLegendIO; |
44 |
import org.gvsig.raster.datastruct.io.exceptions.RasterLegendIONotFound; |
45 |
import org.gvsig.raster.datastruct.serializer.ColorTableLibraryPersistence; |
46 |
import org.gvsig.raster.util.ExtendedFileFilter; |
47 |
import org.gvsig.raster.util.PanelBase; |
48 |
import org.gvsig.raster.util.RasterToolsUtil; |
49 |
import org.gvsig.raster.util.RasterUtilities; |
50 |
|
51 |
import com.iver.andami.PluginServices; |
52 |
/**
|
53 |
* Panel que aparece en la parte inferior derecha que contiene la lista
|
54 |
* de librerias disponibles para las tablas de color, con sus botones correspondientes
|
55 |
* para importar, exportar, borrar y a?adir librerias.
|
56 |
*
|
57 |
* 19/02/2008
|
58 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
59 |
*/
|
60 |
public class ColorTableLibraryPanel extends PanelBase implements ActionListener, ListViewListener { |
61 |
private JPanel panelButtons = null; |
62 |
private JButton buttonAdd = null; |
63 |
private JButton buttonDel = null; |
64 |
private JButton buttonImport = null; |
65 |
private JButton buttonExport = null; |
66 |
private ListViewComponent listViewComponent = null; |
67 |
|
68 |
private boolean hasChanged = false; |
69 |
|
70 |
private ArrayList actionCommandListeners = new ArrayList(); |
71 |
|
72 |
private String palettesPath = System.getProperty("user.home") + |
73 |
File.separator +
|
74 |
"gvSIG" + // PluginServices.getArguments()[0] + |
75 |
File.separator + "colortable"; |
76 |
|
77 |
public ColorTableLibraryPanel() {
|
78 |
initialize(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Inicializa el panel de la libreria de tablas de color
|
83 |
*/
|
84 |
private void initialize() { |
85 |
getPanel().setLayout(new BorderLayout()); |
86 |
|
87 |
JPanel panel = new JPanel(); |
88 |
panel.setLayout(new BorderLayout()); |
89 |
panel.setBorder(javax.swing.BorderFactory.createEmptyBorder(8, 8, 8, 8)); |
90 |
|
91 |
JScrollPane jScrollPane = new JScrollPane(); |
92 |
jScrollPane.setViewportView(getListViewComponent()); |
93 |
jScrollPane.setAutoscrolls(true);
|
94 |
|
95 |
panel.add(jScrollPane, BorderLayout.CENTER);
|
96 |
getPanel().add(panel, BorderLayout.CENTER);
|
97 |
getPanel().add(getPanelButtons(), BorderLayout.SOUTH);
|
98 |
getPanel().setPreferredSize(new Dimension(0, 192)); |
99 |
|
100 |
loadDiskLibrary(); |
101 |
} |
102 |
|
103 |
private void loadDiskLibrary() { |
104 |
ArrayList fileList = ColorTableLibraryPersistence.getPaletteFileList(palettesPath);
|
105 |
for (int i = 0; i < fileList.size(); i++) { |
106 |
ArrayList paletteItems = new ArrayList(); |
107 |
String paletteName = ColorTableLibraryPersistence.loadPalette(palettesPath, (String) fileList.get(i), paletteItems); |
108 |
|
109 |
if (paletteItems.size() <= 0) |
110 |
continue;
|
111 |
|
112 |
ColorTable colorTable = new ColorTable();
|
113 |
colorTable.setName(paletteName); |
114 |
colorTable.createPaletteFromColorItems(paletteItems, true);
|
115 |
colorTable.setInterpolated(true);
|
116 |
|
117 |
ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), paletteName); |
118 |
item.setTag(fileList.get(i)); |
119 |
getListViewComponent().addItem(item); |
120 |
} |
121 |
getListViewComponent().setSelectedIndex(0);
|
122 |
callColorTableChanged(); |
123 |
} |
124 |
|
125 |
/**
|
126 |
* Devuelve el jButtonAdd
|
127 |
* @return
|
128 |
*/
|
129 |
private JButton getButtonAdd() { |
130 |
if (buttonAdd == null) { |
131 |
ImageIcon icon = null; |
132 |
try {
|
133 |
icon = PluginServices.getIconTheme().get("addlayer-icon");
|
134 |
} catch (NullPointerException n) { |
135 |
// No ha encontrado el icono, no lo muestra.
|
136 |
} |
137 |
|
138 |
buttonAdd = new JButton(icon); |
139 |
buttonAdd.setPreferredSize(new Dimension(22, 19)); |
140 |
buttonAdd.addActionListener(this);
|
141 |
buttonAdd.setToolTipText(getText(this, "nueva_libreria_title")); |
142 |
} |
143 |
return buttonAdd;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Devuelve el jButtonDel
|
148 |
* @return
|
149 |
*/
|
150 |
private JButton getButtonDel() { |
151 |
if (buttonDel == null) { |
152 |
ImageIcon icon = null; |
153 |
try {
|
154 |
icon = PluginServices.getIconTheme().get("delall-icon");
|
155 |
} catch (NullPointerException n) { |
156 |
// No ha encontrado el icono, no lo muestra.
|
157 |
} |
158 |
buttonDel = new JButton(icon); |
159 |
buttonDel.setPreferredSize(new Dimension(22, 19)); |
160 |
buttonDel.addActionListener(this);
|
161 |
buttonDel.setToolTipText(getText(this, "borrar_libreria")); |
162 |
} |
163 |
return buttonDel;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* Devuelve el jButtonDel
|
168 |
* @return
|
169 |
*/
|
170 |
private JButton getButtonImport() { |
171 |
if (buttonImport == null) { |
172 |
ImageIcon icon = null; |
173 |
try {
|
174 |
icon = PluginServices.getIconTheme().get("import-icon");
|
175 |
} catch (NullPointerException n) { |
176 |
// No ha encontrado el icono, no lo muestra.
|
177 |
} |
178 |
buttonImport = new JButton(icon); |
179 |
buttonImport.setPreferredSize(new Dimension(22, 19)); |
180 |
buttonImport.addActionListener(this);
|
181 |
buttonImport.setToolTipText(getText(this, "import_libreria")); |
182 |
} |
183 |
return buttonImport;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* Devuelve el jButtonDel
|
188 |
* @return
|
189 |
*/
|
190 |
private JButton getButtonExport() { |
191 |
if (buttonExport == null) { |
192 |
ImageIcon icon = null; |
193 |
try {
|
194 |
icon = PluginServices.getIconTheme().get("export-icon");
|
195 |
} catch (NullPointerException n) { |
196 |
// No ha encontrado el icono, no lo muestra.
|
197 |
} |
198 |
buttonExport = new JButton(icon); |
199 |
buttonExport.setPreferredSize(new Dimension(22, 19)); |
200 |
buttonExport.addActionListener(this);
|
201 |
buttonExport.setToolTipText(getText(this, "export_libreria")); |
202 |
} |
203 |
return buttonExport;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Devuelve el panel de botones de la libreria de a?adir y borrar
|
208 |
* @return
|
209 |
*/
|
210 |
private JPanel getPanelButtons() { |
211 |
if (panelButtons == null) { |
212 |
panelButtons = new JPanel(); |
213 |
panelButtons.setPreferredSize(new Dimension(0, 21)); |
214 |
|
215 |
FlowLayout flowLayout5 = new FlowLayout(); |
216 |
flowLayout5.setHgap(5);
|
217 |
flowLayout5.setVgap(0);
|
218 |
flowLayout5.setAlignment(java.awt.FlowLayout.CENTER); |
219 |
panelButtons.setLayout(flowLayout5); |
220 |
|
221 |
panelButtons.add(getButtonAdd(), null);
|
222 |
panelButtons.add(getButtonExport(), null);
|
223 |
panelButtons.add(getButtonImport(), null);
|
224 |
panelButtons.add(getButtonDel(), null);
|
225 |
} |
226 |
return panelButtons;
|
227 |
} |
228 |
|
229 |
private ListViewComponent getListViewComponent() {
|
230 |
if (listViewComponent == null) { |
231 |
listViewComponent = new ListViewComponent();
|
232 |
listViewComponent.addListSelectionListener(this);
|
233 |
listViewComponent.setEditable(true);
|
234 |
} |
235 |
return listViewComponent;
|
236 |
} |
237 |
|
238 |
private void callColorTableChanged() { |
239 |
Iterator acIterator = actionCommandListeners.iterator();
|
240 |
while (acIterator.hasNext()) {
|
241 |
ColorTableLibraryListener listener = (ColorTableLibraryListener) acIterator.next(); |
242 |
listener.actionColorTableChanged(new ColorTableLibraryEvent(this)); |
243 |
} |
244 |
} |
245 |
|
246 |
/**
|
247 |
* Accion que se ejecuta cuando se disparan los siguientes eventos:
|
248 |
* - Checkbox de interpolacion.
|
249 |
* - Checkbox de habilitacion de colortables.
|
250 |
* - Boton de a?adir un colortable nuevo.
|
251 |
* - Boton de borrar un colortable seleccionado.
|
252 |
*/
|
253 |
public void actionPerformed(ActionEvent e) { |
254 |
if (e.getSource() == getButtonAdd()) {
|
255 |
AddLibrary addLibrary = new AddLibrary();
|
256 |
if (addLibrary.showConfirm(getPanel()) == JOptionPane.OK_OPTION) { |
257 |
ColorTable colorTable = addLibrary.getColorTable(); |
258 |
if (colorTable != null) { |
259 |
ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), colorTable.getName()); |
260 |
item.setTag(item.getName() + ".xml");
|
261 |
getListViewComponent().addItem(item); |
262 |
getListViewComponent().setSelectedIndex(getListViewComponent().getItems().size() - 1);
|
263 |
((ColorTableIconPainter) item.getIcon()).getColorTable().setName(item.getName()); |
264 |
colorTable.setName(((ColorTableIconPainter) item.getIcon()).getColorTable().getName()); |
265 |
ColorTableLibraryPersistence.save_to_1_1(palettesPath, colorTable); |
266 |
callColorTableChanged(); |
267 |
} |
268 |
} |
269 |
} |
270 |
|
271 |
if (e.getSource() == getButtonExport()) {
|
272 |
JFileChooser chooser = new JFileChooser(); |
273 |
chooser.setAcceptAllFileFilterUsed(false);
|
274 |
|
275 |
String[] formats = RasterLegendIO.getFormats(); |
276 |
ExtendedFileFilter fileFilter = null;
|
277 |
ExtendedFileFilter firstFileFilter = null;
|
278 |
for (int i = 0; i < formats.length; i++) { |
279 |
fileFilter = new ExtendedFileFilter();
|
280 |
fileFilter.addExtension(formats[i]); |
281 |
|
282 |
try {
|
283 |
String desc = RasterLegendIO.getRasterLegendIO(RasterUtilities.getExtensionFromFileName(formats[i])).getDescription();
|
284 |
if (desc != null) |
285 |
fileFilter.setDescription(desc); |
286 |
} catch (RasterLegendIONotFound e1) {
|
287 |
// Si no puedo pillar la descripcion no pasa nada
|
288 |
} |
289 |
if (firstFileFilter == null) |
290 |
firstFileFilter = fileFilter; |
291 |
|
292 |
chooser.addChoosableFileFilter(fileFilter); |
293 |
} |
294 |
if (firstFileFilter != null) |
295 |
chooser.setFileFilter(firstFileFilter); |
296 |
|
297 |
int returnVal = chooser.showSaveDialog(getPanel());
|
298 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
299 |
try {
|
300 |
ExtendedFileFilter filter = (ExtendedFileFilter) chooser.getFileFilter(); |
301 |
String file = filter.getNormalizedFilename(chooser.getSelectedFile());
|
302 |
|
303 |
RasterLegendIO.getRasterLegendIO(RasterUtilities.getExtensionFromFileName(file)).write(getColorTableSelected(), new File(file)); |
304 |
} catch (IOException e1) { |
305 |
e1.printStackTrace(); |
306 |
} catch (RasterLegendIONotFound e1) {
|
307 |
e1.printStackTrace(); |
308 |
} |
309 |
} |
310 |
} |
311 |
|
312 |
if (e.getSource() == getButtonImport()) {
|
313 |
JFileChooser chooser = new JFileChooser(); |
314 |
chooser.setAcceptAllFileFilterUsed(false);
|
315 |
chooser.setMultiSelectionEnabled(true);
|
316 |
|
317 |
String[] formats = RasterLegendIO.getFormats(); |
318 |
ExtendedFileFilter allFilters = new ExtendedFileFilter();
|
319 |
ExtendedFileFilter fileFilter = null;
|
320 |
for (int i = 0; i < formats.length; i++) { |
321 |
fileFilter = new ExtendedFileFilter();
|
322 |
fileFilter.addExtension(formats[i]); |
323 |
allFilters.addExtension(formats[i]); |
324 |
|
325 |
try {
|
326 |
String desc = RasterLegendIO.getRasterLegendIO(RasterUtilities.getExtensionFromFileName(formats[i])).getDescription();
|
327 |
if (desc != null) |
328 |
fileFilter.setDescription(desc); |
329 |
} catch (RasterLegendIONotFound e1) {
|
330 |
// Si no puedo pillar la descripcion no pasa nada
|
331 |
} |
332 |
|
333 |
chooser.addChoosableFileFilter(fileFilter); |
334 |
} |
335 |
allFilters.setDescription(getText(this, "todos_soportados")); |
336 |
chooser.addChoosableFileFilter(allFilters); |
337 |
chooser.setFileFilter(allFilters); |
338 |
|
339 |
int returnVal = chooser.showOpenDialog(getPanel());
|
340 |
|
341 |
if (returnVal == JFileChooser.APPROVE_OPTION) { |
342 |
try {
|
343 |
for (int file = 0; file<chooser.getSelectedFiles().length; file++) { |
344 |
fileFilter = null;
|
345 |
for (int i = 0; i < formats.length; i++) { |
346 |
fileFilter = new ExtendedFileFilter();
|
347 |
fileFilter.addExtension(formats[i]); |
348 |
if (fileFilter.accept(chooser.getSelectedFiles()[file])) {
|
349 |
ColorTable colorTable = RasterLegendIO.getRasterLegendIO(formats[i]).read(chooser.getSelectedFiles()[file]); |
350 |
|
351 |
colorTable.setInterpolated(true);
|
352 |
|
353 |
ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), colorTable.getName()); |
354 |
item.setTag(item.getName() + ".xml");
|
355 |
getListViewComponent().addItem(item); |
356 |
getListViewComponent().setSelectedIndex(getListViewComponent().getItems().size() - 1);
|
357 |
((ColorTableIconPainter) item.getIcon()).getColorTable().setName(item.getName()); |
358 |
colorTable.setName(((ColorTableIconPainter) item.getIcon()).getColorTable().getName()); |
359 |
ColorTableLibraryPersistence.save_to_1_1(palettesPath, colorTable); |
360 |
callColorTableChanged(); |
361 |
break;
|
362 |
} |
363 |
} |
364 |
} |
365 |
} catch (IOException e1) { |
366 |
e1.printStackTrace(); |
367 |
} catch (RasterLegendIONotFound e1) {
|
368 |
e1.printStackTrace(); |
369 |
} |
370 |
} |
371 |
} |
372 |
|
373 |
if (e.getSource() == getButtonDel()) {
|
374 |
if (getListViewComponent().getSelectedIndices().length > 0) { |
375 |
if (RasterToolsUtil.messageBoxYesOrNot("desea_borrar_librerias", this)) { |
376 |
File oldFile = new File(palettesPath + File.separator + getListViewComponent().getSelectedValue().getTag()); |
377 |
oldFile.delete(); |
378 |
int pos = getListViewComponent().getSelectedIndices()[0]; |
379 |
getListViewComponent().removeSelecteds(); |
380 |
|
381 |
if (getListViewComponent().getItems().size() == 0) { |
382 |
loadDiskLibrary(); |
383 |
} else {
|
384 |
getListViewComponent().setSelectedIndex(pos - 1);
|
385 |
callColorTableChanged(); |
386 |
} |
387 |
} |
388 |
} |
389 |
return;
|
390 |
} |
391 |
} |
392 |
|
393 |
/*
|
394 |
* (non-Javadoc)
|
395 |
* @see org.gvsig.gui.beans.listview.ListViewListener#actionItemNameChanged(java.util.EventObject, java.lang.String, org.gvsig.gui.beans.listview.ListViewItem)
|
396 |
*/
|
397 |
public void actionItemNameChanged(EventObject e, String oldName, ListViewItem item) { |
398 |
File oldFile = new File(palettesPath + File.separator + oldName + ".xml"); |
399 |
oldFile.delete(); |
400 |
|
401 |
getColorTableSelected().setName(item.getName()); |
402 |
item.setTag(item.getName() + ".xml");
|
403 |
|
404 |
ColorTableLibraryPersistence.save_to_1_1(palettesPath, getColorTableSelected()); |
405 |
|
406 |
callColorTableChanged(); |
407 |
} |
408 |
|
409 |
/**
|
410 |
* Comprueba si el usuario ha cambiado los valores de la tabla de colores,
|
411 |
* si es as?, le pregunta al usuario si quiere guardar los datos, y seg?n la
|
412 |
* selecci?n, restaura los datos antiguos o los machaca.
|
413 |
*/
|
414 |
private void testLibraryChanged() { |
415 |
if (hasChanged) {
|
416 |
if (RasterToolsUtil.messageBoxYesOrNot("desea_guardar_cambios", this)) { |
417 |
ColorTableLibraryPersistence.save_to_1_1(palettesPath, getColorTableSelected()); |
418 |
getListViewComponent().repaint(); |
419 |
} |
420 |
hasChanged = false;
|
421 |
} |
422 |
} |
423 |
|
424 |
/**
|
425 |
* Selecciona la tabla de color por defecto.
|
426 |
*/
|
427 |
public void selectDefault() { |
428 |
int selected = 0; |
429 |
for (int i = 0; i < getListViewComponent().getItems().size(); i++) { |
430 |
if (((ListViewItem) getListViewComponent().getItems().get(i)).getName().equals("Default")) { |
431 |
selected = i; |
432 |
break;
|
433 |
} |
434 |
} |
435 |
getListViewComponent().setSelectedIndex(selected); |
436 |
callColorTableChanged(); |
437 |
} |
438 |
|
439 |
/**
|
440 |
* Inserta una tabla de color en la posicion especificada y selecciona.
|
441 |
*/
|
442 |
public void addColorTable(int pos, ColorTable colorTable) { |
443 |
ListViewItem item = new ListViewItem(new ColorTableIconPainter(colorTable), getText(this, "tabla_actual")); |
444 |
getListViewComponent().addItem(pos, item); |
445 |
getListViewComponent().setSelectedIndex(pos); |
446 |
callColorTableChanged(); |
447 |
} |
448 |
|
449 |
/**
|
450 |
* Accion que se ejecuta cuando cambia la seleccion de un item del
|
451 |
* ListViewComponent
|
452 |
*/
|
453 |
public void actionValueChanged(EventObject e) { |
454 |
testLibraryChanged(); |
455 |
callColorTableChanged(); |
456 |
} |
457 |
|
458 |
/**
|
459 |
* Define si se visualizara el componente con interpolacion o no.
|
460 |
* @param enabled
|
461 |
*/
|
462 |
public void setInterpolated(boolean enabled) { |
463 |
for (int i = 0; i < getListViewComponent().getItems().size(); i++) |
464 |
((ColorTableIconPainter) ((ListViewItem) getListViewComponent().getItems().get(i)).getIcon()).setInterpolated(enabled); |
465 |
getListViewComponent().repaint(); |
466 |
} |
467 |
|
468 |
/**
|
469 |
* A?adir un listener a la lista de eventos
|
470 |
* @param listener
|
471 |
*/
|
472 |
public void addColorTableLibraryListener(ColorTableLibraryListener listener) { |
473 |
if (!actionCommandListeners.contains(listener))
|
474 |
actionCommandListeners.add(listener); |
475 |
} |
476 |
|
477 |
/**
|
478 |
* Borrar un listener de la lista de eventos
|
479 |
* @param listener
|
480 |
*/
|
481 |
public void removeColorTableLibraryListener(ColorTableLibraryListener listener) { |
482 |
actionCommandListeners.remove(listener); |
483 |
} |
484 |
|
485 |
/**
|
486 |
* Devuelve la tabla de color que hay seleccionada en ese momento
|
487 |
* @return
|
488 |
*/
|
489 |
public ColorTable getColorTableSelected() {
|
490 |
return ((ColorTableIconPainter) getListViewComponent().getSelectedValue().getIcon()).getColorTable();
|
491 |
} |
492 |
|
493 |
/**
|
494 |
* Reescribe la tabla de color en la posicion seleccionada
|
495 |
* @param colorTable
|
496 |
*/
|
497 |
public void setColorTableSelected(ColorTable colorTable) { |
498 |
hasChanged = true;
|
499 |
((ColorTableIconPainter) getListViewComponent().getSelectedValue().getIcon()).getColorTable().createPaletteFromColorItems(colorTable.getColorItems(), false);
|
500 |
getListViewComponent().repaint(); |
501 |
} |
502 |
} |