1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.GraphicsEnvironment;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.lang.ref.WeakReference;
|
---|
13 | import java.net.URI;
|
---|
14 | import java.net.URISyntaxException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.text.MessageFormat;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Arrays;
|
---|
19 | import java.util.Collection;
|
---|
20 | import java.util.EnumSet;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.Iterator;
|
---|
23 | import java.util.List;
|
---|
24 | import java.util.Locale;
|
---|
25 | import java.util.Map;
|
---|
26 | import java.util.Objects;
|
---|
27 | import java.util.Set;
|
---|
28 | import java.util.StringTokenizer;
|
---|
29 | import java.util.concurrent.Callable;
|
---|
30 | import java.util.concurrent.ExecutionException;
|
---|
31 | import java.util.concurrent.ExecutorService;
|
---|
32 | import java.util.concurrent.Executors;
|
---|
33 | import java.util.concurrent.Future;
|
---|
34 |
|
---|
35 | import javax.swing.Action;
|
---|
36 | import javax.swing.InputMap;
|
---|
37 | import javax.swing.JComponent;
|
---|
38 | import javax.swing.JOptionPane;
|
---|
39 | import javax.swing.JPanel;
|
---|
40 | import javax.swing.KeyStroke;
|
---|
41 | import javax.swing.LookAndFeel;
|
---|
42 | import javax.swing.UIManager;
|
---|
43 | import javax.swing.UnsupportedLookAndFeelException;
|
---|
44 |
|
---|
45 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
46 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
47 | import org.openstreetmap.josm.actions.OpenFileAction;
|
---|
48 | import org.openstreetmap.josm.actions.OpenLocationAction;
|
---|
49 | import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
|
---|
50 | import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
|
---|
51 | import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
|
---|
52 | import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
|
---|
53 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
---|
54 | import org.openstreetmap.josm.actions.search.SearchAction;
|
---|
55 | import org.openstreetmap.josm.data.Bounds;
|
---|
56 | import org.openstreetmap.josm.data.Preferences;
|
---|
57 | import org.openstreetmap.josm.data.ProjectionBounds;
|
---|
58 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
59 | import org.openstreetmap.josm.data.ViewportData;
|
---|
60 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
61 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
62 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
63 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
64 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
65 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
66 | import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
|
---|
67 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
68 | import org.openstreetmap.josm.gui.MainFrame;
|
---|
69 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
70 | import org.openstreetmap.josm.gui.MainPanel;
|
---|
71 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
72 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
73 | import org.openstreetmap.josm.gui.ProgramArguments;
|
---|
74 | import org.openstreetmap.josm.gui.ProgramArguments.Option;
|
---|
75 | import org.openstreetmap.josm.gui.io.SaveLayersDialog;
|
---|
76 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
77 | import org.openstreetmap.josm.gui.layer.MainLayerManager;
|
---|
78 | import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
|
---|
79 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
80 | import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
|
---|
81 | import org.openstreetmap.josm.gui.preferences.display.LafPreference;
|
---|
82 | import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
|
---|
83 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
84 | import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
|
---|
85 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
86 | import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
|
---|
87 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
88 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
89 | import org.openstreetmap.josm.gui.util.RedirectInputMap;
|
---|
90 | import org.openstreetmap.josm.io.FileWatcher;
|
---|
91 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
92 | import org.openstreetmap.josm.io.OsmApi;
|
---|
93 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
94 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
95 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
96 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
97 | import org.openstreetmap.josm.tools.I18n;
|
---|
98 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
99 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
100 | import org.openstreetmap.josm.tools.Logging;
|
---|
101 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
102 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
103 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
|
---|
104 | import org.openstreetmap.josm.tools.PlatformHook;
|
---|
105 | import org.openstreetmap.josm.tools.PlatformHookOsx;
|
---|
106 | import org.openstreetmap.josm.tools.PlatformHookUnixoid;
|
---|
107 | import org.openstreetmap.josm.tools.PlatformHookWindows;
|
---|
108 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
109 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
110 | import org.openstreetmap.josm.tools.Territories;
|
---|
111 | import org.openstreetmap.josm.tools.Utils;
|
---|
112 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Abstract class holding various static global variables and methods used in large parts of JOSM application.
|
---|
116 | * @since 98
|
---|
117 | */
|
---|
118 | public abstract class Main {
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The JOSM website URL.
|
---|
122 | * @since 6897 (was public from 6143 to 6896)
|
---|
123 | */
|
---|
124 | private static final String JOSM_WEBSITE = "https://josm.openstreetmap.de";
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * The OSM website URL.
|
---|
128 | * @since 6897 (was public from 6453 to 6896)
|
---|
129 | */
|
---|
130 | private static final String OSM_WEBSITE = "https://www.openstreetmap.org";
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
|
---|
134 | * it only shows the MOTD panel.
|
---|
135 | * <p>
|
---|
136 | * You do not need this when accessing the layer manager. The layer manager will be empty if no map view is shown.
|
---|
137 | *
|
---|
138 | * @return <code>true</code> if JOSM currently displays a map view
|
---|
139 | */
|
---|
140 | public static boolean isDisplayingMapView() {
|
---|
141 | return map != null && map.mapView != null;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Global parent component for all dialogs and message boxes
|
---|
146 | */
|
---|
147 | public static Component parent;
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Global application.
|
---|
151 | */
|
---|
152 | public static volatile Main main;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * The worker thread slave. This is for executing all long and intensive
|
---|
156 | * calculations. The executed runnables are guaranteed to be executed separately
|
---|
157 | * and sequential.
|
---|
158 | */
|
---|
159 | public static final ExecutorService worker = new ProgressMonitorExecutor("main-worker-%d", Thread.NORM_PRIORITY);
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Global application preferences
|
---|
163 | */
|
---|
164 | public static final Preferences pref = new Preferences();
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * The MapFrame.
|
---|
168 | * <p>
|
---|
169 | * There should be no need to access this to access any map data. Use {@link #layerManager} instead.
|
---|
170 | *
|
---|
171 | * @see MainPanel
|
---|
172 | */
|
---|
173 | public static MapFrame map;
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Provides access to the layers displayed in the main view.
|
---|
177 | * @since 10271
|
---|
178 | */
|
---|
179 | private static final MainLayerManager layerManager = new MainLayerManager();
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * The toolbar preference control to register new actions.
|
---|
183 | */
|
---|
184 | public static volatile ToolbarPreferences toolbar;
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * The commands undo/redo handler.
|
---|
188 | */
|
---|
189 | public final UndoRedoHandler undoRedo = new UndoRedoHandler();
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * The progress monitor being currently displayed.
|
---|
193 | */
|
---|
194 | public static PleaseWaitProgressMonitor currentProgressMonitor;
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * The main menu bar at top of screen.
|
---|
198 | */
|
---|
199 | public MainMenu menu;
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * The file watcher service.
|
---|
203 | */
|
---|
204 | public static final FileWatcher fileWatcher = new FileWatcher();
|
---|
205 |
|
---|
206 | private static final Map<String, Throwable> NETWORK_ERRORS = new HashMap<>();
|
---|
207 |
|
---|
208 | private static final Set<OnlineResource> OFFLINE_RESOURCES = EnumSet.noneOf(OnlineResource.class);
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Logging level (5 = trace, 4 = debug, 3 = info, 2 = warn, 1 = error, 0 = none).
|
---|
212 | * @since 6248
|
---|
213 | * @deprecated Use {@link Logging} class.
|
---|
214 | */
|
---|
215 | @Deprecated
|
---|
216 | public static int logLevel = 3;
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * The real main panel. This field may be removed any time and made private to {@link MainFrame}
|
---|
220 | * @see #panel
|
---|
221 | */
|
---|
222 | protected static final MainPanel mainPanel = new MainPanel(getLayerManager());
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Replies the first lines of last 5 error and warning messages, used for bug reports
|
---|
226 | * @return the first lines of last 5 error and warning messages
|
---|
227 | * @since 7420
|
---|
228 | */
|
---|
229 | public static final Collection<String> getLastErrorAndWarnings() {
|
---|
230 | return Logging.getLastErrorAndWarnings();
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Clears the list of last error and warning messages.
|
---|
235 | * @since 8959
|
---|
236 | */
|
---|
237 | public static void clearLastErrorAndWarnings() {
|
---|
238 | Logging.clearLastErrorAndWarnings();
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * Prints an error message if logging is on.
|
---|
243 | * @param msg The message to print.
|
---|
244 | * @since 6248
|
---|
245 | */
|
---|
246 | public static void error(String msg) {
|
---|
247 | Logging.error(msg);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Prints a warning message if logging is on.
|
---|
252 | * @param msg The message to print.
|
---|
253 | */
|
---|
254 | public static void warn(String msg) {
|
---|
255 | Logging.warn(msg);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Prints an informational message if logging is on.
|
---|
260 | * @param msg The message to print.
|
---|
261 | */
|
---|
262 | public static void info(String msg) {
|
---|
263 | Logging.info(msg);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Prints a debug message if logging is on.
|
---|
268 | * @param msg The message to print.
|
---|
269 | */
|
---|
270 | public static void debug(String msg) {
|
---|
271 | Logging.debug(msg);
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Prints a trace message if logging is on.
|
---|
276 | * @param msg The message to print.
|
---|
277 | */
|
---|
278 | public static void trace(String msg) {
|
---|
279 | Logging.trace(msg);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /**
|
---|
283 | * Determines if debug log level is enabled.
|
---|
284 | * Useful to avoid costly construction of debug messages when not enabled.
|
---|
285 | * @return {@code true} if log level is at least debug, {@code false} otherwise
|
---|
286 | * @since 6852
|
---|
287 | */
|
---|
288 | public static boolean isDebugEnabled() {
|
---|
289 | return Logging.isLoggingEnabled(Logging.LEVEL_DEBUG);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * Determines if trace log level is enabled.
|
---|
294 | * Useful to avoid costly construction of trace messages when not enabled.
|
---|
295 | * @return {@code true} if log level is at least trace, {@code false} otherwise
|
---|
296 | * @since 6852
|
---|
297 | */
|
---|
298 | public static boolean isTraceEnabled() {
|
---|
299 | return Logging.isLoggingEnabled(Logging.LEVEL_TRACE);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Prints a formatted error message if logging is on. Calls {@link MessageFormat#format}
|
---|
304 | * function to format text.
|
---|
305 | * @param msg The formatted message to print.
|
---|
306 | * @param objects The objects to insert into format string.
|
---|
307 | * @since 6248
|
---|
308 | */
|
---|
309 | public static void error(String msg, Object... objects) {
|
---|
310 | Logging.error(msg, objects);
|
---|
311 | }
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Prints a formatted warning message if logging is on. Calls {@link MessageFormat#format}
|
---|
315 | * function to format text.
|
---|
316 | * @param msg The formatted message to print.
|
---|
317 | * @param objects The objects to insert into format string.
|
---|
318 | */
|
---|
319 | public static void warn(String msg, Object... objects) {
|
---|
320 | Logging.warn(msg, objects);
|
---|
321 | }
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Prints a formatted informational message if logging is on. Calls {@link MessageFormat#format}
|
---|
325 | * function to format text.
|
---|
326 | * @param msg The formatted message to print.
|
---|
327 | * @param objects The objects to insert into format string.
|
---|
328 | */
|
---|
329 | public static void info(String msg, Object... objects) {
|
---|
330 | Logging.info(msg, objects);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Prints a formatted debug message if logging is on. Calls {@link MessageFormat#format}
|
---|
335 | * function to format text.
|
---|
336 | * @param msg The formatted message to print.
|
---|
337 | * @param objects The objects to insert into format string.
|
---|
338 | */
|
---|
339 | public static void debug(String msg, Object... objects) {
|
---|
340 | Logging.debug(msg, objects);
|
---|
341 | }
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * Prints a formatted trace message if logging is on. Calls {@link MessageFormat#format}
|
---|
345 | * function to format text.
|
---|
346 | * @param msg The formatted message to print.
|
---|
347 | * @param objects The objects to insert into format string.
|
---|
348 | */
|
---|
349 | public static void trace(String msg, Object... objects) {
|
---|
350 | Logging.trace(msg, objects);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Prints an error message for the given Throwable.
|
---|
355 | * @param t The throwable object causing the error
|
---|
356 | * @since 6248
|
---|
357 | */
|
---|
358 | public static void error(Throwable t) {
|
---|
359 | Logging.logWithStackTrace(Logging.LEVEL_ERROR, t);
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Prints a warning message for the given Throwable.
|
---|
364 | * @param t The throwable object causing the error
|
---|
365 | * @since 6248
|
---|
366 | */
|
---|
367 | public static void warn(Throwable t) {
|
---|
368 | Logging.logWithStackTrace(Logging.LEVEL_WARN, t);
|
---|
369 | }
|
---|
370 |
|
---|
371 | /**
|
---|
372 | * Prints a debug message for the given Throwable. Useful for exceptions usually ignored
|
---|
373 | * @param t The throwable object causing the error
|
---|
374 | * @since 10420
|
---|
375 | */
|
---|
376 | public static void debug(Throwable t) {
|
---|
377 | Logging.log(Logging.LEVEL_DEBUG, t);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Prints a trace message for the given Throwable. Useful for exceptions usually ignored
|
---|
382 | * @param t The throwable object causing the error
|
---|
383 | * @since 10420
|
---|
384 | */
|
---|
385 | public static void trace(Throwable t) {
|
---|
386 | Logging.log(Logging.LEVEL_TRACE, t);
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Prints an error message for the given Throwable.
|
---|
391 | * @param t The throwable object causing the error
|
---|
392 | * @param stackTrace {@code true}, if the stacktrace should be displayed
|
---|
393 | * @since 6642
|
---|
394 | */
|
---|
395 | public static void error(Throwable t, boolean stackTrace) {
|
---|
396 | if (stackTrace) {
|
---|
397 | Logging.log(Logging.LEVEL_ERROR, t);
|
---|
398 | } else {
|
---|
399 | Logging.logWithStackTrace(Logging.LEVEL_ERROR, t);
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Prints an error message for the given Throwable.
|
---|
405 | * @param t The throwable object causing the error
|
---|
406 | * @param message additional error message
|
---|
407 | * @since 10420
|
---|
408 | */
|
---|
409 | public static void error(Throwable t, String message) {
|
---|
410 | Logging.log(Logging.LEVEL_ERROR, message, t);
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * Prints a warning message for the given Throwable.
|
---|
415 | * @param t The throwable object causing the error
|
---|
416 | * @param stackTrace {@code true}, if the stacktrace should be displayed
|
---|
417 | * @since 6642
|
---|
418 | */
|
---|
419 | public static void warn(Throwable t, boolean stackTrace) {
|
---|
420 | if (stackTrace) {
|
---|
421 | Logging.log(Logging.LEVEL_WARN, t);
|
---|
422 | } else {
|
---|
423 | Logging.logWithStackTrace(Logging.LEVEL_WARN, t);
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Prints a warning message for the given Throwable.
|
---|
429 | * @param t The throwable object causing the error
|
---|
430 | * @param message additional error message
|
---|
431 | * @since 10420
|
---|
432 | */
|
---|
433 | public static void warn(Throwable t, String message) {
|
---|
434 | Logging.log(Logging.LEVEL_WARN, message, t);
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Returns a human-readable message of error, also usable for developers.
|
---|
439 | * @param t The error
|
---|
440 | * @return The human-readable error message
|
---|
441 | * @since 6642
|
---|
442 | */
|
---|
443 | public static String getErrorMessage(Throwable t) {
|
---|
444 | if (t == null) {
|
---|
445 | return null;
|
---|
446 | } else {
|
---|
447 | return Logging.getErrorMessage(t);
|
---|
448 | }
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Platform specific code goes in here.
|
---|
453 | * Plugins may replace it, however, some hooks will be called before any plugins have been loeaded.
|
---|
454 | * So if you need to hook into those early ones, split your class and send the one with the early hooks
|
---|
455 | * to the JOSM team for inclusion.
|
---|
456 | */
|
---|
457 | public static volatile PlatformHook platform;
|
---|
458 |
|
---|
459 | private static volatile InitStatusListener initListener;
|
---|
460 |
|
---|
461 | /**
|
---|
462 | * Initialization task listener.
|
---|
463 | */
|
---|
464 | public interface InitStatusListener {
|
---|
465 |
|
---|
466 | /**
|
---|
467 | * Called when an initialization task updates its status.
|
---|
468 | * @param event task name
|
---|
469 | * @return new status
|
---|
470 | */
|
---|
471 | Object updateStatus(String event);
|
---|
472 |
|
---|
473 | /**
|
---|
474 | * Called when an initialization task completes.
|
---|
475 | * @param status final status
|
---|
476 | */
|
---|
477 | void finish(Object status);
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Sets initialization task listener.
|
---|
482 | * @param listener initialization task listener
|
---|
483 | */
|
---|
484 | public static void setInitStatusListener(InitStatusListener listener) {
|
---|
485 | CheckParameterUtil.ensureParameterNotNull(listener);
|
---|
486 | initListener = listener;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /**
|
---|
490 | * Constructs new {@code Main} object.
|
---|
491 | * @see #initialize()
|
---|
492 | */
|
---|
493 | protected Main() {
|
---|
494 | setInstance(this);
|
---|
495 | mainPanel.addMapFrameListener((o, n) -> redoUndoListener.commandChanged(0, 0));
|
---|
496 | }
|
---|
497 |
|
---|
498 | private static void setInstance(Main instance) {
|
---|
499 | main = instance;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Initializes the main object. A lot of global variables are initialized here.
|
---|
504 | * @since 10340
|
---|
505 | */
|
---|
506 | public void initialize() {
|
---|
507 | fileWatcher.start();
|
---|
508 |
|
---|
509 | new InitializationTask(tr("Executing platform startup hook"), platform::startupHook).call();
|
---|
510 |
|
---|
511 | new InitializationTask(tr("Building main menu"), this::initializeMainWindow).call();
|
---|
512 |
|
---|
513 | undoRedo.addCommandQueueListener(redoUndoListener);
|
---|
514 |
|
---|
515 | // creating toolbar
|
---|
516 | GuiHelper.runInEDTAndWait(() -> contentPanePrivate.add(toolbar.control, BorderLayout.NORTH));
|
---|
517 |
|
---|
518 | registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
|
---|
519 | KeyEvent.VK_F1, Shortcut.DIRECT));
|
---|
520 |
|
---|
521 | // This needs to be done before RightAndLefthandTraffic::initialize is called
|
---|
522 | try {
|
---|
523 | new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize).call();
|
---|
524 | } catch (JosmRuntimeException e) {
|
---|
525 | // Can happen if the current projection needs NTV2 grid which is not available
|
---|
526 | // In this case we want the user be able to change his projection
|
---|
527 | BugReport.intercept(e).warn();
|
---|
528 | }
|
---|
529 |
|
---|
530 | // contains several initialization tasks to be executed (in parallel) by a ExecutorService
|
---|
531 | List<Callable<Void>> tasks = new ArrayList<>();
|
---|
532 |
|
---|
533 | tasks.add(new InitializationTask(tr("Initializing OSM API"), () -> {
|
---|
534 | // We try to establish an API connection early, so that any API
|
---|
535 | // capabilities are already known to the editor instance. However
|
---|
536 | // if it goes wrong that's not critical at this stage.
|
---|
537 | try {
|
---|
538 | OsmApi.getOsmApi().initialize(null, true);
|
---|
539 | } catch (OsmTransferCanceledException | OsmApiInitializationException e) {
|
---|
540 | Main.warn(getErrorMessage(Utils.getRootCause(e)));
|
---|
541 | }
|
---|
542 | }));
|
---|
543 |
|
---|
544 | tasks.add(new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize));
|
---|
545 |
|
---|
546 | tasks.add(new InitializationTask(tr("Initializing validator"), OsmValidator::initialize));
|
---|
547 |
|
---|
548 | tasks.add(new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize));
|
---|
549 |
|
---|
550 | tasks.add(new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize));
|
---|
551 |
|
---|
552 | tasks.add(new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize));
|
---|
553 |
|
---|
554 | try {
|
---|
555 | ExecutorService service = Executors.newFixedThreadPool(
|
---|
556 | Runtime.getRuntime().availableProcessors(), Utils.newThreadFactory("main-init-%d", Thread.NORM_PRIORITY));
|
---|
557 | for (Future<Void> i : service.invokeAll(tasks)) {
|
---|
558 | i.get();
|
---|
559 | }
|
---|
560 | // asynchronous initializations to be completed eventually
|
---|
561 | service.submit((Runnable) TMSLayer::getCache);
|
---|
562 | service.submit((Runnable) OsmValidator::initializeTests);
|
---|
563 | service.submit(OverpassTurboQueryWizard::getInstance);
|
---|
564 | service.shutdown();
|
---|
565 | } catch (InterruptedException | ExecutionException ex) {
|
---|
566 | throw new JosmRuntimeException(ex);
|
---|
567 | }
|
---|
568 |
|
---|
569 | // hooks for the jmapviewer component
|
---|
570 | FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl);
|
---|
571 | FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter());
|
---|
572 | FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger());
|
---|
573 |
|
---|
574 | new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> {
|
---|
575 | toolbar.refreshToolbarControl();
|
---|
576 | toolbar.control.updateUI();
|
---|
577 | contentPanePrivate.updateUI();
|
---|
578 | })).call();
|
---|
579 | }
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Called once at startup to initialize the main window content.
|
---|
583 | * Should set {@link #menu}
|
---|
584 | */
|
---|
585 | protected abstract void initializeMainWindow();
|
---|
586 |
|
---|
587 | private static class InitializationTask implements Callable<Void> {
|
---|
588 |
|
---|
589 | private final String name;
|
---|
590 | private final Runnable task;
|
---|
591 |
|
---|
592 | protected InitializationTask(String name, Runnable task) {
|
---|
593 | this.name = name;
|
---|
594 | this.task = task;
|
---|
595 | }
|
---|
596 |
|
---|
597 | @Override
|
---|
598 | public Void call() {
|
---|
599 | Object status = null;
|
---|
600 | if (initListener != null) {
|
---|
601 | status = initListener.updateStatus(name);
|
---|
602 | }
|
---|
603 | task.run();
|
---|
604 | if (initListener != null) {
|
---|
605 | initListener.finish(status);
|
---|
606 | }
|
---|
607 | return null;
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | /**
|
---|
612 | * Returns the main layer manager that is used by the map view.
|
---|
613 | * @return The layer manager. The value returned will never change.
|
---|
614 | * @since 10279
|
---|
615 | */
|
---|
616 | public static MainLayerManager getLayerManager() {
|
---|
617 | return layerManager;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /**
|
---|
621 | * Add a new layer to the map.
|
---|
622 | *
|
---|
623 | * If no map exists, create one.
|
---|
624 | *
|
---|
625 | * @param layer the layer
|
---|
626 | * @param bounds the bounds of the layer (target zoom area); can be null, then
|
---|
627 | * the viewport isn't changed
|
---|
628 | */
|
---|
629 | public final void addLayer(Layer layer, ProjectionBounds bounds) {
|
---|
630 | addLayer(layer, bounds == null ? null : new ViewportData(bounds));
|
---|
631 | }
|
---|
632 |
|
---|
633 | /**
|
---|
634 | * Add a new layer to the map.
|
---|
635 | *
|
---|
636 | * If no map exists, create one.
|
---|
637 | *
|
---|
638 | * @param layer the layer
|
---|
639 | * @param viewport the viewport to zoom to; can be null, then the viewport isn't changed
|
---|
640 | */
|
---|
641 | public final void addLayer(Layer layer, ViewportData viewport) {
|
---|
642 | getLayerManager().addLayer(layer);
|
---|
643 | if (viewport != null && Main.map.mapView != null) {
|
---|
644 | // MapView may be null in headless mode here.
|
---|
645 | Main.map.mapView.scheduleZoomTo(viewport);
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Replies the current selected primitives, from a end-user point of view.
|
---|
651 | * It is not always technically the same collection of primitives than {@link DataSet#getSelected()}.
|
---|
652 | * Indeed, if the user is currently in drawing mode, only the way currently being drawn is returned,
|
---|
653 | * see {@link DrawAction#getInProgressSelection()}.
|
---|
654 | *
|
---|
655 | * @return The current selected primitives, from a end-user point of view. Can be {@code null}.
|
---|
656 | * @since 6546
|
---|
657 | */
|
---|
658 | public Collection<OsmPrimitive> getInProgressSelection() {
|
---|
659 | if (map != null && map.mapMode instanceof DrawAction) {
|
---|
660 | return ((DrawAction) map.mapMode).getInProgressSelection();
|
---|
661 | } else {
|
---|
662 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
663 | if (ds == null) return null;
|
---|
664 | return ds.getSelected();
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | protected static final JPanel contentPanePrivate = new JPanel(new BorderLayout());
|
---|
669 |
|
---|
670 | public static void redirectToMainContentPane(JComponent source) {
|
---|
671 | RedirectInputMap.redirect(source, contentPanePrivate);
|
---|
672 | }
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * Registers a {@code JosmAction} and its shortcut.
|
---|
676 | * @param action action defining its own shortcut
|
---|
677 | */
|
---|
678 | public static void registerActionShortcut(JosmAction action) {
|
---|
679 | registerActionShortcut(action, action.getShortcut());
|
---|
680 | }
|
---|
681 |
|
---|
682 | /**
|
---|
683 | * Registers an action and its shortcut.
|
---|
684 | * @param action action to register
|
---|
685 | * @param shortcut shortcut to associate to {@code action}
|
---|
686 | */
|
---|
687 | public static void registerActionShortcut(Action action, Shortcut shortcut) {
|
---|
688 | KeyStroke keyStroke = shortcut.getKeyStroke();
|
---|
689 | if (keyStroke == null)
|
---|
690 | return;
|
---|
691 |
|
---|
692 | InputMap inputMap = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
|
---|
693 | Object existing = inputMap.get(keyStroke);
|
---|
694 | if (existing != null && !existing.equals(action)) {
|
---|
695 | info(String.format("Keystroke %s is already assigned to %s, will be overridden by %s", keyStroke, existing, action));
|
---|
696 | }
|
---|
697 | inputMap.put(keyStroke, action);
|
---|
698 |
|
---|
699 | contentPanePrivate.getActionMap().put(action, action);
|
---|
700 | }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Unregisters a shortcut.
|
---|
704 | * @param shortcut shortcut to unregister
|
---|
705 | */
|
---|
706 | public static void unregisterShortcut(Shortcut shortcut) {
|
---|
707 | contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(shortcut.getKeyStroke());
|
---|
708 | }
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * Unregisters a {@code JosmAction} and its shortcut.
|
---|
712 | * @param action action to unregister
|
---|
713 | */
|
---|
714 | public static void unregisterActionShortcut(JosmAction action) {
|
---|
715 | unregisterActionShortcut(action, action.getShortcut());
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Unregisters an action and its shortcut.
|
---|
720 | * @param action action to unregister
|
---|
721 | * @param shortcut shortcut to unregister
|
---|
722 | */
|
---|
723 | public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
|
---|
724 | unregisterShortcut(shortcut);
|
---|
725 | contentPanePrivate.getActionMap().remove(action);
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Replies the registered action for the given shortcut
|
---|
730 | * @param shortcut The shortcut to look for
|
---|
731 | * @return the registered action for the given shortcut
|
---|
732 | * @since 5696
|
---|
733 | */
|
---|
734 | public static Action getRegisteredActionShortcut(Shortcut shortcut) {
|
---|
735 | KeyStroke keyStroke = shortcut.getKeyStroke();
|
---|
736 | if (keyStroke == null)
|
---|
737 | return null;
|
---|
738 | Object action = contentPanePrivate.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).get(keyStroke);
|
---|
739 | if (action instanceof Action)
|
---|
740 | return (Action) action;
|
---|
741 | return null;
|
---|
742 | }
|
---|
743 |
|
---|
744 | ///////////////////////////////////////////////////////////////////////////
|
---|
745 | // Implementation part
|
---|
746 | ///////////////////////////////////////////////////////////////////////////
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Global panel.
|
---|
750 | */
|
---|
751 | public static final JPanel panel = mainPanel;
|
---|
752 |
|
---|
753 | private final CommandQueueListener redoUndoListener = (queueSize, redoSize) -> {
|
---|
754 | menu.undo.setEnabled(queueSize > 0);
|
---|
755 | menu.redo.setEnabled(redoSize > 0);
|
---|
756 | };
|
---|
757 |
|
---|
758 | /**
|
---|
759 | * Should be called before the main constructor to setup some parameter stuff
|
---|
760 | */
|
---|
761 | public static void preConstructorInit() {
|
---|
762 | ProjectionPreference.setProjection();
|
---|
763 |
|
---|
764 | String defaultlaf = platform.getDefaultStyle();
|
---|
765 | String laf = LafPreference.LAF.get();
|
---|
766 | try {
|
---|
767 | UIManager.setLookAndFeel(laf);
|
---|
768 | } catch (final NoClassDefFoundError | ClassNotFoundException e) {
|
---|
769 | // Try to find look and feel in plugin classloaders
|
---|
770 | Main.trace(e);
|
---|
771 | Class<?> klass = null;
|
---|
772 | for (ClassLoader cl : PluginHandler.getResourceClassLoaders()) {
|
---|
773 | try {
|
---|
774 | klass = cl.loadClass(laf);
|
---|
775 | break;
|
---|
776 | } catch (ClassNotFoundException ex) {
|
---|
777 | Main.trace(ex);
|
---|
778 | }
|
---|
779 | }
|
---|
780 | if (klass != null && LookAndFeel.class.isAssignableFrom(klass)) {
|
---|
781 | try {
|
---|
782 | UIManager.setLookAndFeel((LookAndFeel) klass.getConstructor().newInstance());
|
---|
783 | } catch (ReflectiveOperationException ex) {
|
---|
784 | warn(ex, "Cannot set Look and Feel: " + laf + ": "+ex.getMessage());
|
---|
785 | } catch (UnsupportedLookAndFeelException ex) {
|
---|
786 | info("Look and Feel not supported: " + laf);
|
---|
787 | LafPreference.LAF.put(defaultlaf);
|
---|
788 | trace(ex);
|
---|
789 | }
|
---|
790 | } else {
|
---|
791 | info("Look and Feel not found: " + laf);
|
---|
792 | LafPreference.LAF.put(defaultlaf);
|
---|
793 | }
|
---|
794 | } catch (UnsupportedLookAndFeelException e) {
|
---|
795 | info("Look and Feel not supported: " + laf);
|
---|
796 | LafPreference.LAF.put(defaultlaf);
|
---|
797 | trace(e);
|
---|
798 | } catch (InstantiationException | IllegalAccessException e) {
|
---|
799 | error(e);
|
---|
800 | }
|
---|
801 | toolbar = new ToolbarPreferences();
|
---|
802 | contentPanePrivate.updateUI();
|
---|
803 | panel.updateUI();
|
---|
804 |
|
---|
805 | UIManager.put("OptionPane.okIcon", ImageProvider.get("ok"));
|
---|
806 | UIManager.put("OptionPane.yesIcon", UIManager.get("OptionPane.okIcon"));
|
---|
807 | UIManager.put("OptionPane.cancelIcon", ImageProvider.get("cancel"));
|
---|
808 | UIManager.put("OptionPane.noIcon", UIManager.get("OptionPane.cancelIcon"));
|
---|
809 | // Ensures caret color is the same than text foreground color, see #12257
|
---|
810 | // See http://docs.oracle.com/javase/8/docs/api/javax/swing/plaf/synth/doc-files/componentProperties.html
|
---|
811 | for (String p : Arrays.asList(
|
---|
812 | "EditorPane", "FormattedTextField", "PasswordField", "TextArea", "TextField", "TextPane")) {
|
---|
813 | UIManager.put(p+".caretForeground", UIManager.getColor(p+".foreground"));
|
---|
814 | }
|
---|
815 |
|
---|
816 | I18n.translateJavaInternalMessages();
|
---|
817 |
|
---|
818 | // init default coordinate format
|
---|
819 | //
|
---|
820 | try {
|
---|
821 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
|
---|
822 | } catch (IllegalArgumentException iae) {
|
---|
823 | Main.trace(iae);
|
---|
824 | CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | protected static void postConstructorProcessCmdLine(ProgramArguments args) {
|
---|
829 | List<File> fileList = new ArrayList<>();
|
---|
830 | for (String s : args.get(Option.DOWNLOAD)) {
|
---|
831 | DownloadParamType.paramType(s).download(s, fileList);
|
---|
832 | }
|
---|
833 | if (!fileList.isEmpty()) {
|
---|
834 | OpenFileAction.openFiles(fileList, true);
|
---|
835 | }
|
---|
836 | for (String s : args.get(Option.DOWNLOADGPS)) {
|
---|
837 | DownloadParamType.paramType(s).downloadGps(s);
|
---|
838 | }
|
---|
839 | for (String s : args.get(Option.SELECTION)) {
|
---|
840 | SearchAction.search(s, SearchAction.SearchMode.add);
|
---|
841 | }
|
---|
842 | }
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Closes JOSM and optionally terminates the Java Virtual Machine (JVM).
|
---|
846 | * If there are some unsaved data layers, asks first for user confirmation.
|
---|
847 | * @param exit If {@code true}, the JVM is terminated by running {@link System#exit} with a given return code.
|
---|
848 | * @param exitCode The return code
|
---|
849 | * @param reason the reason for exiting
|
---|
850 | * @return {@code true} if JOSM has been closed, {@code false} if the user has cancelled the operation.
|
---|
851 | * @since 11093 (3378 with a different function signature)
|
---|
852 | */
|
---|
853 | public static boolean exitJosm(boolean exit, int exitCode, SaveLayersDialog.Reason reason) {
|
---|
854 | final boolean proceed = Boolean.TRUE.equals(GuiHelper.runInEDTAndWaitAndReturn(() ->
|
---|
855 | SaveLayersDialog.saveUnsavedModifications(getLayerManager().getLayers(),
|
---|
856 | reason != null ? reason : SaveLayersDialog.Reason.EXIT)));
|
---|
857 | if (proceed) {
|
---|
858 | if (Main.main != null) {
|
---|
859 | Main.main.shutdown();
|
---|
860 | }
|
---|
861 |
|
---|
862 | if (exit) {
|
---|
863 | System.exit(exitCode);
|
---|
864 | }
|
---|
865 | return true;
|
---|
866 | }
|
---|
867 | return false;
|
---|
868 | }
|
---|
869 |
|
---|
870 | protected void shutdown() {
|
---|
871 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
872 | worker.shutdown();
|
---|
873 | ImageProvider.shutdown(false);
|
---|
874 | JCSCacheManager.shutdown();
|
---|
875 | }
|
---|
876 | if (map != null) {
|
---|
877 | map.rememberToggleDialogWidth();
|
---|
878 | }
|
---|
879 | // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
|
---|
880 | getLayerManager().resetState();
|
---|
881 | try {
|
---|
882 | pref.saveDefaults();
|
---|
883 | } catch (IOException ex) {
|
---|
884 | Main.warn(ex, tr("Failed to save default preferences."));
|
---|
885 | }
|
---|
886 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
887 | worker.shutdownNow();
|
---|
888 | ImageProvider.shutdown(true);
|
---|
889 | }
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * The type of a command line parameter, to be used in switch statements.
|
---|
894 | * @see #paramType
|
---|
895 | */
|
---|
896 | enum DownloadParamType {
|
---|
897 | httpUrl {
|
---|
898 | @Override
|
---|
899 | void download(String s, Collection<File> fileList) {
|
---|
900 | new OpenLocationAction().openUrl(false, s);
|
---|
901 | }
|
---|
902 |
|
---|
903 | @Override
|
---|
904 | void downloadGps(String s) {
|
---|
905 | final Bounds b = OsmUrlToBounds.parse(s);
|
---|
906 | if (b == null) {
|
---|
907 | JOptionPane.showMessageDialog(
|
---|
908 | Main.parent,
|
---|
909 | tr("Ignoring malformed URL: \"{0}\"", s),
|
---|
910 | tr("Warning"),
|
---|
911 | JOptionPane.WARNING_MESSAGE
|
---|
912 | );
|
---|
913 | return;
|
---|
914 | }
|
---|
915 | downloadFromParamBounds(true, b);
|
---|
916 | }
|
---|
917 | }, fileUrl {
|
---|
918 | @Override
|
---|
919 | void download(String s, Collection<File> fileList) {
|
---|
920 | File f = null;
|
---|
921 | try {
|
---|
922 | f = new File(new URI(s));
|
---|
923 | } catch (URISyntaxException e) {
|
---|
924 | Main.warn(e);
|
---|
925 | JOptionPane.showMessageDialog(
|
---|
926 | Main.parent,
|
---|
927 | tr("Ignoring malformed file URL: \"{0}\"", s),
|
---|
928 | tr("Warning"),
|
---|
929 | JOptionPane.WARNING_MESSAGE
|
---|
930 | );
|
---|
931 | }
|
---|
932 | if (f != null) {
|
---|
933 | fileList.add(f);
|
---|
934 | }
|
---|
935 | }
|
---|
936 | }, bounds {
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Download area specified on the command line as bounds string.
|
---|
940 | * @param rawGps Flag to download raw GPS tracks
|
---|
941 | * @param s The bounds parameter
|
---|
942 | */
|
---|
943 | private void downloadFromParamBounds(final boolean rawGps, String s) {
|
---|
944 | final StringTokenizer st = new StringTokenizer(s, ",");
|
---|
945 | if (st.countTokens() == 4) {
|
---|
946 | Bounds b = new Bounds(
|
---|
947 | new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
|
---|
948 | new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
|
---|
949 | );
|
---|
950 | Main.downloadFromParamBounds(rawGps, b);
|
---|
951 | }
|
---|
952 | }
|
---|
953 |
|
---|
954 | @Override
|
---|
955 | void download(String param, Collection<File> fileList) {
|
---|
956 | downloadFromParamBounds(false, param);
|
---|
957 | }
|
---|
958 |
|
---|
959 | @Override
|
---|
960 | void downloadGps(String param) {
|
---|
961 | downloadFromParamBounds(true, param);
|
---|
962 | }
|
---|
963 | }, fileName {
|
---|
964 | @Override
|
---|
965 | void download(String s, Collection<File> fileList) {
|
---|
966 | fileList.add(new File(s));
|
---|
967 | }
|
---|
968 | };
|
---|
969 |
|
---|
970 | /**
|
---|
971 | * Performs the download
|
---|
972 | * @param param represents the object to be downloaded
|
---|
973 | * @param fileList files which shall be opened, should be added to this collection
|
---|
974 | */
|
---|
975 | abstract void download(String param, Collection<File> fileList);
|
---|
976 |
|
---|
977 | /**
|
---|
978 | * Performs the GPS download
|
---|
979 | * @param param represents the object to be downloaded
|
---|
980 | */
|
---|
981 | void downloadGps(String param) {
|
---|
982 | JOptionPane.showMessageDialog(
|
---|
983 | Main.parent,
|
---|
984 | tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
|
---|
985 | tr("Warning"),
|
---|
986 | JOptionPane.WARNING_MESSAGE
|
---|
987 | );
|
---|
988 | }
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
|
---|
992 | *
|
---|
993 | * @param s A parameter string
|
---|
994 | * @return The guessed parameter type
|
---|
995 | */
|
---|
996 | static DownloadParamType paramType(String s) {
|
---|
997 | if (s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl;
|
---|
998 | if (s.startsWith("file:")) return DownloadParamType.fileUrl;
|
---|
999 | String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
|
---|
1000 | if (s.matches(coorPattern + "(," + coorPattern + "){3}")) return DownloadParamType.bounds;
|
---|
1001 | // everything else must be a file name
|
---|
1002 | return DownloadParamType.fileName;
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | /**
|
---|
1007 | * Download area specified as Bounds value.
|
---|
1008 | * @param rawGps Flag to download raw GPS tracks
|
---|
1009 | * @param b The bounds value
|
---|
1010 | */
|
---|
1011 | private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
|
---|
1012 | DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
|
---|
1013 | // asynchronously launch the download task ...
|
---|
1014 | Future<?> future = task.download(true, b, null);
|
---|
1015 | // ... and the continuation when the download is finished (this will wait for the download to finish)
|
---|
1016 | Main.worker.execute(new PostDownloadHandler(task, future));
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /**
|
---|
1020 | * Identifies the current operating system family and initializes the platform hook accordingly.
|
---|
1021 | * @since 1849
|
---|
1022 | */
|
---|
1023 | public static void determinePlatformHook() {
|
---|
1024 | String os = System.getProperty("os.name");
|
---|
1025 | if (os == null) {
|
---|
1026 | warn("Your operating system has no name, so I'm guessing its some kind of *nix.");
|
---|
1027 | platform = new PlatformHookUnixoid();
|
---|
1028 | } else if (os.toLowerCase(Locale.ENGLISH).startsWith("windows")) {
|
---|
1029 | platform = new PlatformHookWindows();
|
---|
1030 | } else if ("Linux".equals(os) || "Solaris".equals(os) ||
|
---|
1031 | "SunOS".equals(os) || "AIX".equals(os) ||
|
---|
1032 | "FreeBSD".equals(os) || "NetBSD".equals(os) || "OpenBSD".equals(os)) {
|
---|
1033 | platform = new PlatformHookUnixoid();
|
---|
1034 | } else if (os.toLowerCase(Locale.ENGLISH).startsWith("mac os x")) {
|
---|
1035 | platform = new PlatformHookOsx();
|
---|
1036 | } else {
|
---|
1037 | warn("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
|
---|
1038 | platform = new PlatformHookUnixoid();
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | /* ----------------------------------------------------------------------------------------- */
|
---|
1043 | /* projection handling - Main is a registry for a single, global projection instance */
|
---|
1044 | /* */
|
---|
1045 | /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
|
---|
1046 | /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
|
---|
1047 | /* ----------------------------------------------------------------------------------------- */
|
---|
1048 | /**
|
---|
1049 | * The projection method used.
|
---|
1050 | * use {@link #getProjection()} and {@link #setProjection(Projection)} for access.
|
---|
1051 | * Use {@link #setProjection(Projection)} in order to trigger a projection change event.
|
---|
1052 | */
|
---|
1053 | private static volatile Projection proj;
|
---|
1054 |
|
---|
1055 | /**
|
---|
1056 | * Replies the current projection.
|
---|
1057 | *
|
---|
1058 | * @return the currently active projection
|
---|
1059 | */
|
---|
1060 | public static Projection getProjection() {
|
---|
1061 | return proj;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /**
|
---|
1065 | * Sets the current projection
|
---|
1066 | *
|
---|
1067 | * @param p the projection
|
---|
1068 | */
|
---|
1069 | public static void setProjection(Projection p) {
|
---|
1070 | CheckParameterUtil.ensureParameterNotNull(p);
|
---|
1071 | Projection oldValue = proj;
|
---|
1072 | Bounds b = isDisplayingMapView() ? map.mapView.getRealBounds() : null;
|
---|
1073 | proj = p;
|
---|
1074 | fireProjectionChanged(oldValue, proj, b);
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /*
|
---|
1078 | * Keep WeakReferences to the listeners. This relieves clients from the burden of
|
---|
1079 | * explicitly removing the listeners and allows us to transparently register every
|
---|
1080 | * created dataset as projection change listener.
|
---|
1081 | */
|
---|
1082 | private static final List<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<>();
|
---|
1083 |
|
---|
1084 | private static void fireProjectionChanged(Projection oldValue, Projection newValue, Bounds oldBounds) {
|
---|
1085 | if ((newValue == null ^ oldValue == null)
|
---|
1086 | || (newValue != null && oldValue != null && !Objects.equals(newValue.toCode(), oldValue.toCode()))) {
|
---|
1087 | if (Main.map != null) {
|
---|
1088 | // This needs to be called first
|
---|
1089 | Main.map.mapView.fixProjection();
|
---|
1090 | }
|
---|
1091 | synchronized (Main.class) {
|
---|
1092 | Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
|
---|
1093 | while (it.hasNext()) {
|
---|
1094 | WeakReference<ProjectionChangeListener> wr = it.next();
|
---|
1095 | ProjectionChangeListener listener = wr.get();
|
---|
1096 | if (listener == null) {
|
---|
1097 | it.remove();
|
---|
1098 | continue;
|
---|
1099 | }
|
---|
1100 | listener.projectionChanged(oldValue, newValue);
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | if (newValue != null && oldBounds != null) {
|
---|
1104 | Main.map.mapView.zoomTo(oldBounds);
|
---|
1105 | }
|
---|
1106 | /* TODO - remove layers with fixed projection */
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /**
|
---|
1111 | * Register a projection change listener.
|
---|
1112 | *
|
---|
1113 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1114 | */
|
---|
1115 | public static void addProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
1116 | if (listener == null) return;
|
---|
1117 | synchronized (Main.class) {
|
---|
1118 | for (WeakReference<ProjectionChangeListener> wr : listeners) {
|
---|
1119 | // already registered ? => abort
|
---|
1120 | if (wr.get() == listener) return;
|
---|
1121 | }
|
---|
1122 | listeners.add(new WeakReference<>(listener));
|
---|
1123 | }
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | /**
|
---|
1127 | * Removes a projection change listener.
|
---|
1128 | *
|
---|
1129 | * @param listener the listener. Ignored if <code>null</code>.
|
---|
1130 | */
|
---|
1131 | public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
|
---|
1132 | if (listener == null) return;
|
---|
1133 | synchronized (Main.class) {
|
---|
1134 | // remove the listener - and any other listener which got garbage
|
---|
1135 | // collected in the meantime
|
---|
1136 | listeners.removeIf(wr -> wr.get() == null || wr.get() == listener);
|
---|
1137 | }
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | * Listener for window switch events.
|
---|
1142 | *
|
---|
1143 | * These are events, when the user activates a window of another application
|
---|
1144 | * or comes back to JOSM. Window switches from one JOSM window to another
|
---|
1145 | * are not reported.
|
---|
1146 | */
|
---|
1147 | public interface WindowSwitchListener {
|
---|
1148 | /**
|
---|
1149 | * Called when the user activates a window of another application.
|
---|
1150 | */
|
---|
1151 | void toOtherApplication();
|
---|
1152 |
|
---|
1153 | /**
|
---|
1154 | * Called when the user comes from a window of another application back to JOSM.
|
---|
1155 | */
|
---|
1156 | void fromOtherApplication();
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | /**
|
---|
1160 | * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes.
|
---|
1161 | * <p>
|
---|
1162 | * It will fire an initial mapFrameInitialized event when the MapFrame is present.
|
---|
1163 | * Otherwise will only fire when the MapFrame is created or destroyed.
|
---|
1164 | * @param listener The MapFrameListener
|
---|
1165 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1166 | * @see #addMapFrameListener
|
---|
1167 | * @since 11904
|
---|
1168 | */
|
---|
1169 | public static boolean addAndFireMapFrameListener(MapFrameListener listener) {
|
---|
1170 | return mainPanel.addAndFireMapFrameListener(listener);
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /**
|
---|
1174 | * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes
|
---|
1175 | * @param listener The MapFrameListener
|
---|
1176 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1177 | * @see #addAndFireMapFrameListener
|
---|
1178 | * @since 5957
|
---|
1179 | */
|
---|
1180 | public static boolean addMapFrameListener(MapFrameListener listener) {
|
---|
1181 | return mainPanel.addMapFrameListener(listener);
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | /**
|
---|
1185 | * Unregisters the given {@code MapFrameListener} from MapFrame changes
|
---|
1186 | * @param listener The MapFrameListener
|
---|
1187 | * @return {@code true} if the listeners collection changed as a result of the call
|
---|
1188 | * @since 5957
|
---|
1189 | */
|
---|
1190 | public static boolean removeMapFrameListener(MapFrameListener listener) {
|
---|
1191 | return mainPanel.removeMapFrameListener(listener);
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | /**
|
---|
1195 | * Adds a new network error that occur to give a hint about broken Internet connection.
|
---|
1196 | * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
|
---|
1197 | *
|
---|
1198 | * @param url The accessed URL that caused the error
|
---|
1199 | * @param t The network error
|
---|
1200 | * @return The previous error associated to the given resource, if any. Can be {@code null}
|
---|
1201 | * @since 6642
|
---|
1202 | */
|
---|
1203 | public static Throwable addNetworkError(URL url, Throwable t) {
|
---|
1204 | if (url != null && t != null) {
|
---|
1205 | Throwable old = addNetworkError(url.toExternalForm(), t);
|
---|
1206 | if (old != null) {
|
---|
1207 | Main.warn("Already here "+old);
|
---|
1208 | }
|
---|
1209 | return old;
|
---|
1210 | }
|
---|
1211 | return null;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | /**
|
---|
1215 | * Adds a new network error that occur to give a hint about broken Internet connection.
|
---|
1216 | * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
|
---|
1217 | *
|
---|
1218 | * @param url The accessed URL that caused the error
|
---|
1219 | * @param t The network error
|
---|
1220 | * @return The previous error associated to the given resource, if any. Can be {@code null}
|
---|
1221 | * @since 6642
|
---|
1222 | */
|
---|
1223 | public static Throwable addNetworkError(String url, Throwable t) {
|
---|
1224 | if (url != null && t != null) {
|
---|
1225 | return NETWORK_ERRORS.put(url, t);
|
---|
1226 | }
|
---|
1227 | return null;
|
---|
1228 | }
|
---|
1229 |
|
---|
1230 | /**
|
---|
1231 | * Returns the network errors that occured until now.
|
---|
1232 | * @return the network errors that occured until now, indexed by URL
|
---|
1233 | * @since 6639
|
---|
1234 | */
|
---|
1235 | public static Map<String, Throwable> getNetworkErrors() {
|
---|
1236 | return new HashMap<>(NETWORK_ERRORS);
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | /**
|
---|
1240 | * Returns the JOSM website URL.
|
---|
1241 | * @return the josm website URL
|
---|
1242 | * @since 6897
|
---|
1243 | */
|
---|
1244 | public static String getJOSMWebsite() {
|
---|
1245 | if (Main.pref != null)
|
---|
1246 | return Main.pref.get("josm.url", JOSM_WEBSITE);
|
---|
1247 | return JOSM_WEBSITE;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | /**
|
---|
1251 | * Returns the JOSM XML URL.
|
---|
1252 | * @return the josm XML URL
|
---|
1253 | * @since 6897
|
---|
1254 | */
|
---|
1255 | public static String getXMLBase() {
|
---|
1256 | // Always return HTTP (issues reported with HTTPS)
|
---|
1257 | return "http://josm.openstreetmap.de";
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | /**
|
---|
1261 | * Returns the OSM website URL.
|
---|
1262 | * @return the OSM website URL
|
---|
1263 | * @since 6897
|
---|
1264 | */
|
---|
1265 | public static String getOSMWebsite() {
|
---|
1266 | if (Main.pref != null)
|
---|
1267 | return Main.pref.get("osm.url", OSM_WEBSITE);
|
---|
1268 | return OSM_WEBSITE;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | /**
|
---|
1272 | * Returns the OSM website URL depending on the selected {@link OsmApi}.
|
---|
1273 | * @return the OSM website URL depending on the selected {@link OsmApi}
|
---|
1274 | */
|
---|
1275 | private static String getOSMWebsiteDependingOnSelectedApi() {
|
---|
1276 | final String api = OsmApi.getOsmApi().getServerUrl();
|
---|
1277 | if (OsmApi.DEFAULT_API_URL.equals(api)) {
|
---|
1278 | return getOSMWebsite();
|
---|
1279 | } else {
|
---|
1280 | return api.replaceAll("/api$", "");
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | /**
|
---|
1285 | * Replies the base URL for browsing information about a primitive.
|
---|
1286 | * @return the base URL, i.e. https://www.openstreetmap.org
|
---|
1287 | * @since 7678
|
---|
1288 | */
|
---|
1289 | public static String getBaseBrowseUrl() {
|
---|
1290 | if (Main.pref != null)
|
---|
1291 | return Main.pref.get("osm-browse.url", getOSMWebsiteDependingOnSelectedApi());
|
---|
1292 | return getOSMWebsiteDependingOnSelectedApi();
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | /**
|
---|
1296 | * Replies the base URL for browsing information about a user.
|
---|
1297 | * @return the base URL, i.e. https://www.openstreetmap.org/user
|
---|
1298 | * @since 7678
|
---|
1299 | */
|
---|
1300 | public static String getBaseUserUrl() {
|
---|
1301 | if (Main.pref != null)
|
---|
1302 | return Main.pref.get("osm-user.url", getOSMWebsiteDependingOnSelectedApi() + "/user");
|
---|
1303 | return getOSMWebsiteDependingOnSelectedApi() + "/user";
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | /**
|
---|
1307 | * Determines if we are currently running on OSX.
|
---|
1308 | * @return {@code true} if we are currently running on OSX
|
---|
1309 | * @since 6957
|
---|
1310 | */
|
---|
1311 | public static boolean isPlatformOsx() {
|
---|
1312 | return Main.platform instanceof PlatformHookOsx;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | /**
|
---|
1316 | * Determines if we are currently running on Windows.
|
---|
1317 | * @return {@code true} if we are currently running on Windows
|
---|
1318 | * @since 7335
|
---|
1319 | */
|
---|
1320 | public static boolean isPlatformWindows() {
|
---|
1321 | return Main.platform instanceof PlatformHookWindows;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | /**
|
---|
1325 | * Determines if the given online resource is currently offline.
|
---|
1326 | * @param r the online resource
|
---|
1327 | * @return {@code true} if {@code r} is offline and should not be accessed
|
---|
1328 | * @since 7434
|
---|
1329 | */
|
---|
1330 | public static boolean isOffline(OnlineResource r) {
|
---|
1331 | return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL);
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | /**
|
---|
1335 | * Sets the given online resource to offline state.
|
---|
1336 | * @param r the online resource
|
---|
1337 | * @return {@code true} if {@code r} was not already offline
|
---|
1338 | * @since 7434
|
---|
1339 | */
|
---|
1340 | public static boolean setOffline(OnlineResource r) {
|
---|
1341 | return OFFLINE_RESOURCES.add(r);
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 | /**
|
---|
1345 | * Sets the given online resource to online state.
|
---|
1346 | * @param r the online resource
|
---|
1347 | * @return {@code true} if {@code r} was offline
|
---|
1348 | * @since 8506
|
---|
1349 | */
|
---|
1350 | public static boolean setOnline(OnlineResource r) {
|
---|
1351 | return OFFLINE_RESOURCES.remove(r);
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /**
|
---|
1355 | * Replies the set of online resources currently offline.
|
---|
1356 | * @return the set of online resources currently offline
|
---|
1357 | * @since 7434
|
---|
1358 | */
|
---|
1359 | public static Set<OnlineResource> getOfflineResources() {
|
---|
1360 | return EnumSet.copyOf(OFFLINE_RESOURCES);
|
---|
1361 | }
|
---|
1362 | }
|
---|