Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / FilePage.java @ 11018

History | View | Annotate | Download (7.76 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
 * 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: FilePage.java 11018 2007-04-03 09:12:28Z cesar $
45
* $Log$
46
* Revision 1.3  2007-04-03 09:12:28  cesar
47
* When saving legends, ask before overwriting files
48
*
49
* Revision 1.2  2007/03/09 11:25:00  jaume
50
* Advanced symbology (start committing)
51
*
52
* Revision 1.1.2.1  2007/02/01 12:12:41  jaume
53
* theme manager window and all its components are now dynamic
54
*
55
*
56
*/
57
package com.iver.cit.gvsig.project.documents.view.legend.gui;
58

    
59
import java.awt.Component;
60
import java.awt.event.ActionListener;
61
import java.io.File;
62
import java.io.FileNotFoundException;
63
import java.io.FileReader;
64
import java.io.FileWriter;
65

    
66
import javax.swing.JComponent;
67
import javax.swing.JFileChooser;
68
import javax.swing.JOptionPane;
69

    
70
import org.exolab.castor.xml.MarshalException;
71
import org.exolab.castor.xml.Marshaller;
72
import org.exolab.castor.xml.ValidationException;
73
import org.gvsig.gui.beans.swing.JButton;
74

    
75
import com.iver.andami.PluginServices;
76
import com.iver.andami.messages.NotificationManager;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.XMLException;
79
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
80
import com.iver.cit.gvsig.fmap.rendering.Legend;
81
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
82
import com.iver.utiles.GenericFileFilter;
83
import com.iver.utiles.XMLEntity;
84
import com.iver.utiles.xmlEntity.generate.XmlTag;
85

    
86
public class FilePage extends AbstractThemeManagerPage {
87

    
88
        private Legend legend;
89
        private FLayer layer;
90
        private JButton btnSaveLegend;
91
        private JButton btnLoadLegend;
92
        private ActionListener actionListener = new ActionListener() {
93
                public void actionPerformed(java.awt.event.ActionEvent e) {
94
                        JComponent c = (JComponent) e.getSource();
95
                        if (c.equals(getBtnSaveLegend())) {
96
                                JFileChooser jfc = new JFileChooser();
97
                                jfc.addChoosableFileFilter(new GenericFileFilter("sld","*.sld"));
98
                                jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
99
                                                PluginServices.getText(this, "tipo_de_leyenda")));
100
                                File basedir = null;
101
                                Object[] options = {PluginServices.getText(this, "Yes"),
102
                            PluginServices.getText(this, "No"),
103
                            PluginServices.getText(this, "Cancel")};
104

    
105
                                while (true) {
106
                                        jfc.setCurrentDirectory(basedir);
107
                                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION)
108
                                        {
109
                                                File file=jfc.getSelectedFile();
110
                                                if (file.exists()) {
111
                                                        int answer = JOptionPane.showOptionDialog((Component)PluginServices.getMainFrame(),
112
                                                                        PluginServices.getText(this, "error_file_exists"),
113
                                                                        PluginServices.getText(this, "confirmation_dialog"),
114
                                                                        JOptionPane.YES_NO_CANCEL_OPTION,
115
                                                                        JOptionPane.QUESTION_MESSAGE,
116
                                                                        null,
117
                                                                        options, options[1]);
118
                                                        if (answer==2 || answer==JOptionPane.CLOSED_OPTION) {
119
                                                                // 'Cancel' pressed or window closed: don't save anythig, exit save dialog
120
                                                                break;
121
                                                        }
122
                                                        else if (answer==1) {
123
                                                                // 'No' pressed, show jfc dialog again
124
                                                                basedir = file.getParentFile();
125
                                                                continue;
126
                                                        }
127
                                                        // "Yes" pressed, overwrite the file...
128
                                                }
129
                                                if (jfc.getFileFilter().accept(new File("dummy.sld")))
130
                                                {
131
                                                        if (!(file.getPath().toLowerCase().endsWith(".sld"))){
132
                                                                file=new File(file.getPath()+".sld");
133
                                                        }
134
                                                        export2SLD(file);
135
                                                }
136
                                                else{
137
                                                        if (!(file.getPath().toLowerCase().endsWith(".gvl"))){
138
                                                                file=new File(file.getPath()+".gvl");
139
                                                        }
140
                                                        writeLegend(file);
141
                                                }
142
                                                break;
143
                                        }
144
                                }
145
                        } else if (c.equals(getBtnLoadLegend())) {
146
                                JFileChooser jfc = new JFileChooser();
147
                            jfc.addChoosableFileFilter(new GenericFileFilter("gvl",
148
                                            PluginServices.getText(this, "tipo_de_leyenda")));
149
                            //jfc.addChoosableFileFilter(new GenericFileFilter("sld","sld"));
150

    
151
                            if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
152
                                    File file=jfc.getSelectedFile();
153
                                    if (!(file.getPath().endsWith(".gvl") || file.getPath().endsWith(".GVL")))        {
154
                                            file=new File(file.getPath()+".gvl");
155
                                    }
156
                                    loadLegend(file);
157
                            }
158
                        }
