| 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.awt.event.WindowAdapter; |
|---|
| 10 | import java.awt.event.WindowEvent; |
|---|
| 11 | import java.awt.event.WindowListener; |
|---|
| 12 | import java.util.ArrayList; |
|---|
| 13 | import java.util.List; |
|---|
| 14 | |
|---|
| 15 | import javax.swing.ButtonModel; |
|---|
| 16 | |
|---|
| 17 | import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager; |
|---|
| 18 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 19 | /** |
|---|
| 20 | * This action toggles the visibility of the {@see ChangesetCacheManager} dialog. |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | public class ChangesetManagerToggleAction extends JosmAction { |
|---|
| 24 | private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>(); |
|---|
| 25 | //FIXME: replace with property Action.SELECTED_KEY when migrating to |
|---|
| 26 | // Java 6 |
|---|
| 27 | private boolean selected; |
|---|
| 28 | private WindowListener changesetCacheManagerClosedHandler; |
|---|
| 29 | |
|---|
| 30 | public ChangesetManagerToggleAction() { |
|---|
| 31 | super( |
|---|
| 32 | tr("Changeset Manager"), |
|---|
| 33 | "dialogs/changeset/changesetmanager", |
|---|
| 34 | tr("Toggle visibility of Changeset Manager window"), |
|---|
| 35 | Shortcut.registerShortcut("menu:windows:changesetdialog", |
|---|
| 36 | tr("Toggle visibility of Changeset Manager window"), |
|---|
| 37 | KeyEvent.VK_C, Shortcut.ALT_CTRL), |
|---|
| 38 | true /* register shortcut */ |
|---|
| 39 | ); |
|---|
| 40 | notifySelectedState(); |
|---|
| 41 | changesetCacheManagerClosedHandler = new ChangesetCacheManagerClosedHandler(); |
|---|
| 42 | putValue("help", ht("/Dialog/ChangesetManager")); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | public void addButtonModel(ButtonModel model) { |
|---|
| 46 | if (model != null && !buttonModels.contains(model)) { |
|---|
| 47 | buttonModels.add(model); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public void removeButtonModel(ButtonModel model) { |
|---|
| 52 | if (model != null && buttonModels.contains(model)) { |
|---|
| 53 | buttonModels.remove(model); |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | protected void notifySelectedState() { |
|---|
| 58 | for (ButtonModel model: buttonModels) { |
|---|
| 59 | if (model.isSelected() != selected) { |
|---|
| 60 | model.setSelected(selected); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | protected void toggleSelectedState() { |
|---|
| 66 | selected = !selected; |
|---|
| 67 | notifySelectedState(); |
|---|
| 68 | if (selected) { |
|---|
| 69 | ChangesetCacheManager.getInstance().addWindowListener(changesetCacheManagerClosedHandler); |
|---|
| 70 | ChangesetCacheManager.getInstance().setVisible(true); |
|---|
| 71 | } else { |
|---|
| 72 | ChangesetCacheManager.getInstance().removeWindowListener(changesetCacheManagerClosedHandler); |
|---|
| 73 | ChangesetCacheManager.destroyInstance(); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public void actionPerformed(ActionEvent e) { |
|---|
| 78 | toggleSelectedState(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | private class ChangesetCacheManagerClosedHandler extends WindowAdapter { |
|---|
| 82 | @Override |
|---|
| 83 | public void windowClosed(WindowEvent e) { |
|---|
| 84 | selected = false; |
|---|
| 85 | notifySelectedState(); |
|---|
| 86 | ChangesetCacheManager.getInstance().removeWindowListener(changesetCacheManagerClosedHandler); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|