source: subversion/applications/editors/josm/plugins/cadastre-fr/src/org/openstreetmap/josm/plugins/fr/cadastre/CadastrePlugin.java

Last change on this file was 34458, checked in by donvip, 8 months ago

update to JOSM 14153

  • Property svn:eol-style set to native
File size: 27.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.plugins.fr.cadastre;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.io.session.SessionReader.registerSessionLayerImporter;
6import static org.openstreetmap.josm.io.session.SessionWriter.registerSessionLayerExporter;
7import static org.openstreetmap.josm.tools.I18n.tr;
8
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.awt.event.KeyEvent;
12import java.io.File;
13import java.util.Arrays;
14import java.util.Calendar;
15import java.util.HashMap;
16import java.util.Map;
17
18import javax.swing.JCheckBoxMenuItem;
19import javax.swing.JDialog;
20import javax.swing.JMenu;
21import javax.swing.JMenuItem;
22import javax.swing.JOptionPane;
23import javax.swing.KeyStroke;
24
25import org.openstreetmap.josm.actions.ExtensionFileFilter;
26import org.openstreetmap.josm.actions.JosmAction;
27import org.openstreetmap.josm.actions.UploadAction;
28import org.openstreetmap.josm.data.projection.AbstractProjection;
29import org.openstreetmap.josm.data.projection.Projection;
30import org.openstreetmap.josm.data.projection.ProjectionRegistry;
31import org.openstreetmap.josm.gui.IconToggleButton;
32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.MainMenu;
34import org.openstreetmap.josm.gui.MapFrame;
35import org.openstreetmap.josm.gui.download.DownloadDialog;
36import org.openstreetmap.josm.gui.layer.Layer;
37import org.openstreetmap.josm.gui.preferences.PreferenceDialog;
38import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
39import org.openstreetmap.josm.gui.preferences.map.MapPreference;
40import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
41import org.openstreetmap.josm.gui.util.GuiHelper;
42import org.openstreetmap.josm.plugins.Plugin;
43import org.openstreetmap.josm.plugins.PluginInformation;
44import org.openstreetmap.josm.plugins.fr.cadastre.actions.MenuActionGrab;
45import org.openstreetmap.josm.plugins.fr.cadastre.actions.MenuActionGrabPlanImage;
46import org.openstreetmap.josm.plugins.fr.cadastre.actions.MenuActionLoadFromCache;
47import org.openstreetmap.josm.plugins.fr.cadastre.actions.MenuActionNewLocation;
48import org.openstreetmap.josm.plugins.fr.cadastre.actions.MenuActionOpenPreferences;
49import org.openstreetmap.josm.plugins.fr.cadastre.actions.mapmode.Address;
50import org.openstreetmap.josm.plugins.fr.cadastre.actions.mapmode.WMSAdjustAction;
51import org.openstreetmap.josm.plugins.fr.cadastre.actions.upload.CheckSourceUploadHook;
52import org.openstreetmap.josm.plugins.fr.cadastre.download.CadastreDownloadSource;
53import org.openstreetmap.josm.plugins.fr.cadastre.download.CadastreDownloadTask;
54import org.openstreetmap.josm.plugins.fr.cadastre.edigeo.pci.EdigeoPciImporter;
55import org.openstreetmap.josm.plugins.fr.cadastre.preferences.CadastrePreferenceSetting;
56import org.openstreetmap.josm.plugins.fr.cadastre.session.CadastreSessionExporter;
57import org.openstreetmap.josm.plugins.fr.cadastre.session.CadastreSessionImporter;
58import org.openstreetmap.josm.plugins.fr.cadastre.wms.WMSLayer;
59import org.openstreetmap.josm.spi.preferences.Config;
60import org.openstreetmap.josm.tools.Logging;
61
62/**
63 * Plugin to access the French Cadastre WMS server at <a href="https://www.cadastre.gouv.fr">
64 * www.cadastre.gouv.fr</a>.<br>
65 * This WMS server requires some specific handling like retrieving a cookie for a
66 * limitation in case of no activity, or the request to the server shall provide
67 * a city/town/village code.
68 *
69 * @author Pieren &lt;pieren3@gmail.com&gt;,
70 *         &lt;matthieu.lochegnies@gmail.com&gt; for the extension to codeCommune;
71 *         Don-vip&lt;vincent.privat@gmail.com&gt; for the maintenance and Edigeo support
72 *
73 * @version 3.0
74 * <br>History:
75 * <br>0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
76 * <br>0.2 22-Jun-2008 first stable version
77 * <br>0.3 24-Jun-2008 add code departement
78 * <br>0.4 06-Jul-2008 - add images scales, icons, menu items disabling
79 * <br>                - remove dependencies of wmsplugin
80 * <br>                - add option to force a Lambert zone (for median locations)
81 * <br>                - add auto-sourcing
82 * <br>0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
83 * <br>                - no overlapping of grabbed images if transparency is enabled
84 * <br>                - set one layer per location
85 * <br>                - use utf-8 charset in POST request to server
86 * <br>                - improve the preferences setting dialog
87 * <br>                - cancel the current download is now possible
88 * <br>                - add automatic images caching and load on request (+ manage cache directory size)
89 * <br>                - enable auto-sourcing only if a WMS layer is used
90 * <br>0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
91 * <br>                - process the overlapping images when cache is loaded from disk
92 * <br>                - save the last 'new location request' text again in preferences
93 * <br>                - avoid duplicate layers with same name
94 * <br>                - set text input for new locations in upper case
95 * <br>                - the cache directory is configurable in "cadastrewms.cacheDir"
96 * <br>                - improve configuration change updates
97 * <br>0.7 24-Aug-2008 - mask images only if transparency enabled
98 * <br>                - validate projection name by Lambert.toString() method
99 * <br>0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
100 * <br>                - new possible grab factor of 100 square meters fixed size
101 * <br>                - minor fixes due to changes in JOSM core classes
102 * <br>                - first draft of raster image support
103 * <br>0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
104 *                       and simplify
105 * <br>1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
106 * <br>                - increase PNG picture size requested to WMS (800x1000)
107 * <br>                - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
108 * <br>1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
109 * <br>                - propose to use shortcut F11 for grabbing
110 * <br>1.2 16-Aug-2009 - implementation of raster image grabbing, cropping and georeferencing (not the
111 * <br>                  overview rasters (Tableau d'assemblage) but directly small units (Feuille)
112 * <br>1.3 23-Aug-2009 - improve georeferencing action cancellation
113 * <br>                - fixed bug of raster image loaded from cache not working on Java1.6
114 * <br>                - improve mouse click bounce detection during georeferencing process
115 * <br>1.4 25-Oct-2009 - add support for new Lambert CC 9 Zones projection
116 * <br>                - add optional crosspieces display on raster image layers
117 * <br>                - add automatic raster images georeferencing when WMS provides data
118 * <br>                - re-implement manual adjustment mode in raster image layer
119 * <br>1.5 21-Nov-2009 - major changes in projection in core : no magical zone prediction anymore for
120 *                       Lambert 4 and 9 zones; grid translation implemented for Lambert 4 zones;
121 *                       support of subprojections in preferences for zones setting and UTM20N
122 * <br>                - removed autosourcing of empty new nodes
123 * <br>1.6 28-Nov-2009 - Fix minor issues if Grab is called without layer (possible since projection rework)
124 * <br>1.7 12-Dec-2009 - Change URL's changes for cookie and downgrade imgs resolution due to WMS changes
125 * <br>1.8 11-Mar-2010 - filter the mouse button 1 during georeferencing
126 * <br>                - retry if getting a new cookie failed (10 times during 30 seconds)
127 * <br>                - cookie expiration automatically detected and renewed (after 30 minutes)
128 * <br>                - proper WMS layer cleanup at destruction (workaround for memory leak)
129 * <br>                - new cache format (v3) storing original image and cropped image bbox + angle
130 * <br>                - new cache format (v4) storing original image size for later rotation
131 * <br>                - cache files read compatible with previous formats
132 * <br>                - raster image rotation issues fixed, now using shift+ctrl key instead of ctrl
133 * <br>                - raster image adjustment using default system menu modifier (ctrl for windows) for Mac support
134 * <br>                - image resolution configurable (high, medium, low) like the online interface
135 * <br>                - layer selection configurable for vectorized images
136 * <br>                - improved download cancellation
137 * <br>                - from Erik Amzallag:
138 * <br>                -     possibility to modify the auto-sourcing text just before upload
139 * <br>                - from Clément Ménier:
140 * <br>                -     new option allowing an auto-selection of the first cadastre layer for grab
141 * <br>                -     non-modal JDialog in MenuActionGrabPlanImage
142 * <br>                -     new options in the image filter (bilinear, bicubic)
143 * <br>1.9 05-Apr-2010 - added a scroll bar in preferences
144 * <br>                - download cancellation improved
145 * <br>                - last deployment for Java1.5 compatibility
146 * <br>2.0 07-Jul-2010 - update projection for "La Reunion" departement to RGR92, UTM40S.
147 * <br>                - add 'departement' as option in the municipality selection
148 * <br>                - fixed bug in cache directory size control (and disabled by default)
149 * <br>                - add map mode for addressing
150 * <br>                - from Nicolas Dumoulin:
151 * <br>                -     add "tableau d'assemblage" in raster images for georeferencing (as option)
152 * <br>2.1 14-Jan-2011 - add GrabThread moving the grab to a separate thread
153 * <br>                - the divided BBox mode starts from the central square and loads the next in a spiral
154 * <br>                - move the grabber from CadastrPlugin singleton to each wmsLayer instance to allow grabbing
155 *                       of multiple municipalities in parallel.
156 * <br>2.2 01-Jul-2011 - replace deprecated Main.proj by newest ProjectionRegistry.getProjection()
157 * <br>                - fix list of raster images (Feuilles) parsing failing after a Cadastre server change/maintenance
158 * <br>2.3 11-Jan-2013 - add various improvements from Don-Vip (Vincent Privat) trac #8175, #8229 and #5626.
159 * <br>2.4 27-Jun-2013 - fix raster image georeferencing issues. Add new MenuActionRefineGeoRef for a new georeferencing
160 *                       of already referenced plan image.
161 * <br>2.5 06-Aug-2013 - fix transparency issue on new raster images. Temporary disable georeferences parsing not
162 *                       working on new cadastre WMS.
163 * <br>                - workaround on address help tool when switching to full screen
164 * <br>                - improvement when clicking on existing node address street in mode relation
165 * <br>                - option to simplify raster images in 2 bits colors (like images served in the past).
166 * <br>2.6 10-Sep-2013 - add JOSM "sessions" feature support (list of layers stored in a file)
167 * <br>2.7 26-Apr-2014 - switch to Java 7 + bugfixes
168 * <br>2.8 21-Jul-2016 - switch to Java 8 + bugfixes
169 * <br>2.9 23-Aug-2017 - use new HTTPS links from French cadastre - requires JOSM 12623+ to load Certigna certificate
170 * <br>3.0 30-Sep-2017 - add support for direct access to Cadastre vectorial data (Edigeo files)
171 */
172public class CadastrePlugin extends Plugin {
173    static String VERSION = "3.0";
174
175    static JMenu cadastreJMenu;
176
177    public static String source = "";
178
179    // true if the checkbox "auto-sourcing" is set in the plugin menu
180    public static boolean autoSourcing = false;
181
182    // true when the plugin is first used, e.g. grab from WMS or download cache file
183    public static boolean pluginUsed = false;
184
185    public static String cacheDir = null;
186
187    public static boolean alterColors = false;
188
189    public static boolean backgroundTransparent = false;
190
191    public static float transparency = 1.0f;
192
193    public static boolean drawBoundaries = false;
194
195    public static int imageWidth, imageHeight;
196
197    public static String grabLayers, grabStyles = null;
198
199    private static boolean menuEnabled = false;
200
201    private static String LAYER_BULDINGS = "CDIF:LS2";
202    private static String STYLE_BUILDING = "LS2_90";
203    private static String LAYER_WATER = "CDIF:LS3";
204    private static String STYLE_WATER = "LS3_90";
205    private static String LAYER_SYMBOL = "CDIF:LS1";
206    private static String STYLE_SYMBOL = "LS1_90";
207    private static String LAYER_PARCELS = "CDIF:PARCELLE";
208    private static String STYLE_PARCELS = "PARCELLE_90";
209    private static String LAYER_NUMERO = "CDIF:NUMERO";
210    private static String STYLE_NUMERO = "NUMERO_90";
211    private static String LAYER_LABEL = "CDIF:PT3,CDIF:PT2,CDIF:PT1";
212    private static String STYLE_LABEL = "PT3_90,PT2_90,PT1_90";
213    private static String LAYER_LIEUDIT = "CDIF:LIEUDIT";
214    private static String STYLE_LIEUDIT = "LIEUDIT_90";
215    private static String LAYER_SECTION = "CDIF:SUBSECTION,CDIF:SECTION";
216    private static String STYLE_SECTION = "SUBSECTION_90,SECTION_90";
217    private static String LAYER_COMMUNE = "CDIF:COMMUNE";
218    private static String STYLE_COMMUNE = "COMMUNE_90";
219
220    /**
221     * Creates the plugin and setup the default settings if necessary.
222     * @param info plugin information
223     */
224    public CadastrePlugin(PluginInformation info) {
225        super(info);
226        Logging.info("Pluging cadastre-fr v"+VERSION+" started...");
227        initCacheDir();
228
229        refreshConfiguration();
230
231        UploadAction.registerUploadHook(new CheckSourceUploadHook());
232        ExtensionFileFilter.addImporter(new EdigeoPciImporter());
233
234        registerSessionLayerExporter(WMSLayer.class, CadastreSessionExporter.class);
235        registerSessionLayerImporter("cadastre-fr", CadastreSessionImporter.class);
236
237        MainApplication.getMenu().openLocation.addDownloadTaskClass(CadastreDownloadTask.class);
238        DownloadDialog.addDownloadSource(new CadastreDownloadSource());
239    }
240
241    private static void initCacheDir() {
242        if (Config.getPref().get("cadastrewms.cacheDir").isEmpty()) {
243            cacheDir = new File(Config.getDirs().getCacheDirectory(true), "cadastrewms").getAbsolutePath();
244        } else {
245            cacheDir = Config.getPref().get("cadastrewms.cacheDir");
246        }
247        if (cacheDir.charAt(cacheDir.length()-1) != File.separatorChar)
248            cacheDir += File.separatorChar;
249        Logging.info("current cache directory: "+cacheDir);
250    }
251
252    public static void refreshMenu() {
253        MainMenu menu = MainApplication.getMenu();
254
255        if (cadastreJMenu == null) {
256            cadastreJMenu = menu.addMenu("Cadastre", tr("Cadastre"), KeyEvent.VK_C, menu.getDefaultMenuPos(), ht("/Plugin/CadastreFr"));
257            JosmAction grab = new MenuActionGrab();
258            JMenuItem menuGrab = new JMenuItem(grab);
259            KeyStroke ks = grab.getShortcut().getKeyStroke();
260            if (ks != null) {
261                menuGrab.setAccelerator(ks);
262            }
263            JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
264            JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
265            final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
266            menuSource.setSelected(autoSourcing);
267            menuSource.addActionListener(new ActionListener() {
268                @Override
269                public void actionPerformed(ActionEvent ev) {
270                    Config.getPref().putBoolean("cadastrewms.autosourcing", menuSource.isSelected());
271                    autoSourcing = menuSource.isSelected();
272                }
273            });
274
275            //JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
276            //JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
277            JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
278            // temporary disabled:
279            //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
280            //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
281
282            cadastreJMenu.add(menuGrab);
283            cadastreJMenu.add(menuActionGrabPlanImage);
284            cadastreJMenu.add(menuSettings);
285            cadastreJMenu.add(menuSource);
286            //cadastreJMenu.add(menuResetCookie); not required any more
287            //cadastreJMenu.add(menuLambertZone);
288            //if (Config.getPref().getBoolean("cadastrewms.buildingsMenu", false))
289            //    cadastreJMenu.add(menuActionBuildings);
290            cadastreJMenu.add(menuLoadFromCache);
291            // all SVG features disabled until official WMS is released
292            //cadastreJMenu.add(menuActionBoundaries);
293            cadastreJMenu.add(new JMenuItem(new MenuActionOpenPreferences()));
294        }
295        setEnabledAll(menuEnabled);
296    }
297
298    public static void refreshConfiguration() {
299        source = checkSourceMillesime();
300        autoSourcing = Config.getPref().getBoolean("cadastrewms.autosourcing", true);
301        alterColors = Config.getPref().getBoolean("cadastrewms.alterColors");
302        drawBoundaries = Config.getPref().getBoolean("cadastrewms.drawBoundaries", false);
303        if (alterColors) {
304            backgroundTransparent = Config.getPref().getBoolean("cadastrewms.backgroundTransparent");
305            transparency = Float.parseFloat(Config.getPref().get("cadastrewms.brightness", "1.0f"));
306        } else {
307            backgroundTransparent = false;
308            transparency = 1.0f;
309        }
310        String currentResolution = Config.getPref().get("cadastrewms.resolution", "high");
311        if (currentResolution.equals("high")) {
312            imageWidth = 1000; imageHeight = 800;
313        } else if (currentResolution.equals("medium")) {
314            imageWidth = 800; imageHeight = 600;
315        } else {
316            imageWidth = 600; imageHeight = 400;
317        }
318        refreshLayersURL();
319        refreshMenu();
320    }
321
322    private static void refreshLayersURL() {
323        grabLayers = "";
324        grabStyles = "";
325        int countLayers = 0;
326        if (Config.getPref().getBoolean("cadastrewms.layerWater", true)) {
327            grabLayers += LAYER_WATER + ",";
328            grabStyles += STYLE_WATER + ",";
329            countLayers++;
330        }
331        if (Config.getPref().getBoolean("cadastrewms.layerBuilding", true)) {
332            grabLayers += LAYER_BULDINGS + ",";
333            grabStyles += STYLE_BUILDING + ",";
334            countLayers++;
335        }
336        if (Config.getPref().getBoolean("cadastrewms.layerSymbol", true)) {
337            grabLayers += LAYER_SYMBOL + ",";
338            grabStyles += STYLE_SYMBOL + ",";
339            countLayers++;
340        }
341        if (Config.getPref().getBoolean("cadastrewms.layerParcel", true)) {
342            grabLayers += LAYER_PARCELS + ",";
343            grabStyles += STYLE_PARCELS + ",";
344            countLayers++;
345        }
346        if (Config.getPref().getBoolean("cadastrewms.layerNumero", true)) {
347            grabLayers += LAYER_NUMERO + ",";
348            grabStyles += STYLE_NUMERO + ",";
349            countLayers++;
350        }
351        if (Config.getPref().getBoolean("cadastrewms.layerLabel", true)) {
352            grabLayers += LAYER_LABEL + ",";
353            grabStyles += STYLE_LABEL + ",";
354            countLayers++;
355        }
356        if (Config.getPref().getBoolean("cadastrewms.layerLieudit", true)) {
357            grabLayers += LAYER_LIEUDIT + ",";
358            grabStyles += STYLE_LIEUDIT + ",";
359            countLayers++;
360        }
361        if (Config.getPref().getBoolean("cadastrewms.layerSection", true)) {
362            grabLayers += LAYER_SECTION + ",";
363            grabStyles += STYLE_SECTION + ",";
364            countLayers++;
365        }
366        if (Config.getPref().getBoolean("cadastrewms.layerCommune", true)) {
367            grabLayers += LAYER_COMMUNE + ",";
368            grabStyles += STYLE_COMMUNE + ",";
369            countLayers++;
370        }
371        if (countLayers > 2) { // remove the last ','
372            grabLayers = grabLayers.substring(0, grabLayers.length()-1);
373            grabStyles = grabStyles.substring(0, grabStyles.length()-1);
374        } else {
375            JOptionPane.showMessageDialog(MainApplication.getMainFrame(), tr("Please enable at least two WMS layers in the cadastre-fr "
376                    + "plugin configuration.\nLayers ''Building'' and ''Parcel'' added by default."));
377            Config.getPref().putBoolean("cadastrewms.layerBuilding", true);
378            Config.getPref().putBoolean("cadastrewms.layerParcel", true);
379            grabLayers += LAYER_BULDINGS + "," + LAYER_PARCELS;
380            grabStyles += STYLE_BUILDING + "," + STYLE_PARCELS;
381        }
382    }
383
384    @Override
385    public PreferenceSetting getPreferenceSetting() {
386        return new CadastrePreferenceSetting();
387    }
388
389    private static void setEnabledAll(boolean isEnabled) {
390        for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
391            JMenuItem item = cadastreJMenu.getItem(i);
392            if (item != null)
393                if (item.getText().equals(MenuActionGrabPlanImage.NAME) /*||
394                    item.getText().equals(MenuActionGrab.name) ||
395                    item.getText().equals(MenuActionBoundaries.name) ||
396                    item.getText().equals(MenuActionBuildings.name)*/) {
397                    item.setEnabled(isEnabled);
398                }
399        }
400        menuEnabled = isEnabled;
401    }
402
403    @Override
404    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
405        if (cadastreJMenu != null) {
406            if (oldFrame == null && newFrame != null) {
407                setEnabledAll(true);
408                MainApplication.getMap().addMapMode(new IconToggleButton(new WMSAdjustAction()));
409                MainApplication.getMap().addMapMode(new IconToggleButton(new Address()));
410            } else if (oldFrame != null && newFrame == null) {
411                setEnabledAll(false);
412                //Lambert.layoutZone = -1;
413                //LambertCC9Zones.layoutZone = -1;
414            }
415        }
416    }
417
418    public static boolean isLambert() {
419        String code = ProjectionRegistry.getProjection().toCode();
420        return Arrays.asList(ProjectionPreference.lambert.allCodes()).contains(code);
421    }
422
423    public static boolean isUtm_france_dom() {
424        String code = ProjectionRegistry.getProjection().toCode();
425        return Arrays.asList(ProjectionPreference.utm_france_dom.allCodes()).contains(code);
426    }
427
428    public static boolean isLambert_cc9() {
429        String code = ProjectionRegistry.getProjection().toCode();
430        return Arrays.asList(ProjectionPreference.lambert_cc9.allCodes()).contains(code);
431    }
432
433    public static boolean isCadastreProjection() {
434        return isLambert() || isUtm_france_dom() || isLambert_cc9();
435    }
436
437    public static int getCadastreProjectionLayoutZone() {
438        int zone = -1;
439        Projection proj = ProjectionRegistry.getProjection();
440        if (proj instanceof AbstractProjection) {
441            Integer code = ((AbstractProjection) proj).getEpsgCode();
442            if (code != null) {
443                if (code >= 3942 && code <= 3950) {                 // LambertCC9Zones
444                    zone = code - 3942;
445                } else if (code >= 27561 && 27564 <= code) {        // Lambert
446                    zone = code - 27561;
447                } else {                                            // UTM_France_DOM
448                    Map<Integer, Integer> utmfr = new HashMap<>();
449                    utmfr.put(2969, 0);
450                    utmfr.put(2970, 1);
451                    utmfr.put(2973, 2);
452                    utmfr.put(2975, 3);
453                    utmfr.put(2972, 4);
454                    if (utmfr.containsKey(code)) {
455                        zone = utmfr.get(code);
456                    }
457                }
458            }
459        }
460        return zone;
461    }
462
463    public static void safeSleep(long milliseconds) {
464        try {
465            Thread.sleep(milliseconds);
466        } catch (InterruptedException e) {
467            Logging.debug(e);
468        }
469    }
470
471    // See OptionPaneUtil
472    // FIXME: this is a temporary solution.
473    public static void prepareDialog(JDialog dialog) {
474        if (Config.getPref().getBoolean("window-handling.option-pane-always-on-top", true)) {
475            try {
476                dialog.setAlwaysOnTop(true);
477            } catch (SecurityException e) {
478                Logging.warn(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
479            }
480        }
481        dialog.setModal(true);
482        dialog.toFront();
483        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
484    }
485
486    /**
487     * Adds the WMSLayer following this rule:<ul>
488     * <li>if a WMSLayer exists place this new layer just before this layer</li>
489     * <li>Otherwise place it at the bottom</li>
490     * </ul>
491     * @param wmsLayer the wmsLayer to add
492     */
493    public static void addWMSLayer(WMSLayer wmsLayer) {
494        if (MainApplication.isDisplayingMapView()) {
495            int wmsNewLayerPos = MainApplication.getLayerManager().getLayers().size();
496            for (Layer l : MainApplication.getLayerManager().getLayersOfType(WMSLayer.class)) {
497                int wmsPos = MainApplication.getLayerManager().getLayers().indexOf(l);
498                if (wmsPos < wmsNewLayerPos) wmsNewLayerPos = wmsPos;
499            }
500            MainApplication.getLayerManager().addLayer(wmsLayer);
501            // Move the layer to its new position
502            MainApplication.getMap().mapView.moveLayer(wmsLayer, wmsNewLayerPos);
503        } else
504            MainApplication.getLayerManager().addLayer(wmsLayer);
505    }
506
507    private static String checkSourceMillesime() {
508        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
509        String src = Config.getPref().get("cadastrewms.source",
510            "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre. Mise \u00e0 jour : AAAA");
511        String srcYear = src.substring(src.lastIndexOf(" ")+1);
512        Integer year = null;
513        try {
514            year = Integer.decode(srcYear);
515        } catch (NumberFormatException e) {
516            Logging.debug(e);
517        }
518        if (srcYear.equals("AAAA") || (year != null && year < currentYear)) {
519            Logging.info("Replace source year "+srcYear+" by current year "+currentYear);
520            src = src.substring(0, src.lastIndexOf(" ")+1)+currentYear;
521            Config.getPref().put("cadastrewms.source", src);
522        }
523        return src;
524    }
525
526    /**
527     * Ask to change projection if current one is not suitable for French cadastre.
528     */
529    public static void askToChangeProjection() {
530        GuiHelper.runInEDTAndWait(new Runnable() {
531            @Override
532            public void run() {
533                if (JOptionPane.showConfirmDialog(MainApplication.getMainFrame(),
534                        tr("To enable the cadastre WMS plugin, change\n"
535                                + "the current projection to one of the cadastre\n"
536                                + "projections and retry"),
537                                tr("Change the current projection"), JOptionPane.OK_CANCEL_OPTION)
538                    == JOptionPane.OK_OPTION) {
539                    PreferenceDialog p = new PreferenceDialog(MainApplication.getMainFrame());
540                    p.selectPreferencesTabByClass(MapPreference.class);
541                    p.getTabbedPane().getSetting(ProjectionPreference.class).selectProjection(ProjectionPreference.lambert_cc9);
542                    p.setVisible(true);
543                }
544            }
545        });
546    }
547}
Note: See TracBrowser for help on using the repository browser.