| 1 |
|
| 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.Collections; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Objects; |
| 13 |
import java.util.concurrent.Callable; |
| 14 |
|
| 15 |
import org.openstreetmap.gui.jmapviewer.FeatureAdapter; |
| 16 |
import org.openstreetmap.gui.jmapviewer.FeatureAdapter.SettingsAdapter; |
| 17 |
import org.openstreetmap.josm.data.UndoRedoHandler; |
| 18 |
import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager; |
| 19 |
import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat; |
| 20 |
import org.openstreetmap.josm.data.coor.conversion.ICoordinateFormat; |
| 21 |
import org.openstreetmap.josm.data.validation.OsmValidator; |
| 22 |
import org.openstreetmap.josm.gui.layer.ImageryLayer; |
| 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.io.imagery.ApiKeyProvider; |
| 33 |
import org.openstreetmap.josm.spi.lifecycle.InitializationSequence; |
| 34 |
import org.openstreetmap.josm.spi.lifecycle.InitializationTask; |
| 35 |
import org.openstreetmap.josm.spi.preferences.Config; |
| 36 |
import org.openstreetmap.josm.tools.I18n; |
| 37 |
import org.openstreetmap.josm.tools.ImageProvider; |
| 38 |
import org.openstreetmap.josm.tools.Logging; |
| 39 |
import org.openstreetmap.josm.tools.OpenBrowser; |
| 40 |
import org.openstreetmap.josm.tools.PlatformManager; |
| 41 |
import org.openstreetmap.josm.tools.Shortcut; |
| 42 |
import org.openstreetmap.josm.tools.Tag2Link; |
| 43 |
import org.openstreetmap.josm.tools.Territories; |
| 44 |
import org.openstreetmap.josm.tools.Utils; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class MainInitialization implements InitializationSequence { |
| 51 |
|
| 52 |
private final MainApplication application; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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, MainApplication::sanityCheckFailed)), |
| 75 |
new InitializationTask(tr("Building main menu"), application::initializeMainWindow), |
| 76 |
new InitializationTask(tr("Updating user interface"), () -> { |
| 77 |
UndoRedoHandler.getInstance().addCommandQueueListener(application.redoUndoListener); |
| 78 |
|
| 79 |
GuiHelper.runInEDTAndWait(() -> MainApplication.contentPanePrivate.add(MainApplication.toolbar.control, BorderLayout.NORTH)); |
| 80 |
|
| 81 |
MainApplication.registerActionShortcut(MainApplication.menu.help, |
| 82 |
Shortcut.registerShortcut("system:help", tr("Help: {0}", tr("Help")), KeyEvent.VK_F1, Shortcut.DIRECT)); |
| 83 |
}), |
| 84 |
new InitializationTask(tr("Initializing internal boundaries data"), () -> { |
| 85 |
Territories.initialize(); |
| 86 |
if (Config.getPref().getBoolean("override.numbering.format", true)) { |
| 87 |
I18n.initializeNumberingFormat(); |
| 88 |
} |
| 89 |
}) |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public Collection<InitializationTask> parallelInitializationTasks() { |
| 95 |
return Arrays.asList( |
| 96 |
new InitializationTask(tr("Initializing OSM API"), () -> { |
| 97 |
OsmApi.addOsmApiInitializationListener(api -> { |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
if (MainApplication.isDisplayingMapView()) { |
| 102 |
for (ImageryLayer l : MainApplication.getLayerManager().getLayersOfType(ImageryLayer.class)) { |
| 103 |
if (l.getInfo().isBlacklisted()) { |
| 104 |
Logging.info(tr("Removed layer {0} because it is not allowed by the configured API.", l.getName())); |
| 105 |
MainApplication.getLayerManager().removeLayer(l); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
}); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
try { |
| 114 |
OsmApi.getOsmApi().initialize(null, true); |
| 115 |
} catch (OsmTransferCanceledException | OsmApiInitializationException | SecurityException e) { |
| 116 |
Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e))); |
| 117 |
} |
| 118 |
}), |
| 119 |
new InitializationTask(tr("Initializing validator"), OsmValidator::initialize), |
| 120 |
new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize), |
| 121 |
new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize), |
| 122 |
new InitializationTask(tr("Initializing Tag2Link rules"), Tag2Link::initialize), |
| 123 |
new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public List<Callable<?>> asynchronousCallableTasks() { |
| 129 |
return Collections.emptyList(); |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public List<Runnable> asynchronousRunnableTasks() { |
| 134 |
return Arrays.asList( |
| 135 |
TMSLayer::getCache, |
| 136 |
OsmValidator::initializeTests |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public List<InitializationTask> afterInitializationTasks() { |
| 142 |
return Arrays.asList( |
| 143 |
new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> { |
| 144 |
|
| 145 |
FeatureAdapter.registerApiKeyAdapter(ApiKeyProvider::retrieveApiKey); |
| 146 |
FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl); |
| 147 |
FeatureAdapter.registerImageAdapter(ImageProvider::read); |
| 148 |
FeatureAdapter.registerTranslationAdapter(I18n::tr); |
| 149 |
FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger()); |
| 150 |
FeatureAdapter.registerSettingsAdapter(new JosmSettingsAdapter()); |
| 151 |
|
| 152 |
MainApplication.toolbar.refreshToolbarControl(); |
| 153 |
MainApplication.toolbar.control.updateUI(); |
| 154 |
MainApplication.contentPanePrivate.updateUI(); |
| 155 |
})) |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
private static final class JosmSettingsAdapter implements SettingsAdapter { |
| 160 |
|
| 161 |
@Override |
| 162 |
public String get(String key, String def) { |
| 163 |
return Config.getPref().get(key, def); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public boolean put(String key, String value) { |
| 168 |
return Config.getPref().put(key, value); |
| 169 |
} |
| 170 |
} |
| 171 |
} |