Changeset 14140 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2018-08-12T13:08:27+02:00 (6 years ago)
Author:
Don-vip
Message:

see #15229 - move Main* termination methods to lifecycle SPI + new class MainTermination

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
4 edited

Legend:

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

    r14139 r14140  
    22package org.openstreetmap.josm;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    64import java.awt.Component;
    7 import java.io.IOException;
    85import java.net.URL;
    9 import java.nio.file.InvalidPathException;
    106import java.util.Collection;
    117import java.util.Collections;
     
    1511import org.openstreetmap.josm.data.Preferences;
    1612import 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;
    2013import org.openstreetmap.josm.data.osm.DataSet;
    2114import org.openstreetmap.josm.data.osm.IPrimitive;
     
    2922import org.openstreetmap.josm.io.NetworkManager;
    3023import org.openstreetmap.josm.io.OnlineResource;
     24import org.openstreetmap.josm.spi.lifecycle.Lifecycle;
    3125import org.openstreetmap.josm.spi.preferences.Config;
    3226import org.openstreetmap.josm.spi.preferences.IUrls;
    33 import org.openstreetmap.josm.tools.ImageProvider;
    34 import org.openstreetmap.josm.tools.Logging;
    3527import org.openstreetmap.josm.tools.PlatformHook;
    3628import org.openstreetmap.josm.tools.PlatformManager;
     
    148140
    149141    /**
    150      * Should be called before the main constructor to setup some parameter stuff
    151      */
    152     public static void preConstructorInit() {
    153         // init default coordinate format
    154         ICoordinateFormat fmt = CoordinateFormatManager.getCoordinateFormat(Config.getPref().get("coordinates"));
    155         if (fmt == null) {
    156             fmt = DecimalDegreesCoordinateFormat.INSTANCE;
    157         }
    158         CoordinateFormatManager.setCoordinateFormat(fmt);
    159     }
    160 
    161     /**
    162142     * Closes JOSM and optionally terminates the Java Virtual Machine (JVM).
    163143     * @param exit If {@code true}, the JVM is terminated by running {@link System#exit} with a given return code.
     
    165145     * @return {@code true}
    166146     * @since 12636
    167      */
     147     * @deprecated Use {@link Lifecycle#exitJosm}
     148     */
     149    @Deprecated
    168150    public static boolean exitJosm(boolean exit, int exitCode) {
    169         if (Main.main != null) {
    170             Main.main.shutdown();
    171         }
    172 
    173         if (exit) {
    174             System.exit(exitCode);
    175         }
    176         return true;
    177     }
    178 
    179     /**
    180      * Shutdown JOSM.
    181      */
    182     protected void shutdown() {
    183         ImageProvider.shutdown(false);
    184         try {
    185             pref.saveDefaults();
    186         } catch (IOException | InvalidPathException ex) {
    187             Logging.log(Logging.LEVEL_WARN, tr("Failed to save default preferences."), ex);
    188         }
    189         ImageProvider.shutdown(true);
     151        return Lifecycle.exitJosm(exit, exitCode);
    190152    }
    191153
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r14139 r14140  
    8787import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
    8888import org.openstreetmap.josm.data.Version;
    89 import org.openstreetmap.josm.data.cache.JCSCacheManager;
    9089import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
    9190import org.openstreetmap.josm.data.osm.DataSet;
     
    347346        getLayerManager().addLayerChangeListener(undoRedoCleaner);
    348347        ProjectionRegistry.setboundsProvider(mainBoundsProvider);
     348        Lifecycle.setShutdownSequence(new MainTermination(this));
    349349    }
    350350
     
    409409    }
    410410
    411     @Override
    412     protected void shutdown() {
    413         try {
    414             worker.shutdown();
    415         } catch (SecurityException e) {
    416             Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    417         }
    418         JCSCacheManager.shutdown();
    419 
    420         if (mainFrame != null) {
    421             mainFrame.storeState();
    422         }
    423         if (map != null) {
    424             map.rememberToggleDialogWidth();
    425         }
    426         // Remove all layers because somebody may rely on layerRemoved events (like AutosaveTask)
    427         layerManager.resetState();
    428         super.shutdown();
    429 
    430         try {
    431             // in case the current task still hasn't finished
    432             worker.shutdownNow();
    433         } catch (SecurityException e) {
    434             Logging.log(Logging.LEVEL_ERROR, "Unable to shutdown worker", e);
    435         }
     411    /**
     412     * Returns the JOSM main frame.
     413     * @return the JOSM main frame
     414     * @since 14140
     415     */
     416    public final MainFrame getMainFrame() {
     417        return mainFrame;
    436418    }
    437419
     
    566548                        reason != null ? reason : SaveLayersDialog.Reason.EXIT)));
    567549        if (proceed) {
    568             return Main.exitJosm(exit, exitCode);
     550            return Lifecycle.exitJosm(exit, exitCode);
    569551        }
    570552        return false;
     
    980962        setupNadGridSources();
    981963        GuiHelper.translateJavaInternalMessages();
    982         preConstructorInit();
    983964
    984965        monitor.indeterminateSubTask(tr("Creating main GUI"));
  • trunk/src/org/openstreetmap/josm/gui/MainInitialization.java

    r14139 r14140  
    1414import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
    1515import org.openstreetmap.josm.data.UndoRedoHandler;
     16import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
     17import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
     18import org.openstreetmap.josm.data.coor.conversion.ICoordinateFormat;
    1619import org.openstreetmap.josm.data.validation.OsmValidator;
    1720import org.openstreetmap.josm.gui.layer.ImageryLayer;
     
    2831import org.openstreetmap.josm.spi.lifecycle.InitializationSequence;
    2932import org.openstreetmap.josm.spi.lifecycle.InitializationTask;
     33import org.openstreetmap.josm.spi.preferences.Config;
    3034import org.openstreetmap.josm.tools.I18n;
    3135import org.openstreetmap.josm.tools.Logging;
     
    5761    public List<InitializationTask> beforeInitializationTasks() {
    5862        return Arrays.asList(
     63            new InitializationTask(tr("Initializing coordinate format"), () -> {
     64                ICoordinateFormat fmt = CoordinateFormatManager.getCoordinateFormat(Config.getPref().get("coordinates"));
     65                if (fmt == null) {
     66                    fmt = DecimalDegreesCoordinateFormat.INSTANCE;
     67                }
     68                CoordinateFormatManager.setCoordinateFormat(fmt);
     69            }),
    5970            new InitializationTask(tr("Starting file watcher"), FileWatcher.getDefaultInstance()::start),
    6071            new InitializationTask(tr("Executing platform startup hook"),
  • trunk/src/org/openstreetmap/josm/spi/lifecycle/Lifecycle.java

    r14139 r14140  
    99import java.util.concurrent.Future;
    1010
     11import org.openstreetmap.josm.tools.JosmRuntimeException;
    1112import org.openstreetmap.josm.tools.Logging;
    1213import org.openstreetmap.josm.tools.Utils;
     
    2021
    2122    private static volatile InitStatusListener initStatusListener;
     23
     24    private static volatile Runnable shutdownSequence;
    2225
    2326    private Lifecycle() {
     
    3942    public static void setInitStatusListener(InitStatusListener listener) {
    4043        initStatusListener = Objects.requireNonNull(listener);
     44    }
     45
     46    /**
     47     * Gets shutdown sequence.
     48     * @return shutdown sequence
     49     * @since 14140
     50     */
     51    public static Runnable getShutdownSequence() {
     52        return shutdownSequence;
     53    }
     54
     55    /**
     56     * Sets shutdown sequence.
     57     * @param sequence shutdown sequence. Must not be null
     58     * @since 14140
     59     */
     60    public static void setShutdownSequence(Runnable sequence) {
     61        shutdownSequence = Objects.requireNonNull(sequence);
    4162    }
    4263
     
    6687            }
    6788        } catch (InterruptedException | ExecutionException ex) {
    68             throw new RuntimeException(ex);
     89            throw new JosmRuntimeException(ex);
    6990        }
    7091
     
    7798            try {
    7899                task.call();
    79             } catch (RuntimeException e) {
     100            } catch (JosmRuntimeException e) {
    80101                // Can happen if the current projection needs NTV2 grid which is not available
    81102                // In this case we want the user be able to change his projection
     
    84105        }
    85106    }
     107
     108    /**
     109     * Closes JOSM and optionally terminates the Java Virtual Machine (JVM).
     110     * @param exit If {@code true}, the JVM is terminated by running {@link System#exit} with a given return code.
     111     * @param exitCode The return code
     112     * @return {@code true}
     113     * @since 14140
     114     */
     115    public static boolean exitJosm(boolean exit, int exitCode) {
     116        if (shutdownSequence != null) {
     117            shutdownSequence.run();
     118        }
     119
     120        if (exit) {
     121            System.exit(exitCode);
     122        }
     123        return true;
     124    }
    86125}
Note: See TracChangeset for help on using the changeset viewer.