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