svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / legend / gui / legendactions / SaveLegendAction.java @ 43599
History | View | Annotate | Download (3.59 KB)
1 |
package org.gvsig.app.project.documents.view.legend.gui.legendactions; |
---|---|
2 |
|
3 |
import java.awt.event.ActionEvent; |
4 |
import java.io.File; |
5 |
import java.util.List; |
6 |
|
7 |
import javax.swing.Action; |
8 |
import javax.swing.JFileChooser; |
9 |
import javax.swing.JOptionPane; |
10 |
|
11 |
import org.slf4j.Logger; |
12 |
import org.slf4j.LoggerFactory; |
13 |
|
14 |
import org.gvsig.andami.PluginServices; |
15 |
import org.gvsig.app.ApplicationLocator; |
16 |
import org.gvsig.app.project.documents.view.legend.gui.LegendFileFilter; |
17 |
import org.gvsig.app.project.documents.view.legend.gui.LegendsPanel; |
18 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
19 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
20 |
import org.gvsig.fmap.mapcontext.exceptions.WriteLegendException; |
21 |
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend; |
22 |
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter; |
23 |
import org.gvsig.i18n.Messages; |
24 |
|
25 |
public class SaveLegendAction extends AbstractLoadSaveLegendAction { |
26 |
|
27 |
private static final long serialVersionUID = -382469306285635077L; |
28 |
private static final Logger logger = LoggerFactory.getLogger(SaveLegendAction.class); |
29 |
|
30 |
public SaveLegendAction() {
|
31 |
super();
|
32 |
this.putValue(Action.NAME, PluginServices.getText(this, "Guardar_leyenda") + "..."); |
33 |
} |
34 |
|
35 |
@Override
|
36 |
public void actionPerformed(ActionEvent e) { |
37 |
if( ! (e.getSource() instanceof LegendsPanel) ) { |
38 |
return;
|
39 |
} |
40 |
try {
|
41 |
saveLegend((LegendsPanel) e.getSource()); |
42 |
} catch (Exception exc) { |
43 |
logger.warn("_Unable_to_save_legend", exc);
|
44 |
JOptionPane.showMessageDialog(
|
45 |
ApplicationLocator.getManager().getRootComponent(), |
46 |
Messages.getText("_Unable_to_save_legend")
|
47 |
+ "\n\n" + exc.getMessage(),
|
48 |
Messages.getText("legend"),
|
49 |
JOptionPane.WARNING_MESSAGE);
|
50 |
} |
51 |
} |
52 |
|
53 |
private void saveLegend(LegendsPanel legends) throws Exception { |
54 |
|
55 |
List<LegendFileFilter> filters = getLegendFileFilters(true); |
56 |
if( filters.isEmpty() ) {
|
57 |
JOptionPane.showMessageDialog(
|
58 |
ApplicationLocator.getManager().getRootComponent(), |
59 |
Messages.getText("_No_output_format_available"),
|
60 |
Messages.getText("legend"),
|
61 |
JOptionPane.WARNING_MESSAGE);
|
62 |
return;
|
63 |
} |
64 |
|
65 |
ILegend legend = legends.getLegend(); |
66 |
MapContextManager mcoman = MapContextLocator.getMapContextManager(); |
67 |
|
68 |
JFileChooser fileChooser = new JFileChooser(); |
69 |
fileChooser.setAcceptAllFileFilterUsed(false);
|
70 |
|
71 |
for( int i = 0; i < filters.size(); i++ ) { |
72 |
fileChooser.addChoosableFileFilter(filters.get(i)); |
73 |
} |
74 |
|
75 |
if( fileChooser.showSaveDialog(
|
76 |
ApplicationLocator.getManager().getRootComponent()) |
77 |
== JFileChooser.APPROVE_OPTION ) {
|
78 |
|
79 |
LegendFileFilter lff = (LegendFileFilter) fileChooser.getFileFilter(); |
80 |
|
81 |
File selfile = fileChooser.getSelectedFile();
|
82 |
if( !selfile.getAbsolutePath().toLowerCase().endsWith(
|
83 |
"." + lff.getFileExtension().toLowerCase()) ) {
|
84 |
|
85 |
selfile = new File(selfile.getAbsolutePath() |
86 |
+ "." + lff.getFileExtension().toLowerCase());
|
87 |
} |
88 |
|
89 |
ILegendWriter lw = mcoman.createLegendWriter( |
90 |
legend.getClass(), |
91 |
lff.getFileExtension() |
92 |
); |
93 |
|
94 |
if( lw == null ) { |
95 |
throw new WriteLegendException(legend, |
96 |
new Exception("Unable to create legend writer.")); |
97 |
} |
98 |
lw.write(legend, selfile, lff.getFileExtension()); |
99 |
} |
100 |
} |
101 |
|
102 |
} |