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