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 javax.swing.JOptionPane;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.data.projection.Lambert;
|
---|
13 | import org.openstreetmap.josm.data.projection.LambertCC9Zones;
|
---|
14 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
15 |
|
---|
16 | public 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 | }
|
---|