gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.app / org.gvsig.raster.app.mainplugin / src / main / java / org / gvsig / raster / app / mainplugin / colortable / ColorTablesLibraryExtension.java @ 6899
History | View | Annotate | Download (8.85 KB)
1 |
package org.gvsig.raster.app.mainplugin.colortable; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.io.File; |
7 |
import java.util.Iterator; |
8 |
import java.util.List; |
9 |
|
10 |
import org.apache.commons.io.FilenameUtils; |
11 |
import org.apache.commons.lang3.StringUtils; |
12 |
import org.apache.commons.lang3.tuple.Pair; |
13 |
import org.slf4j.Logger; |
14 |
import org.slf4j.LoggerFactory; |
15 |
|
16 |
import org.gvsig.andami.IconThemeHelper; |
17 |
import org.gvsig.andami.PluginServices; |
18 |
import org.gvsig.andami.PluginsLocator; |
19 |
import org.gvsig.andami.plugins.Extension; |
20 |
import org.gvsig.app.ApplicationLocator; |
21 |
import org.gvsig.app.ApplicationManager; |
22 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
23 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
24 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
25 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIOFactory; |
26 |
import org.gvsig.raster.lib.legend.api.exceptions.ColorTableIOException; |
27 |
import org.gvsig.raster.swing.legend.RasterSwingLegendLocator; |
28 |
import org.gvsig.raster.swing.legend.RasterSwingLegendManager; |
29 |
import org.gvsig.raster.swing.legend.colortable.selector.ColorTableSelectorPanel; |
30 |
import org.gvsig.tools.ToolsLocator; |
31 |
import org.gvsig.tools.dynobject.DynObject; |
32 |
import org.gvsig.tools.i18n.I18nManager; |
33 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
34 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
35 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
36 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author fdiaz
|
41 |
*
|
42 |
*/
|
43 |
public class ColorTablesLibraryExtension extends Extension { |
44 |
|
45 |
private static final Logger LOG = LoggerFactory.getLogger(ColorTablesLibraryExtension.class); |
46 |
private List<Pair<File, ColorTable>> previousColorTables; |
47 |
|
48 |
@Override
|
49 |
public void initialize() { |
50 |
IconThemeHelper.registerIcon("action", "color-tables-library-icon", this); |
51 |
IconThemeHelper.registerIcon("colortable", "new-color-table-icon", this); |
52 |
IconThemeHelper.registerIcon("colortable", "edit-color-table-icon", this); |
53 |
IconThemeHelper.registerIcon("colortable", "import-color-table-icon", this); |
54 |
IconThemeHelper.registerIcon("colortable", "export-color-table-icon", this); |
55 |
IconThemeHelper.registerIcon("colortable", "remove-color-table-icon", this); |
56 |
|
57 |
|
58 |
IconThemeHelper.registerIcon("editcolortable","add-color-table-class-icon", this); |
59 |
IconThemeHelper.registerIcon("editcolortable","remove-color-table-class-icon", this); |
60 |
IconThemeHelper.registerIcon("editcolortable","remove-all-color-table-class-icon", this); |
61 |
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public void execute(String actionCommand) { |
66 |
ApplicationManager applicationManager = ApplicationLocator.getManager(); |
67 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
68 |
if (StringUtils.equalsIgnoreCase(actionCommand, "color-tables-library")) { |
69 |
//Mostrar una ventana con el controller
|
70 |
|
71 |
RasterSwingLegendManager rasterSwingLegendManager = RasterSwingLegendLocator.getSwingManager(); |
72 |
final ColorTableSelectorPanel panel = rasterSwingLegendManager.createColorTableSelectorPanel();
|
73 |
|
74 |
PluginServices rasterPlugin = PluginsLocator.getManager().getPlugin("org.gvsig.raster.app.mainplugin");
|
75 |
DynObject properties = rasterPlugin.getPluginProperties(); |
76 |
File defaultColorTableLibraryPath = (File) properties.getDynValue("defaultColorTableLibraryPath"); |
77 |
previousColorTables = RasterLegendLocator.getRasterLegendManager().getColorTables(defaultColorTableLibraryPath); |
78 |
// List<Triple<File, ColorTable, Modification>> modifiableColorTables = new ArrayList<Triple<File, ColorTable, Modification>>(previousColorTables.size());
|
79 |
// for (Iterator iterator = previousColorTables.iterator(); iterator.hasNext();) {
|
80 |
// Pair<File, ColorTable> pair = (Pair<File, ColorTable>) iterator.next();
|
81 |
// ImmutableTriple<File, ColorTable, Modification> triple = new ImmutableTriple<File, ColorTable, Modification>(pair.getKey(), pair.getValue(), Modification.NONE);
|
82 |
// modifiableColorTables.add(triple);
|
83 |
// }
|
84 |
panel.set(previousColorTables); |
85 |
panel.asJComponent().setPreferredSize(new Dimension(600, 400)); |
86 |
|
87 |
final WindowManager_v2 winManager = (WindowManager_v2) ToolsSwingLocator.getWindowManager();
|
88 |
|
89 |
final Dialog dialog = |
90 |
winManager.createDialog(panel.asJComponent(), i18nManager.getTranslation("color-tables-library"),
|
91 |
null, WindowManager_v2.BUTTONS_APPLY_OK_CANCEL);
|
92 |
|
93 |
dialog.addActionListener(new ActionListener() { |
94 |
|
95 |
@Override
|
96 |
public void actionPerformed(ActionEvent e) { |
97 |
switch (dialog.getAction()) {
|
98 |
case WindowManager_v2.BUTTON_APPLY:
|
99 |
doApply(panel); |
100 |
break;
|
101 |
|
102 |
case WindowManager_v2.BUTTON_OK:
|
103 |
if (doApply(panel)) {
|
104 |
panel.asJComponent().setVisible(false);
|
105 |
} |
106 |
break;
|
107 |
|
108 |
case WindowManager_v2.BUTTON_CANCEL:
|
109 |
panel.asJComponent().setVisible(false);
|
110 |
break;
|
111 |
} |
112 |
|
113 |
} |
114 |
|
115 |
}); |
116 |
dialog.show(WindowManager.MODE.WINDOW); |
117 |
} |
118 |
|
119 |
} |
120 |
|
121 |
protected boolean doApply(ColorTableSelectorPanel panel) { |
122 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
123 |
|
124 |
List<Pair<File, ColorTable>> removedColorTables = panel.getRemovedColorTables(); |
125 |
for (Pair<File, ColorTable> pair : removedColorTables) { |
126 |
File file = pair.getLeft();
|
127 |
file.delete(); |
128 |
} |
129 |
|
130 |
List<Pair<File, ColorTable>> modifiedColorTables = panel.getModifiedColorTables(); |
131 |
for (Pair<File, ColorTable> pair : modifiedColorTables) { |
132 |
|
133 |
File oldFile = null; |
134 |
File file = pair.getLeft();
|
135 |
ColorTable colorTable = pair.getRight(); |
136 |
if (!FilenameUtils.getBaseName(file.getName()).equalsIgnoreCase(colorTable.getName())) {
|
137 |
oldFile = file; |
138 |
file = new File(file.getParent(), colorTable.getName()+ "." + FilenameUtils.getExtension(file.getAbsolutePath())); |
139 |
} |
140 |
List<ColorTableIOFactory> factories = legendManager.getColorTableIOFactories();
|
141 |
for (Iterator factoriesIt = factories.iterator(); factoriesIt.hasNext();) { |
142 |
ColorTableIOFactory colorTableIOFactory = (ColorTableIOFactory) factoriesIt.next(); |
143 |
if (colorTableIOFactory.accept(file)) {
|
144 |
try {
|
145 |
colorTableIOFactory.create().write(colorTable, file); |
146 |
} catch (ColorTableIOException e) {
|
147 |
LOG.warn("Can't write color table '" + file.getName() + "'.", e); |
148 |
// FIXME: mostrar aviso al usuario
|
149 |
throw new RuntimeException("Can't write color table '" + file.getName() + "'.", e); |
150 |
} |
151 |
} |
152 |
} |
153 |
if(oldFile!=null){ |
154 |
oldFile.delete(); |
155 |
} |
156 |
} |
157 |
|
158 |
List<ColorTable> addedColorTables = panel.getAddedColorTables();
|
159 |
for (ColorTable colorTable : addedColorTables) {
|
160 |
colorTable.getName(); |
161 |
|
162 |
PluginServices rasterPlugin = |
163 |
PluginsLocator.getManager().getPlugin("org.gvsig.raster.app.mainplugin");
|
164 |
DynObject properties = rasterPlugin.getPluginProperties(); |
165 |
File defaultColorTableLibraryPath = (File) properties.getDynValue("defaultColorTableLibraryPath"); |
166 |
// FIXME: De momento creo tablas de color en formato de gvSIG
|
167 |
// (ver de coger la extension en lugar de ponerla fija)
|
168 |
File file = new File(defaultColorTableLibraryPath, colorTable.getName() + ".xml"); |
169 |
List<ColorTableIOFactory> factories = legendManager.getColorTableIOFactories();
|
170 |
for (Iterator factoriesIt = factories.iterator(); factoriesIt.hasNext();) { |
171 |
ColorTableIOFactory colorTableIOFactory = (ColorTableIOFactory) factoriesIt.next(); |
172 |
if (colorTableIOFactory.accept(file)) {
|
173 |
try {
|
174 |
colorTableIOFactory.create().write(colorTable, file); |
175 |
} catch (ColorTableIOException e) {
|
176 |
LOG.warn("Can't write color table '" + file.getName() + "'.", e); |
177 |
// FIXME: mostrar aviso al usuario
|
178 |
throw new RuntimeException("Can't write color table '" + file.getName() + "'.", e); |
179 |
} |
180 |
} |
181 |
} |
182 |
} |
183 |
return true; |
184 |
} |
185 |
|
186 |
@Override
|
187 |
public boolean isEnabled() { |
188 |
return true; |
189 |
} |
190 |
|
191 |
@Override
|
192 |
public boolean isVisible() { |
193 |
return true; |
194 |
} |
195 |
} |