Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2035 / extensions / org.gvsig.symbology.app / org.gvsig.symbology.app.importsymbols / src / main / java / org / gvsig / symbology / app / importsymbols / ImportSymbolsExtension.java @ 36343

History | View | Annotate | Download (10 KB)

1 34821 nfrancisco
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
23
package org.gvsig.symbology.app.importsymbols;
24
25
import java.awt.Component;
26
import java.io.File;
27
import java.io.FileInputStream;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.net.URL;
31
import java.text.MessageFormat;
32 35933 cordinyana
import java.util.HashSet;
33
import java.util.Set;
34 34821 nfrancisco
35
import javax.swing.JFileChooser;
36
import javax.swing.JOptionPane;
37 35933 cordinyana
import javax.swing.filechooser.FileFilter;
38 34821 nfrancisco
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41
42
import org.gvsig.andami.plugins.Extension;
43
import org.gvsig.fmap.mapcontext.MapContextLocator;
44
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
45
import org.gvsig.symbology.SymbologyLocator;
46
import org.gvsig.symbology.SymbologyManager;
47
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.persistence.PersistenceManager;
50
import org.gvsig.tools.persistence.PersistentState;
51
import org.gvsig.tools.persistence.exception.PersistenceException;
52
import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions;
53
54
public class ImportSymbolsExtension extends Extension {
55
56
    private static Logger LOG =
57
        LoggerFactory.getLogger(ImportSymbolsExtension.class);
58 35933 cordinyana
59
    private static final Set<String> imageExtensionsAllowed;
60
61
    static {
62
        imageExtensionsAllowed = new HashSet<String>();
63
        imageExtensionsAllowed.add("png");
64
        imageExtensionsAllowed.add("jpg");
65
        imageExtensionsAllowed.add("jpeg");
66
        imageExtensionsAllowed.add("gif");
67
        imageExtensionsAllowed.add("bmp");
68
        imageExtensionsAllowed.add("svg");
69
    }
70 34821 nfrancisco
71
    // returns the output folder (/home/user/gvSIG/Symbols)
72
    private File getOutputFolderPath() {
73
74
        SymbolManager manager = MapContextLocator.getSymbolManager();
75
        return new File(manager.getSymbolPreferences().getSymbolLibraryPath());
76
    }
77
78
    private String deleteExtension(File file) {
79
        String outFileName =
80
            file.getPath().substring(0, file.getPath().lastIndexOf("."));
81
        return outFileName;
82
83
    }
84
85 34824 nfrancisco
    // gets the selection image (from filename.png it returns filenamesel.png,
86
    // for example)
87 34821 nfrancisco
    private File getSelImage(File imageIn) {
88
89
        String cadena = deleteExtension(imageIn);
90
        String extension =
91
            imageIn.getPath().substring(imageIn.getPath().lastIndexOf("."),
92
                imageIn.getPath().length());
93
94
        String selPath = cadena + "sel" + extension;
95
        File imageOut = new File(selPath);
96
97
        return imageOut;
98
99
    }
100
101
    // Copies File imageIn to imageOut
102
    private void copy(File imageIn, File imageOut) throws IOException {
103
104
        FileInputStream from = null;
105
        FileOutputStream to = null;
106
107
        from = new FileInputStream(imageIn.getPath());
108
        to = new FileOutputStream(imageOut.getPath());
109
110
        try {
111
112
            byte[] buffer = new byte[4096];
113
            int bytesRead;
114
115
            while ((bytesRead = from.read(buffer)) != -1) {
116
                to.write(buffer, 0, bytesRead); // write
117
            }
118
119
        } finally {
120
            if (from != null) {
121
                try {
122
                    from.close();
123
                } catch (IOException e) {
124
                    ;
125
                }
126
            }
127
            if (to != null) {
128
                try {
129
                    to.close();
130
                } catch (IOException e) {
131
                    ;
132
                }
133
            }
134
        }
135
136
    }
137
138
    private void addImageSymbol(File image) throws IOException {
139
140
        int option;
141
        boolean sobreescribir = true;
142
        File selImage = getSelImage(image);
143
144
        File imageOut = new File(getOutputFolderPath() + "/" + image.getName());
145
        File selImageOut =
146
            new File(getOutputFolderPath() + "/" + selImage.getName());
147
148
        // Overwrite or not
149
        if (imageOut.exists()) {
150 34824 nfrancisco
            Object[] options = { getText("_Overwrite"), getText("_Cancel") };
151 34821 nfrancisco
152
            option =
153 34824 nfrancisco
                JOptionPane
154
                    .showOptionDialog(
155
                        null,
156
                        MessageFormat
157
                            .format(
158
                                getText("The file_{0}_already_exists.\n\n_Do_you_want_to_overwrite_it?"),
159
                                imageOut.getPath()),
160
                        getText("_Import_symbols"), JOptionPane.DEFAULT_OPTION,
161
                        JOptionPane.INFORMATION_MESSAGE, null, options,
162
                        options[0]);
163 34821 nfrancisco
164
            if (option == 1) {
165
                sobreescribir = false;
166
            }
167
        }
168
169
        SymbologyManager manager = SymbologyLocator.getSymbologyManager();
170
        SymbolManager symbolManager = MapContextLocator.getSymbolManager();
171
172
        if (sobreescribir) {
173
            try {
174
175
                // Copies the image to the output folder
176
                copy(image, imageOut);
177
178
                URL imageURL = imageOut.toURI().toURL();
179
                URL selImageURL = null;
180
181
                // tries to copy the selection image
182
                if (selImage.exists()) {
183
                    copy(selImage, selImageOut);
184
                    selImageURL = selImageOut.toURI().toURL();
185
                }
186
187
                IPictureMarkerSymbol symbol =
188
                    manager.createPictureMarkerSymbol(imageURL, selImageURL);
189
190
                PersistenceManager persistenceManager =
191
                    ToolsLocator.getPersistenceManager();
192
193
                PersistentState state = persistenceManager.getState(symbol);
194
195
                // change file extension
196 34824 nfrancisco
                String outFileName =
197
                    deleteExtension(imageOut)
198
                        + symbolManager.getSymbolPreferences()
199
                            .getSymbolFileExtension();
200 34821 nfrancisco
201
                FileOutputStream out = new FileOutputStream(outFileName);
202
                persistenceManager.saveState(state, out);
203
204
            } catch (PersistenceValidateExceptions e) {
205 35929 cordinyana
                IOException ioex = new IOException();
206
                ioex.initCause(e);
207
                throw ioex;
208 34821 nfrancisco
            } catch (PersistenceException e) {
209 35929 cordinyana
                IOException ioex = new IOException();
210
                ioex.initCause(e);
211
                throw ioex;
212 34821 nfrancisco
            }
213
        } else {
214
215
        }
216
    }
217
218
    public void execute(String actionCommand) {
219
        if (actionCommand.equalsIgnoreCase("Import_symbols")) {
220
221
            // Create a file chooser
222
            final JFileChooser fc = new JFileChooser();
223
224
            Component parent = null;
225
226
            // the fileChooser will only accept jpg, gif, png, jpeg, bmp and svg
227
            // image formats
228 35933 cordinyana
            fc.setFileFilter(new FileFilter() {
229
230
                /*
231
                 * Get the extension of a file.
232
                 */
233
                public String getExtension(File f) {
234
                    String ext = null;
235
                    String s = f.getName();
236
                    int i = s.lastIndexOf('.');
237
238
                    if (i > 0 && i < s.length() - 1) {
239
                        ext = s.substring(i + 1).toLowerCase();
240
                    }
241
                    return ext;
242
                }
243
244
                @Override
245
                public String getDescription() {
246
                    return "JPG, GIF, PNG, JPEG, BMP & SVG Images";
247
                }
248
249
                @Override
250
                public boolean accept(File f) {
251
                    if (f.isDirectory()) {
252
                        return true;
253
                    }
254
255
                    String extension = getExtension(f);
256 35934 cordinyana
                    return extension != null
257
                        && imageExtensionsAllowed.contains(extension);
258 35933 cordinyana
                }
259
            });
260
261 34821 nfrancisco
            fc.setMultiSelectionEnabled(true);
262
263 34824 nfrancisco
            fc.showDialog(parent, getText("_Import_symbols"));
264 34821 nfrancisco
265
            // for every selected file, a symbol is created
266
            for (int i = 0; i < fc.getSelectedFiles().length; i++) {
267
                File image = fc.getSelectedFiles()[i];
268
                try {
269
                    addImageSymbol(image);
270
                } catch (IOException e) {
271 34824 nfrancisco
                    LOG.error(MessageFormat.format(
272
                        getText("_An error_ocurred_creating_the_symbol_related_to_{0}"),
273
                        image.getAbsolutePath()), e);
274
275 34821 nfrancisco
                    // Error message, the symbol may not have been imported.
276 34824 nfrancisco
                    JOptionPane
277
                        .showMessageDialog(
278
                            null,
279
                            MessageFormat
280
                                .format(
281
                                    getText("_An_error_ocurred_creating_the_simbol_related_to_{0}"),
282
                                    image.getAbsolutePath()),
283
                            "_Import_symbols", JOptionPane.ERROR_MESSAGE);
284 34821 nfrancisco
                }
285
            }
286
            // Message that the import has finished, with or without errors.
287
            JOptionPane.showMessageDialog(null,
288 34824 nfrancisco
                getText("_Importing_symbols_has_finished."),
289
                "_Import_symbols", JOptionPane.INFORMATION_MESSAGE);
290 34821 nfrancisco
291
        }
292
    }
293
294
    public void initialize() {
295
296
    }
297
298
    @Override
299
    public void postInitialize() {
300
301
    }
302
303
    public boolean isEnabled() {
304
        return true;
305
    }
306
307
    public boolean isVisible() {
308
        return true;
309
    }
310
311
}