001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.util.concurrent.Future;
008
009import org.openstreetmap.josm.Main;
010import org.openstreetmap.josm.actions.downloadtasks.DownloadNotesTask;
011import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
012import org.openstreetmap.josm.data.Bounds;
013import org.openstreetmap.josm.io.OnlineResource;
014
015/**
016 * Action that downloads the notes within the current view from the server.
017 *
018 * No interaction is required.
019 */
020public final class DownloadNotesInViewAction extends JosmAction {
021
022    private DownloadNotesInViewAction(String iconName) {
023        super(tr("Download notes in current view"), iconName, tr("Download notes in current view"), null, false,
024                "dialogs/notes/download_in_view", true);
025    }
026
027    public static DownloadNotesInViewAction newActionWithNoteIcon() {
028        return new DownloadNotesInViewAction("dialogs/notes/note_open");
029    }
030
031    public static DownloadNotesInViewAction newActionWithDownloadIcon() {
032        return new DownloadNotesInViewAction("download");
033    }
034
035    @Override
036    public void actionPerformed(ActionEvent e) {
037        final Bounds bounds = Main.map.mapView.getRealBounds();
038        DownloadNotesTask task = new DownloadNotesTask();
039        Future<?> future = task.download(false, bounds, null);
040        Main.worker.submit(new PostDownloadHandler(task, future));
041    }
042
043    @Override
044    protected void updateEnabledState() {
045        setEnabled(Main.getLayerManager().getActiveLayer() != null
046                && !Main.isOffline(OnlineResource.OSM_API));
047    }
048}