Changeset 14125 in josm


Ignore:
Timestamp:
2018-08-11T19:01:26+02:00 (6 years ago)
Author:
Don-vip
Message:

see #15229 - extract lifecycle Main classes to a new lifecycle SPI

Location:
trunk
Files:
6 added
4 edited

Legend:

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

    r14121 r14125  
    3636import org.openstreetmap.josm.io.NetworkManager;
    3737import org.openstreetmap.josm.io.OnlineResource;
     38import org.openstreetmap.josm.spi.lifecycle.InitializationTask;
    3839import org.openstreetmap.josm.spi.preferences.Config;
    3940import org.openstreetmap.josm.spi.preferences.IUrls;
    40 import org.openstreetmap.josm.tools.CheckParameterUtil;
    4141import org.openstreetmap.josm.tools.ImageProvider;
    4242import org.openstreetmap.josm.tools.JosmRuntimeException;
     
    8787     */
    8888    public static volatile PlatformHook platform;
    89 
    90     private static volatile InitStatusListener initListener;
    91 
    92     /**
    93      * Initialization task listener.
    94      */
    95     public interface InitStatusListener {
    96 
    97         /**
    98          * Called when an initialization task updates its status.
    99          * @param event task name
    100          * @return new status
    101          */
    102         Object updateStatus(String event);
    103 
    104         /**
    105          * Called when an initialization task completes.
    106          * @param status final status
    107          */
    108         void finish(Object status);
    109     }
    110 
    111     /**
    112      * Sets initialization task listener.
    113      * @param listener initialization task listener
    114      */
    115     public static void setInitStatusListener(InitStatusListener listener) {
    116         CheckParameterUtil.ensureParameterNotNull(listener);
    117         initListener = listener;
    118     }
    11989
    12090    /**
     
    217187    }
    218188
    219     protected static final class InitializationTask implements Callable<Void> {
    220 
    221         private final String name;
    222         private final Runnable task;
    223 
    224         /**
    225          * Constructs a new {@code InitializationTask}.
    226          * @param name translated name to be displayed to user
    227          * @param task runnable initialization task
    228          */
    229         public InitializationTask(String name, Runnable task) {
    230             this.name = name;
    231             this.task = task;
    232         }
    233 
    234         @Override
    235         public Void call() {
    236             Object status = null;
    237             if (initListener != null) {
    238                 status = initListener.updateStatus(name);
    239             }
    240             task.run();
    241             if (initListener != null) {
    242                 initListener.finish(status);
    243             }
    244             return null;
    245         }
    246     }
    247 
    248189    /**
    249190     * Replies the current selected OSM primitives, from a end-user point of view.
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r14121 r14125  
    161161import org.openstreetmap.josm.plugins.PluginHandler;
    162162import org.openstreetmap.josm.plugins.PluginInformation;
     163import org.openstreetmap.josm.spi.lifecycle.InitStatusListener;
     164import org.openstreetmap.josm.spi.lifecycle.InitializationTask;
     165import org.openstreetmap.josm.spi.lifecycle.Lifecycle;
    163166import org.openstreetmap.josm.spi.preferences.Config;
    164167import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
     
    318321        String location = Config.getUrls().getJOSMWebsite() + "/proj/" + gridFileName;
    319322        // Try to load grid file
     323        @SuppressWarnings("resource")
    320324        CachedFile cf = new CachedFile(location);
    321325        try {
     
    667671    }
    668672
     673    /**
     674     * Redirects the key inputs from {@code source} to main content pane.
     675     * @param source source component from which key inputs are redirected
     676     */
    669677    public static void redirectToMainContentPane(JComponent source) {
    670678        RedirectInputMap.redirect(source, contentPanePrivate);
     
    10451053            GuiHelper.runInEDT(() -> splash.setVisible(Config.getPref().getBoolean("draw.splashscreen", true)));
    10461054        }
    1047         Main.setInitStatusListener(new InitStatusListener() {
     1055        Lifecycle.setInitStatusListener(new InitStatusListener() {
    10481056
    10491057            @Override
  • trunk/test/unit/org/openstreetmap/josm/JOSMFixture.java

    r14120 r14125  
    151151    }
    152152
    153     @SuppressWarnings("deprecation")
    154153    private void setupGUI() {
    155154        JOSMTestRules.cleanLayerEnvironment();
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r14121 r14125  
    22package org.openstreetmap.josm;
    33
    4 import static org.junit.Assert.assertFalse;
    54import static org.junit.Assert.assertNotNull;
    6 import static org.junit.Assert.assertTrue;
    75
    86import org.junit.Rule;
    97import org.junit.Test;
    10 import org.openstreetmap.josm.Main.InitStatusListener;
    11 import org.openstreetmap.josm.Main.InitializationTask;
    128import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
    139import org.openstreetmap.josm.testutils.JOSMTestRules;
     
    3531        assertNotNull(CoordinateFormatManager.getDefaultFormat());
    3632    }
    37 
    38     private static class InitStatusListenerStub implements InitStatusListener {
    39 
    40         boolean updated;
    41         boolean finished;
    42 
    43         @Override
    44         public Object updateStatus(String event) {
    45             updated = true;
    46             return null;
    47         }
    48 
    49         @Override
    50         public void finish(Object status) {
    51             finished = true;
    52         }
    53     }
    54 
    55     /**
    56      * Unit test of {@link Main#setInitStatusListener}.
    57      */
    58     @Test
    59     public void testSetInitStatusListener() {
    60         InitStatusListenerStub listener = new InitStatusListenerStub();
    61         Main.setInitStatusListener(listener);
    62         assertFalse(listener.updated);
    63         assertFalse(listener.finished);
    64         new InitializationTask("", () -> { }).call();
    65         assertTrue(listener.updated);
    66         assertTrue(listener.finished);
    67     }
    6833}
Note: See TracChangeset for help on using the changeset viewer.