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

Last change on this file since 18544 was 18544, checked in by pieren, 17 years ago

Add licence in headers for GPL compliance.

  • Property svn:eol-style set to native
File size: 4.6 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.gui.layer.Layer;
16
17public class MenuActionLoadFromCache extends JosmAction {
18 private static final long serialVersionUID = 1L;
19
20 public static String name = "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);
24 }
25
26 public void actionPerformed(ActionEvent e) {
27 JFileChooser fc = createAndOpenFileChooser();
28 if (fc == null)
29 return;
30
31 File[] files = fc.getSelectedFiles();
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() > 2 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z) &&
38 !(Main.proj instanceof LambertCC9Zones))
39 || (ext.length() == 1) && !(Main.proj instanceof Lambert)) {
40 JOptionPane.showMessageDialog(Main.parent, tr("{0} not allowed with the current projection", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
41 continue;
42 } else {
43 String location = filename.substring(0, filename.lastIndexOf("."));
44 if (ext.length() > 2 && ext.substring(0, CacheControl.cLambertCC9Z.length()).equals(CacheControl.cLambertCC9Z))
45 ext = ext.substring(2);
46 // check the extension and its Lambert zone consistency
47 try {
48 int cacheZone = Integer.parseInt(ext) - 1;
49 if (cacheZone >=0 && cacheZone <= 3) {
50 if (Lambert.layoutZone == -1) {
51 Lambert.layoutZone = cacheZone;
52 System.out.println("Load cache \"" + filename + "\" in Lambert Zone " + (Lambert.layoutZone+1));
53 } else if (cacheZone != Lambert.layoutZone) {
54 System.out.println("Cannot load cache \"" + filename + "\" which is not in current Lambert Zone "
55 + Lambert.layoutZone);
56 continue nextFile;
57 } else
58 System.out.println("Load cache " + filename);
59 }
60 } catch (NumberFormatException ex) {
61 System.out.println("Selected file \"" + filename + "\" is not a WMS cache file (invalid extension)");
62 continue;
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 System.out.println("The location " + filename + " is already on screen. Cache not loaded.");
69 continue nextFile;
70 }
71 }
72 }
73 // create layer and load cache
74 WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
75 if (wmsLayer.getCacheControl().loadCache(file, Lambert.layoutZone))
76 Main.main.addLayer(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 if (Lambert.layoutZone != -1)
88 fc.addChoosableFileFilter(CacheFileFilter.filters[Lambert.layoutZone]);
89 fc.setAcceptAllFileFilterUsed(false);
90
91 int answer = fc.showOpenDialog(Main.parent);
92 if (answer != JFileChooser.APPROVE_OPTION)
93 return null;
94
95 return fc;
96 }
97
98}
Note: See TracBrowser for help on using the repository browser.