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