Ignore:
Timestamp:
2018-08-12T02:52:44+02:00 (6 years ago)
Author:
Don-vip
Message:

see #15229 - move Main initialization methods to lifecycle SPI + new class MainInitialization

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r14138 r14139  
    66import static org.openstreetmap.josm.tools.Utils.getSystemProperty;
    77
    8 import java.awt.BorderLayout;
    98import java.awt.Container;
    109import java.awt.Dimension;
     
    1312import java.awt.GridBagLayout;
    1413import java.awt.Toolkit;
    15 import java.awt.event.KeyEvent;
    1614import java.io.File;
    1715import java.io.IOException;
     
    4644import java.util.Set;
    4745import java.util.TreeSet;
    48 import java.util.concurrent.Callable;
    4946import java.util.concurrent.ExecutorService;
    5047import java.util.concurrent.Executors;
     
    6966
    7067import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
    71 import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    7268import org.openstreetmap.josm.Main;
    7369import org.openstreetmap.josm.actions.DeleteAction;
     
    108104import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileWrapper;
    109105import org.openstreetmap.josm.data.projection.datum.NTV2Proj4DirGridShiftFileSource;
    110 import org.openstreetmap.josm.data.validation.OsmValidator;
    111106import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    112107import org.openstreetmap.josm.gui.ProgramArguments.Option;
     
    118113import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    119114import org.openstreetmap.josm.gui.layer.AutosaveTask;
    120 import org.openstreetmap.josm.gui.layer.ImageryLayer;
    121115import org.openstreetmap.josm.gui.layer.Layer;
    122116import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
     
    126120import org.openstreetmap.josm.gui.layer.MainLayerManager;
    127121import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    128 import org.openstreetmap.josm.gui.layer.TMSLayer;
    129122import org.openstreetmap.josm.gui.mappaint.RenderingCLI;
    130123import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
     
    132125import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
    133126import org.openstreetmap.josm.gui.preferences.display.LafPreference;
    134 import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
    135 import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    136127import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    137128import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
    138129import org.openstreetmap.josm.gui.progress.swing.ProgressMonitorExecutor;
    139 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    140130import org.openstreetmap.josm.gui.util.GuiHelper;
    141131import org.openstreetmap.josm.gui.util.RedirectInputMap;
     
    149139import org.openstreetmap.josm.io.NetworkManager;
    150140import org.openstreetmap.josm.io.OnlineResource;
    151 import org.openstreetmap.josm.io.OsmApi;
    152 import org.openstreetmap.josm.io.OsmApiInitializationException;
    153141import org.openstreetmap.josm.io.OsmConnection;
    154 import org.openstreetmap.josm.io.OsmTransferCanceledException;
    155142import org.openstreetmap.josm.io.OsmTransferException;
    156143import org.openstreetmap.josm.io.auth.AbstractCredentialsAgent;
     
    162149import org.openstreetmap.josm.plugins.PluginInformation;
    163150import org.openstreetmap.josm.spi.lifecycle.InitStatusListener;
    164 import org.openstreetmap.josm.spi.lifecycle.InitializationTask;
    165151import org.openstreetmap.josm.spi.lifecycle.Lifecycle;
    166152import org.openstreetmap.josm.spi.preferences.Config;
     
    173159import org.openstreetmap.josm.tools.JosmRuntimeException;
    174160import org.openstreetmap.josm.tools.Logging;
    175 import org.openstreetmap.josm.tools.OpenBrowser;
    176161import org.openstreetmap.josm.tools.OsmUrlToBounds;
    177 import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    178162import org.openstreetmap.josm.tools.PlatformHook.NativeOsCallback;
    179163import org.openstreetmap.josm.tools.PlatformHookWindows;
    180164import org.openstreetmap.josm.tools.PlatformManager;
    181 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
    182165import org.openstreetmap.josm.tools.Shortcut;
    183 import org.openstreetmap.josm.tools.Territories;
    184166import org.openstreetmap.josm.tools.Utils;
    185167import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
     
    312294     * Listener that sets the enabled state of undo/redo menu entries.
    313295     */
    314     private final CommandQueueListener redoUndoListener = (queueSize, redoSize) -> {
     296    final CommandQueueListener redoUndoListener = (queueSize, redoSize) -> {
    315297            menu.undo.setEnabled(queueSize > 0);
    316298            menu.redo.setEnabled(redoSize > 0);
     
    407389            }
    408390        }
    409     }
    410 
    411     @Override
    412     protected List<InitializationTask> beforeInitializationTasks() {
    413         return Arrays.asList(
    414             new InitializationTask(tr("Starting file watcher"), FileWatcher.getDefaultInstance()::start),
    415             new InitializationTask(tr("Executing platform startup hook"),
    416                     () -> PlatformManager.getPlatform().startupHook(MainApplication::askUpdateJava)),
    417             new InitializationTask(tr("Building main menu"), this::initializeMainWindow),
    418             new InitializationTask(tr("Updating user interface"), () -> {
    419                 UndoRedoHandler.getInstance().addCommandQueueListener(redoUndoListener);
    420                 // creating toolbar
    421                 GuiHelper.runInEDTAndWait(() -> contentPanePrivate.add(toolbar.control, BorderLayout.NORTH));
    422                 // help shortcut
    423                 registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
    424                         KeyEvent.VK_F1, Shortcut.DIRECT));
    425             }),
    426             // This needs to be done before RightAndLefthandTraffic::initialize is called
    427             new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize)
    428         );
    429     }
    430 
    431     @Override
    432     protected Collection<InitializationTask> parallelInitializationTasks() {
    433         return Arrays.asList(
    434             new InitializationTask(tr("Initializing OSM API"), () -> {
    435                     OsmApi.addOsmApiInitializationListener(api -> {
    436                         // This checks if there are any layers currently displayed that are now on the blacklist, and removes them.
    437                         // This is a rare situation - probably only occurs if the user changes the API URL in the preferences menu.
    438                         // Otherwise they would not have been able to load the layers in the first place because they would have been disabled
    439                         if (isDisplayingMapView()) {
    440                             for (Layer l : getLayerManager().getLayersOfType(ImageryLayer.class)) {
    441                                 if (((ImageryLayer) l).getInfo().isBlacklisted()) {
    442                                     Logging.info(tr("Removed layer {0} because it is not allowed by the configured API.", l.getName()));
    443                                     getLayerManager().removeLayer(l);
    444                                 }
    445                             }
    446                         }
    447                     });
    448                     // We try to establish an API connection early, so that any API
    449                     // capabilities are already known to the editor instance. However
    450                     // if it goes wrong that's not critical at this stage.
    451                     try {
    452                         OsmApi.getOsmApi().initialize(null, true);
    453                     } catch (OsmTransferCanceledException | OsmApiInitializationException | SecurityException e) {
    454                         Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e)));
    455                     }
    456                 }),
    457             new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize),
    458             new InitializationTask(tr("Initializing validator"), OsmValidator::initialize),
    459             new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize),
    460             new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize),
    461             new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize)
    462         );
    463     }
    464 
    465     @Override
    466     protected List<Callable<?>> asynchronousCallableTasks() {
    467         return Arrays.asList(
    468                 OverpassTurboQueryWizard::getInstance
    469             );
    470     }
    471 
    472     @Override
    473     protected List<Runnable> asynchronousRunnableTasks() {
    474         return Arrays.asList(
    475                 TMSLayer::getCache,
    476                 OsmValidator::initializeTests
    477             );
    478     }
    479 
    480     @Override
    481     protected List<InitializationTask> afterInitializationTasks() {
    482         return Arrays.asList(
    483             new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> {
    484                 // hooks for the jmapviewer component
    485                 FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl);
    486                 FeatureAdapter.registerTranslationAdapter(I18n::tr);
    487                 FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger());
    488                 // UI update
    489                 toolbar.refreshToolbarControl();
    490                 toolbar.control.updateUI();
    491                 contentPanePrivate.updateUI();
    492             }))
    493         );
    494391    }
    495392
     
    1086983
    1087984        monitor.indeterminateSubTask(tr("Creating main GUI"));
    1088         final Main main = new MainApplication(mainFrame);
    1089         main.initialize();
     985        Lifecycle.initialize(new MainInitialization(new MainApplication(mainFrame)));
    1090986
    1091987        if (!skipLoadingPlugins) {
Note: See TracChangeset for help on using the changeset viewer.