Changeset 12804 in josm


Ignore:
Timestamp:
2017-09-09T16:58:45+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15229 - see #15182 - remove GUI references from OsmApi

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

Legend:

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

    r12803 r12804  
    9999import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    100100import org.openstreetmap.josm.gui.layer.AutosaveTask;
     101import org.openstreetmap.josm.gui.layer.ImageryLayer;
    101102import org.openstreetmap.josm.gui.layer.Layer;
    102103import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
     
    387388        return Arrays.asList(
    388389            new InitializationTask(tr("Initializing OSM API"), () -> {
     390                    OsmApi.addOsmApiInitializationListener(api -> {
     391                        // This checks if there are any layers currently displayed that are now on the blacklist, and removes them.
     392                        // This is a rare situation - probably only occurs if the user changes the API URL in the preferences menu.
     393                        // Otherwise they would not have been able to load the layers in the first place because they would have been disabled
     394                        if (isDisplayingMapView()) {
     395                            for (Layer l : getLayerManager().getLayersOfType(ImageryLayer.class)) {
     396                                if (((ImageryLayer) l).getInfo().isBlacklisted()) {
     397                                    Logging.info(tr("Removed layer {0} because it is not allowed by the configured API.", l.getName()));
     398                                    getLayerManager().removeLayer(l);
     399                                }
     400                            }
     401                        }
     402                    });
    389403                    // We try to establish an API connection early, so that any API
    390404                    // capabilities are already known to the editor instance. However
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r12636 r12804  
    3030import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3131import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    32 import org.openstreetmap.josm.gui.MainApplication;
    33 import org.openstreetmap.josm.gui.layer.ImageryLayer;
    34 import org.openstreetmap.josm.gui.layer.Layer;
    3532import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3633import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    3835import org.openstreetmap.josm.tools.CheckParameterUtil;
    3936import org.openstreetmap.josm.tools.HttpClient;
     37import org.openstreetmap.josm.tools.ListenerList;
    4038import org.openstreetmap.josm.tools.Logging;
    4139import org.openstreetmap.josm.tools.Utils;
     
    5250 * It is conceivable to extract this into an interface later and create various
    5351 * classes implementing the interface, to be able to talk to various kinds of servers.
    54  *
     52 * @ince 1523
    5553 */
    5654public class OsmApi extends OsmConnection {
     
    7674
    7775    // The collection of instantiated OSM APIs
    78     private static Map<String, OsmApi> instances = new HashMap<>();
     76    private static final Map<String, OsmApi> instances = new HashMap<>();
     77
     78    private static final ListenerList<OsmApiInitializationListener> listeners = ListenerList.create();
    7979
    8080    private URL url;
     81
     82    /**
     83     * OSM API initialization listener.
     84     * @since 12804
     85     */
     86    public interface OsmApiInitializationListener {
     87        /**
     88         * Called when an OSM API instance has been successfully initialized.
     89         * @param instance the initialized OSM API instance
     90         */
     91        void apiInitialized(OsmApi instance);
     92    }
     93
     94    /**
     95     * Adds a new OSM API initialization listener.
     96     * @param listener OSM API initialization listener to add
     97     * @since 12804
     98     */
     99    public static void addOsmApiInitializationListener(OsmApiInitializationListener listener) {
     100        listeners.addListener(listener);
     101    }
     102
     103    /**
     104     * Removes an OSM API initialization listener.
     105     * @param listener OSM API initialization listener to remove
     106     * @since 12804
     107     */
     108    public static void removeOsmApiInitializationListener(OsmApiInitializationListener listener) {
     109        listeners.removeListener(listener);
     110    }
    81111
    82112    /**
     
    236266            }
    237267
    238             /* This checks if there are any layers currently displayed that
    239              * are now on the blacklist, and removes them. This is a rare
    240              * situation - probably only occurs if the user changes the API URL
    241              * in the preferences menu. Otherwise they would not have been able
    242              * to load the layers in the first place because they would have
    243              * been disabled! */
    244             if (MainApplication.isDisplayingMapView()) {
    245                 for (Layer l : MainApplication.getLayerManager().getLayersOfType(ImageryLayer.class)) {
    246                     if (((ImageryLayer) l).getInfo().isBlacklisted()) {
    247                         Logging.info(tr("Removed layer {0} because it is not allowed by the configured API.", l.getName()));
    248                         MainApplication.getLayerManager().removeLayer(l);
    249                     }
    250                 }
    251             }
    252 
     268            listeners.fireEvent(l -> l.apiInitialized(this));
    253269        } catch (OsmTransferCanceledException e) {
    254270            throw e;
Note: See TracChangeset for help on using the changeset viewer.