| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.JOptionPane;
|
|---|
| 11 | import org.openstreetmap.josm.actions.mapmode.DrawAction;
|
|---|
| 12 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
|---|
| 13 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 14 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 15 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * This action toggles automatic moving of the map view to last placed node
|
|---|
| 19 | * @since 3837
|
|---|
| 20 | */
|
|---|
| 21 | public class ViewportFollowToggleAction extends ToggleAction {
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Defines if a notification should be displayed after enabling and disabling
|
|---|
| 25 | */
|
|---|
| 26 | public static final BooleanProperty PROP_NOTIFICATION = new BooleanProperty("viewportfollow.notification", true);
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Constructs a new {@code ViewportFollowToggleAction}.
|
|---|
| 30 | */
|
|---|
| 31 | public ViewportFollowToggleAction() {
|
|---|
| 32 | super(tr("Viewport Following"),
|
|---|
| 33 | "viewport-follow",
|
|---|
| 34 | tr("Enable/disable automatic moving of the map view to last placed node"),
|
|---|
| 35 | Shortcut.registerShortcut("menu:view:viewportfollow", tr("View: {0}", tr("Viewport Following")),
|
|---|
| 36 | KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
|
|---|
| 37 | true /* register shortcut */
|
|---|
| 38 | );
|
|---|
| 39 | setHelpId(ht("/Action/ViewportFollowing"));
|
|---|
| 40 | setSelected(DrawAction.VIEWPORT_FOLLOWING.get());
|
|---|
| 41 | notifySelectedState();
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | @Override
|
|---|
| 45 | public void actionPerformed(ActionEvent e) {
|
|---|
| 46 | if (!ExpertToggleAction.isExpert()) {
|
|---|
| 47 | // #16848 (Possible to activate "Viewport following" mode when not in "Expert mode" through keyboard shortcut)
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 | toggleSelectedState(e);
|
|---|
| 51 | DrawAction.VIEWPORT_FOLLOWING.put(isSelected());
|
|---|
| 52 | if (!getShortcut().getKeyText().isEmpty() && PROP_NOTIFICATION.get()) {
|
|---|
| 53 | String msg = isSelected()
|
|---|
| 54 | ? tr("Viewport following is enabled, press {0} to disable it", getShortcut().getKeyText())
|
|---|
| 55 | : tr("Viewport following is disabled");
|
|---|
| 56 | GuiHelper.runInEDT(() -> new Notification(msg).setIcon(JOptionPane.INFORMATION_MESSAGE).show());
|
|---|
| 57 | }
|
|---|
| 58 | notifySelectedState();
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | @Override
|
|---|
| 62 | protected boolean listenToSelectionChange() {
|
|---|
| 63 | return false;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @Override
|
|---|
| 67 | protected void updateEnabledState() {
|
|---|
| 68 | setEnabled(getLayerManager().getEditDataSet() != null);
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|