Changeset 12629 in josm


Ignore:
Timestamp:
2017-08-23T21:00:42+02:00 (3 months ago)
Author:
Don-vip
Message:

see #15182 - move GUI initialization stuff from Main (abstract) to gui.MainApplication

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

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

    r12620 r12629  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.BorderLayout;
    76import java.awt.Component;
    87import java.awt.GraphicsEnvironment;
    9 import java.awt.event.KeyEvent;
    108import java.io.File;
    119import java.io.IOException;
     
    4341import javax.swing.UnsupportedLookAndFeelException;
    4442
    45 import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    4643import org.openstreetmap.josm.actions.JosmAction;
    4744import org.openstreetmap.josm.actions.OpenFileAction;
     
    6360import org.openstreetmap.josm.data.projection.Projection;
    6461import org.openstreetmap.josm.data.projection.ProjectionChangeListener;
    65 import org.openstreetmap.josm.data.validation.OsmValidator;
    6662import org.openstreetmap.josm.gui.MainMenu;
    6763import org.openstreetmap.josm.gui.MainPanel;
     
    7369import org.openstreetmap.josm.gui.layer.MainLayerManager;
    7470import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
    75 import org.openstreetmap.josm.gui.layer.TMSLayer;
    7671import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
    7772import org.openstreetmap.josm.gui.preferences.display.LafPreference;
    78 import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
    79 import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    8073import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    8174import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
    8275import org.openstreetmap.josm.gui.progress.ProgressMonitorExecutor;
    83 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    8476import org.openstreetmap.josm.gui.util.GuiHelper;
    8577import org.openstreetmap.josm.gui.util.RedirectInputMap;
     
    8779import org.openstreetmap.josm.io.OnlineResource;
    8880import org.openstreetmap.josm.io.OsmApi;
    89 import org.openstreetmap.josm.io.OsmApiInitializationException;
    90 import org.openstreetmap.josm.io.OsmTransferCanceledException;
    9181import org.openstreetmap.josm.plugins.PluginHandler;
    9282import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    9585import org.openstreetmap.josm.tools.JosmRuntimeException;
    9686import org.openstreetmap.josm.tools.Logging;
    97 import org.openstreetmap.josm.tools.OpenBrowser;
    9887import org.openstreetmap.josm.tools.OsmUrlToBounds;
    99 import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    10088import org.openstreetmap.josm.tools.PlatformHook;
    10189import org.openstreetmap.josm.tools.PlatformHookOsx;
    10290import org.openstreetmap.josm.tools.PlatformHookUnixoid;
    10391import org.openstreetmap.josm.tools.PlatformHookWindows;
    104 import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
    10592import org.openstreetmap.josm.tools.Shortcut;
    106 import org.openstreetmap.josm.tools.Territories;
    10793import org.openstreetmap.josm.tools.Utils;
    10894import org.openstreetmap.josm.tools.bugreport.BugReport;
     
    558544     */
    559545    public void initialize() {
    560         fileWatcher.start();
    561 
    562         new InitializationTask(tr("Executing platform startup hook"), platform::startupHook).call();
    563 
    564         new InitializationTask(tr("Building main menu"), this::initializeMainWindow).call();
    565 
    566         undoRedo.addCommandQueueListener(redoUndoListener);
    567 
    568         // creating toolbar
    569         GuiHelper.runInEDTAndWait(() -> contentPanePrivate.add(toolbar.control, BorderLayout.NORTH));
    570 
    571         registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
    572                 KeyEvent.VK_F1, Shortcut.DIRECT));
    573 
    574         // This needs to be done before RightAndLefthandTraffic::initialize is called
    575         try {
    576             new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize).call();
    577         } catch (JosmRuntimeException e) {
    578             // Can happen if the current projection needs NTV2 grid which is not available
    579             // In this case we want the user be able to change his projection
    580             BugReport.intercept(e).warn();
    581         }
    582 
    583         // contains several initialization tasks to be executed (in parallel) by a ExecutorService
    584         List<Callable<Void>> tasks = new ArrayList<>();
    585 
    586         tasks.add(new InitializationTask(tr("Initializing OSM API"), () -> {
    587                 // We try to establish an API connection early, so that any API
    588                 // capabilities are already known to the editor instance. However
    589                 // if it goes wrong that's not critical at this stage.
    590                 try {
    591                     OsmApi.getOsmApi().initialize(null, true);
    592                 } catch (OsmTransferCanceledException | OsmApiInitializationException e) {
    593                     Logging.warn(getErrorMessage(Utils.getRootCause(e)));
    594                 }
    595             }));
    596 
    597         tasks.add(new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize));
    598 
    599         tasks.add(new InitializationTask(tr("Initializing validator"), OsmValidator::initialize));
    600 
    601         tasks.add(new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize));
    602 
    603         tasks.add(new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize));
    604 
    605         tasks.add(new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize));
    606 
     546        // Initializes tasks that must be run before parallel tasks
     547        runInitializationTasks(beforeInitializationTasks());
     548
     549        // Initializes tasks to be executed (in parallel) by a ExecutorService
    607550        try {
    608551            ExecutorService service = Executors.newFixedThreadPool(
    609552                    Runtime.getRuntime().availableProcessors(), Utils.newThreadFactory("main-init-%d", Thread.NORM_PRIORITY));
    610             for (Future<Void> i : service.invokeAll(tasks)) {
     553            for (Future<Void> i : service.invokeAll(parallelInitializationTasks())) {
    611554                i.get();
    612555            }
    613556            // asynchronous initializations to be completed eventually
    614             service.submit((Runnable) TMSLayer::getCache);
    615             service.submit((Runnable) OsmValidator::initializeTests);
    616             service.submit(OverpassTurboQueryWizard::getInstance);
     557            asynchronousRunnableTasks().forEach(service::submit);
     558            asynchronousCallableTasks().forEach(service::submit);
    617559            service.shutdown();
    618560        } catch (InterruptedException | ExecutionException ex) {
     
    620562        }
    621563
    622         // hooks for the jmapviewer component
    623         FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl);
    624         FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter());
    625         FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger());
    626 
    627         new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> {
    628             toolbar.refreshToolbarControl();
    629             toolbar.control.updateUI();
    630             contentPanePrivate.updateUI();
    631         })).call();
     564        // Initializes tasks that must be run after parallel tasks
     565        runInitializationTasks(afterInitializationTasks());
     566    }
     567
     568    private static void runInitializationTasks(List<InitializationTask> tasks) {
     569        for (InitializationTask task : tasks) {
     570            try {
     571                task.call();
     572            } catch (JosmRuntimeException e) {
     573                // Can happen if the current projection needs NTV2 grid which is not available
     574                // In this case we want the user be able to change his projection
     575                BugReport.intercept(e).warn();
     576            }
     577        }
     578    }
     579
     580    /**
     581     * Returns tasks that must be run before parallel tasks.
     582     * @return tasks that must be run before parallel tasks
     583     * @see #afterInitializationTasks
     584     * @see #parallelInitializationTasks
     585     */
     586    protected List<InitializationTask> beforeInitializationTasks() {
     587        return Collections.emptyList();
     588    }
     589
     590    /**
     591     * Returns tasks to be executed (in parallel) by a ExecutorService.
     592     * @return tasks to be executed (in parallel) by a ExecutorService
     593     */
     594    protected Collection<InitializationTask> parallelInitializationTasks() {
     595        return Collections.emptyList();
     596    }
     597
     598    /**
     599     * Returns asynchronous callable initializations to be completed eventually
     600     * @return asynchronous callable initializations to be completed eventually
     601     */
     602    protected List<Callable<?>> asynchronousCallableTasks() {
     603        return Collections.emptyList();
     604    }
     605
     606    /**
     607     * Returns asynchronous runnable initializations to be completed eventually
     608     * @return asynchronous runnable initializations to be completed eventually
     609     */
     610    protected List<Runnable> asynchronousRunnableTasks() {
     611        return Collections.emptyList();
     612    }
     613
     614    /**
     615     * Returns tasks that must be run after parallel tasks.
     616     * @return tasks that must be run after parallel tasks
     617     * @see #beforeInitializationTasks
     618     * @see #parallelInitializationTasks
     619     */
     620    protected List<InitializationTask> afterInitializationTasks() {
     621        return Collections.emptyList();
    632622    }
    633623
     
    638628    protected abstract void initializeMainWindow();
    639629
    640     static final class InitializationTask implements Callable<Void> {
     630    protected static final class InitializationTask implements Callable<Void> {
    641631
    642632        private final String name;
    643633        private final Runnable task;
    644634
    645         protected InitializationTask(String name, Runnable task) {
     635        /**
     636         * Constructs a new {@code InitializationTask}.
     637         * @param name translated name to be displayed to user
     638         * @param task runnable initialization task
     639         */
     640        public InitializationTask(String name, Runnable task) {
    646641            this.name = name;
    647642            this.task = task;
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r12620 r12629  
    55import static org.openstreetmap.josm.tools.I18n.trn;
    66
     7import java.awt.BorderLayout;
    78import java.awt.Dimension;
     9import java.awt.event.KeyEvent;
    810import java.io.File;
    911import java.io.IOException;
     
    3335import java.util.Set;
    3436import java.util.TreeSet;
     37import java.util.concurrent.Callable;
    3538import java.util.logging.Level;
    3639import java.util.stream.Collectors;
     
    4447
    4548import org.jdesktop.swinghelper.debug.CheckThreadViolationRepaintManager;
     49import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    4650import org.openstreetmap.josm.Main;
    4751import org.openstreetmap.josm.actions.PreferencesAction;
     
    5054import org.openstreetmap.josm.data.CustomConfigurator;
    5155import org.openstreetmap.josm.data.Version;
     56import org.openstreetmap.josm.data.validation.OsmValidator;
    5257import org.openstreetmap.josm.gui.ProgramArguments.Option;
    5358import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
    5459import org.openstreetmap.josm.gui.download.DownloadDialog;
     60import org.openstreetmap.josm.gui.layer.TMSLayer;
     61import org.openstreetmap.josm.gui.preferences.imagery.ImageryPreference;
     62import org.openstreetmap.josm.gui.preferences.map.MapPaintPreference;
    5563import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
    5664import org.openstreetmap.josm.gui.preferences.server.ProxyPreference;
     65import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    5766import org.openstreetmap.josm.gui.util.GuiHelper;
    5867import org.openstreetmap.josm.io.CertificateAmendment;
     
    6069import org.openstreetmap.josm.io.MessageNotifier;
    6170import org.openstreetmap.josm.io.OnlineResource;
     71import org.openstreetmap.josm.io.OsmApi;
     72import org.openstreetmap.josm.io.OsmApiInitializationException;
     73import org.openstreetmap.josm.io.OsmTransferCanceledException;
    6274import org.openstreetmap.josm.io.auth.CredentialsManager;
    6375import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
     
    7082import org.openstreetmap.josm.tools.I18n;
    7183import org.openstreetmap.josm.tools.Logging;
     84import org.openstreetmap.josm.tools.OpenBrowser;
    7285import org.openstreetmap.josm.tools.OsmUrlToBounds;
     86import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
    7387import org.openstreetmap.josm.tools.PlatformHookWindows;
     88import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
     89import org.openstreetmap.josm.tools.Shortcut;
     90import org.openstreetmap.josm.tools.Territories;
    7491import org.openstreetmap.josm.tools.Utils;
    7592import org.openstreetmap.josm.tools.WindowGeometry;
     
    95112     */
    96113    public MainApplication() {
    97         // Allow subclassing (see JOSM.java)
    98114        this(null);
    99115    }
     
    106122    public MainApplication(MainFrame mainFrame) {
    107123        this.mainFrame = mainFrame;
     124    }
     125
     126    @Override
     127    protected List<InitializationTask> beforeInitializationTasks() {
     128        return Arrays.asList(
     129            new InitializationTask(tr("Starting file watcher"), fileWatcher::start),
     130            new InitializationTask(tr("Executing platform startup hook"), platform::startupHook),
     131            new InitializationTask(tr("Building main menu"), this::initializeMainWindow),
     132            new InitializationTask(tr("Updating user interface"), () -> {
     133                undoRedo.addCommandQueueListener(redoUndoListener);
     134                // creating toolbar
     135                GuiHelper.runInEDTAndWait(() -> contentPanePrivate.add(toolbar.control, BorderLayout.NORTH));
     136                // help shortcut
     137                registerActionShortcut(menu.help, Shortcut.registerShortcut("system:help", tr("Help"),
     138                        KeyEvent.VK_F1, Shortcut.DIRECT));
     139            }),
     140            // This needs to be done before RightAndLefthandTraffic::initialize is called
     141            new InitializationTask(tr("Initializing internal boundaries data"), Territories::initialize)
     142        );
     143    }
     144
     145    @Override
     146    protected Collection<InitializationTask> parallelInitializationTasks() {
     147        return Arrays.asList(
     148            new InitializationTask(tr("Initializing OSM API"), () -> {
     149                    // We try to establish an API connection early, so that any API
     150                    // capabilities are already known to the editor instance. However
     151                    // if it goes wrong that's not critical at this stage.
     152                    try {
     153                        OsmApi.getOsmApi().initialize(null, true);
     154                    } catch (OsmTransferCanceledException | OsmApiInitializationException e) {
     155                        Logging.warn(Logging.getErrorMessage(Utils.getRootCause(e)));
     156                    }
     157                }),
     158            new InitializationTask(tr("Initializing internal traffic data"), RightAndLefthandTraffic::initialize),
     159            new InitializationTask(tr("Initializing validator"), OsmValidator::initialize),
     160            new InitializationTask(tr("Initializing presets"), TaggingPresets::initialize),
     161            new InitializationTask(tr("Initializing map styles"), MapPaintPreference::initialize),
     162            new InitializationTask(tr("Loading imagery preferences"), ImageryPreference::initialize)
     163        );
     164    }
     165
     166    @Override
     167    protected List<Callable<?>> asynchronousCallableTasks() {
     168        return Arrays.asList(
     169                OverpassTurboQueryWizard::getInstance
     170            );
     171    }
     172
     173    @Override
     174    protected List<Runnable> asynchronousRunnableTasks() {
     175        return Arrays.asList(
     176                TMSLayer::getCache,
     177                OsmValidator::initializeTests
     178            );
     179    }
     180
     181    @Override
     182    protected List<InitializationTask> afterInitializationTasks() {
     183        return Arrays.asList(
     184            new InitializationTask(tr("Updating user interface"), () -> GuiHelper.runInEDTAndWait(() -> {
     185                // hooks for the jmapviewer component
     186                FeatureAdapter.registerBrowserAdapter(OpenBrowser::displayUrl);
     187                FeatureAdapter.registerTranslationAdapter(I18n.getTranslationAdapter());
     188                FeatureAdapter.registerLoggingAdapter(name -> Logging.getLogger());
     189                // UI update
     190                toolbar.refreshToolbarControl();
     191                toolbar.control.updateUI();
     192                contentPanePrivate.updateUI();
     193            }))
     194        );
    108195    }
    109196
Note: See TracChangeset for help on using the changeset viewer.