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

Last change on this file since 28961 was 28887, checked in by bastik, 12 years ago

update to latest josm (projection)

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