source: josm/trunk/src/org/openstreetmap/josm/Main.java@ 12696

Last change on this file since 12696 was 12696, checked in by Don-vip, 7 years ago

see #15182 - remove deprecation of Main.undoRedo until I figure how to deal with this class

  • Property svn:eol-style set to native
File size: 37.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GraphicsEnvironment;
8import java.io.IOException;
9import java.lang.ref.WeakReference;
10import java.net.URL;
11import java.text.MessageFormat;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.Collections;
15import java.util.EnumSet;
16import java.util.HashMap;
17import java.util.Iterator;
18import java.util.List;
19import java.util.Locale;
20import java.util.Map;
21import java.util.Objects;
22import java.util.Set;
23import java.util.concurrent.Callable;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.ExecutorService;
26import java.util.concurrent.Executors;
27import java.util.concurrent.Future;
28
29import javax.swing.Action;
30
31import org.openstreetmap.josm.actions.JosmAction;
32import org.openstreetmap.josm.data.Bounds;
33import org.openstreetmap.josm.data.Preferences;
34import org.openstreetmap.josm.data.UndoRedoHandler;
35import org.openstreetmap.josm.data.cache.JCSCacheManager;
36import org.openstreetmap.josm.data.coor.CoordinateFormat;
37import org.openstreetmap.josm.data.osm.DataSet;
38import org.openstreetmap.josm.data.osm.OsmPrimitive;
39import org.openstreetmap.josm.data.projection.Projection;
40import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
41import org.openstreetmap.josm.gui.MainApplication;
42import org.openstreetmap.josm.gui.MainMenu;
43import org.openstreetmap.josm.gui.MainPanel;
44import org.openstreetmap.josm.gui.MapFrame;
45import org.openstreetmap.josm.gui.MapFrameListener;
46import org.openstreetmap.josm.gui.layer.MainLayerManager;
47import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
48import org.openstreetmap.josm.io.FileWatcher;
49import org.openstreetmap.josm.io.OnlineResource;
50import org.openstreetmap.josm.io.OsmApi;
51import org.openstreetmap.josm.tools.CheckParameterUtil;
52import org.openstreetmap.josm.tools.ImageProvider;
53import org.openstreetmap.josm.tools.JosmRuntimeException;
54import org.openstreetmap.josm.tools.Logging;
55import org.openstreetmap.josm.tools.PlatformHook;
56import org.openstreetmap.josm.tools.PlatformHookOsx;
57import org.openstreetmap.josm.tools.PlatformHookUnixoid;
58import org.openstreetmap.josm.tools.PlatformHookWindows;
59import org.openstreetmap.josm.tools.Shortcut;
60import org.openstreetmap.josm.tools.Utils;
61import org.openstreetmap.josm.tools.bugreport.BugReport;
62
63/**
64 * Abstract class holding various static global variables and methods used in large parts of JOSM application.
65 * @since 98
66 */
67public abstract class Main {
68
69 /**
70 * The JOSM website URL.
71 * @since 6897 (was public from 6143 to 6896)
72 */
73 private static final String JOSM_WEBSITE = "https://josm.openstreetmap.de";
74
75 /**
76 * The OSM website URL.
77 * @since 6897 (was public from 6453 to 6896)
78 */
79 private static final String OSM_WEBSITE = "https://www.openstreetmap.org";
80
81 /**
82 * Replies true if JOSM currently displays a map view. False, if it doesn't, i.e. if
83 * it only shows the MOTD panel.
84 * <p>
85 * You do not need this when accessing the layer manager. The layer manager will be empty if no map view is shown.
86 *
87 * @return <code>true</code> if JOSM currently displays a map view
88 * @deprecated use {@link org.openstreetmap.josm.gui.MainApplication#isDisplayingMapView()}
89 */
90 @Deprecated
91 public static boolean isDisplayingMapView() {
92 return MainApplication.isDisplayingMapView();
93 }
94
95 /**
96 * Global parent component for all dialogs and message boxes
97 */
98 public static Component parent;
99
100 /**
101 * Global application.
102 */
103 public static volatile Main main;
104
105 /**
106 * The worker thread slave. This is for executing all long and intensive
107 * calculations. The executed runnables are guaranteed to be executed separately and sequential.
108 * @deprecated use {@link MainApplication#worker} instead
109 */
110 @Deprecated
111 public static ExecutorService worker;
112
113 /**
114 * Global application preferences
115 */
116 public static final Preferences pref = new Preferences();
117
118 /**
119 * The MapFrame.
120 * <p>
121 * There should be no need to access this to access any map data. Use {@link MainApplication#getLayerManager} instead.
122 *
123 * @deprecated Use {@link MainApplication#getMap()} instead
124 * @see MainPanel
125 */
126 @Deprecated
127 public static MapFrame map;
128
129 /**
130 * The toolbar preference control to register new actions.
131 * @deprecated Use {@link MainApplication#getToolbar} instead
132 */
133 @Deprecated
134 public static volatile ToolbarPreferences toolbar;
135
136 /**
137 * The commands undo/redo handler.
138 */
139 public final UndoRedoHandler undoRedo = MainApplication.undoRedo;
140
141 /**
142 * The main menu bar at top of screen.
143 * @deprecated Use {@link MainApplication#getMenu} instead
144 */
145 @Deprecated
146 public MainMenu menu;
147
148 /**
149 * The main panel.
150 * @deprecated Use {@link MainApplication#getMainPanel} instead
151 * @since 12125
152 */
153 @Deprecated
154 public MainPanel panel;
155
156 /**
157 * The file watcher service.
158 */
159 public static final FileWatcher fileWatcher = new FileWatcher();
160
161 private static final Map<String, Throwable> NETWORK_ERRORS = new HashMap<>();
162
163 private static final Set<OnlineResource> OFFLINE_RESOURCES = EnumSet.noneOf(OnlineResource.class);
164
165 /**
166 * Logging level (5 = trace, 4 = debug, 3 = info, 2 = warn, 1 = error, 0 = none).
167 * @since 6248
168 * @deprecated Use {@link Logging} class.
169 */
170 @Deprecated
171 public static int logLevel = 3;
172
173 /**
174 * Replies the first lines of last 5 error and warning messages, used for bug reports
175 * @return the first lines of last 5 error and warning messages
176 * @since 7420
177 * @deprecated Use {@link Logging#getLastErrorAndWarnings}.
178 */
179 @Deprecated
180 public static final Collection<String> getLastErrorAndWarnings() {
181 return Logging.getLastErrorAndWarnings();
182 }
183
184 /**
185 * Clears the list of last error and warning messages.
186 * @since 8959
187 * @deprecated Use {@link Logging#clearLastErrorAndWarnings}.
188 */
189 @Deprecated
190 public static void clearLastErrorAndWarnings() {
191 Logging.clearLastErrorAndWarnings();
192 }
193
194 /**
195 * Prints an error message if logging is on.
196 * @param msg The message to print.
197 * @since 6248
198 * @deprecated Use {@link Logging#error(String)}.
199 */
200 @Deprecated
201 public static void error(String msg) {
202 Logging.error(msg);
203 }
204
205 /**
206 * Prints a warning message if logging is on.
207 * @param msg The message to print.
208 * @deprecated Use {@link Logging#warn(String)}.
209 */
210 @Deprecated
211 public static void warn(String msg) {
212 Logging.warn(msg);
213 }
214
215 /**
216 * Prints an informational message if logging is on.
217 * @param msg The message to print.
218 * @deprecated Use {@link Logging#info(String)}.
219 */
220 @Deprecated
221 public static void info(String msg) {
222 Logging.info(msg);
223 }
224
225 /**
226 * Prints a debug message if logging is on.
227 * @param msg The message to print.
228 * @deprecated Use {@link Logging#debug(String)}.
229 */
230 @Deprecated
231 public static void debug(String msg) {
232 Logging.debug(msg);
233 }
234
235 /**
236 * Prints a trace message if logging is on.
237 * @param msg The message to print.
238 * @deprecated Use {@link Logging#trace(String)}.
239 */
240 @Deprecated
241 public static void trace(String msg) {
242 Logging.trace(msg);
243 }
244
245 /**
246 * Determines if debug log level is enabled.
247 * Useful to avoid costly construction of debug messages when not enabled.
248 * @return {@code true} if log level is at least debug, {@code false} otherwise
249 * @since 6852
250 * @deprecated Use {@link Logging#isDebugEnabled}.
251 */
252 @Deprecated
253 public static boolean isDebugEnabled() {
254 return Logging.isDebugEnabled();
255 }
256
257 /**
258 * Determines if trace log level is enabled.
259 * Useful to avoid costly construction of trace messages when not enabled.
260 * @return {@code true} if log level is at least trace, {@code false} otherwise
261 * @since 6852
262 * @deprecated Use {@link Logging#isTraceEnabled}.
263 */
264 @Deprecated
265 public static boolean isTraceEnabled() {
266 return Logging.isTraceEnabled();
267 }
268
269 /**
270 * Prints a formatted error message if logging is on. Calls {@link MessageFormat#format}
271 * function to format text.
272 * @param msg The formatted message to print.
273 * @param objects The objects to insert into format string.
274 * @since 6248
275 * @deprecated Use {@link Logging#error(String, Object...)}.
276 */
277 @Deprecated
278 public static void error(String msg, Object... objects) {
279 Logging.error(msg, objects);
280 }
281
282 /**
283 * Prints a formatted warning message if logging is on. Calls {@link MessageFormat#format}
284 * function to format text.
285 * @param msg The formatted message to print.
286 * @param objects The objects to insert into format string.
287 * @deprecated Use {@link Logging#warn(String, Object...)}.
288 */
289 @Deprecated
290 public static void warn(String msg, Object... objects) {
291 Logging.warn(msg, objects);
292 }
293
294 /**
295 * Prints a formatted informational message if logging is on. Calls {@link MessageFormat#format}
296 * function to format text.
297 * @param msg The formatted message to print.
298 * @param objects The objects to insert into format string.
299 * @deprecated Use {@link Logging#info(String, Object...)}.
300 */
301 @Deprecated
302 public static void info(String msg, Object... objects) {
303 Logging.info(msg, objects);
304 }
305
306 /**
307 * Prints a formatted debug message if logging is on. Calls {@link MessageFormat#format}
308 * function to format text.
309 * @param msg The formatted message to print.
310 * @param objects The objects to insert into format string.
311 * @deprecated Use {@link Logging#debug(String, Object...)}.
312 */
313 @Deprecated
314 public static void debug(String msg, Object... objects) {
315 Logging.debug(msg, objects);
316 }
317
318 /**
319 * Prints a formatted trace message if logging is on. Calls {@link MessageFormat#format}
320 * function to format text.
321 * @param msg The formatted message to print.
322 * @param objects The objects to insert into format string.
323 * @deprecated Use {@link Logging#trace(String, Object...)}.
324 */
325 @Deprecated
326 public static void trace(String msg, Object... objects) {
327 Logging.trace(msg, objects);
328 }
329
330 /**
331 * Prints an error message for the given Throwable.
332 * @param t The throwable object causing the error
333 * @since 6248
334 * @deprecated Use {@link Logging#error(Throwable)}.
335 */
336 @Deprecated
337 public static void error(Throwable t) {
338 Logging.error(t);
339 }
340
341 /**
342 * Prints a warning message for the given Throwable.
343 * @param t The throwable object causing the error
344 * @since 6248
345 * @deprecated Use {@link Logging#warn(Throwable)}.
346 */
347 @Deprecated
348 public static void warn(Throwable t) {
349 Logging.warn(t);
350 }
351
352 /**
353 * Prints a debug message for the given Throwable. Useful for exceptions usually ignored
354 * @param t The throwable object causing the error
355 * @since 10420
356 * @deprecated Use {@link Logging#debug(Throwable)}.
357 */
358 @Deprecated
359 public static void debug(Throwable t) {
360 Logging.debug(t);
361 }
362
363 /**
364 * Prints a trace message for the given Throwable. Useful for exceptions usually ignored
365 * @param t The throwable object causing the error
366 * @since 10420
367 * @deprecated Use {@link Logging#trace(Throwable)}.
368 */
369 @Deprecated
370 public static void trace(Throwable t) {
371 Logging.trace(t);
372 }
373
374 /**
375 * Prints an error message for the given Throwable.
376 * @param t The throwable object causing the error
377 * @param stackTrace {@code true}, if the stacktrace should be displayed
378 * @since 6642
379 * @deprecated Use {@link Logging#log(java.util.logging.Level, Throwable)}
380 * or {@link Logging#logWithStackTrace(java.util.logging.Level, Throwable)}.
381 */
382 @Deprecated
383 public static void error(Throwable t, boolean stackTrace) {
384 if (stackTrace) {
385 Logging.log(Logging.LEVEL_ERROR, t);
386 } else {
387 Logging.logWithStackTrace(Logging.LEVEL_ERROR, t);
388 }
389 }
390
391 /**
392 * Prints an error message for the given Throwable.
393 * @param t The throwable object causing the error
394 * @param message additional error message
395 * @since 10420
396 * @deprecated Use {@link Logging#log(java.util.logging.Level, String, Throwable)}.
397 */
398 @Deprecated
399 public static void error(Throwable t, String message) {
400 Logging.log(Logging.LEVEL_ERROR, message, t);
401 }
402
403 /**
404 * Prints a warning message for the given Throwable.
405 * @param t The throwable object causing the error
406 * @param stackTrace {@code true}, if the stacktrace should be displayed
407 * @since 6642
408 * @deprecated Use {@link Logging#log(java.util.logging.Level, Throwable)}
409 * or {@link Logging#logWithStackTrace(java.util.logging.Level, Throwable)}.
410 */
411 @Deprecated
412 public static void warn(Throwable t, boolean stackTrace) {
413 if (stackTrace) {
414 Logging.log(Logging.LEVEL_WARN, t);
415 } else {
416 Logging.logWithStackTrace(Logging.LEVEL_WARN, t);
417 }
418 }
419
420 /**
421 * Prints a warning message for the given Throwable.
422 * @param t The throwable object causing the error
423 * @param message additional error message
424 * @since 10420
425 * @deprecated Use {@link Logging#log(java.util.logging.Level, String, Throwable)}.
426 */
427 @Deprecated
428 public static void warn(Throwable t, String message) {
429 Logging.log(Logging.LEVEL_WARN, message, t);
430 }
431
432 /**
433 * Returns a human-readable message of error, also usable for developers.
434 * @param t The error
435 * @return The human-readable error message
436 * @since 6642
437 * @deprecated Use {@link Logging#getErrorMessage}.
438 */
439 @Deprecated
440 public static String getErrorMessage(Throwable t) {
441 if (t == null) {
442 return null;
443 } else {
444 return Logging.getErrorMessage(t);
445 }
446 }
447
448 /**
449 * Platform specific code goes in here.
450 * Plugins may replace it, however, some hooks will be called before any plugins have been loaded.
451 * So if you need to hook into those early ones, split your class and send the one with the early hooks
452 * to the JOSM team for inclusion.
453 */
454 public static volatile PlatformHook platform;
455
456 private static volatile InitStatusListener initListener;
457
458 /**
459 * Initialization task listener.
460 */
461 public interface InitStatusListener {
462
463 /**
464 * Called when an initialization task updates its status.
465 * @param event task name
466 * @return new status
467 */
468 Object updateStatus(String event);
469
470 /**
471 * Called when an initialization task completes.
472 * @param status final status
473 */
474 void finish(Object status);
475 }
476
477 /**
478 * Sets initialization task listener.
479 * @param listener initialization task listener
480 */
481 public static void setInitStatusListener(InitStatusListener listener) {
482 CheckParameterUtil.ensureParameterNotNull(listener);
483 initListener = listener;
484 }
485
486 /**
487 * Constructs new {@code Main} object.
488 * @see #initialize()
489 */
490 protected Main() {
491 setInstance(this);
492 }
493
494 private static void setInstance(Main instance) {
495 main = instance;
496 }
497
498 /**
499 * Initializes the main object. A lot of global variables are initialized here.
500 * @since 10340
501 */
502 public void initialize() {
503 // Initializes tasks that must be run before parallel tasks
504 runInitializationTasks(beforeInitializationTasks());
505
506 // Initializes tasks to be executed (in parallel) by a ExecutorService
507 try {
508 ExecutorService service = Executors.newFixedThreadPool(
509 Runtime.getRuntime().availableProcessors(), Utils.newThreadFactory("main-init-%d", Thread.NORM_PRIORITY));
510 for (Future<Void> i : service.invokeAll(parallelInitializationTasks())) {
511 i.get();
512 }
513 // asynchronous initializations to be completed eventually
514 asynchronousRunnableTasks().forEach(service::submit);
515 asynchronousCallableTasks().forEach(service::submit);
516 service.shutdown();
517 } catch (InterruptedException | ExecutionException ex) {
518 throw new JosmRuntimeException(ex);
519 }
520
521 // Initializes tasks that must be run after parallel tasks
522 runInitializationTasks(afterInitializationTasks());
523 }
524
525 private static void runInitializationTasks(List<InitializationTask> tasks) {
526 for (InitializationTask task : tasks) {
527 try {
528 task.call();
529 } catch (JosmRuntimeException e) {
530 // Can happen if the current projection needs NTV2 grid which is not available
531 // In this case we want the user be able to change his projection
532 BugReport.intercept(e).warn();
533 }
534 }
535 }
536
537 /**
538 * Returns tasks that must be run before parallel tasks.
539 * @return tasks that must be run before parallel tasks
540 * @see #afterInitializationTasks
541 * @see #parallelInitializationTasks
542 */
543 protected List<InitializationTask> beforeInitializationTasks() {
544 return Collections.emptyList();
545 }
546
547 /**
548 * Returns tasks to be executed (in parallel) by a ExecutorService.
549 * @return tasks to be executed (in parallel) by a ExecutorService
550 */
551 protected Collection<InitializationTask> parallelInitializationTasks() {
552 return Collections.emptyList();
553 }
554
555 /**
556 * Returns asynchronous callable initializations to be completed eventually
557 * @return asynchronous callable initializations to be completed eventually
558 */
559 protected List<Callable<?>> asynchronousCallableTasks() {
560 return Collections.emptyList();
561 }
562
563 /**
564 * Returns asynchronous runnable initializations to be completed eventually
565 * @return asynchronous runnable initializations to be completed eventually
566 */
567 protected List<Runnable> asynchronousRunnableTasks() {
568 return Collections.emptyList();
569 }
570
571 /**
572 * Returns tasks that must be run after parallel tasks.
573 * @return tasks that must be run after parallel tasks
574 * @see #beforeInitializationTasks
575 * @see #parallelInitializationTasks
576 */
577 protected List<InitializationTask> afterInitializationTasks() {
578 return Collections.emptyList();
579 }
580
581 protected static final class InitializationTask implements Callable<Void> {
582
583 private final String name;
584 private final Runnable task;
585
586 /**
587 * Constructs a new {@code InitializationTask}.
588 * @param name translated name to be displayed to user
589 * @param task runnable initialization task
590 */
591 public InitializationTask(String name, Runnable task) {
592 this.name = name;
593 this.task = task;
594 }
595
596 @Override
597 public Void call() {
598 Object status = null;
599 if (initListener != null) {
600 status = initListener.updateStatus(name);
601 }
602 task.run();
603 if (initListener != null) {
604 initListener.finish(status);
605 }
606 return null;
607 }
608 }
609
610 /**
611 * Returns the main layer manager that is used by the map view.
612 * @return The layer manager. The value returned will never change.
613 * @since 10279
614 * @deprecated use {@link MainApplication#getLayerManager} instead
615 */
616 @Deprecated
617 public static MainLayerManager getLayerManager() {
618 return MainApplication.getLayerManager();
619 }
620
621 /**
622 * Replies the current selected primitives, from a end-user point of view.
623 * It is not always technically the same collection of primitives than {@link DataSet#getSelected()}.
624 * @return The current selected primitives, from a end-user point of view. Can be {@code null}.
625 * @since 6546
626 */
627 public Collection<OsmPrimitive> getInProgressSelection() {
628 return Collections.emptyList();
629 }
630
631 /**
632 * Gets the data set of the active edit layer.
633 * @return That data set, <code>null</code> if there is no edit layer.
634 * @since 12691
635 */
636 public DataSet getEditDataSet() {
637 return null;
638 }
639
640 /**
641 * Registers a {@code JosmAction} and its shortcut.
642 * @param action action defining its own shortcut
643 * @deprecated use {@link MainApplication#registerActionShortcut(JosmAction)} instead
644 */
645 @Deprecated
646 public static void registerActionShortcut(JosmAction action) {
647 MainApplication.registerActionShortcut(action);
648 }
649
650 /**
651 * Registers an action and its shortcut.
652 * @param action action to register
653 * @param shortcut shortcut to associate to {@code action}
654 * @deprecated use {@link MainApplication#registerActionShortcut(Action, Shortcut)} instead
655 */
656 @Deprecated
657 public static void registerActionShortcut(Action action, Shortcut shortcut) {
658 MainApplication.registerActionShortcut(action, shortcut);
659 }
660
661 /**
662 * Unregisters a shortcut.
663 * @param shortcut shortcut to unregister
664 * @deprecated use {@link MainApplication#unregisterShortcut(Shortcut)} instead
665 */
666 @Deprecated
667 public static void unregisterShortcut(Shortcut shortcut) {
668 MainApplication.unregisterShortcut(shortcut);
669 }
670
671 /**
672 * Unregisters a {@code JosmAction} and its shortcut.
673 * @param action action to unregister
674 * @deprecated use {@link MainApplication#unregisterActionShortcut(JosmAction)} instead
675 */
676 @Deprecated
677 public static void unregisterActionShortcut(JosmAction action) {
678 MainApplication.unregisterActionShortcut(action);
679 }
680
681 /**
682 * Unregisters an action and its shortcut.
683 * @param action action to unregister
684 * @param shortcut shortcut to unregister
685 * @deprecated use {@link MainApplication#unregisterActionShortcut(Action, Shortcut)} instead
686 */
687 @Deprecated
688 public static void unregisterActionShortcut(Action action, Shortcut shortcut) {
689 MainApplication.unregisterActionShortcut(action, shortcut);
690 }
691
692 /**
693 * Replies the registered action for the given shortcut
694 * @param shortcut The shortcut to look for
695 * @return the registered action for the given shortcut
696 * @deprecated use {@link MainApplication#getRegisteredActionShortcut(Shortcut)} instead
697 * @since 5696
698 */
699 @Deprecated
700 public static Action getRegisteredActionShortcut(Shortcut shortcut) {
701 return MainApplication.getRegisteredActionShortcut(shortcut);
702 }
703
704 ///////////////////////////////////////////////////////////////////////////
705 // Implementation part
706 ///////////////////////////////////////////////////////////////////////////
707
708 /**
709 * Should be called before the main constructor to setup some parameter stuff
710 */
711 public static void preConstructorInit() {
712 // init default coordinate format
713 try {
714 CoordinateFormat.setCoordinateFormat(CoordinateFormat.valueOf(Main.pref.get("coordinates")));
715 } catch (IllegalArgumentException iae) {
716 Logging.trace(iae);
717 CoordinateFormat.setCoordinateFormat(CoordinateFormat.DECIMAL_DEGREES);
718 }
719 }
720
721 /**
722 * Closes JOSM and optionally terminates the Java Virtual Machine (JVM).
723 * @param exit If {@code true}, the JVM is terminated by running {@link System#exit} with a given return code.
724 * @param exitCode The return code
725 * @return {@code true}
726 * @since 12636
727 */
728 public static boolean exitJosm(boolean exit, int exitCode) {
729 if (Main.main != null) {
730 Main.main.shutdown();
731 }
732
733 if (exit) {
734 System.exit(exitCode);
735 }
736 return true;
737 }
738
739 /**
740 * Shutdown JOSM.
741 */
742 protected void shutdown() {
743 if (!GraphicsEnvironment.isHeadless()) {
744 ImageProvider.shutdown(false);
745 JCSCacheManager.shutdown();
746 }
747 try {
748 pref.saveDefaults();
749 } catch (IOException ex) {
750 Logging.log(Logging.LEVEL_WARN, tr("Failed to save default preferences."), ex);
751 }
752 if (!GraphicsEnvironment.isHeadless()) {
753 ImageProvider.shutdown(true);
754 }
755 }
756
757 /**
758 * Identifies the current operating system family and initializes the platform hook accordingly.
759 * @since 1849
760 */
761 public static void determinePlatformHook() {
762 String os = System.getProperty("os.name");
763 if (os == null) {
764 Logging.warn("Your operating system has no name, so I'm guessing its some kind of *nix.");
765 platform = new PlatformHookUnixoid();
766 } else if (os.toLowerCase(Locale.ENGLISH).startsWith("windows")) {
767 platform = new PlatformHookWindows();
768 } else if ("Linux".equals(os) || "Solaris".equals(os) ||
769 "SunOS".equals(os) || "AIX".equals(os) ||
770 "FreeBSD".equals(os) || "NetBSD".equals(os) || "OpenBSD".equals(os)) {
771 platform = new PlatformHookUnixoid();
772 } else if (os.toLowerCase(Locale.ENGLISH).startsWith("mac os x")) {
773 platform = new PlatformHookOsx();
774 } else {
775 Logging.warn("I don't know your operating system '"+os+"', so I'm guessing its some kind of *nix.");
776 platform = new PlatformHookUnixoid();
777 }
778 }
779
780 /* ----------------------------------------------------------------------------------------- */
781 /* projection handling - Main is a registry for a single, global projection instance */
782 /* */
783 /* TODO: For historical reasons the registry is implemented by Main. An alternative approach */
784 /* would be a singleton org.openstreetmap.josm.data.projection.ProjectionRegistry class. */
785 /* ----------------------------------------------------------------------------------------- */
786 /**
787 * The projection method used.
788 * Use {@link #getProjection()} and {@link #setProjection(Projection)} for access.
789 * Use {@link #setProjection(Projection)} in order to trigger a projection change event.
790 */
791 private static volatile Projection proj;
792
793 /**
794 * Replies the current projection.
795 *
796 * @return the currently active projection
797 */
798 public static Projection getProjection() {
799 return proj;
800 }
801
802 /**
803 * Sets the current projection
804 *
805 * @param p the projection
806 */
807 public static void setProjection(Projection p) {
808 CheckParameterUtil.ensureParameterNotNull(p);
809 Projection oldValue = proj;
810 Bounds b = main != null ? main.getRealBounds() : null;
811 proj = p;
812 fireProjectionChanged(oldValue, proj, b);
813 }
814
815 /**
816 * Returns the bounds for the current projection. Used for projection events.
817 * @return the bounds for the current projection
818 * @see #restoreOldBounds
819 */
820 protected Bounds getRealBounds() {
821 // To be overriden
822 return null;
823 }
824
825 /**
826 * Restore clean state corresponding to old bounds after a projection change event.
827 * @param oldBounds bounds previously returned by {@link #getRealBounds}, before the change of projection
828 * @see #getRealBounds
829 */
830 protected void restoreOldBounds(Bounds oldBounds) {
831 // To be overriden
832 }
833
834 /*
835 * Keep WeakReferences to the listeners. This relieves clients from the burden of
836 * explicitly removing the listeners and allows us to transparently register every
837 * created dataset as projection change listener.
838 */
839 private static final List<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<>();
840
841 private static void fireProjectionChanged(Projection oldValue, Projection newValue, Bounds oldBounds) {
842 if ((newValue == null ^ oldValue == null)
843 || (newValue != null && oldValue != null && !Objects.equals(newValue.toCode(), oldValue.toCode()))) {
844 synchronized (Main.class) {
845 Iterator<WeakReference<ProjectionChangeListener>> it = listeners.iterator();
846 while (it.hasNext()) {
847 WeakReference<ProjectionChangeListener> wr = it.next();
848 ProjectionChangeListener listener = wr.get();
849 if (listener == null) {
850 it.remove();
851 continue;
852 }
853 listener.projectionChanged(oldValue, newValue);
854 }
855 }
856 if (newValue != null && oldBounds != null && main != null) {
857 main.restoreOldBounds(oldBounds);
858 }
859 /* TODO - remove layers with fixed projection */
860 }
861 }
862
863 /**
864 * Register a projection change listener.
865 * The listener is registered to be weak, so keep a reference of it if you want it to be preserved.
866 *
867 * @param listener the listener. Ignored if <code>null</code>.
868 */
869 public static void addProjectionChangeListener(ProjectionChangeListener listener) {
870 if (listener == null) return;
871 synchronized (Main.class) {
872 for (WeakReference<ProjectionChangeListener> wr : listeners) {
873 // already registered ? => abort
874 if (wr.get() == listener) return;
875 }
876 listeners.add(new WeakReference<>(listener));
877 }
878 }
879
880 /**
881 * Removes a projection change listener.
882 *
883 * @param listener the listener. Ignored if <code>null</code>.
884 */
885 public static void removeProjectionChangeListener(ProjectionChangeListener listener) {
886 if (listener == null) return;
887 synchronized (Main.class) {
888 // remove the listener - and any other listener which got garbage
889 // collected in the meantime
890 listeners.removeIf(wr -> wr.get() == null || wr.get() == listener);
891 }
892 }
893
894 /**
895 * Listener for window switch events.
896 *
897 * These are events, when the user activates a window of another application
898 * or comes back to JOSM. Window switches from one JOSM window to another
899 * are not reported.
900 */
901 public interface WindowSwitchListener {
902 /**
903 * Called when the user activates a window of another application.
904 */
905 void toOtherApplication();
906
907 /**
908 * Called when the user comes from a window of another application back to JOSM.
909 */
910 void fromOtherApplication();
911 }
912
913 /**
914 * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes.
915 * <p>
916 * It will fire an initial mapFrameInitialized event when the MapFrame is present.
917 * Otherwise will only fire when the MapFrame is created or destroyed.
918 * @param listener The MapFrameListener
919 * @return {@code true} if the listeners collection changed as a result of the call
920 * @see #addMapFrameListener
921 * @deprecated use {@link MainApplication#addAndFireMapFrameListener} instead
922 * @since 11904
923 */
924 @Deprecated
925 public static boolean addAndFireMapFrameListener(MapFrameListener listener) {
926 return MainApplication.addAndFireMapFrameListener(listener);
927 }
928
929 /**
930 * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes
931 * @param listener The MapFrameListener
932 * @return {@code true} if the listeners collection changed as a result of the call
933 * @see #addAndFireMapFrameListener
934 * @deprecated use {@link MainApplication#addMapFrameListener} instead
935 * @since 5957
936 */
937 @Deprecated
938 public static boolean addMapFrameListener(MapFrameListener listener) {
939 return MainApplication.addMapFrameListener(listener);
940 }
941
942 /**
943 * Unregisters the given {@code MapFrameListener} from MapFrame changes
944 * @param listener The MapFrameListener
945 * @return {@code true} if the listeners collection changed as a result of the call
946 * @deprecated use {@link MainApplication#removeMapFrameListener} instead
947 * @since 5957
948 */
949 @Deprecated
950 public static boolean removeMapFrameListener(MapFrameListener listener) {
951 return MainApplication.removeMapFrameListener(listener);
952 }
953
954 /**
955 * Adds a new network error that occur to give a hint about broken Internet connection.
956 * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
957 *
958 * @param url The accessed URL that caused the error
959 * @param t The network error
960 * @return The previous error associated to the given resource, if any. Can be {@code null}
961 * @since 6642
962 */
963 public static Throwable addNetworkError(URL url, Throwable t) {
964 if (url != null && t != null) {
965 Throwable old = addNetworkError(url.toExternalForm(), t);
966 if (old != null) {
967 Logging.warn("Already here "+old);
968 }
969 return old;
970 }
971 return null;
972 }
973
974 /**
975 * Adds a new network error that occur to give a hint about broken Internet connection.
976 * Do not use this method for errors known for sure thrown because of a bad proxy configuration.
977 *
978 * @param url The accessed URL that caused the error
979 * @param t The network error
980 * @return The previous error associated to the given resource, if any. Can be {@code null}
981 * @since 6642
982 */
983 public static Throwable addNetworkError(String url, Throwable t) {
984 if (url != null && t != null) {
985 return NETWORK_ERRORS.put(url, t);
986 }
987 return null;
988 }
989
990 /**
991 * Returns the network errors that occured until now.
992 * @return the network errors that occured until now, indexed by URL
993 * @since 6639
994 */
995 public static Map<String, Throwable> getNetworkErrors() {
996 return new HashMap<>(NETWORK_ERRORS);
997 }
998
999 /**
1000 * Clears the network errors cache.
1001 * @since 12011
1002 */
1003 public static void clearNetworkErrors() {
1004 NETWORK_ERRORS.clear();
1005 }
1006
1007 /**
1008 * Returns the JOSM website URL.
1009 * @return the josm website URL
1010 * @since 6897
1011 */
1012 public static String getJOSMWebsite() {
1013 if (Main.pref != null)
1014 return Main.pref.get("josm.url", JOSM_WEBSITE);
1015 return JOSM_WEBSITE;
1016 }
1017
1018 /**
1019 * Returns the JOSM XML URL.
1020 * @return the josm XML URL
1021 * @since 6897
1022 */
1023 public static String getXMLBase() {
1024 // Always return HTTP (issues reported with HTTPS)
1025 return "http://josm.openstreetmap.de";
1026 }
1027
1028 /**
1029 * Returns the OSM website URL.
1030 * @return the OSM website URL
1031 * @since 6897
1032 */
1033 public static String getOSMWebsite() {
1034 if (Main.pref != null)
1035 return Main.pref.get("osm.url", OSM_WEBSITE);
1036 return OSM_WEBSITE;
1037 }
1038
1039 /**
1040 * Returns the OSM website URL depending on the selected {@link OsmApi}.
1041 * @return the OSM website URL depending on the selected {@link OsmApi}
1042 */
1043 private static String getOSMWebsiteDependingOnSelectedApi() {
1044 final String api = OsmApi.getOsmApi().getServerUrl();
1045 if (OsmApi.DEFAULT_API_URL.equals(api)) {
1046 return getOSMWebsite();
1047 } else {
1048 return api.replaceAll("/api$", "");
1049 }
1050 }
1051
1052 /**
1053 * Replies the base URL for browsing information about a primitive.
1054 * @return the base URL, i.e. https://www.openstreetmap.org
1055 * @since 7678
1056 */
1057 public static String getBaseBrowseUrl() {
1058 if (Main.pref != null)
1059 return Main.pref.get("osm-browse.url", getOSMWebsiteDependingOnSelectedApi());
1060 return getOSMWebsiteDependingOnSelectedApi();
1061 }
1062
1063 /**
1064 * Replies the base URL for browsing information about a user.
1065 * @return the base URL, i.e. https://www.openstreetmap.org/user
1066 * @since 7678
1067 */
1068 public static String getBaseUserUrl() {
1069 if (Main.pref != null)
1070 return Main.pref.get("osm-user.url", getOSMWebsiteDependingOnSelectedApi() + "/user");
1071 return getOSMWebsiteDependingOnSelectedApi() + "/user";
1072 }
1073
1074 /**
1075 * Determines if we are currently running on OSX.
1076 * @return {@code true} if we are currently running on OSX
1077 * @since 6957
1078 */
1079 public static boolean isPlatformOsx() {
1080 return Main.platform instanceof PlatformHookOsx;
1081 }
1082
1083 /**
1084 * Determines if we are currently running on Windows.
1085 * @return {@code true} if we are currently running on Windows
1086 * @since 7335
1087 */
1088 public static boolean isPlatformWindows() {
1089 return Main.platform instanceof PlatformHookWindows;
1090 }
1091
1092 /**
1093 * Determines if the given online resource is currently offline.
1094 * @param r the online resource
1095 * @return {@code true} if {@code r} is offline and should not be accessed
1096 * @since 7434
1097 */
1098 public static boolean isOffline(OnlineResource r) {
1099 return OFFLINE_RESOURCES.contains(r) || OFFLINE_RESOURCES.contains(OnlineResource.ALL);
1100 }
1101
1102 /**
1103 * Sets the given online resource to offline state.
1104 * @param r the online resource
1105 * @return {@code true} if {@code r} was not already offline
1106 * @since 7434
1107 */
1108 public static boolean setOffline(OnlineResource r) {
1109 return OFFLINE_RESOURCES.add(r);
1110 }
1111
1112 /**
1113 * Sets the given online resource to online state.
1114 * @param r the online resource
1115 * @return {@code true} if {@code r} was offline
1116 * @since 8506
1117 */
1118 public static boolean setOnline(OnlineResource r) {
1119 return OFFLINE_RESOURCES.remove(r);
1120 }
1121
1122 /**
1123 * Replies the set of online resources currently offline.
1124 * @return the set of online resources currently offline
1125 * @since 7434
1126 */
1127 public static Set<OnlineResource> getOfflineResources() {
1128 return EnumSet.copyOf(OFFLINE_RESOURCES);
1129 }
1130}
Note: See TracBrowser for help on using the repository browser.