159
                };
160
        };
161
        public FilePage() {
162
                super();
163
                initialize();
164
        }
165

    
166
        private void initialize() {
167

    
168
        }
169

    
170
        private JButton getBtnSaveLegend() {
171
                if (btnSaveLegend == null) {
172
                        btnSaveLegend = new JButton(PluginServices.getText(this,"Guardar_leyenda")+"...");
173
                        btnSaveLegend.setActionCommand("SAVE_LEGEND");
174
                        btnSaveLegend.addActionListener(actionListener);
175
                }
176
                return btnSaveLegend;
177
        }
178

    
179
        private JButton getBtnLoadLegend() {
180
                if (btnLoadLegend == null) {
181
                        btnLoadLegend = new JButton(PluginServices.getText(this,"Recuperar_leyenda")+"...");
182
                        btnLoadLegend.setActionCommand("LOAD_LEGEND");
183
                        btnLoadLegend.addActionListener(actionListener);
184
                }
185
                return btnLoadLegend;
186
        }
187

    
188
        public String getName() {
189
                return PluginServices.getText(this, "file");
190
        }
191

    
192
        public void acceptAction() {
193
        }
194

    
195
        public void cancelAction() {
196
        }
197

    
198
        public void applyAction() {
199
        }
200

    
201
         /**
202
     * Reads a legend file and loads into the this LegendManager
203
     * @param file
204
     */
205
    private void loadLegend(File file) {
206
            File xmlFile = new File(file.getAbsolutePath());
207
            FileReader reader;
208
            try {
209
                    reader = new FileReader(xmlFile);
210

    
211
                    XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
212
                    
213
                    legend = LegendFactory.createFromXML(new XMLEntity(tag));
214
            } catch (FileNotFoundException e) {
215
                    // should be unreachable code
216
                    NotificationManager.addError(PluginServices.getText(this, "file_not_found"), e);
217
            } catch (MarshalException e) {
218
                    NotificationManager.addError(PluginServices.getText(this, "file_corrupt"), e);
219
            } catch (ValidationException e) {
220
                    // should be unreachable code
221
                    NotificationManager.addError("ValidationException", e);
222
            } catch (XMLException e) {
223
                    NotificationManager.addError(PluginServices.getText(this, "unsupported_legend"), e);
224
            }
225
    }
226

    
227

    
228
        public void setModel(FLayer layer) {
229
                this.layer = layer;
230
                this.legend = ((Classifiable) layer).getLegend();
231
        }
232

    
233
        private void export2SLD(File file) {
234
                try {
235
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
236
                        writer.write( legend.getSLDString(layer.getName()));
237
                        writer.close();
238

    
239
                } catch (Exception e) {
240
                        NotificationManager.addError(PluginServices.getText(this, "Error_exportando_SLD"), e);
241
                }
242
        }
243

    
244
    private void writeLegend(File file) {
245
                try {
246
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
247
                        Marshaller m = new Marshaller(writer);
248
                        m.setEncoding("ISO-8859-1");
249
                        m.marshal(legend.getXMLEntity().getXmlTag());
250
                } catch (Exception e) {
251
                        NotificationManager.addError(PluginServices.getText(this, "Error_guardando_la_leyenda"), e);
252
                }
253
        }
254

    
255
}