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