source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/MenuActionLoadFromCache.java@ 33514

Last change on this file since 33514 was 32556, checked in by donvip, 9 years ago

checkstyle

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package cadastre_fr;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.io.File;
9
10import javax.swing.JFileChooser;
11import javax.swing.JOptionPane;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.JosmAction;
15import org.openstreetmap.josm.gui.layer.Layer;
16
17public class MenuActionLoadFromCache extends JosmAction {
18 private static final long serialVersionUID = 1L;
19
20 public static final String name = marktr("Load layer from cache");
21
22 /**
23 * Constructs a new {@code MenuActionLoadFromCache}.
24 */
25 public MenuActionLoadFromCache() {
26 super(tr(name), "cadastre_small", tr("Load location from cache (only if cache is enabled)"), null, false, "cadastrefr/loadfromcache", true);
27 }
28
29 @Override
30 public void actionPerformed(ActionEvent e) {
31 JFileChooser fc = createAndOpenFileChooser();
32 if (fc == null)
33 return;
34
35 File[] files = fc.getSelectedFiles();
36 int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
37 nextFile:
38 for (File file : files) {
39 if (file.exists()) {
40 String filename = file.getName();
41 String ext = (filename.lastIndexOf('.') == -1) ? "" : filename.substring(filename.lastIndexOf('.')+1, filename.length());
42 if ((ext.length() == 3 && ext.substring(0, CacheControl.C_LAMBERT_CC_9Z.length()).equals(CacheControl.C_LAMBERT_CC_9Z) &&
43 !(CadastrePlugin.isLambert_cc9()))
44 || (ext.length() == 4 && ext.substring(0, CacheControl.C_UTM20N.length()).equals(CacheControl.C_UTM20N) &&
45 !(CadastrePlugin.isUtm_france_dom()))
46 || (ext.length() == 1) && !(CadastrePlugin.isLambert())) {
47 JOptionPane.showMessageDialog(Main.parent, tr("{0} not allowed with the current projection", filename),
48 tr("Error"), JOptionPane.ERROR_MESSAGE);
49 continue;
50 } else {
51 String location = filename.substring(0, filename.lastIndexOf('.'));
52 if (ext.length() == 3 && ext.substring(0, CacheControl.C_LAMBERT_CC_9Z.length()).equals(CacheControl.C_LAMBERT_CC_9Z))
53 ext = ext.substring(2);
54 else if (ext.length() == 4 && ext.substring(0, CacheControl.C_UTM20N.length()).equals(CacheControl.C_UTM20N))
55 ext = ext.substring(3);
56 // check the extension and its compatibility with current projection
57 try {
58 int cacheZone = Integer.parseInt(ext) - 1;
59 if (cacheZone >= 0 && cacheZone <= 9) {
60 if (cacheZone != layoutZone) {
61 JOptionPane.showMessageDialog(Main.parent,
62 tr("Cannot load cache {0} which is not compatible with current projection zone", filename),
63 tr("Error"), JOptionPane.ERROR_MESSAGE);
64 continue nextFile;
65 } else
66 Main.info("Load cache " + filename);
67 }
68 } catch (NumberFormatException ex) {
69 JOptionPane.showMessageDialog(Main.parent, tr("Selected file {0} is not a cache file from this plugin (invalid extension)", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
70 continue nextFile;
71 }
72 // check if the selected cache is not already displayed
73 if (Main.map != null) {
74 for (Layer l : Main.getLayerManager().getLayers()) {
75 if (l instanceof WMSLayer && l.getName().equals(location)) {
76 JOptionPane.showMessageDialog(Main.parent, tr("The location {0} is already on screen. Cache not loaded.", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
77 continue nextFile;
78 }
79 }
80 }
81 // create layer and load cache
82 WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
83 if (wmsLayer.grabThread.getCacheControl().loadCache(file, layoutZone)) {
84 CadastrePlugin.addWMSLayer(wmsLayer);
85 }
86 }
87 }
88 }
89
90 }
91
92 protected static JFileChooser createAndOpenFileChooser() {
93 JFileChooser fc = new JFileChooser(new File(CadastrePlugin.cacheDir));
94 fc.setMultiSelectionEnabled(true);
95 int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
96 if (layoutZone != -1) {
97 if (CadastrePlugin.isLambert())
98 fc.addChoosableFileFilter(CacheFileLambert4ZoneFilter.filters[layoutZone]);
99 else if (CadastrePlugin.isLambert_cc9())
100 fc.addChoosableFileFilter(CacheFileLambert9ZoneFilter.filters[layoutZone]);
101 else if (CadastrePlugin.isUtm_france_dom())
102 fc.addChoosableFileFilter(CacheFileUTM20NFilter.filters[layoutZone]);
103 }
104 fc.setAcceptAllFileFilterUsed(false);
105
106 int answer = fc.showOpenDialog(Main.parent);
107 if (answer != JFileChooser.APPROVE_OPTION)
108 return null;
109
110 return fc;
111 }
112
113}
Note: See TracBrowser for help on using the repository browser.