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

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

Use the new cadastre projection LambertCC9Zones

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1package cadastre_fr;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.event.ActionEvent;
6import java.io.File;
7import javax.swing.JFileChooser;
8import javax.swing.JOptionPane;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.actions.JosmAction;
12import org.openstreetmap.josm.data.projection.Lambert;
13import org.openstreetmap.josm.data.projection.LambertCC9Zones;
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 = "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);
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 nextFile:
32 for (File file : files) {
33 if (file.exists()) {
34 String filename = file.getName();
35 String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());
36 if ((ext.length() > 2 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z) &&
37 !(Main.proj instanceof LambertCC9Zones))
38 || (ext.length() == 1) && !(Main.proj instanceof Lambert)) {
39 JOptionPane.showMessageDialog(Main.parent, tr("{0} not allowed with the current projection", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
40 continue;
41 } else {
42 String location = filename.substring(0, filename.lastIndexOf("."));
43 if (ext.length() > 2 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z))
44 ext = ext.substring(2);
45 // check the extension and its Lambert zone consistency
46 try {
47 int cacheZone = Integer.parseInt(ext) - 1;
48 if (cacheZone >=0 && cacheZone <= 3) {
49 if (Lambert.layoutZone == -1) {
50 Lambert.layoutZone = cacheZone;
51 System.out.println("Load cache \"" + filename + "\" in Lambert Zone " + (Lambert.layoutZone+1));
52 } else if (cacheZone != Lambert.layoutZone) {
53 System.out.println("Cannot load cache \"" + filename + "\" which is not in current Lambert Zone "
54 + Lambert.layoutZone);
55 continue nextFile;
56 } else
57 System.out.println("Load cache " + filename);
58 }
59 } catch (NumberFormatException ex) {
60 System.out.println("Selected file \"" + filename + "\" is not a WMS cache file (invalid extension)");
61 continue;
62 }
63 // check if the selected cache is not already displayed
64 if (Main.map != null) {
65 for (Layer l : Main.map.mapView.getAllLayers()) {
66 if (l instanceof WMSLayer && l.getName().equals(location)) {
67 System.out.println("The location " + filename + " is already on screen. Cache not loaded.");
68 continue nextFile;
69 }
70 }
71 }
72 // create layer and load cache
73 WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
74 if (wmsLayer.getCacheControl().loadCache(file, Lambert.layoutZone))
75 Main.main.addLayer(wmsLayer);
76
77 }
78 }
79 }
80
81 }
82
83 protected static JFileChooser createAndOpenFileChooser() {
84 JFileChooser fc = new JFileChooser(new File(CadastrePlugin.cacheDir));
85 fc.setMultiSelectionEnabled(true);
86 if (Lambert.layoutZone != -1)
87 fc.addChoosableFileFilter(CacheFileFilter.filters[Lambert.layoutZone]);
88 fc.setAcceptAllFileFilterUsed(false);
89
90 int answer = fc.showOpenDialog(Main.parent);
91 if (answer != JFileChooser.APPROVE_OPTION)
92 return null;
93
94 return fc;
95 }
96
97}
Note: See TracBrowser for help on using the repository browser.