1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.Objects;
|
---|
12 | import java.util.concurrent.Callable;
|
---|
13 |
|
---|
14 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
|
---|
15 | import org.openstreetmap.gui.jmapviewer.FeatureAdapter.SettingsAdapter;
|
---|
16 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
17 | import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
|
---|
18 | import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
|
---|
19 | import org.openstreetmap.josm.data.coor.conversion.ICoordinateFormat;
|
---|
20 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
21 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
22 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
|
---|
26 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
27 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
28 | import org.openstreetmap.josm.io.FileWatcher;
|
---|
29 | import org.openstreetmap.josm.io.OsmApi;
|
---|
30 | import org.openstreetmap.josm.io.OsmApiInitializationException;
|
---|
31 | import org.openstreetmap.josm.io.OsmTransferCanceledException;
|
---|
32 | import org.openstreetmap.josm.spi.lifecycle.InitializationSequence;
|
---|
33 | import org.openstreetmap.josm.spi.lifecycle.InitializationTask;
|
---|
34 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
35 | import org.openstreetmap.josm.tools.I18n;
|
---|
36 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
37 | import org.openstreetmap.josm.tools.Logging;
|
---|
38 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
39 | import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
|
---|
40 | import org.openstreetmap.josm.tools.PlatformManager;
|
---|
41 | import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
|
---|
42 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
43 | import org.openstreetmap.josm.tools.Territories;
|
---|
44 | import org.openstreetmap.josm.tools.Utils;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * JOSM initialization sequence.
|
---|
48 | * @since 14139
|
---|
49 | */
|
---|
50 | public class MainInitialization implements InitializationSequence {
|
---|
51 |
|
---|
52 | private final MainApplication application;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Constructs a new {@code MainInitialization}
|
---|
56 | * @param application Main application. Must not be null
|
---|
57 | */
|
---|
58 | public MainInitialization(MainApplication application) {
|
---|
59 | this.application = Objects.requireNonNull(application);
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public List<InitializationTask> beforeInitializationTasks() {
|
---|
64 | return Arrays.asList(
|
---|
65 | new InitializationTask(tr("Initializing coordinate format"), () -> {
|
---|
66 | ICoordinateFormat fmt = CoordinateFormatManager.getCoordinateFormat(Config.getPref().get("coordinates"));
|
---|
67 | if (fmt == null) {
|
---|
68 | fmt = DecimalDegreesCoordinateFormat.INSTANCE;
|
---|
69 | }
|
---|
70 | CoordinateFormatManager.setCoordinateFormat(fmt);
|
---|
71 | }),
|
---|
72 | new InitializationTask(tr("Starting file watcher"), FileWatcher.getDefaultInstance()::start),
|
---|
73 | new InitializationTask(tr("Executing platform startup hook"),
|
---|
74 | () -> PlatformManager.getPlatform().startupHook(MainApplication::askUpdateJava)),
|
---|
75 | new InitializationTask(tr("Building main menu"), application::initializeMainWindow),
|
---|
76 | new InitializationTask(tr("Updating user interface"), () -> {
|
---|
77 | UndoRedoHandler.getInstance().addCommandQueueListener(application.redoUndoListener);
|
---|
78 | // creating toolbar
|
---|
79 | GuiHelper.runInEDTAndWait(() -> MainApplication.contentPanePrivate.add(MainApplication.toolbar.control, BorderLayout.NORTH));
|
---|
80 | // help shortcut
|
---|
81 | MainApplication.registerActionShortcut(MainApplication.menu.help,
|
---|
82 | Shortcut.registerShortcut("system:help", tr("Help"), KeyEvent.VK_F1, Shortcut.DIRECT));
|
---|
83 | }),
|
---|
84 | // This needs to be done before RightAndLefthandTraffic::initialize is called
|
---|
85 | new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize)
|
---|
86 | );
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public Collection<InitializationTask> parallelInitializationTasks() {
|
---|
91 | return Arrays.asList(
|
---|
92 | new InitializationTask(tr("Initializing OSM API"), () -> {
|
---|
93 | OsmApi.addOsmApiInitializationListener(api -> {
|
---|
94 | // This checks if there are any layers currently displayed that are now on the blacklist, and removes them.
|
---|
95 | // This is a rare situation - probably only occurs if the user changes the API URL in the preferences menu.
|
---|
96 | // Otherwise they would not have been able to load the layers in the first place because they would have been disabled
|
---|
97 | if (MainApplication.isDisplayingMapView()) {
|
---|
98 | for (Layer l : MainApplication.getLayerManager().getLayersOfType(ImageryLayer.class)) {
|
---|
99 | if (((ImageryLayer) l).getInfo().isBlacklisted()) {
|
---|
100 | Logging.info(tr("Removed layer {0} because it is not allowed by the configured API.", l.getName()));
|
---|
101 | MainApplication.getLayerManager().removeLayer(l);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | });
|
---|
106 | // We try to establish an API connection early, so that any API
|
---|
107 | // capabilities are already known to the editor instance. However
|
---|
108 | // if it goes wrong that's not critical at this stage.
|
---|
109 | try {
|
---|
110 | OsmApi.getOsmApi().initialize(null, true);
|
---|
111 | } catch (OsmTransferCanceledException | OsmApiInitializationException | SecurityException e) {
|
---|
112 | Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e)));
|
---|
113 | }
|
---|
114 | }),
|
---|
115 | new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize),
|
---|
116 | new InitializationTask(tr("Initializing validator"), OsmValidator::initialize),
|
---|
117 | new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize),
|
---|
118 | new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize),
|
---|
119 | new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize)
|
---|
120 | );
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public List<Callable<?>> asynchronousCallableTasks() {
|
---|
125 | return Arrays.asList(
|
---|
126 | OverpassTurboQueryWizard::getInstance
|
---|
127 | );
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public List<Runnable> asynchronousRunnableTasks() {
|
---|
132 | return Arrays.asList(
|
---|
133 | TMSLayer::getCache,
|
---|
134 | OsmValidator::initializeTests
|
---|
135 | );
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public List<InitializationTask> afterInitializationTasks() {
|
---|
140 | return Arrays.asList(
|
---|
141 | new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> {
|
---|
142 | // hooks for the jmapviewer component
|
---|
143 | FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl);
|
---|
144 | FeatureAdapter.registerImageAdapter(ImageProvider::read);
|
---|
145 | FeatureAdapter.registerTranslationAdapter(I18n::tr);
|
---|
146 | FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger());
|
---|
147 | FeatureAdapter.registerSettingsAdapter(new JosmSettingsAdapter());
|
---|
148 | // UI update
|
---|
149 | MainApplication.toolbar.refreshToolbarControl();
|
---|
150 | MainApplication.toolbar.control.updateUI();
|
---|
151 | MainApplication.contentPanePrivate.updateUI();
|
---|
152 | }))
|
---|
153 | );
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static class JosmSettingsAdapter implements SettingsAdapter {
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public String get(String key, String def) {
|
---|
160 | return Config.getPref().get(key, def);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public boolean put(String key, String value) {
|
---|
165 | return Config.getPref().put(key, value);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|