source: josm/trunk/src/org/openstreetmap/josm/actions/ViewportFollowToggleAction.java @ 5241

Revision 4982, 2.1 KB checked in by stoecker, 3 months ago (diff)

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.ArrayList;
10import java.util.List;
11
12import javax.swing.ButtonModel;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.tools.Shortcut;
16
17public class ViewportFollowToggleAction extends JosmAction {
18    private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
19    private boolean selected;
20    public ViewportFollowToggleAction() {
21        super(
22                tr("Viewport Following"),
23                "viewport-follow",
24                tr("Enable/disable automatic moving of the map view to last placed node"),
25                Shortcut.registerShortcut("menu:view:viewportfollow", tr("Toggle Viewport Following"),
26                KeyEvent.VK_F, Shortcut.CTRL_SHIFT),
27                true /* register shortcut */
28        );
29        putValue("help", ht("/Action/ViewportFollowing"));
30        selected = false; 
31        notifySelectedState();
32    }
33
34    public void addButtonModel(ButtonModel model) {
35        if (model != null && !buttonModels.contains(model)) {
36            buttonModels.add(model);
37            model.setSelected(selected);
38        }
39    }
40
41    public void removeButtonModel(ButtonModel model) {
42        if (model != null && buttonModels.contains(model)) {
43            buttonModels.remove(model);
44        }
45    }
46
47    protected void notifySelectedState() {
48        for (ButtonModel model: buttonModels) {
49            if (model.isSelected() != selected) {
50                model.setSelected(selected);
51            }
52        }
53    }
54
55    protected void toggleSelectedState() {
56        selected = !selected;
57        Main.map.mapView.viewportFollowing = selected;
58        notifySelectedState();
59    }
60    public void actionPerformed(ActionEvent e) {
61        toggleSelectedState();
62    }
63
64    @Override
65    protected void updateEnabledState() {
66        setEnabled(Main.map != null && Main.main.getEditLayer() != null);
67    }
68}
Note: See TracBrowser for help on using the repository browser.