1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.awt.Toolkit;
|
---|
12 | import java.awt.event.ComponentEvent;
|
---|
13 | import java.awt.event.ComponentListener;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.awt.event.WindowAdapter;
|
---|
16 | import java.awt.event.WindowEvent;
|
---|
17 | import java.io.File;
|
---|
18 | import java.lang.ref.WeakReference;
|
---|
19 | import java.net.URI;
|
---|
20 | import java.net.URISyntaxException;
|
---|
21 | import java.util.ArrayList;
|
---|
22 | import java.util.Collection;
|
---|
23 | import java.util.Iterator;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.StringTokenizer;
|
---|
27 | import java.util.concurrent.ExecutorService;
|
---|
28 | import java.util.concurrent.Executors;
|
---|
29 | import java.util.concurrent.Future;
|
---|
30 | import java.util.regex.Matcher;
|
---|
31 | import java.util.regex.Pattern;
|
---|
32 |
|
---|
33 | import javax.swing.Action;
|
---|
34 | import javax.swing.InputMap;
|
---|
35 | import javax.swing.JComponent;
|
---|
36 | import javax.swing.JFrame;
|
---|
37 | import javax.swing.JLabel;
|
---|
38 | import javax.swing.JOptionPane;
|
---|
39 | import javax.swing.JPanel;
|
---|
40 | import javax.swing.JTextArea;
|
---|
41 | import javax.swing.KeyStroke;
|
---|
42 | import javax.swing.UIManager;
|
---|
43 |
|
---|
44 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
45 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
46 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
47 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
48 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
49 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
50 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
51 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
52 | import org.openstreetmap.josm.data.Bounds;
|
---|
53 | import org.openstreetmap.josm.data.Preferences;
|
---|
54 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
55 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
56 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
57 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
58 | import org.openstreetmap.josm.data.osm.PrimitiveDeepCopy;
|
---|
59 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
60 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
61 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
62 | import org.openstreetmap.josm.gui.GettingStarted;
|
---|
63 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
64 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
65 | import org.openstreetmap.josm.gui.MapView;
|
---|
66 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
67 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
68 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
69 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
70 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
71 | import org.openstreetmap.josm.gui.preferences.ImageryPreference;
|
---|
72 | import org.openstreetmap.josm.gui.preferences.MapPaintPreference;
|
---|
73 | import org.openstreetmap.josm.gui.preferences.ProjectionPreference;
|
---|
74 | import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
|
---|
75 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
76 | import org.openstreetmap.josm.io.OsmApi;
|
---|
77 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
78 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
79 | import org.openstreetmap.josm.tools.I18n;
|
---|
80 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
81 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
82 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
83 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
---|
84 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
85 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
86 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
87 | import org.openstreetmap.josm.tools.Utils;
|
---|
88 |
|
---|
89 | abstract public class Main {
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
|
---|
93 | * it only shows the MOTD panel.
|
---|
94 | *
|
---|
95 | * @return true if JOSM currently displays a map view
|
---|
96 | */
|
---|
97 | static public boolean isDisplayingMapView() {
|
---|
98 | if (map == null) return false;
|
---|
99 | if (map.mapView == null) return false;
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 | /**
|
---|
103 | * Global parent component for all dialogs and message boxes
|
---|
104 | */
|
---|
105 | public static Component parent;
|
---|
106 | /**
|
---|
107 | * Global application.
|
---|
108 | */
|
---|
109 | public static Main main;
|
---|
110 | /**
|
---|
111 | * The worker thread slave. This is for executing all long and intensive
|
---|
112 | * calculations. The executed runnables are guaranteed to be executed separately
|
---|
113 | * and sequential.
|
---|
114 | */
|
---|
115 | public final static ExecutorService worker = Executors.newSingleThreadExecutor();
|
---|
116 | /**
|
---|
117 | * Global application preferences
|
---|
118 | */
|
---|
119 | public static Preferences pref;
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * The global paste buffer.
|
---|
123 | */
|
---|
124 | public static PrimitiveDeepCopy pasteBuffer = new PrimitiveDeepCopy();
|
---|
125 | public static Layer pasteSource;
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * The MapFrame. Use setMapFrame to set or clear it.
|
---|
129 | */
|
---|
130 | public static MapFrame map;
|
---|
131 | /**
|
---|
132 | * True, when in applet mode
|
---|
133 | */
|
---|
134 | public static boolean applet = false;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * The toolbar preference control to register new actions.
|
---|
138 | */
|
---|
139 | public static ToolbarPreferences toolbar;
|
---|
140 |
|
---|
141 | public UndoRedoHandler undoRedo = new UndoRedoHandler();
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * The main menu bar at top of screen.
|
---|
145 | */
|
---|
146 | public final MainMenu menu;
|
---|
147 |
|
---|
148 | public final OsmValidator validator;
|
---|
149 | /**
|
---|
150 | * The MOTD Layer.
|
---|
151 | */
|
---|
152 | private GettingStarted gettingStarted = new GettingStarted();
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Print a message if logging is on.
|
---|
156 | */
|
---|
157 | static public int log_level = 2;
|
---|
158 | static public void warn(String msg) {
|
---|
159 | if (log_level < 1)
|
---|
160 | return;
|
---|
161 | System.out.println(msg);
|
---|
162 | }
|
---|
163 | static public void info(String msg) {
|
---|
164 | if (log_level < 2)
|
---|
165 | return;
|
---|
166 | System.out.println(msg);
|
---|
167 | }
|
---|
168 | static public void debug(String msg) {
|
---|
169 | if (log_level < 3)
|
---|
170 | return;
|
---|
171 | System.out.println(msg);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Platform specific code goes in here.
|
---|
176 | * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
|
---|
177 | * So if you need to hook into those early ones, split your class and send the one with the early hooks
|
---|
178 | * to the JOSM team for inclusion.
|
---|
179 | */
|
---|
180 | public static PlatformHook platform;
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Wheather or not the java vm is openjdk
|
---|
184 | * We use this to work around openjdk bugs
|
---|
185 | */
|
---|
186 | public static boolean isOpenjdk;
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Set or clear (if passed <code>null</code>) the map.
|
---|
190 | */
|
---|
191 | public final void setMapFrame(final MapFrame map) {
|
---|
192 | MapFrame old = Main.map;
|
---|
193 | panel.setVisible(false);
|
---|
194 | panel.removeAll();
|
---|
195 | if (map != null) {
|
---|
196 | map.fillPanel(panel);
|
---|
197 | } else {
|
---|
198 | old.destroy();
|
---|
199 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
200 | }
|
---|
201 | panel.setVisible(true);
|
---|
202 | redoUndoListener.commandChanged(0,0);
|
---|
203 |
|
---|
204 | Main.map = map;
|
---|
205 |
|
---|
206 | PluginHandler.notifyMapFrameChanged(old, map);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Remove the specified layer from the map. If it is the last layer,
|
---|
211 | * remove the map as well.
|
---|
212 | */
|
---|
213 | public final void removeLayer(final Layer layer) {
|
---|
214 | if (map != null) {
|
---|
215 | map.mapView.removeLayer(layer);
|
---|
216 | if (map.mapView.getAllLayers().isEmpty()) {
|
---|
217 | setMapFrame(null);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | public Main() {
|
---|
223 | main = this;
|
---|
224 | isOpenjdk = System.getProperty("java.vm.name").toUpperCase().indexOf("OPENJDK") != -1;
|
---|
225 | platform.startupHook();
|
---|
226 |
|
---|
227 | // We try to establish an API connection early, so that any API
|
---|
228 | // capabilities are already known to the editor instance. However
|
---|
229 | // if it goes wrong that's not critical at this stage.
|
---|
230 | try {
|
---|
231 | OsmApi.getOsmApi().initialize(null, true);
|
---|
232 | } catch (Exception x) {
|
---|
233 | // ignore any exception here.
|
---|
234 | }
|
---|
235 |
|
---|
236 | contentPanePrivate.add(panel, BorderLayout.CENTER);
|
---|
237 | panel.add(gettingStarted, BorderLayout.CENTER);
|
---|
238 | menu = new MainMenu();
|
---|
239 |
|
---|
240 | undoRedo.listenerCommands.add(redoUndoListener);
|
---|
241 |
|
---|
242 | // creating toolbar
|
---|
243 | contentPanePrivate.add(toolbar.control, BorderLayout.NORTH);
|
---|
244 |
|
---|
245 | registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
|
---|
246 | KeyEvent.VK_F1, Shortcut.GROUP_DIRECT));
|
---|
247 |
|
---|
248 | TaggingPresetPreference.initialize();
|
---|
249 | MapPaintPreference.initialize();
|
---|
250 | ImageryPreference.initialize();
|
---|
251 |
|
---|
252 | validator = new OsmValidator();
|
---|
253 | MapView.addLayerChangeListener(validator);
|
---|
254 |
|
---|
255 | toolbar.refreshToolbarControl();
|
---|
256 |
|
---|
257 | toolbar.control.updateUI();
|
---|
258 | contentPanePrivate.updateUI();
|
---|
259 |
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Add a new layer to the map. If no map exists, create one.
|
---|
264 | */
|
---|
265 | public final void addLayer(final Layer layer) {
|
---|
266 | if (map == null) {
|
---|
267 | final MapFrame mapFrame = new MapFrame(contentPanePrivate);
|
---|
268 | setMapFrame(mapFrame);
|
---|
269 | mapFrame.selectMapMode((MapMode)mapFrame.getDefaultButtonAction());
|
---|
270 | mapFrame.setVisible(true);
|
---|
271 | mapFrame.initializeDialogsPane();
|
---|
272 | // bootstrapping problem: make sure the layer list dialog is going to
|
---|
273 | // listen to change events of the very first layer
|
---|
274 | //
|
---|
275 | layer.addPropertyChangeListener(LayerListDialog.getInstance().getModel());
|
---|
276 | }
|
---|
277 | map.mapView.addLayer(layer);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Replies true if there is an edit layer
|
---|
282 | *
|
---|
283 | * @return true if there is an edit layer
|
---|
284 | */
|
---|
285 | public boolean hasEditLayer() {
|
---|
286 | if (getEditLayer() == null) return false;
|
---|
287 | return true;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Replies the current edit layer
|
---|
292 | *
|
---|
293 | * @return the current edit layer. null, if no current edit layer exists
|
---|
294 | */
|
---|
295 | public OsmDataLayer getEditLayer() {
|
---|
296 | if (map == null) return null;
|
---|
297 | if (map.mapView == null) return null;
|
---|
298 | return map.mapView.getEditLayer();
|
---|
299 | }
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Replies the current data set.
|
---|
303 | *
|
---|
304 | * @return the current data set. null, if no current data set exists
|
---|
305 | */
|
---|
306 | public DataSet getCurrentDataSet() {
|
---|
307 | if (!hasEditLayer()) return null;
|
---|
308 | return getEditLayer().data;
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Returns the currently active layer
|
---|
313 | *
|
---|
314 | * @return the currently active layer. null, if currently no active layer exists
|
---|
315 | */
|
---|
316 | public Layer getActiveLayer() {
|
---|
317 | if (map == null) return null;
|
---|
318 | if (map.mapView == null) return null;
|
---|
319 | return map.mapView.getActiveLayer();
|
---|
320 | }
|
---|
321 |
|
---|
322 | protected static JPanel contentPanePrivate = new JPanel(new BorderLayout());
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * @deprecated If you just need to register shortcut for action, use registerActionShortcut instead of accessing InputMap directly
|
---|
326 | */
|
---|
327 | @Deprecated
|
---|
328 | public static final JPanel contentPane = contentPanePrivate;
|
---|
329 |
|
---|
330 | public static void registerActionShortcut(Action action, Shortcut shortcut) {
|
---|
331 | registerActionShortcut(action, shortcut.getKeyStroke());
|
---|
332 | }
|
---|
333 |
|
---|
334 | public static void registerActionShortcut(Action action, KeyStroke keyStroke) {
|
---|
335 | if (keyStroke == null)
|
---|
336 | return;
|
---|
337 |
|
---|
338 | InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
339 | Object existing = inputMap.get(keyStroke);
|
---|
340 | if (existing != null && !existing.equals(action)) {
|
---|
341 | System.out.println(String.format("Keystroke %s is already assigned to %s, will be overridden by %s", keyStroke, existing, action));
|
---|
342 | }
|
---|
343 | inputMap.put(keyStroke, action);
|
---|
344 |
|
---|
345 | contentPanePrivate.getActionMap().put(action, action);
|
---|
346 | }
|
---|
347 |
|
---|
348 | public static void unregisterActionShortcut(Shortcut shortcut) {
|
---|
349 | contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
|
---|
350 | }
|
---|
351 |
|
---|
352 | public static void unregisterActionShortcut(JosmAction action) {
|
---|
353 | unregisterActionShortcut(action, action.getShortcut());
|
---|
354 | }
|
---|
355 |
|
---|
356 | public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
|
---|
357 | contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
|
---|
358 | contentPanePrivate.getActionMap().remove(action);
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | ///////////////////////////////////////////////////////////////////////////
|
---|
363 | // Implementation part
|
---|
364 | ///////////////////////////////////////////////////////////////////////////
|
---|
365 |
|
---|
366 | public static JPanel panel = new JPanel(new BorderLayout());
|
---|
367 |
|
---|
368 | protected static Rectangle bounds;
|
---|
369 | protected static int windowState = JFrame.NORMAL;
|
---|
370 |
|
---|
371 | private final CommandQueueListener redoUndoListener = new CommandQueueListener(){
|
---|
372 | public void commandChanged(final int queueSize, final int redoSize) {
|
---|
373 | menu.undo.setEnabled(queueSize > 0);
|
---|
374 | menu.redo.setEnabled(redoSize > 0);
|
---|
375 | }
|
---|
376 | };
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * Should be called before the main constructor to setup some parameter stuff
|
---|
380 | * @param args The parsed argument list.
|
---|
381 | */
|
---|
382 | public static void preConstructorInit(Map<String, Collection<String>> args) {
|
---|
383 | ProjectionPreference.setProjection();
|
---|
384 |
|
---|
385 | try {
|
---|
386 | String defaultlaf = platform.getDefaultStyle();
|
---|
387 | String laf = Main.pref.get("laf", defaultlaf);
|
---|
388 | try {
|
---|
389 | UIManager.setLookAndFeel(laf);
|
---|
390 | }
|
---|
391 | catch (final java.lang.ClassNotFoundException e) {
|
---|
392 | System.out.println("Look and Feel not found: " + laf);
|
---|
393 | Main.pref.put("laf", defaultlaf);
|
---|
394 | }
|
---|
395 | catch (final javax.swing.UnsupportedLookAndFeelException e) {
|
---|
396 | System.out.println("Look and Feel not supported: " + laf);
|
---|
397 | Main.pref.put("laf", defaultlaf);
|
---|
398 | }
|
---|
399 | toolbar = new ToolbarPreferences();
|
---|
400 | contentPanePrivate.updateUI();
|
---|
401 | panel.updateUI();
|
---|
402 | } catch (final Exception e) {
|
---|
403 | e.printStackTrace();
|
---|
404 | }
|
---|
405 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
406 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
407 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
408 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
409 |
|
---|
410 | I18n.translateJavaInternalMessages();
|
---|
411 |
|
---|
412 | // init default coordinate format
|
---|
413 | //
|
---|
414 | try {
|
---|
415 | //CoordinateFormat format = CoordinateFormat.valueOf(Main.pref.get("coordinates"));
|
---|
416 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
|
---|
417 | } catch (IllegalArgumentException iae) {
|
---|
418 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
|
---|
419 | }
|
---|
420 |
|
---|
421 | Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
422 | String geometry = null;
|
---|
423 | if (args.containsKey("geometry")) {
|
---|
424 | geometry = args.get("geometry").iterator().next();
|
---|
425 | } else {
|
---|
426 | geometry = Main.pref.get("gui.geometry");
|
---|
427 | }
|
---|
428 | if (geometry.length() != 0) {
|
---|
429 | final Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(geometry);
|
---|
430 | if (m.matches()) {
|
---|
431 | int w = Integer.valueOf(m.group(1));
|
---|
432 | int h = Integer.valueOf(m.group(2));
|
---|
433 | int x = 0, y = 0;
|
---|
434 | if (m.group(3) != null) {
|
---|
435 | x = Integer.valueOf(m.group(5));
|
---|
436 | y = Integer.valueOf(m.group(7));
|
---|
437 | if (m.group(4).equals("-")) {
|
---|
438 | x = screenDimension.width - x - w;
|
---|
439 | }
|
---|
440 | if (m.group(6).equals("-")) {
|
---|
441 | y = screenDimension.height - y - h;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | // copied from WindowsGeometry.applySafe()
|
---|
445 | if (x > Toolkit.getDefaultToolkit().getScreenSize().width - 10) {
|
---|
446 | x = 0;
|
---|
447 | }
|
---|
448 | if (y > Toolkit.getDefaultToolkit().getScreenSize().height - 10) {
|
---|
449 | y = 0;
|
---|
450 | }
|
---|
451 | bounds = new Rectangle(x,y,w,h);
|
---|
452 | if(!Main.pref.get("gui.geometry").equals(geometry)) {
|
---|
453 | // remember this geometry
|
---|
454 | Main.pref.put("gui.geometry", geometry);
|
---|
455 | }
|
---|
456 | } else {
|
---|
457 | System.out.println("Ignoring malformed geometry: "+geometry);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | if (bounds == null) {
|
---|
461 | bounds = !args.containsKey("no-maximize") ? new Rectangle(0,0,screenDimension.width,screenDimension.height) : new Rectangle(1000,740);
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
|
---|
466 | if (args.containsKey("download")) {
|
---|
467 | List<File> fileList = new ArrayList<File>();
|
---|
468 | for (String s : args.get("download")) {
|
---|
469 | File f = null;
|
---|
470 | switch(paramType(s)) {
|
---|
471 | case httpUrl:
|
---|
472 | downloadFromParamHttp(false, s);
|
---|
473 | break;
|
---|
474 | case bounds:
|
---|
475 | downloadFromParamBounds(false, s);
|
---|
476 | break;
|
---|
477 | case fileUrl:
|
---|
478 | try {
|
---|
479 | f = new File(new URI(s));
|
---|
480 | } catch (URISyntaxException e) {
|
---|
481 | JOptionPane.showMessageDialog(
|
---|
482 | Main.parent,
|
---|
483 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
484 | tr("Warning"),
|
---|
485 | JOptionPane.WARNING_MESSAGE
|
---|
486 | );
|
---|
487 | }
|
---|
488 | if (f!=null) {
|
---|
489 | fileList.add(f);
|
---|
490 | }
|
---|
491 | break;
|
---|
492 | case fileName:
|
---|
493 | f = new File(s);
|
---|
494 | fileList.add(f);
|
---|
495 | break;
|
---|
496 | }
|
---|
497 | }
|
---|
498 | if(!fileList.isEmpty())
|
---|
499 | {
|
---|
500 | OpenFileAction.openFiles(fileList, true);
|
---|
501 | }
|
---|
502 | }
|
---|
503 | if (args.containsKey("downloadgps")) {
|
---|
504 | for (String s : args.get("downloadgps")) {
|
---|
505 | switch(paramType(s)) {
|
---|
506 | case httpUrl:
|
---|
507 | downloadFromParamHttp(true, s);
|
---|
508 | break;
|
---|
509 | case bounds:
|
---|
510 | downloadFromParamBounds(true, s);
|
---|
511 | break;
|
---|
512 | case fileUrl:
|
---|
513 | case fileName:
|
---|
514 | JOptionPane.showMessageDialog(
|
---|
515 | Main.parent,
|
---|
516 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
517 | tr("Warning"),
|
---|
518 | JOptionPane.WARNING_MESSAGE
|
---|
519 | );
|
---|
520 | }
|
---|
521 | }
|
---|
522 | }
|
---|
523 | if (args.containsKey("selection")) {
|
---|
524 | for (String s : args.get("selection")) {
|
---|
525 | SearchAction.search(s, SearchAction.SearchMode.add);
|
---|
526 | }
|
---|
527 | }
|
---|
528 | }
|
---|
529 |
|
---|
530 | public static boolean saveUnsavedModifications() {
|
---|
531 | if (map == null) return true;
|
---|
532 | SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
|
---|
533 | List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
|
---|
534 | for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
|
---|
535 | if (l.requiresSaveToFile() || l.requiresUploadToServer()) {
|
---|
536 | layersWithUnmodifiedChanges.add(l);
|
---|
537 | }
|
---|
538 | }
|
---|
539 | dialog.prepareForSavingAndUpdatingLayersBeforeExit();
|
---|
540 | if (!layersWithUnmodifiedChanges.isEmpty()) {
|
---|
541 | dialog.getModel().populate(layersWithUnmodifiedChanges);
|
---|
542 | dialog.setVisible(true);
|
---|
543 | switch(dialog.getUserAction()) {
|
---|
544 | case CANCEL: return false;
|
---|
545 | case PROCEED: return true;
|
---|
546 | default: return false;
|
---|
547 | }
|
---|
548 | }
|
---|
549 |
|
---|
550 | return true;
|
---|
551 | }
|
---|
552 |
|
---|
553 | public static boolean exitJosm(boolean exit) {
|
---|
554 | if (Main.saveUnsavedModifications()) {
|
---|
555 | Main.saveGuiGeometry();
|
---|
556 | // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
|
---|
557 | if (Main.isDisplayingMapView()) {
|
---|
558 | Collection<Layer> layers = new ArrayList<Layer>(Main.map.mapView.getAllLayers());
|
---|
559 | for (Layer l: layers) {
|
---|
560 | Main.map.mapView.removeLayer(l);
|
---|
561 | }
|
---|
562 | }
|
---|
563 | if (exit) {
|
---|
564 | System.exit(0);
|
---|
565 | return true;
|
---|
566 | } else
|
---|
567 | return true;
|
---|
568 | } else
|
---|
569 | return false;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * The type of a command line parameter, to be used in switch statements.
|
---|
574 | * @see paramType
|
---|
575 | */
|
---|
576 | private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
|
---|
577 |
|
---|
578 | /**
|
---|
579 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
580 | * @param s A parameter string
|
---|
581 | * @return The guessed parameter type
|
---|
582 | */
|
---|
583 | private DownloadParamType paramType(String s) {
|
---|
584 | if(s.startsWith("http:")) return DownloadParamType.httpUrl;
|
---|
585 | if(s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
586 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
587 | // we assume a string with exactly 3 commas is a bounds parameter
|
---|
588 | if (st.countTokens() == 4) return DownloadParamType.bounds;
|
---|
589 | // everything else must be a file name
|
---|
590 | return DownloadParamType.fileName;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Download area specified on the command line as OSM URL.
|
---|
595 | * @param rawGps Flag to download raw GPS tracks
|
---|
596 | * @param s The URL parameter
|
---|
597 | */
|
---|
598 | private static void downloadFromParamHttp(final boolean rawGps, String s) {
|
---|
599 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
600 | if (b == null) {
|
---|
601 | JOptionPane.showMessageDialog(
|
---|
602 | Main.parent,
|
---|
603 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
604 | tr("Warning"),
|
---|
605 | JOptionPane.WARNING_MESSAGE
|
---|
606 | );
|
---|
607 | } else {
|
---|
608 | downloadFromParamBounds(rawGps, b);
|
---|
609 | }
|
---|
610 | }
|
---|
611 |
|
---|
612 | /**
|
---|
613 | * Download area specified on the command line as bounds string.
|
---|
614 | * @param rawGps Flag to download raw GPS tracks
|
---|
615 | * @param s The bounds parameter
|
---|
616 | */
|
---|
617 | private static void downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
618 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
619 | if (st.countTokens() == 4) {
|
---|
620 | Bounds b = new Bounds(
|
---|
621 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken())),
|
---|
622 | new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()))
|
---|
623 | );
|
---|
624 | downloadFromParamBounds(rawGps, b);
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Download area specified as Bounds value.
|
---|
630 | * @param rawGps Flag to download raw GPS tracks
|
---|
631 | * @param b The bounds value
|
---|
632 | * @see downloadFromParamBounds(final boolean rawGps, String s)
|
---|
633 | * @see downloadFromParamHttp
|
---|
634 | */
|
---|
635 | private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
|
---|
636 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
637 | // asynchronously launch the download task ...
|
---|
638 | Future<?> future = task.download(true, b, null);
|
---|
639 | // ... and the continuation when the download is finished (this will wait for the download to finish)
|
---|
640 | Main.worker.execute(new PostDownloadHandler(task, future));
|
---|
641 | }
|
---|
642 |
|
---|
643 | public static void determinePlatformHook() {
|
---|
644 | String os = System.getProperty("os.name");
|
---|
645 | if (os == null) {
|
---|
646 | System.err.println("Your operating system has no name, so I'm guessing its some kind of *nix.");
|
---|
647 | platform = new PlatformHookUnixoid();
|
---|
648 | } else if (os.toLowerCase().startsWith("windows")) {
|
---|
649 | platform = new PlatformHookWindows();
|
---|
650 | } else if (os.equals("Linux") || os.equals("Solaris") ||
|
---|
651 | os.equals("SunOS") || os.equals("AIX") ||
|
---|
652 | os.equals("FreeBSD") || os.equals("NetBSD") || os.equals("OpenBSD")) {
|
---|
653 | platform = new PlatformHookUnixoid();
|
---|
654 | } else if (os.toLowerCase().startsWith("mac os x")) {
|
---|
655 | platform = new PlatformHookOsx();
|
---|
656 | } else {
|
---|
657 | System.err.println("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
|
---|
658 | platform = new PlatformHookUnixoid();
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | static public void saveGuiGeometry() {
|
---|
663 | // save the current window geometry and the width of the toggle dialog area
|
---|
664 | String newGeometry = "";
|
---|
665 | String newToggleDlgWidth = null;
|
---|
666 | try {
|
---|
667 | Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
668 | int width = (int)bounds.getWidth();
|
---|
669 | int height = (int)bounds.getHeight();
|
---|
670 | int x = (int)bounds.getX();
|
---|
671 | int y = (int)bounds.getY();
|
---|
672 | if (width > screenDimension.width) {
|
---|
673 | width = screenDimension.width;
|
---|
674 | }
|
---|
675 | if (height > screenDimension.height) {
|
---|
676 | width = screenDimension.height;
|
---|
677 | }
|
---|
678 | if (x < 0) {
|
---|
679 | x = 0;
|
---|
680 | }
|
---|
681 | if (y < 0) {
|
---|
682 | y = 0;
|
---|
683 | }
|
---|
684 | newGeometry = width + "x" + height + "+" + x + "+" + y;
|
---|
685 |
|
---|
686 | if (map != null) {
|
---|
687 | newToggleDlgWidth = Integer.toString(map.getToggleDlgWidth());
|
---|
688 | if (newToggleDlgWidth.equals(Integer.toString(MapFrame.DEF_TOGGLE_DLG_WIDTH))) {
|
---|
689 | newToggleDlgWidth = "";
|
---|
690 | }
|
---|
691 | }
|
---|
692 | }
|
---|
693 | catch (Exception e) {
|
---|
694 | System.out.println("Failed to get GUI geometry: " + e);
|
---|
695 | e.printStackTrace();
|
---|
696 | }
|
---|
697 | boolean maximized = (windowState & JFrame.MAXIMIZED_BOTH) != 0;
|
---|
698 | // Main.debug("Main window: saving geometry \"" + newGeometry + "\" " + (maximized?"maximized":"normal"));
|
---|
699 | pref.put("gui.maximized", maximized);
|
---|
700 | pref.put("gui.geometry", newGeometry);
|
---|
701 | if (newToggleDlgWidth != null) {
|
---|
702 | pref.put("toggleDialogs.width", newToggleDlgWidth);
|
---|
703 | }
|
---|
704 | }
|
---|
705 | private static class WindowPositionSizeListener extends WindowAdapter implements
|
---|
706 | ComponentListener {
|
---|
707 |
|
---|
708 | @Override
|
---|
709 | public void windowStateChanged(WindowEvent e) {
|
---|
710 | Main.windowState = e.getNewState();
|
---|
711 | // Main.debug("Main window state changed to " + Main.windowState);
|
---|
712 | }
|
---|
713 |
|
---|
714 | public void componentHidden(ComponentEvent e) {
|
---|
715 | }
|
---|
716 |
|
---|
717 | public void componentMoved(ComponentEvent e) {
|
---|
718 | handleComponentEvent(e);
|
---|
719 | }
|
---|
720 |
|
---|
721 | public void componentResized(ComponentEvent e) {
|
---|
722 | handleComponentEvent(e);
|
---|
723 | }
|
---|
724 |
|
---|
725 | public void componentShown(ComponentEvent e) {
|
---|
726 | }
|
---|
727 |
|
---|
728 | private void handleComponentEvent(ComponentEvent e) {
|
---|
729 | Component c = e.getComponent();
|
---|
730 | if (c instanceof JFrame) {
|
---|
731 | if (Main.windowState == JFrame.NORMAL) {
|
---|
732 | Main.bounds = ((JFrame) c).getBounds();
|
---|
733 | // Main.debug("Main window: new geometry " + Main.bounds);
|
---|
734 | } else {
|
---|
735 | // Main.debug("Main window state is " + Main.windowState);
|
---|
736 | }
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | }
|
---|
741 | public static void addListener() {
|
---|
742 | parent.addComponentListener(new WindowPositionSizeListener());
|
---|
743 | ((JFrame)parent).addWindowStateListener(new WindowPositionSizeListener());
|
---|
744 | }
|
---|
745 |
|
---|
746 | public static void checkJava6() {
|
---|
747 | String version = System.getProperty("java.version");
|
---|
748 | if (version != null) {
|
---|
749 | if (version.startsWith("1.6") || version.startsWith("6") ||
|
---|
750 | version.startsWith("1.7") || version.startsWith("7"))
|
---|
751 | return;
|
---|
752 | if (version.startsWith("1.5") || version.startsWith("5")) {
|
---|
753 | JLabel ho = new JLabel("<html>"+
|
---|
754 | tr("<h2>JOSM requires Java version 6.</h2>"+
|
---|
755 | "Detected Java version: {0}.<br>"+
|
---|
756 | "You can <ul><li>update your Java (JRE) or</li>"+
|
---|
757 | "<li>use an earlier (Java 5 compatible) version of JOSM.</li></ul>"+
|
---|
758 | "More Info:", version)+"</html>");
|
---|
759 | JTextArea link = new JTextArea("http://josm.openstreetmap.de/wiki/Help/SystemRequirements");
|
---|
760 | link.setEditable(false);
|
---|
761 | link.setBackground(panel.getBackground());
|
---|
762 | JPanel panel = new JPanel(new GridBagLayout());
|
---|
763 | GridBagConstraints gbc = new GridBagConstraints();
|
---|
764 | gbc.gridwidth = GridBagConstraints.REMAINDER;
|
---|
765 | gbc.anchor = GridBagConstraints.WEST;
|
---|
766 | gbc.weightx = 1.0;
|
---|
767 | panel.add(ho, gbc);
|
---|
768 | panel.add(link, gbc);
|
---|
769 | final String EXIT = tr("Exit JOSM");
|
---|
770 | final String CONTINUE = tr("Continue, try anyway");
|
---|
771 | int ret = JOptionPane.showOptionDialog(null, panel, tr("Error"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, new String[] {EXIT, CONTINUE}, EXIT);
|
---|
772 | if (ret == 0) {
|
---|
773 | System.exit(0);
|
---|
774 | }
|
---|
775 | return;
|
---|
776 | }
|
---|
777 | }
|
---|
778 | System.err.println("Error: Could not recognize Java Version: "+version);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /* ----------------------------------------------------------------------------------------- */
|
---|
782 | /* projection handling - Main is a registry for a single, global projection instance */
|
---|
783 | /* */
|
---|
784 | /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
|
---|
785 | /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
|
---|
786 | /* ----------------------------------------------------------------------------------------- */
|
---|
787 | /**
|
---|
788 | * The projection method used.
|
---|
789 | * @deprecated use {@link #getProjection()} and {@link #setProjection(Projection)} instead.
|
---|
790 | * For the time being still publicly available, but avoid/migrate write access to it. Use
|
---|
791 | * {@link #setProjection(Projection)} in order to trigger a projection change event.
|
---|
792 | */
|
---|
793 | @Deprecated
|
---|
794 | public static Projection proj;
|
---|
795 |
|
---|
796 | /**
|
---|
797 | * Replies the current projection.
|
---|
798 | *
|
---|
799 | * @return
|
---|
800 | */
|
---|
801 | public static Projection getProjection() {
|
---|
802 | return proj;
|
---|
803 | }
|
---|
804 |
|
---|
805 | /**
|
---|
806 | * Sets the current projection
|
---|
807 | *
|
---|
808 | * @param p the projection
|
---|
809 | */
|
---|
810 | public static void setProjection(Projection p) {
|
---|
811 | CheckParameterUtil.ensureParameterNotNull(p);
|
---|
812 | Projection oldValue = proj;
|
---|
813 | proj = p;
|
---|
814 | fireProjectionChanged(oldValue, proj);
|
---|
815 | }
|
---|
816 |
|
---|
817 | /*
|
---|
818 | * Keep WeakReferences to the listeners. This relieves clients from the burden of
|
---|
819 | * explicitly removing the listeners and allows us to transparently register every
|
---|
820 | * created dataset as projection change listener.
|
---|
821 | */
|
---|
822 | private static final ArrayList<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<WeakReference<ProjectionChangeListener>>();
|
---|
823 |
|
---|
824 | private static void fireProjectionChanged(Projection oldValue, Projection newValue) {
|
---|
825 | if (newValue == null ^ oldValue == null
|
---|
826 | || (newValue != null && oldValue != null && !Utils.equal(newValue.toCode(), oldValue.toCode()))) {
|
---|
827 |
|
---|
828 | synchronized(Main.class) {
|
---|
829 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
830 | while(it.hasNext()){
|
---|
831 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
832 | if (wr.get() == null) {
|
---|
833 | it.remove();
|
---|
834 | continue;
|
---|
835 | }
|
---|
836 | wr.get().projectionChanged(oldValue, newValue);
|
---|
837 | }
|
---|
838 | }
|
---|
839 | if (newValue != null) {
|
---|
840 | Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
|
---|
841 | if (b != null){
|
---|
842 | Main.map.mapView.zoomTo(b);
|
---|
843 | }
|
---|
844 | }
|
---|
845 | /* TODO - remove layers with fixed projection */
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | /**
|
---|
850 | * Register a projection change listener
|
---|
851 | *
|
---|
852 | * @param listener the listener. Ignored if null.
|
---|
853 | */
|
---|
854 | public static void addProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
855 | if (listener == null) return;
|
---|
856 | synchronized (Main.class) {
|
---|
857 | for (WeakReference<ProjectionChangeListener> wr : listeners) {
|
---|
858 | // already registered ? => abort
|
---|
859 | if (wr.get() == listener) return;
|
---|
860 | }
|
---|
861 | }
|
---|
862 | listeners.add(new WeakReference<ProjectionChangeListener>(listener));
|
---|
863 | }
|
---|
864 |
|
---|
865 | /**
|
---|
866 | * Removes a projection change listener
|
---|
867 | *
|
---|
868 | * @param listener the listener. Ignored if null.
|
---|
869 | */
|
---|
870 | public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
871 | if (listener == null) return;
|
---|
872 | synchronized(Main.class){
|
---|
873 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
874 | while(it.hasNext()){
|
---|
875 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
876 | // remove the listener - and any other listener which god garbage
|
---|
877 | // collected in the meantime
|
---|
878 | if (wr.get() == null || wr.get() == listener) {
|
---|
879 | it.remove();
|
---|
880 | }
|
---|
881 | }
|
---|
882 | }
|
---|
883 | }
|
---|
884 | }
|
---|