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.actions.downloadtasks.DownloadNotesTask;
010import org.openstreetmap.josm.actions.downloadtasks.DownloadParams;
011import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
012import org.openstreetmap.josm.data.Bounds;
013import org.openstreetmap.josm.gui.MainApplication;
014import org.openstreetmap.josm.io.NetworkManager;
015import org.openstreetmap.josm.io.OnlineResource;
016
017/**
018 * Action that downloads the notes within the current view from the server.
019 *
020 * No interaction is required.
021 */
022public final class DownloadNotesInViewAction extends JosmAction {
023
024    private DownloadNotesInViewAction(String iconName) {
025        super(tr("Download notes in current view"), iconName, tr("Download notes in current view"), null, false,
026                "dialogs/notes/download_in_view", true);
027    }
028
029    /**
030     * Constructs a new {@code DownloadNotesInViewAction} with note icon.
031     * @return a new {@code DownloadNotesInViewAction} with note icon
032     */
033    public static DownloadNotesInViewAction newActionWithNoteIcon() {
034        return new DownloadNotesInViewAction("dialogs/notes/note_open");
035    }
036
037    /**
038     * Constructs a new {@code DownloadNotesInViewAction} with download icon.
039     * @return a new {@code DownloadNotesInViewAction} with download icon
040     */
041    public static DownloadNotesInViewAction newActionWithDownloadIcon() {
042        return new DownloadNotesInViewAction("download");
043    }
044
045    @Override
046    public void actionPerformed(ActionEvent e) {
047        final Bounds bounds = MainApplication.getMap().mapView.getRealBounds();
048        DownloadNotesTask task = new DownloadNotesTask();
049        task.setZoomAfterDownload(false);
050        Future<?> future = task.download(new DownloadParams(), bounds, null);
051        MainApplication.worker.submit(new PostDownloadHandler(task, future));
052    }
053
054    @Override
055    protected void updateEnabledState() {
056        setEnabled(getLayerManager().getActiveLayer() != null
057                && !NetworkManager.isOffline(OnlineResource.OSM_API));
058    }
059}