| 1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
|---|
| 2 | package cadastre_fr;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.io.File;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JFileChooser;
|
|---|
| 11 | import javax.swing.JOptionPane;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 16 |
|
|---|
| 17 | public class MenuActionLoadFromCache extends JosmAction {
|
|---|
| 18 | private static final long serialVersionUID = 1L;
|
|---|
| 19 |
|
|---|
| 20 | public static final String name = marktr("Load layer from cache");
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Constructs a new {@code MenuActionLoadFromCache}.
|
|---|
| 24 | */
|
|---|
| 25 | public MenuActionLoadFromCache() {
|
|---|
| 26 | super(tr(name), "cadastre_small", tr("Load location from cache (only if cache is enabled)"), null, false, "cadastrefr/loadfromcache", true);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | @Override
|
|---|
| 30 | public void actionPerformed(ActionEvent e) {
|
|---|
| 31 | JFileChooser fc = createAndOpenFileChooser();
|
|---|
| 32 | if (fc == null)
|
|---|
| 33 | return;
|
|---|
| 34 |
|
|---|
| 35 | File[] files = fc.getSelectedFiles();
|
|---|
| 36 | int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
|
|---|
| 37 | nextFile:
|
|---|
| 38 | for (File file : files) {
|
|---|
| 39 | if (file.exists()) {
|
|---|
| 40 | String filename = file.getName();
|
|---|
| 41 | String ext = (filename.lastIndexOf('.')==-1)?"":filename.substring(filename.lastIndexOf('.')+1,filename.length());
|
|---|
| 42 | if ((ext.length() == 3 && ext.substring(0, CacheControl.C_LAMBERT_CC_9Z.length()).equals(CacheControl.C_LAMBERT_CC_9Z) &&
|
|---|
| 43 | !(CadastrePlugin.isLambert_cc9()))
|
|---|
| 44 | || (ext.length() == 4 && ext.substring(0, CacheControl.C_UTM20N.length()).equals(CacheControl.C_UTM20N) &&
|
|---|
| 45 | !(CadastrePlugin.isUtm_france_dom()))
|
|---|
| 46 | || (ext.length() == 1) && !(CadastrePlugin.isLambert())) {
|
|---|
| 47 | JOptionPane.showMessageDialog(Main.parent, tr("{0} not allowed with the current projection", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 48 | continue;
|
|---|
| 49 | } else {
|
|---|
| 50 | String location = filename.substring(0, filename.lastIndexOf('.'));
|
|---|
| 51 | if (ext.length() == 3 && ext.substring(0, CacheControl.C_LAMBERT_CC_9Z.length()).equals(CacheControl.C_LAMBERT_CC_9Z))
|
|---|
| 52 | ext = ext.substring(2);
|
|---|
| 53 | else if (ext.length() == 4 && ext.substring(0, CacheControl.C_UTM20N.length()).equals(CacheControl.C_UTM20N))
|
|---|
| 54 | ext = ext.substring(3);
|
|---|
| 55 | // check the extension and its compatibility with current projection
|
|---|
| 56 | try {
|
|---|
| 57 | int cacheZone = Integer.parseInt(ext) - 1;
|
|---|
| 58 | if (cacheZone >=0 && cacheZone <= 9) {
|
|---|
| 59 | if (cacheZone != layoutZone) {
|
|---|
| 60 | JOptionPane.showMessageDialog(Main.parent, tr("Cannot load cache {0} which is not compatible with current projection zone", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 61 | continue nextFile;
|
|---|
| 62 | } else
|
|---|
| 63 | Main.info("Load cache " + filename);
|
|---|
| 64 | }
|
|---|
| 65 | } catch (NumberFormatException ex) {
|
|---|
| 66 | JOptionPane.showMessageDialog(Main.parent, tr("Selected file {0} is not a cache file from this plugin (invalid extension)", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 67 | continue nextFile;
|
|---|
| 68 | }
|
|---|
| 69 | // check if the selected cache is not already displayed
|
|---|
| 70 | if (Main.map != null) {
|
|---|
| 71 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
|---|
| 72 | if (l instanceof WMSLayer && l.getName().equals(location)) {
|
|---|
| 73 | JOptionPane.showMessageDialog(Main.parent, tr("The location {0} is already on screen. Cache not loaded.", filename), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 74 | continue nextFile;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 | // create layer and load cache
|
|---|
| 79 | WMSLayer wmsLayer = new WMSLayer("", "", Integer.parseInt(ext)-1);
|
|---|
| 80 | if (wmsLayer.grabThread.getCacheControl().loadCache(file, layoutZone)) {
|
|---|
| 81 | CadastrePlugin.addWMSLayer(wmsLayer);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | protected static JFileChooser createAndOpenFileChooser() {
|
|---|
| 90 | JFileChooser fc = new JFileChooser(new File(CadastrePlugin.cacheDir));
|
|---|
| 91 | fc.setMultiSelectionEnabled(true);
|
|---|
| 92 | int layoutZone = CadastrePlugin.getCadastreProjectionLayoutZone();
|
|---|
| 93 | if (layoutZone != -1) {
|
|---|
| 94 | if (CadastrePlugin.isLambert())
|
|---|
| 95 | fc.addChoosableFileFilter(CacheFileLambert4ZoneFilter.filters[layoutZone]);
|
|---|
| 96 | else if (CadastrePlugin.isLambert_cc9())
|
|---|
| 97 | fc.addChoosableFileFilter(CacheFileLambert9ZoneFilter.filters[layoutZone]);
|
|---|
| 98 | else if (CadastrePlugin.isUtm_france_dom())
|
|---|
| 99 | fc.addChoosableFileFilter(CacheFileUTM20NFilter.filters[layoutZone]);
|
|---|
| 100 | }
|
|---|
| 101 | fc.setAcceptAllFileFilterUsed(false);
|
|---|
| 102 |
|
|---|
| 103 | int answer = fc.showOpenDialog(Main.parent);
|
|---|
| 104 | if (answer != JFileChooser.APPROVE_OPTION)
|
|---|
| 105 | return null;
|
|---|
| 106 |
|
|---|
| 107 | return fc;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | }
|
|---|