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

Last change on this file since 18314 was 18314, checked in by pieren, 16 years ago

Fixed deprecated method.

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