| 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 | import java.util.ArrayList; |
|---|
| 10 | import java.util.List; |
|---|
| 11 | |
|---|
| 12 | import javax.swing.ButtonModel; |
|---|
| 13 | |
|---|
| 14 | import org.openstreetmap.josm.Main; |
|---|
| 15 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 16 | |
|---|
| 17 | public 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 | } |
|---|