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

Last change on this file since 18207 was 18207, checked in by pieren, 15 years ago

Use the new cadastre projection LambertCC9Zones

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
1package cadastre_fr;
2
3import static org.openstreetmap.josm.tools.I18n.marktr;
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.KeyEvent;
9
10import javax.swing.JCheckBoxMenuItem;
11import javax.swing.JDialog;
12import javax.swing.JMenu;
13import javax.swing.JMenuItem;
14import javax.swing.JOptionPane;
15import javax.swing.KeyStroke;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.JosmAction;
19import org.openstreetmap.josm.actions.UploadAction;
20import org.openstreetmap.josm.gui.MainMenu;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
23import org.openstreetmap.josm.plugins.Plugin;
24import org.openstreetmap.josm.data.projection.*;
25
26/**
27 *
28 * Plugin to access the French Cadastre WMS server at www.cadastre.gouv.fr This
29 * WMS server requires some specific handling like retrieving a cookie for a
30 * limitation in case of no activity, or the request to the server shall provide
31 * a city/town/village code.
32 *
33 * @author Pieren <pieren3@gmail.com>,
34 * @author <matthieu.lochegnies@gmail.com> for the extension to codeCommune
35 * @version 0.8
36 * History:
37 * 0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
38 * 0.2 22-Jun-2008 first stable version
39 * 0.3 24-Jun-2008 add code departement
40 * 0.4 06-Jul-2008 - add images scales, icons, menu items disabling
41 * - remove dependencies of wmsplugin
42 * - add option to force a Lambert zone (for median locations)
43 * - add auto-sourcing
44 * 0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
45 * - no overlapping of grabbed images if transparency is enabled
46 * - set one layer per location
47 * - use utf-8 charset in POST request to server
48 * - improve the preferences setting dialog
49 * - cancel the current download is now possible
50 * - add automatic images caching and load on request (+ manage cache directory size)
51 * - enable auto-sourcing only if a WMS layer is used
52 * 0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
53 * - process the overlapping images when cache is loaded from disk
54 * - save the last 'new location request' text again in preferences
55 * - avoid duplicate layers with same name
56 * - set text input for new locations in upper case
57 * - the cache directory is configurable in "cadastrewms.cacheDir"
58 * - improve configuration change updates
59 * 0.7 24-Aug-2008 - mask images only if transparency enabled
60 * - validate projection name by Lambert.toString() method
61 * 0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
62 * - new possible grab factor of 100 square meters fixed size
63 * - minor fixes due to changes in JOSM core classes
64 * - first draft of raster image support
65 * 0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
66 * and simplify
67 * 1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
68 * - increase PNG picture size requested to WMS (800x1000)
69 * - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
70 * 1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
71 * - propose to use shortcut F11 for grabbing
72 * 1.2 16-Aug-2009 - implementation of raster image grabbing, cropping and georeferencing (not the
73 * overview rasters (Tableau d'assemblage) but directly small units (Feuille)
74 * 1.3 23-Aug-2009 - improve georeferencing action cancellation
75 * - fixed bug of raster image loaded from cache not working on Java1.6
76 * - improve mouse click bounce detection during georeferencing process
77 * 1.4 17-Oct-2009 - add support for new Lambert CC 9 Zones projection
78 */
79public class CadastrePlugin extends Plugin {
80 static String VERSION = "1.4";
81
82 static JMenu cadastreJMenu;
83
84 public static CadastreGrabber cadastreGrabber = new CadastreGrabber();
85
86 public static String source = "";
87
88 // true if the checkbox "auto-sourcing" is set in the plugin menu
89 public static boolean autoSourcing = false;
90
91 // true when the plugin is first used, e.g. grab from WMS or download cache file
92 public static boolean pluginUsed = false;
93
94 public static String cacheDir = null;
95
96 public static boolean alterColors = false;
97
98 public static boolean backgroundTransparent = false;
99
100 public static float transparency = 1.0f;
101
102 public static boolean drawBoundaries = false;
103
104 static private boolean menuEnabled = false;
105
106 /**
107 * Creates the plugin and setup the default settings if necessary
108 *
109 * @throws Exception
110 */
111 public CadastrePlugin() throws Exception {
112 System.out.println("Pluging \"cadastre-fr\" started...");
113 if (Main.pref.get("cadastrewms.cacheDir").equals(""))
114 cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
115 else {
116 cacheDir = Main.pref.get("cadastrewms.cacheDir");
117 if (cacheDir.charAt(cacheDir.length()-1) != '\\' )
118 cacheDir += '\\';
119 }
120 System.out.println("current cache directory: "+cacheDir);
121
122 refreshConfiguration();
123
124 UploadAction.registerUploadHook(new CheckSourceUploadHook());
125 }
126
127 public static void refreshMenu() {
128 MainMenu menu = Main.main.menu;
129
130 if (cadastreJMenu == null) {
131 cadastreJMenu = menu.addMenu(marktr("Cadastre"), KeyEvent.VK_C, menu.defaultMenuPos);
132 JosmAction grab = new MenuActionGrab();
133 JMenuItem menuGrab = new JMenuItem(grab);
134 KeyStroke ks = grab.getShortcut().getKeyStroke();
135 if (ks != null) {
136 menuGrab.setAccelerator(ks);
137 }
138 JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
139 JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
140 final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
141 menuSource.setSelected(autoSourcing);
142 menuSource.addActionListener(new ActionListener() {
143 public void actionPerformed(ActionEvent ev) {
144 Main.pref.put("cadastrewms.autosourcing", menuSource.isSelected());
145 autoSourcing = menuSource.isSelected();
146 }
147 });
148
149 JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
150 JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
151 JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
152 //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
153 //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
154
155 cadastreJMenu.add(menuGrab);
156 cadastreJMenu.add(menuActionGrabPlanImage);
157 cadastreJMenu.add(menuSettings);
158 cadastreJMenu.add(menuSource);
159 cadastreJMenu.add(menuResetCookie);
160 cadastreJMenu.add(menuLambertZone);
161 cadastreJMenu.add(menuLoadFromCache);
162 // all SVG features disabled until official WMS is released
163 //cadastreJMenu.add(menuActionBoundaries);
164 //cadastreJMenu.add(menuActionBuildings);
165 }
166 setEnabledAll(menuEnabled);
167 }
168
169 public static void refreshConfiguration() {
170 source = Main.pref.get("cadastrewms.source",
171 "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre ; mise \u00e0 jour : AAAA");
172 autoSourcing = Main.pref.getBoolean("cadastrewms.autosourcing", true);
173 alterColors = Main.pref.getBoolean("cadastrewms.alterColors");
174 drawBoundaries = Main.pref.getBoolean("cadastrewms.drawBoundaries", false);
175 if (alterColors) {
176 backgroundTransparent = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
177 transparency = Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"));
178 } else {
179 backgroundTransparent = false;
180 transparency = 1.0f;
181 }
182 // overwrite F11 shortcut used from the beginning by this plugin and recently used
183 // for full-screen switch in JOSM core
184 int i = 0;
185 String p = Main.pref.get("shortcut.shortcut."+i, null);
186 boolean alreadyRedefined = false;
187 while (p != null) {
188 String[] s = p.split(";");
189 alreadyRedefined = alreadyRedefined || s[0].equals("menu:view:fullscreen");
190 i++;
191 p = Main.pref.get("shortcut.shortcut."+i, null);
192 }
193 if (!alreadyRedefined) {
194 int reply = JOptionPane.showConfirmDialog(null,
195 tr("Plugin cadastre-fr used traditionaly for grabbing the key shortcut F11\n"+
196 "which is currently allocated for full-screen switch by default\n"+
197 "Would you like to restore F11 for grab action ?"),
198 tr("Restore grab shortcut F11"),
199 JOptionPane.YES_NO_OPTION);
200 if (reply == JOptionPane.OK_OPTION) {
201 System.out.println("redefine fullscreen shortcut F11 to shift+F11");
202 Main.pref.put("shortcut.shortcut."+i, "menu:view:fullscreen;Toggle Full Screen view;122;5;122;64;false;true");
203 JOptionPane.showMessageDialog(Main.parent,tr("JOSM is stopped for the change to take effect."));
204 System.exit(0);
205 }
206 } else
207 System.out.println("shortcut F11 already redefined; do not change");
208
209 refreshMenu();
210 }
211
212 @Override
213 public PreferenceSetting getPreferenceSetting() {
214 return new CadastrePreferenceSetting();
215 }
216
217 private static void setEnabledAll(boolean isEnabled) {
218 boolean isLambertCC9Zones = Main.proj instanceof LambertCC9Zones;
219 for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
220 JMenuItem item = cadastreJMenu.getItem(i);
221 if (item != null)
222 if (item.getText().equals(MenuActionGrabPlanImage.name) /*||
223 item.getText().equals(MenuActionGrab.name) ||
224 item.getText().equals(MenuActionBoundaries.name) ||
225 item.getText().equals(MenuActionBuildings.name)*/) {
226 item.setEnabled(isEnabled);
227 } else if (item.getText().equals(MenuActionLambertZone.name)) {
228 item.setEnabled(!isEnabled);
229 }
230 }
231 menuEnabled = isEnabled;
232 }
233
234 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
235 if (cadastreJMenu != null) {
236 if (oldFrame == null && newFrame != null) {
237 setEnabledAll(true);
238 /*Main.map.addMapMode(new IconToggleButton
239 (new WMSAdjustAction(Main.map)));*/
240 } else if (oldFrame != null && newFrame == null) {
241 setEnabledAll(false);
242 Lambert.layoutZone = -1;
243 LambertCC9Zones.layoutZone = -1;
244 }
245 }
246 }
247
248 public static boolean isCadastreProjection() {
249 return Main.proj.toString().equals(new Lambert().toString())
250 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Fort_Marigot().toString())
251 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Ste_Anne().toString())
252 || Main.proj.toString().equals(new UTM_20N_Martinique_Fort_Desaix().toString())
253 || Main.proj.toString().equals(new GaussLaborde_Reunion().toString())
254 || Main.proj.toString().equals(new LambertCC9Zones().toString());
255 }
256
257 public static void safeSleep(long milliseconds) {
258 try {
259 Thread.sleep(milliseconds);
260 } catch (InterruptedException e) {}
261 }
262
263 // See OptionPaneUtil
264 // FIXME: this is a temporary solution.
265 public static void prepareDialog(JDialog dialog) {
266 if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
267 try {
268 dialog.setAlwaysOnTop(true);
269 } catch(SecurityException e) {
270 System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
271 }
272 }
273 dialog.setModal(true);
274 dialog.toFront();
275 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
276 }
277}
Note: See TracBrowser for help on using the repository browser.