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

Last change on this file since 17528 was 17528, checked in by guggis, 15 years ago

Updating to JOSM r2082

  • Property svn:eol-style set to native
File size: 12.5 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;
9import java.util.LinkedList;
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.actions.UploadAction.UploadHook;
22import org.openstreetmap.josm.gui.MainMenu;
23import org.openstreetmap.josm.gui.MapFrame;
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 */
80public class CadastrePlugin extends Plugin {
81 static String VERSION = "1.3";
82
83 static JMenu cadastreJMenu;
84
85 public static CadastreGrabber cadastreGrabber = new CadastreGrabber();
86
87 public static String source = "";
88
89 // true if the checkbox "auto-sourcing" is set in the plugin menu
90 public static boolean autoSourcing = false;
91
92 // true when the plugin is first used, e.g. grab from WMS or download cache file
93 public static boolean pluginUsed = false;
94
95 public static String cacheDir = null;
96
97 public static boolean alterColors = false;
98
99 public static boolean backgroundTransparent = false;
100
101 public static float transparency = 1.0f;
102
103 public static boolean drawBoundaries = false;
104
105 static private boolean menuEnabled = false;
106
107 /**
108 * Creates the plugin and setup the default settings if necessary
109 *
110 * @throws Exception
111 */
112 public CadastrePlugin() throws Exception {
113 System.out.println("Pluging \"cadastre-fr\" started...");
114 if (Main.pref.get("cadastrewms.cacheDir").equals(""))
115 cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
116 else {
117 cacheDir = Main.pref.get("cadastrewms.cacheDir");
118 if (cacheDir.charAt(cacheDir.length()-1) != '\\' )
119 cacheDir += '\\';
120 }
121 System.out.println("current cache directory: "+cacheDir);
122
123 refreshConfiguration();
124 refreshMenu();
125
126 UploadAction.registerUploadHook(new CheckSourceUploadHook());
127 }
128
129 public void refreshMenu() throws Exception {
130 MainMenu menu = Main.main.menu;
131
132 if (cadastreJMenu == null) {
133 cadastreJMenu = menu.addMenu(marktr("Cadastre"), KeyEvent.VK_C, menu.defaultMenuPos);
134 JosmAction grab = new MenuActionGrab();
135 JMenuItem menuGrab = new JMenuItem(grab);
136 KeyStroke ks = grab.getShortcut().getKeyStroke();
137 if (ks != null) {
138 menuGrab.setAccelerator(ks);
139 }
140 JMenuItem menuActionGrabPlanImage = new JMenuItem(new MenuActionGrabPlanImage());
141 JMenuItem menuSettings = new JMenuItem(new MenuActionNewLocation());
142 final JCheckBoxMenuItem menuSource = new JCheckBoxMenuItem(tr("Auto sourcing"));
143 menuSource.setSelected(autoSourcing);
144 menuSource.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent ev) {
146 Main.pref.put("cadastrewms.autosourcing", menuSource.isSelected());
147 autoSourcing = menuSource.isSelected();
148 }
149 });
150
151 JMenuItem menuResetCookie = new JMenuItem(new MenuActionResetCookie());
152 JMenuItem menuLambertZone = new JMenuItem(new MenuActionLambertZone());
153 JMenuItem menuLoadFromCache = new JMenuItem(new MenuActionLoadFromCache());
154 //JMenuItem menuActionBoundaries = new JMenuItem(new MenuActionBoundaries());
155 //JMenuItem menuActionBuildings = new JMenuItem(new MenuActionBuildings());
156
157 cadastreJMenu.add(menuGrab);
158 cadastreJMenu.add(menuActionGrabPlanImage);
159 cadastreJMenu.add(menuSettings);
160 cadastreJMenu.add(menuSource);
161 cadastreJMenu.add(menuResetCookie);
162 cadastreJMenu.add(menuLambertZone);
163 cadastreJMenu.add(menuLoadFromCache);
164 // all SVG features disabled until official WMS is released
165 //cadastreJMenu.add(menuActionBoundaries);
166 //cadastreJMenu.add(menuActionBuildings);
167 }
168 setEnabledAll(menuEnabled);
169 }
170
171 public static void refreshConfiguration() {
172 source = Main.pref.get("cadastrewms.source",
173 "cadastre-dgi-fr source : Direction G\u00e9n\u00e9rale des Imp\u00f4ts - Cadastre ; mise \u00e0 jour : AAAA");
174 autoSourcing = Main.pref.getBoolean("cadastrewms.autosourcing", true);
175 alterColors = Main.pref.getBoolean("cadastrewms.alterColors");
176 drawBoundaries = Main.pref.getBoolean("cadastrewms.drawBoundaries", false);
177 if (alterColors) {
178 backgroundTransparent = Main.pref.getBoolean("cadastrewms.backgroundTransparent");
179 transparency = Float.parseFloat(Main.pref.get("cadastrewms.brightness", "1.0f"));
180 } else {
181 backgroundTransparent = false;
182 transparency = 1.0f;
183 }
184 // overwrite F11 shortcut used from the beginning by this plugin and recently used
185 // for full-screen switch in JOSM core
186 int i = 0;
187 String p = Main.pref.get("shortcut.shortcut."+i, null);
188 boolean alreadyRedefined = false;
189 while (p != null) {
190 String[] s = p.split(";");
191 alreadyRedefined = alreadyRedefined || s[0].equals("menu:view:fullscreen");
192 i++;
193 p = Main.pref.get("shortcut.shortcut."+i, null);
194 }
195 if (!alreadyRedefined) {
196 int reply = JOptionPane.showConfirmDialog(null,
197 tr("Plugin cadastre-fr used traditionaly for grabbing the key shortcut F11\n"+
198 "which is currently allocated for full-screen switch by default\n"+
199 "Would you like to restore F11 for grab action ?"),
200 tr("Restore grab shortcut F11"),
201 JOptionPane.YES_NO_OPTION);
202 if (reply == JOptionPane.OK_OPTION) {
203 System.out.println("redefine fullscreen shortcut F11 to shift+F11");
204 Main.pref.put("shortcut.shortcut."+i, "menu:view:fullscreen;Toggle Full Screen view;122;5;122;64;false;true");
205 JOptionPane.showMessageDialog(Main.parent,tr("JOSM is stopped for the change to take effect."));
206 System.exit(0);
207 }
208 } else
209 System.out.println("shortcut F11 already redefined; do not change");
210 }
211
212 @Override
213 public PreferenceSetting getPreferenceSetting() {
214 return new CadastrePreferenceSetting();
215 }
216
217 private void setEnabledAll(boolean isEnabled) {
218 for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
219 JMenuItem item = cadastreJMenu.getItem(i);
220 if (item != null)
221 if (item.getText().equals(MenuActionGrab.name) ||
222 item.getText().equals(MenuActionGrabPlanImage.name) /* ||
223 item.getText().equals(MenuActionBoundaries.name) ||
224 item.getText().equals(MenuActionBuildings.name)*/) {
225 item.setEnabled(isEnabled);
226 } else if (item.getText().equals(MenuActionLambertZone.name)) {
227 item.setEnabled(!isEnabled);
228 }
229 }
230 menuEnabled = isEnabled;
231 }
232
233 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
234 if (cadastreJMenu != null) {
235 if (oldFrame == null && newFrame != null) {
236 setEnabledAll(true);
237 /*Main.map.addMapMode(new IconToggleButton
238 (new WMSAdjustAction(Main.map)));*/
239 } else if (oldFrame != null && newFrame == null) {
240 setEnabledAll(false);
241 Lambert.layoutZone = -1;
242 }
243 }
244 }
245
246 public static boolean isCadastreProjection() {
247 return Main.proj.toString().equals(new Lambert().toString())
248 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Fort_Marigot().toString())
249 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Ste_Anne().toString())
250 || Main.proj.toString().equals(new UTM_20N_Martinique_Fort_Desaix().toString())
251 || Main.proj.toString().equals(new GaussLaborde_Reunion().toString());
252 }
253
254 public static void safeSleep(long milliseconds) {
255 try {
256 Thread.sleep(milliseconds);
257 } catch (InterruptedException e) {}
258 }
259
260 // See OptionPaneUtil
261 // FIXME: this is a temporary solution.
262 public static void prepareDialog(JDialog dialog) {
263 if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
264 try {
265 dialog.setAlwaysOnTop(true);
266 } catch(SecurityException e) {
267 System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
268 }
269 }
270 dialog.setModal(true);
271 dialog.toFront();
272 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
273 }
274}
Note: See TracBrowser for help on using the repository browser.