001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009
010import org.openstreetmap.josm.actions.mapmode.DrawAction;
011import org.openstreetmap.josm.data.preferences.BooleanProperty;
012import org.openstreetmap.josm.gui.Notification;
013import org.openstreetmap.josm.gui.util.GuiHelper;
014import org.openstreetmap.josm.tools.Shortcut;
015
016/**
017 * This action toggles automatic moving of the map view to last placed node
018 * @since 3837
019 */
020public class ViewportFollowToggleAction extends ToggleAction {
021
022    /**
023     * Defines if a notification should be displayed after enabling and disabling
024     */
025    public static final BooleanProperty PROP_NOTIFICATION = new BooleanProperty("viewportfollow.notification", true);
026
027    /**
028     * Constructs a new {@code ViewportFollowToggleAction}.
029     */
030    public ViewportFollowToggleAction() {
031        super(tr("Viewport Following"),
032              "viewport-follow",
033              tr("Enable/disable automatic moving of the map view to last placed node"),
034              Shortcut.registerShortcut("menu:view:viewportfollow", tr("Toggle Viewport Following"),
035              KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
036              true /* register shortcut */
037        );
038        setHelpId(ht("/Action/ViewportFollowing"));
039        setSelected(DrawAction.VIEWPORT_FOLLOWING.get());
040        notifySelectedState();
041    }
042
043    @Override
044    public void actionPerformed(ActionEvent e) {
045        toggleSelectedState(e);
046        DrawAction.VIEWPORT_FOLLOWING.put(isSelected());
047        if (!getShortcut().getKeyText().isEmpty() && PROP_NOTIFICATION.get()) {
048            String msg = isSelected()
049                    ? tr("Viewport following is enabled, press {0} to disable it", getShortcut().getKeyText())
050                    : tr("Viewport following is disabled");
051            GuiHelper.runInEDT(() -> new Notification(msg).show());
052        }
053        notifySelectedState();
054    }
055
056    @Override
057    protected void updateEnabledState() {
058        setEnabled(getLayerManager().getEditDataSet() != null);
059    }
060}