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

Last change on this file since 27788 was 26330, checked in by pieren, 14 years ago

Added more i18n calls.

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