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

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

raster image feature implementation

  • Property svn:eol-style set to native
File size: 12.2 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.IconToggleButton;
23import org.openstreetmap.josm.gui.MainMenu;
24import org.openstreetmap.josm.gui.MapFrame;
25import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
26import org.openstreetmap.josm.plugins.Plugin;
27import org.openstreetmap.josm.data.projection.*;
28
29/**
30 *
31 * Plugin to access the French Cadastre WMS server at www.cadastre.gouv.fr This
32 * WMS server requires some specific handling like retrieving a cookie for a
33 * limitation in case of no activity, or the request to the server shall provide
34 * a city/town/village code.
35 *
36 * @author Pieren <pieren3@gmail.com>,
37 * @author <matthieu.lochegnies@gmail.com> for the extension to codeCommune
38 * @version 0.8
39 * History:
40 * 0.1 17-Jun-2008 first prototype using a first Lambert projection impl. in core
41 * 0.2 22-Jun-2008 first stable version
42 * 0.3 24-Jun-2008 add code departement
43 * 0.4 06-Jul-2008 - add images scales, icons, menu items disabling
44 * - remove dependencies of wmsplugin
45 * - add option to force a Lambert zone (for median locations)
46 * - add auto-sourcing
47 * 0.5 16-Aug-2008 - add transparency in layer (allowing multiple wms layers displayed together)
48 * - no overlapping of grabbed images if transparency is enabled
49 * - set one layer per location
50 * - use utf-8 charset in POST request to server
51 * - improve the preferences setting dialog
52 * - cancel the current download is now possible
53 * - add automatic images caching and load on request (+ manage cache directory size)
54 * - enable auto-sourcing only if a WMS layer is used
55 * 0.6 18-Aug-2008 - suppress the null-exception message after the dialog 'open a layer first'
56 * - process the overlapping images when cache is loaded from disk
57 * - save the last 'new location request' text again in preferences
58 * - avoid duplicate layers with same name
59 * - set text input for new locations in upper case
60 * - the cache directory is configurable in "cadastrewms.cacheDir"
61 * - improve configuration change updates
62 * 0.7 24-Aug-2008 - mask images only if transparency enabled
63 * - validate projection name by Lambert.toString() method
64 * 0.8 25-Jan-2009 - display returned list of communes if direct name is not recognized by server
65 * - new possible grab factor of 100 square meters fixed size
66 * - minor fixes due to changes in JOSM core classes
67 * - first draft of raster image support
68 * 0.9 05-Feb-2009 - grab vectorized full commune bbox, save in file, convert to OSM way
69 * and simplify
70 * 1.0 18-Feb-2009 - fix various bugs in color management and preference dialog
71 * - increase PNG picture size requested to WMS (800x1000)
72 * - set 4th grab scale fixed size configurable (from 25 to 1000 meters)
73 * 1.1 11-Jun-2009 - fixed a null exception error when trying to displace a vectorized layer
74 * - propose to use shortcut F11 for grabbing
75 */
76public class CadastrePlugin extends Plugin {
77 static String VERSION = "1.0";
78
79 static JMenu cadastreJMenu;
80
81 public static CadastreGrabber cadastreGrabber = new CadastreGrabber();
82
83 public static String source = "";
84
85 // true if the checkbox "auto-sourcing" is set in the plugin menu
86 public static boolean autoSourcing = false;
87
88 // true when the plugin is first used, e.g. grab from WMS or download cache file
89 public static boolean pluginUsed = false;
90
91 public static String cacheDir = null;
92
93 public static boolean alterColors = false;
94
95 public static boolean backgroundTransparent = false;
96
97 public static float transparency = 1.0f;
98
99 public static boolean drawBoundaries = false;
100
101 static private boolean menuEnabled = false;
102
103 /**
104 * Creates the plugin and setup the default settings if necessary
105 *
106 * @throws Exception
107 */
108 public CadastrePlugin() throws Exception {
109 System.out.println("Pluging \"cadastre-fr\" started...");
110 if (Main.pref.get("cadastrewms.cacheDir").equals(""))
111 cacheDir = Main.pref.getPreferencesDir()+"plugins/cadastrewms/";
112 else {
113 cacheDir = Main.pref.get("cadastrewms.cacheDir");
114 if (cacheDir.charAt(cacheDir.length()-1) != '\\' )
115 cacheDir += '\\';
116 }
117 System.out.println("current cache directory: "+cacheDir);
118
119 refreshConfiguration();
120 refreshMenu();
121
122 // add a hook at uploading to insert/verify the source=cadastre tag
123 LinkedList<UploadHook> hooks = ((UploadAction) Main.main.menu.upload).uploadHooks;
124 hooks.add(0, new CheckSourceUploadHook());
125 }
126
127 public void refreshMenu() throws Exception {
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
210 @Override
211 public PreferenceSetting getPreferenceSetting() {
212 return new CadastrePreferenceSetting();
213 }
214
215 private void setEnabledAll(boolean isEnabled) {
216 for (int i = 0; i < cadastreJMenu.getItemCount(); i++) {
217 JMenuItem item = cadastreJMenu.getItem(i);
218 if (item != null)
219 if (item.getText().equals(MenuActionGrab.name) ||
220 item.getText().equals(MenuActionGrabPlanImage.name) /* ||
221 item.getText().equals(MenuActionBoundaries.name) ||
222 item.getText().equals(MenuActionBuildings.name)*/) {
223 item.setEnabled(isEnabled);
224 } else if (item.getText().equals(MenuActionLambertZone.name)) {
225 item.setEnabled(!isEnabled);
226 }
227 }
228 menuEnabled = isEnabled;
229 }
230
231 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
232 if (cadastreJMenu != null) {
233 if (oldFrame == null && newFrame != null) {
234 setEnabledAll(true);
235 /*Main.map.addMapMode(new IconToggleButton
236 (new WMSAdjustAction(Main.map)));*/
237 } else if (oldFrame != null && newFrame == null) {
238 setEnabledAll(false);
239 Lambert.layoutZone = -1;
240 }
241 }
242 }
243
244 public static boolean isCadastreProjection() {
245 return Main.proj.toString().equals(new Lambert().toString())
246 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Fort_Marigot().toString())
247 || Main.proj.toString().equals(new UTM_20N_Guadeloupe_Ste_Anne().toString())
248 || Main.proj.toString().equals(new UTM_20N_Martinique_Fort_Desaix().toString())
249 || Main.proj.toString().equals(new GaussLaborde_Reunion().toString());
250 }
251
252 public static void safeSleep(long milliseconds) {
253 try {
254 Thread.sleep(milliseconds);
255 } catch (InterruptedException e) {}
256 }
257
258 // See OptionPaneUtil
259 // FIXME: this is a temporary solution.
260 public static void prepareDialog(JDialog dialog) {
261 if (Main.pref.getBoolean("window-handling.option-pane-always-on-top", true)) {
262 try {
263 dialog.setAlwaysOnTop(true);
264 } catch(SecurityException e) {
265 System.out.println(tr("Warning: failed to put option pane dialog always on top. Exception was: {0}", e.toString()));
266 }
267 }
268 dialog.setModal(true);
269 dialog.toFront();
270 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
271 }
272}
Note: See TracBrowser for help on using the repository browser.