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

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

remove tabs

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
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 String name = marktr("Load layer from cache");
21
22 public MenuActionLoadFromCache() {
23 super(tr(name), "cadastre_small", tr("Load location from cache (only if cache is enabled)"), null, false, "cadastrefr/loadfromcache", true);
24 }
25
26 @Override
27 public void actionPerformed(ActionEvent e) {
28 JFileChooser fc = createAndOpenFileChooser();
29 if (fc == null)
30 return;
31
32 File[] files = fc.getSelectedFiles();
33 int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
34 nextFile:
35 for (File file : files) {
36 if (file.exists()) {
37 String filename = file.getName();
38 String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());
39 if ((ext.length() == 3 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z) &&
40 !(CadastrePlugin.isLambert_cc9()))
41 || (ext.length() == 4 && ext.substring(0, CacheControl.cUTM20N.length()).equals(CacheControl.cUTM20N) &&
42 !(CadastrePlugin.isUtm_france_dom()))
43 || (ext.length() == 1) && !(CadastrePlugin.isLambert())) {
44 JOptionPane.showMessageDialog(Main.parent, tr("{0} not allowed with the current projection", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
45 continue;
46 } else {
47 String location = filename.substring(0, filename.lastIndexOf("."));
48 if (ext.length() == 3 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z))
49 ext = ext.substring(2);
50 else if (ext.length() == 4 && ext.substring(0, CacheControl.cUTM20N.length()).equals(CacheControl.cUTM20N))
51 ext = ext.substring(3);
52 // check the extension and its compatibility with current projection
53 try {
54 int cacheZone = Integer.parseInt(ext) - 1;
55 if (cacheZone >=0 && cacheZone <= 9) {
56 if (cacheZone != layoutZone) {
57 JOptionPane.showMessageDialog(Main.parent, tr("Cannot load cache {0} which is not compatible with current projection zone", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
58 continue nextFile;
59 } else
60 Main.info("Load cache " + filename);
61 }
62 } catch (NumberFormatException ex) {
63 JOptionPane.showMessageDialog(Main.parent, tr("Selected file {0} is not a cache file from this plugin (invalid extension)", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
64 continue nextFile;
65 }
66 // check if the selected cache is not already displayed
67 if (Main.map != null) {
68 for (Layer l : Main.map.mapView.getAllLayers()) {
69 if (l instanceof WMSLayer && l.getName().equals(location)) {
70 JOptionPane.showMessageDialog(Main.parent, tr("The location {0} is already on screen. Cache not loaded.", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
71 continue nextFile;
72 }
73 }
74 }
75 // create layer and load cache
76 WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
77 if (wmsLayer.grabThread.getCacheControl().loadCache(file, layoutZone)) {
78 CadastrePlugin.addWMSLayer(wmsLayer);
79 }
80 }
81 }
82 }
83
84 }
85
86 protected static JFileChooser createAndOpenFileChooser() {
87 JFileChooser fc = new JFileChooser(new File(CadastrePlugin.cacheDir));
88 fc.setMultiSelectionEnabled(true);
89 int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
90 if (layoutZone != -1) {
91 if (CadastrePlugin.isLambert())
92 fc.addChoosableFileFilter(CacheFileLambert4ZoneFilter.filters[layoutZone]);
93 else if (CadastrePlugin.isLambert_cc9())
94 fc.addChoosableFileFilter(CacheFileLambert9ZoneFilter.filters[layoutZone]);
95 else if (CadastrePlugin.isUtm_france_dom())
96 fc.addChoosableFileFilter(CacheFileUTM20NFilter.filters[layoutZone]);
97 }
98 fc.setAcceptAllFileFilterUsed(false);
99
100 int answer = fc.showOpenDialog(Main.parent);
101 if (answer != JFileChooser.APPROVE_OPTION)
102 return null;
103
104 return fc;
105 }
106
107}
Note: See TracBrowser for help on using the repository browser.