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 |
|
* For more information, contact:
|
20 |
|
*
|
21 |
|
* Generalitat Valenciana
|
22 |
|
* Conselleria d'Infraestructures i Transport
|
23 |
|
* Av. Blasco Ib??ez, 50
|
24 |
|
* 46010 VALENCIA
|
25 |
|
* SPAIN
|
26 |
|
*
|
27 |
|
* +34 963862235
|
28 |
|
* gvsig@gva.es
|
29 |
|
* www.gvsig.gva.es
|
30 |
|
*
|
31 |
|
* or
|
32 |
|
*
|
33 |
|
* IVER T.I. S.A
|
34 |
|
* Salamanca 50
|
35 |
|
* 46005 Valencia
|
36 |
|
* Spain
|
37 |
|
*
|
38 |
|
* +34 963163400
|
39 |
|
* dac@iver.es
|
40 |
|
*/
|
41 |
|
|
42 |
|
/* CVS MESSAGES:
|
43 |
|
*
|
44 |
|
* $Id$
|
45 |
|
* $Log$
|
46 |
|
* Revision 1.8 2007-09-04 10:53:10 caballero
|
47 |
|
* show page
|
48 |
|
*
|
49 |
|
* Revision 1.7 2007/07/30 12:56:04 jaume
|
50 |
|
* organize imports, java 5 code downgraded to 1.4 and added PictureFillSymbol
|
51 |
|
*
|
52 |
|
* Revision 1.6 2007/07/26 12:28:29 jaume
|
53 |
|
* maybe finished ArrowMarkerSymbol and ArrowDecoratorStyle
|
54 |
|
*
|
55 |
|
* Revision 1.5 2007/05/10 09:46:45 jaume
|
56 |
|
* Refactored legend interface names
|
57 |
|
*
|
58 |
|
* Revision 1.4 2007/04/05 14:31:31 cesar
|
59 |
|
* Change translation keys
|
60 |
|
*
|
61 |
|
* Revision 1.3 2007/04/03 09:12:28 cesar
|
62 |
|
* When saving legends, ask before overwriting files
|
63 |
|
*
|
64 |
|
* Revision 1.2 2007/03/09 11:25:00 jaume
|
65 |
|
* Advanced symbology (start committing)
|
66 |
|
*
|
67 |
|
* Revision 1.1.2.1 2007/02/01 12:12:41 jaume
|
68 |
|
* theme manager window and all its components are now dynamic
|
69 |
|
*
|
70 |
|
*
|
71 |
|
*/
|
72 |
|
package com.iver.cit.gvsig.project.documents.view.legend.gui;
|
73 |
|
|
74 |
|
import java.awt.Component;
|
75 |
|
import java.awt.event.ActionListener;
|
76 |
|
import java.io.File;
|
77 |
|
import java.io.FileNotFoundException;
|
78 |
|
import java.io.FileReader;
|
79 |
|
import java.io.FileWriter;
|
80 |
|
|
81 |
|
import javax.swing.JComponent;
|
82 |
|
import javax.swing.JFileChooser;
|
83 |
|
import javax.swing.JOptionPane;
|
84 |
|
|
85 |
|
import org.exolab.castor.xml.MarshalException;
|
86 |
|
import org.exolab.castor.xml.Marshaller;
|
87 |
|
import org.exolab.castor.xml.ValidationException;
|
88 |
|
import org.gvsig.gui.beans.swing.JButton;
|
89 |
|
|
90 |
|
import com.iver.andami.PluginServices;
|
91 |
|
import com.iver.andami.messages.NotificationManager;
|
92 |
|
import com.iver.cit.gvsig.fmap.layers.FLayer;
|
93 |
|
import com.iver.cit.gvsig.fmap.layers.XMLException;
|
94 |
|
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
|
95 |
|
import com.iver.cit.gvsig.fmap.rendering.ILegend;
|
96 |
|
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
|
97 |
|
import com.iver.utiles.GenericFileFilter;
|
98 |
|
import com.iver.utiles.XMLEntity;
|
99 |
|
import com.iver.utiles.xmlEntity.generate.XmlTag;
|
100 |
|
|
101 |
|
public class FilePage extends AbstractThemeManagerPage {
|
102 |
|
|
103 |
|
private ILegend legend;
|
104 |
|
private FLayer layer;
|
105 |
|
private JButton btnSaveLegend;
|
106 |
|
private JButton btnLoadLegend;
|
107 |
|
private ActionListener actionListener = new ActionListener() {
|
108 |
|
public void actionPerformed(java.awt.event.ActionEvent e) {
|
109 |
|
JComponent c = (JComponent) e.getSource();
|
110 |
|
if (c.equals(getBtnSaveLegend())) {
|
111 |
|
JFileChooser jfc = new JFileChooser();
|
112 |
|
jfc.addChoosableFileFilter(new GenericFileFilter("sld","*.sld"));
|
113 |
|
jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
|
114 |
|
PluginServices.getText(this, "tipo_de_leyenda")));
|
115 |
|
File basedir = null;
|
116 |
|
Object[] options = {PluginServices.getText(this, "yes"),
|
117 |
|
PluginServices.getText(this, "no"),
|
118 |
|
PluginServices.getText(this, "Cancel")};
|
119 |
|
|
120 |
|
while (true) {
|
121 |
|
jfc.setCurrentDirectory(basedir);
|
122 |
|
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
|
123 |
|
{
|
124 |
|
File file=jfc.getSelectedFile();
|
125 |
|
if (file.exists()) {
|
126 |
|
int answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
|
127 |
|
PluginServices.getText(this, "error_file_exists"),
|
128 |
|
PluginServices.getText(this, "confirmation_dialog"),
|
129 |
|
JOptionPane.YES_NO_CANCEL_OPTION,
|
130 |
|
JOptionPane.QUESTION_MESSAGE,
|
131 |
|
null,
|
132 |
|
options, options[1]);
|
133 |
|
if (answer==2 || answer==JOptionPane.CLOSED_OPTION) {
|
134 |
|
// 'Cancel' pressed or window closed: don't save anythig, exit save dialog
|
135 |
|
break;
|
136 |
|
}
|
137 |
|
else if (answer==1) {
|
138 |
|
// 'No' pressed, show jfc dialog again
|
139 |
|
basedir = file.getParentFile();
|
140 |
|
continue;
|
141 |
|
}
|
142 |
|
// "Yes" pressed, overwrite the file...
|
143 |
|
}
|
144 |
|
if (jfc.getFileFilter().accept(new File("dummy.sld")))
|
145 |
|
{
|
146 |
|
if (!(file.getPath().toLowerCase().endsWith(".sld"))){
|
147 |
|
file=new File(file.getPath()+".sld");
|
148 |
|
}
|
149 |
|
export2SLD(file);
|
150 |
|
}
|
151 |
|
else{
|
152 |
|
if (!(file.getPath().toLowerCase().endsWith(".gvl"))){
|
153 |
|
file=new File(file.getPath()+".gvl");
|
154 |
|
}
|
155 |
|
writeLegend(file);
|
156 |
|
}
|
157 |
|
break;
|
158 |
|
}
|
159 |
|
}
|
160 |
|
} else if (c.equals(getBtnLoadLegend())) {
|
161 |
|
JFileChooser jfc = new JFileChooser();
|
162 |
|
jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
|
163 |
|
PluginServices.getText(this, "tipo_de_leyenda")));
|
164 |
|
//jfc.addChoosableFileFilter(new GenericFileFilter("sld","sld"));
|
165 |
|
|
166 |
|
if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
|
167 |
|
File file=jfc.getSelectedFile();
|
168 |
|
if (!(file.getPath().endsWith(".gvl") || file.getPath().endsWith(".GVL"))) {
|
169 |
|
file=new File(file.getPath()+".gvl");
|
170 |
|
}
|
171 |
|
loadLegend(file);
|
172 |
|
}
|
173 |
|
}
|
174 |
|
};
|
175 |
|
};
|
176 |
|
public FilePage() {
|
177 |
|
super();
|
178 |
|
initialize();
|
179 |
|
}
|
180 |
|
|
181 |
|
private void initialize() {
|
182 |
|
|
183 |
|
}
|
184 |
|
|
185 |
|
private JButton getBtnSaveLegend() {
|
186 |
|
if (btnSaveLegend == null) {
|
187 |
|
btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"...");
|
188 |
|
btnSaveLegend.setActionCommand("SAVE_LEGEND");
|
189 |
|
btnSaveLegend.addActionListener(actionListener);
|
190 |
|
}
|
191 |
|
return btnSaveLegend;
|
192 |
|
}
|
193 |
|
|
194 |
|
private JButton getBtnLoadLegend() {
|
195 |
|
if (btnLoadLegend == null) {
|
196 |
|
btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"...");
|
197 |
|
btnLoadLegend.setActionCommand("LOAD_LEGEND");
|
198 |
|
btnLoadLegend.addActionListener(actionListener);
|
199 |
|
}
|
200 |
|
return btnLoadLegend;
|
201 |
|
}
|
202 |
|
|
203 |
|
public String getName() {
|
204 |
|
return PluginServices.getText(this, "file");
|
205 |
|
}
|
206 |
|
|
207 |
|
public void acceptAction() {
|
208 |
|
}
|
209 |
|
|
210 |
|
public void cancelAction() {
|
211 |
|
}
|
212 |
|
|
213 |
|
public void applyAction() {
|
214 |
|
}
|
215 |
|
|
216 |
|
/**
|
217 |
|
* Reads a legend file and loads into the this LegendManager
|
218 |
|
* @param file
|
219 |
|
*/
|
220 |
|
private void loadLegend(File file) {
|
221 |
|
File xmlFile = new File(file.getAbsolutePath());
|
222 |
|
FileReader reader;
|
223 |
|
try {
|
224 |
|
reader = new FileReader(xmlFile);
|
225 |
|
|
226 |
|
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
|
227 |
|
|
228 |
|
legend = LegendFactory.createFromXML(new XMLEntity(tag));
|
229 |
|
} catch (FileNotFoundException e) {
|
230 |
|
// should be unreachable code
|
231 |
|
NotificationManager.addError(PluginServices.getText(this, "file_not_found"), e);
|
232 |
|
} catch (MarshalException e) {
|
233 |
|
NotificationManager.addError(PluginServices.getText(this, "file_corrupt"), e);
|
234 |
|
} catch (ValidationException e) {
|
235 |
|
// should be unreachable code
|
236 |
|
NotificationManager.addError("ValidationException", e);
|
237 |
|
} catch (XMLException e) {
|
238 |
|
NotificationManager.addError(PluginServices.getText(this, "unsupported_legend"), e);
|
239 |
|
}
|
240 |
|
}
|
241 |
|
|
242 |
|
|
243 |
|
public void setModel(FLayer layer) {
|
244 |
|
this.layer = layer;
|
245 |
|
this.legend = ((Classifiable) layer).getLegend();
|
246 |
|
}
|
247 |
|
|
248 |
|
private void export2SLD(File file) {
|
249 |
|
try {
|
250 |
|
FileWriter writer = new FileWriter(file.getAbsolutePath());
|
251 |
|
writer.write( legend.getSLDString(layer.getName()));
|
252 |
|
writer.close();
|
253 |
|
|
254 |
|
} catch (Exception e) {
|
255 |
|
NotificationManager.addError(PluginServices.getText(this, "Error_exportando_SLD"), e);
|
256 |
|
}
|
257 |
|
}
|
258 |
|
|
259 |
|
private void writeLegend(File file) {
|
260 |
|
try {
|
261 |
|
FileWriter writer = new FileWriter(file.getAbsolutePath());
|
262 |
|
Marshaller m = new Marshaller(writer);
|
263 |
|
m.setEncoding("ISO-8859-1");
|
264 |
|
m.marshal(legend.getXMLEntity().getXmlTag());
|
265 |
|
} catch (Exception e) {
|
266 |
|
NotificationManager.addError(PluginServices.getText(this, "Error_guardando_la_leyenda"), e);
|
267 |
|
}
|
268 |
|
}
|
269 |
|
}
|