Changeset 12695 in josm


Ignore:
Timestamp:
2017-08-29T00:16:25+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15182 - refactor PlatformHookOsx so that all GUI actions are performed in MainApplication and only macOS-specific stuff remains in platform hook

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

Legend:

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

    r12691 r12695  
    3838import java.util.concurrent.Callable;
    3939import java.util.concurrent.ExecutorService;
     40import java.util.concurrent.Executors;
    4041import java.util.concurrent.Future;
    4142import java.util.logging.Level;
     
    6061import org.openstreetmap.josm.actions.JosmAction;
    6162import org.openstreetmap.josm.actions.OpenFileAction;
     63import org.openstreetmap.josm.actions.OpenFileAction.OpenFileTask;
    6264import org.openstreetmap.josm.actions.PreferencesAction;
    6365import org.openstreetmap.josm.actions.RestartAction;
     
    103105import org.openstreetmap.josm.io.OsmApiInitializationException;
    104106import org.openstreetmap.josm.io.OsmTransferCanceledException;
     107import org.openstreetmap.josm.io.OsmTransferException;
    105108import org.openstreetmap.josm.io.auth.CredentialsManager;
    106109import org.openstreetmap.josm.io.auth.DefaultAuthenticator;
     
    117120import org.openstreetmap.josm.tools.OsmUrlToBounds;
    118121import org.openstreetmap.josm.tools.OverpassTurboQueryWizard;
     122import org.openstreetmap.josm.tools.PlatformHook.NativeOsCallback;
    119123import org.openstreetmap.josm.tools.PlatformHookWindows;
    120124import org.openstreetmap.josm.tools.RightAndLefthandTraffic;
     
    124128import org.openstreetmap.josm.tools.bugreport.BugReport;
    125129import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
     130import org.xml.sax.SAXException;
    126131
    127132/**
     
    725730        // initialize the platform hook, and
    726731        Main.determinePlatformHook();
     732        Main.platform.setNativeOsCallback(new DefaultNativeOsCallback());
    727733        // call the really early hook before we do anything else
    728734        Main.platform.preStartupHook();
     
    11881194        }
    11891195    }
     1196
     1197    private static class DefaultNativeOsCallback implements NativeOsCallback {
     1198        @Override
     1199        public void openFiles(List<File> files) {
     1200            Executors.newSingleThreadExecutor(Utils.newThreadFactory("openFiles-%d", Thread.NORM_PRIORITY)).submit(
     1201                    new OpenFileTask(files, null) {
     1202                @Override
     1203                protected void realRun() throws SAXException, IOException, OsmTransferException {
     1204                    // Wait for JOSM startup is advanced enough to load a file
     1205                    while (Main.parent == null || !Main.parent.isVisible()) {
     1206                        try {
     1207                            Thread.sleep(25);
     1208                        } catch (InterruptedException e) {
     1209                            Logging.warn(e);
     1210                            Thread.currentThread().interrupt();
     1211                        }
     1212                    }
     1213                    super.realRun();
     1214                }
     1215            });
     1216        }
     1217
     1218        @Override
     1219        public boolean handleQuitRequest() {
     1220            return MainApplication.exitJosm(false, 0, null);
     1221        }
     1222
     1223        @Override
     1224        public void handleAbout() {
     1225            MainApplication.getMenu().about.actionPerformed(null);
     1226        }
     1227
     1228        @Override
     1229        public void handlePreferences() {
     1230            MainApplication.getMenu().preferences.actionPerformed(null);
     1231        }
     1232    }
    11901233}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHook.java

    r12676 r12695  
    272272        }
    273273    }
     274
     275    /**
     276     * Called when interfacing with native OS functions. Currently only used with macOS.
     277     * The callback must perform all GUI-related tasks associated to an OS request.
     278     * The non-GUI, platform-specific tasks, are usually performed by the {@code PlatformHook}.
     279     * @since 12695
     280     */
     281    interface NativeOsCallback {
     282        /**
     283         * macOS: Called when JOSM is asked to open a list of files.
     284         * @param files list of files to open
     285         */
     286        void openFiles(List<File> files);
     287
     288        /**
     289         * macOS: Invoked when JOSM is asked to quit.
     290         * @return {@code true} if JOSM has been closed, {@code false} if the user has cancelled the operation.
     291         */
     292        boolean handleQuitRequest();
     293
     294        /**
     295         * macOS: Called when JOSM is asked to show it's about dialog.
     296         */
     297        void handleAbout();
     298
     299        /**
     300         * macOS: Called when JOSM is asked to show it's preferences UI.
     301         */
     302        void handlePreferences();
     303    }
     304
     305    /**
     306     * Registers the native OS callback. Currently only needed for macOS.
     307     * @param callback the native OS callback
     308     * @since 12695
     309     */
     310    default void setNativeOsCallback(NativeOsCallback callback) {
     311        // To be implemented if needed
     312    }
    274313}
  • trunk/src/org/openstreetmap/josm/tools/PlatformHookOsx.java

    r12670 r12695  
    1717import java.util.Collections;
    1818import java.util.List;
    19 import java.util.concurrent.Executors;
     19import java.util.Objects;
    2020
    2121import javax.swing.UIManager;
    2222
    2323import org.openstreetmap.josm.Main;
    24 import org.openstreetmap.josm.actions.OpenFileAction.OpenFileTask;
    25 import org.openstreetmap.josm.gui.MainApplication;
    26 import org.openstreetmap.josm.io.OsmTransferException;
    27 import org.xml.sax.SAXException;
    2824
    2925/**
     
    3632
    3733    private String oSBuildNumber;
     34
     35    private NativeOsCallback osCallback;
    3836
    3937    @Override
     
    138136    }
    139137
     138    @Override
     139    public void setNativeOsCallback(NativeOsCallback callback) {
     140        osCallback = Objects.requireNonNull(callback);
     141    }
     142
    140143    @SuppressWarnings("unchecked")
    141144    @Override
     
    150153                    Object oFiles = args[0].getClass().getMethod("getFiles").invoke(args[0]);
    151154                    if (oFiles instanceof List) {
    152                         Executors.newSingleThreadExecutor(Utils.newThreadFactory("openFiles-%d", Thread.NORM_PRIORITY)).submit(
    153                                 new OpenFileTask((List<File>) oFiles, null) {
    154                             @Override
    155                             protected void realRun() throws SAXException, IOException, OsmTransferException {
    156                                 // Wait for JOSM startup is advanced enough to load a file
    157                                 while (Main.parent == null || !Main.parent.isVisible()) {
    158                                     try {
    159                                         Thread.sleep(25);
    160                                     } catch (InterruptedException e) {
    161                                         Logging.warn(e);
    162                                         Thread.currentThread().interrupt();
    163                                     }
    164                                 }
    165                                 super.realRun();
    166                             }
    167                         });
     155                        osCallback.openFiles((List<File>) oFiles);
    168156                    }
    169157                } catch (ReflectiveOperationException | SecurityException | IllegalArgumentException ex) {
     
    173161            break;
    174162        case "handleQuitRequestWith":
    175             boolean closed = MainApplication.exitJosm(false, 0, null);
     163            boolean closed = osCallback.handleQuitRequest();
    176164            if (args[1] != null) {
    177165                try {
     
    185173            break;
    186174        case "handleAbout":
    187             MainApplication.getMenu().about.actionPerformed(null);
     175            osCallback.handleAbout();
    188176            break;
    189177        case "handlePreferences":
    190             MainApplication.getMenu().preferences.actionPerformed(null);
     178            osCallback.handlePreferences();
    191179            break;
    192180        default:
Note: See TracChangeset for help on using the changeset viewer.