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

Last change on this file since 17719 was 17719, checked in by pieren, 16 years ago

Fix delays on raster cache saving; small issue fixed in file selection dialog.

  • Property svn:eol-style set to native
File size: 3.6 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 org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.actions.JosmAction;
10import org.openstreetmap.josm.data.projection.Lambert;
11import org.openstreetmap.josm.gui.layer.Layer;
12
13public class MenuActionLoadFromCache extends JosmAction {
14 private static final long serialVersionUID = 1L;
15
16 public static String name = "Load layer from cache";
17
18 public MenuActionLoadFromCache() {
19 super(tr(name), "cadastre_small", tr("Load location from cache (only if cache is enabled)"), null, false);
20 }
21
22 public void actionPerformed(ActionEvent e) {
23 JFileChooser fc = createAndOpenFileChooser();
24 if (fc == null)
25 return;
26
27 File[] files = fc.getSelectedFiles();
28 nextFile:
29 for (File file : files) {
30 if (file.exists()) {
31 String filename = file.getName();
32 String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());
33 String location = filename.substring(0, filename.lastIndexOf("."));
34 // check the extension and its Lambert zone consistency
35 try {
36 int cacheZone = Integer.parseInt(ext) - 1;
37 if (cacheZone >=0 && cacheZone <= 3) {
38 if (Lambert.layoutZone == -1) {
39 Lambert.layoutZone = cacheZone;
40 System.out.println("Load cache \"" + filename + "\" in Lambert Zone " + (Lambert.layoutZone+1));
41 } else if (cacheZone != Lambert.layoutZone) {
42 System.out.println("Cannot load cache \"" + filename + "\" which is not in current Lambert Zone "
43 + Lambert.layoutZone);
44 continue nextFile;
45 } else
46 System.out.println("Load cache " + filename);
47 }
48 } catch (NumberFormatException ex) {
49 System.out.println("Selected file \"" + filename + "\" is not a WMS cache file (invalid extension)");
50 continue;
51 }
52 // check if the selected cache is not already displayed
53 if (Main.map != null) {
54 for (Layer l : Main.map.mapView.getAllLayers()) {
55 if (l instanceof WMSLayer && l.getName().equals(location)) {
56 System.out.println("The location " + filename + " is already on screen. Cache not loaded.");
57 continue nextFile;
58 }
59 }
60 }
61 // create layer and load cache
62 WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
63 if (wmsLayer.getCacheControl().loadCache(file, Lambert.layoutZone))
64 Main.main.addLayer(wmsLayer);
65 }
66 }
67
68 }
69
70 protected static JFileChooser createAndOpenFileChooser() {
71 JFileChooser fc = new JFileChooser(new File(CadastrePlugin.cacheDir));
72 fc.setMultiSelectionEnabled(true);
73 if (Lambert.layoutZone != -1)
74 fc.addChoosableFileFilter(CacheFileFilter.filters[Lambert.layoutZone]);
75 fc.setAcceptAllFileFilterUsed(false);
76
77 int answer = fc.showOpenDialog(Main.parent);
78 if (answer != JFileChooser.APPROVE_OPTION)
79 return null;
80
81 return fc;
82 }
83
84}
Note: See TracBrowser for help on using the repository browser.