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

Last change on this file since 20214 was 19928, checked in by pieren, 15 years ago

from Clément Ménier, new WMLayer always on bottom

  • Property svn:eol-style set to native
File size: 5.8 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;
5
6import java.awt.event.ActionEvent;
7import java.io.File;
8import javax.swing.JFileChooser;
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.JosmAction;
13import org.openstreetmap.josm.data.projection.Lambert;
14import org.openstreetmap.josm.data.projection.LambertCC9Zones;
15import org.openstreetmap.josm.data.projection.UTM_20N_France_DOM;
16import org.openstreetmap.josm.gui.layer.Layer;
17
18public class MenuActionLoadFromCache extends JosmAction {
19 private static final long serialVersionUID = 1L;
20
21 public static String name = "Load layer from cache";
22
23 public MenuActionLoadFromCache() {
24 super(tr(name), "cadastre_small", tr("Load location from cache (only if cache is enabled)"), null, false);
25 }
26
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 = getCurrentProjZone();
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 !(Main.proj instanceof LambertCC9Zones))
41 || (ext.length() == 4 && ext.substring(0, CacheControl.cUTM20N.length()).equals(CacheControl.cUTM20N) &&
42 !(Main.proj instanceof UTM_20N_France_DOM))
43 || (ext.length() == 1) && !(Main.proj instanceof Lambert)) {
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 System.out.println("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.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 = new MenuActionLoadFromCache().getCurrentProjZone();
90 if (layoutZone != -1) {
91 if (Main.proj instanceof Lambert)
92 fc.addChoosableFileFilter(CacheFileLambert4ZoneFilter.filters[layoutZone]);
93 else if (Main.proj instanceof LambertCC9Zones)
94 fc.addChoosableFileFilter(CacheFileLambert9ZoneFilter.filters[layoutZone]);
95 else if (Main.proj instanceof UTM_20N_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 private int getCurrentProjZone() {
108 int zone = -1;
109 if (Main.proj instanceof LambertCC9Zones)
110 zone = ((LambertCC9Zones)Main.proj).getLayoutZone();
111 else if (Main.proj instanceof Lambert)
112 zone = ((Lambert)Main.proj).getLayoutZone();
113 else if (Main.proj instanceof UTM_20N_France_DOM)
114 zone = ((UTM_20N_France_DOM)Main.proj).getCurrentGeodesic();
115 return zone;
116 }
117}
Note: See TracBrowser for help on using the repository browser.