Changeset 12670 in josm


Ignore:
Timestamp:
2017-08-27T00:45:21+02:00 (3 months ago)
Author:
Don-vip
Message:

see #15182 - remove dependence on GUI for tools.PlatformHook

Location:
trunk
Files:
6 edited

Legend:

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

    r12665 r12670  
    210210    }
    211211
     212    /**
     213     * Asks user to update its version of Java.
     214     * @param updVersion target update version
     215     * @param url download URL
     216     * @param major true for a migration towards a major version of Java (8:9), false otherwise
     217     * @param eolDate the EOL/expiration date
     218     * @since 12270
     219     */
     220    public static void askUpdateJava(String updVersion, String url, String eolDate, boolean major) {
     221        ExtendedDialog ed = new ExtendedDialog(
     222                Main.parent,
     223                tr("Outdated Java version"),
     224                tr("OK"), tr("Update Java"), tr("Cancel"));
     225        // Check if the dialog has not already been permanently hidden by user
     226        if (!ed.toggleEnable("askUpdateJava"+updVersion).toggleCheckState()) {
     227            ed.setButtonIcons("ok", "java", "cancel").setCancelButton(3);
     228            ed.setMinimumSize(new Dimension(480, 300));
     229            ed.setIcon(JOptionPane.WARNING_MESSAGE);
     230            StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.",
     231                    "<b>"+System.getProperty("java.version")+"</b>")).append("<br><br>");
     232            if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !platform.isOpenJDK()) {
     233                content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
     234                        "Oracle", eolDate)).append("</b><br><br>");
     235            }
     236            content.append("<b>")
     237                   .append(major ?
     238                        tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", updVersion) :
     239                        tr("You may face critical Java bugs; we highly recommend you to update to Java {0}.", updVersion))
     240                   .append("</b><br><br>")
     241                   .append(tr("Would you like to update now ?"));
     242            ed.setContent(content.toString());
     243
     244            if (ed.showDialog().getValue() == 2) {
     245                try {
     246                    platform.openUrl(url);
     247                } catch (IOException e) {
     248                    Logging.warn(e);
     249                }
     250            }
     251        }
     252    }
     253
    212254    @Override
    213255    protected List<InitializationTask> beforeInitializationTasks() {
    214256        return Arrays.asList(
    215257            new InitializationTask(tr("Starting file watcher"), fileWatcher::start),
    216             new InitializationTask(tr("Executing platform startup hook"), platform::startupHook),
     258            new InitializationTask(tr("Executing platform startup hook"), () -> platform.startupHook(MainApplication::askUpdateJava)),
    217259            new InitializationTask(tr("Building main menu"), this::initializeMainWindow),
    218260            new InitializationTask(tr("Updating user interface"), () -> {
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r12620 r12670  
    22package org.openstreetmap.josm.tools;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.Dimension;
    74import java.awt.GraphicsEnvironment;
    85import java.io.BufferedReader;
     
    2017import java.util.List;
    2118
    22 import javax.swing.JOptionPane;
    23 
    2419import org.openstreetmap.josm.Main;
    25 import org.openstreetmap.josm.gui.ExtendedDialog;
    2620import org.openstreetmap.josm.io.CertificateAmendment.CertAmend;
    2721import org.openstreetmap.josm.tools.date.DateUtils;
     
    6155      * Reason: On OSX we need to register some callbacks with the
    6256      * OS, so we'll receive events from the system menu.
    63       */
    64     default void startupHook() {
     57     * @param callback Java expiration callback, providing GUI feedback
     58     * @since 12270 (signature)
     59      */
     60    default void startupHook(JavaExpirationCallback callback) {
    6561        // Do nothing
    6662    }
     
    246242
    247243    /**
    248      * Asks user to update its version of Java.
    249      * @param updVersion target update version
    250      * @param url download URL
    251      * @param major true for a migration towards a major version of Java (8:9), false otherwise
    252      * @param eolDate the EOL/expiration date
     244     * Called when an outdated version of Java is detected at startup.
     245     * @since 12270
     246     */
     247    @FunctionalInterface
     248    public interface JavaExpirationCallback {
     249        /**
     250         * Asks user to update its version of Java.
     251         * @param updVersion target update version
     252         * @param url download URL
     253         * @param major true for a migration towards a major version of Java (8:9), false otherwise
     254         * @param eolDate the EOL/expiration date
     255         */
     256        void askUpdateJava(String updVersion, String url, String eolDate, boolean major);
     257    }
     258
     259    /**
     260     * Checks if the running version of Java has expired, proposes to user to update it if needed.
     261     * @param callback Java expiration callback
     262     * @since 12270 (signature)
    253263     * @since 12219
    254264     */
    255     default void askUpdateJava(String updVersion, String url, String eolDate, boolean major) {
    256         ExtendedDialog ed = new ExtendedDialog(
    257                 Main.parent,
    258                 tr("Outdated Java version"),
    259                 tr("OK"), tr("Update Java"), tr("Cancel"));
    260         // Check if the dialog has not already been permanently hidden by user
    261         if (!ed.toggleEnable("askUpdateJava"+updVersion).toggleCheckState()) {
    262             ed.setButtonIcons("ok", "java", "cancel").setCancelButton(3);
    263             ed.setMinimumSize(new Dimension(480, 300));
    264             ed.setIcon(JOptionPane.WARNING_MESSAGE);
    265             StringBuilder content = new StringBuilder(tr("You are running version {0} of Java.",
    266                     "<b>"+System.getProperty("java.version")+"</b>")).append("<br><br>");
    267             if ("Sun Microsystems Inc.".equals(System.getProperty("java.vendor")) && !isOpenJDK()) {
    268                 content.append("<b>").append(tr("This version is no longer supported by {0} since {1} and is not recommended for use.",
    269                         "Oracle", eolDate)).append("</b><br><br>");
    270             }
    271             content.append("<b>")
    272                    .append(major ?
    273                         tr("JOSM will soon stop working with this version; we highly recommend you to update to Java {0}.", updVersion) :
    274                         tr("You may face critical Java bugs; we highly recommend you to update to Java {0}.", updVersion))
    275                    .append("</b><br><br>")
    276                    .append(tr("Would you like to update now ?"));
    277             ed.setContent(content.toString());
    278 
    279             if (ed.showDialog().getValue() == 2) {
    280                 try {
    281                     openUrl(url);
    282                 } catch (IOException e) {
    283                     Logging.warn(e);
    284                 }
    285             }
    286         }
    287     }
    288 
    289     /**
    290      * Checks if the running version of Java has expired, proposes to user to update it if needed.
    291      * @since 12219
    292      */
    293     default void checkExpiredJava() {
     265    default void checkExpiredJava(JavaExpirationCallback callback) {
    294266        Date expiration = Utils.getJavaExpirationDate();
    295267        if (expiration != null && expiration.before(new Date())) {
    296268            String version = Utils.getJavaLatestVersion();
    297             askUpdateJava(version != null ? version : "latest",
     269            callback.askUpdateJava(version != null ? version : "latest",
    298270                    Main.pref.get("java.update.url", "https://www.java.com/download"),
    299271                    DateUtils.getDateFormat(DateFormat.MEDIUM).format(expiration), false);
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r12643 r12670  
    4848
    4949    @Override
    50     public void startupHook() {
     50    public void startupHook(JavaExpirationCallback callback) {
    5151        // Here we register callbacks for the menu entries in the system menu and file opening through double-click
    5252        // http://openjdk.java.net/jeps/272
     
    7979            Logging.warn("Failed to register with OSX: " + ex);
    8080        }
    81         checkExpiredJava();
     81        checkExpiredJava(callback);
    8282    }
    8383
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookWindows.java

    r12620 r12670  
    183183
    184184    @Override
    185     public void startupHook() {
    186         checkExpiredJava();
     185    public void startupHook(JavaExpirationCallback callback) {
     186        checkExpiredJava(callback);
    187187    }
    188188
  • trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookOsxTest.java

    r12620 r12670  
    3737    @Test
    3838    public void testStartupHook() {
    39         hook.startupHook();
     39        hook.startupHook((a,b,c,d) -> System.out.println("callback"));
    4040    }
    4141
  • trunk/test/unit/org/openstreetmap/josm/tools/PlatformHookWindowsTest.java

    r12620 r12670  
    4444    @Test
    4545    public void testStartupHook() {
    46         hook.startupHook();
     46        hook.startupHook((a,b,c,d) -> System.out.println("callback"));
    4747    }
    4848
Note: See TracChangeset for help on using the changeset viewer.