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("View: {0}", tr("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 if (!ExpertToggleAction.isExpert()) { 046 // #16848 (Possible to activate "Viewport following" mode when not in "Expert mode" through keyboard shortcut) 047 return; 048 } 049 toggleSelectedState(e); 050 DrawAction.VIEWPORT_FOLLOWING.put(isSelected()); 051 if (!getShortcut().getKeyText().isEmpty() && PROP_NOTIFICATION.get()) { 052 String msg = isSelected() 053 ? tr("Viewport following is enabled, press {0} to disable it", getShortcut().getKeyText()) 054 : tr("Viewport following is disabled"); 055 GuiHelper.runInEDT(() -> new Notification(msg).show()); 056 } 057 notifySelectedState(); 058 } 059 060 @Override 061 protected boolean listenToSelectionChange() { 062 return false; 063 } 064 065 @Override 066 protected void updateEnabledState() { 067 setEnabled(getLayerManager().getEditDataSet() != null); 068 } 069}