Changeset 11925 in josm


Ignore:
Timestamp:
2017-04-16T02:18:09+02:00 (6 weeks ago)
Author:
Don-vip
Message:

drop Main.MasterWindowListener (unused)

Location:
trunk
Files:
3 edited

Legend:

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

    r11921 r11925  
    77import java.awt.Component;
    88import java.awt.GraphicsEnvironment;
    9 import java.awt.Window;
    109import java.awt.event.KeyEvent;
    11 import java.awt.event.WindowAdapter;
    12 import java.awt.event.WindowEvent;
    1310import java.io.File;
    1411import java.io.IOException;
     
    11601157    }
    11611158
    1162     private static final List<WeakReference<WindowSwitchListener>> windowSwitchListeners = new ArrayList<>();
    1163 
    1164     /**
    1165      * Register a window switch listener.
    1166      *
    1167      * @param listener the listener. Ignored if <code>null</code>.
    1168      */
    1169     public static void addWindowSwitchListener(WindowSwitchListener listener) {
    1170         if (listener == null) return;
    1171         synchronized (Main.class) {
    1172             for (WeakReference<WindowSwitchListener> wr : windowSwitchListeners) {
    1173                 // already registered ? => abort
    1174                 if (wr.get() == listener) return;
    1175             }
    1176             boolean wasEmpty = windowSwitchListeners.isEmpty();
    1177             windowSwitchListeners.add(new WeakReference<>(listener));
    1178             if (wasEmpty) {
    1179                 // The following call will have no effect, when there is no window
    1180                 // at the time. Therefore, MasterWindowListener.setup() will also be
    1181                 // called, as soon as the main window is shown.
    1182                 MasterWindowListener.setup();
    1183             }
    1184         }
    1185     }
    1186 
    1187     /**
    1188      * Removes a window switch listener.
    1189      *
    1190      * @param listener the listener. Ignored if <code>null</code>.
    1191      */
    1192     public static void removeWindowSwitchListener(WindowSwitchListener listener) {
    1193         if (listener == null) return;
    1194         synchronized (Main.class) {
    1195             // remove the listener - and any other listener which got garbage
    1196             // collected in the meantime
    1197             windowSwitchListeners.removeIf(wr -> wr.get() == null || wr.get() == listener);
    1198             if (windowSwitchListeners.isEmpty()) {
    1199                 MasterWindowListener.teardown();
    1200             }
    1201         }
    1202     }
    1203 
    1204     /**
    1205      * WindowListener, that is registered on all Windows of the application.
    1206      *
    1207      * Its purpose is to notify WindowSwitchListeners, that the user switches to
    1208      * another application, e.g. a browser, or back to JOSM.
    1209      *
    1210      * When changing from JOSM to another application and back (e.g. two times
    1211      * alt+tab), the active Window within JOSM may be different.
    1212      * Therefore, we need to register listeners to <strong>all</strong> (visible)
    1213      * Windows in JOSM, and it does not suffice to monitor the one that was
    1214      * deactivated last.
    1215      *
    1216      * This class is only "active" on demand, i.e. when there is at least one
    1217      * WindowSwitchListener registered.
    1218      */
    1219     protected static class MasterWindowListener extends WindowAdapter {
    1220 
    1221         private static MasterWindowListener INSTANCE;
    1222 
    1223         /**
    1224          * Returns the unique {@code MasterWindowListener} instance.
    1225          * @return the unique {@code MasterWindowListener} instance
    1226          */
    1227         public static synchronized MasterWindowListener getInstance() {
    1228             if (INSTANCE == null) {
    1229                 INSTANCE = new MasterWindowListener();
    1230             }
    1231             return INSTANCE;
    1232         }
    1233 
    1234         /**
    1235          * Register listeners to all non-hidden windows.
    1236          *
    1237          * Windows that are created later, will be cared for in {@link #windowDeactivated(WindowEvent)}.
    1238          */
    1239         public static void setup() {
    1240             if (!windowSwitchListeners.isEmpty()) {
    1241                 for (Window w : Window.getWindows()) {
    1242                     if (w.isShowing() && !Arrays.asList(w.getWindowListeners()).contains(getInstance())) {
    1243                         w.addWindowListener(getInstance());
    1244                     }
    1245                 }
    1246             }
    1247         }
    1248 
    1249         /**
    1250          * Unregister all listeners.
    1251          */
    1252         public static void teardown() {
    1253             for (Window w : Window.getWindows()) {
    1254                 w.removeWindowListener(getInstance());
    1255             }
    1256         }
    1257 
    1258         @Override
    1259         public void windowActivated(WindowEvent e) {
    1260             if (e.getOppositeWindow() == null) { // we come from a window of a different application
    1261                 // fire WindowSwitchListeners
    1262                 synchronized (Main.class) {
    1263                     Iterator<WeakReference<WindowSwitchListener>> it = windowSwitchListeners.iterator();
    1264                     while (it.hasNext()) {
    1265                         WeakReference<WindowSwitchListener> wr = it.next();
    1266                         WindowSwitchListener listener = wr.get();
    1267                         if (listener == null) {
    1268                             it.remove();
    1269                             continue;
    1270                         }
    1271                         listener.fromOtherApplication();
    1272                     }
    1273                 }
    1274             }
    1275         }
    1276 
    1277         @Override
    1278         public void windowDeactivated(WindowEvent e) {
    1279             // set up windows that have been created in the meantime
    1280             for (Window w : Window.getWindows()) {
    1281                 if (!w.isShowing()) {
    1282                     w.removeWindowListener(getInstance());
    1283                 } else {
    1284                     if (!Arrays.asList(w.getWindowListeners()).contains(getInstance())) {
    1285                         w.addWindowListener(getInstance());
    1286                     }
    1287                 }
    1288             }
    1289             if (e.getOppositeWindow() == null) { // we go to a window of a different application
    1290                 // fire WindowSwitchListeners
    1291                 synchronized (Main.class) {
    1292                     Iterator<WeakReference<WindowSwitchListener>> it = windowSwitchListeners.iterator();
    1293                     while (it.hasNext()) {
    1294                         WeakReference<WindowSwitchListener> wr = it.next();
    1295                         WindowSwitchListener listener = wr.get();
    1296                         if (listener == null) {
    1297                             it.remove();
    1298                             continue;
    1299                         }
    1300                         listener.toOtherApplication();
    1301                     }
    1302                 }
    1303             }
    1304         }
    1305     }
    1306 
    13071159    /**
    13081160     * Registers a new {@code MapFrameListener} that will be notified of MapFrame changes.
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r11713 r11925  
    363363        });
    364364
    365         Main.MasterWindowListener.setup();
    366 
    367365        boolean maximized = Main.pref.getBoolean("gui.maximized", false);
    368366        if ((!args.hasOption(Option.NO_MAXIMIZE) && maximized) || args.hasOption(Option.MAXIMIZE)) {
  • trunk/test/unit/org/openstreetmap/josm/MainTest.java

    r11921 r11925  
    1414import org.junit.Test;
    1515import org.openstreetmap.josm.Main.DownloadParamType;
    16 import org.openstreetmap.josm.Main.MasterWindowListener;
    1716import org.openstreetmap.josm.testutils.JOSMTestRules;
    1817
     
    8281        assertNotNull(Main.toolbar);
    8382    }
    84 
    85     /**
    86      * Unit test of {@link Main.MasterWindowListener}.
    87      */
    88     @Test
    89     public void testMasterWindowListener() {
    90         MasterWindowListener.setup();
    91         MasterWindowListener.teardown();
    92         assertNotNull(MasterWindowListener.getInstance());
    93     }
    9483}
Note: See TracChangeset for help on using the changeset viewer.