1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import javax.swing.ButtonModel;
|
---|
8 | import javax.swing.Icon;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Abtract class for Toggle Actions.
|
---|
14 | * @since 6220
|
---|
15 | */
|
---|
16 | public abstract class ToggleAction extends JosmAction {
|
---|
17 |
|
---|
18 | private final List<ButtonModel> buttonModels = new ArrayList<ButtonModel>();
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a {@code ToggleAction}.
|
---|
22 | *
|
---|
23 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
24 | * @param icon the icon to use
|
---|
25 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
26 | * that html is not supported for menu actions on some platforms.
|
---|
27 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
28 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
29 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
30 | * the user CANNOT configure a shortcut for your action.
|
---|
31 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
32 | * @param toolbarId identifier for the toolbar preferences. The iconName is used, if this parameter is null
|
---|
33 | * @param installAdapters false, if you don't want to install layer changed and selection changed adapters
|
---|
34 | */
|
---|
35 | public ToggleAction(String name, Icon icon, String tooltip, Shortcut shortcut, boolean registerInToolbar, String toolbarId, boolean installAdapters) {
|
---|
36 | super(name, icon, tooltip, shortcut, registerInToolbar, toolbarId, installAdapters);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a {@code ToggleAction}.
|
---|
41 | *
|
---|
42 | * @param name the action's text as displayed on the menu (if it is added to a menu)
|
---|
43 | * @param iconName the name of icon to use
|
---|
44 | * @param tooltip a longer description of the action that will be displayed in the tooltip. Please note
|
---|
45 | * that html is not supported for menu actions on some platforms.
|
---|
46 | * @param shortcut a ready-created shortcut object or null if you don't want a shortcut. But you always
|
---|
47 | * do want a shortcut, remember you can always register it with group=none, so you
|
---|
48 | * won't be assigned a shortcut unless the user configures one. If you pass null here,
|
---|
49 | * the user CANNOT configure a shortcut for your action.
|
---|
50 | * @param registerInToolbar register this action for the toolbar preferences?
|
---|
51 | */
|
---|
52 | public ToggleAction(String name, String iconName, String tooltip, Shortcut shortcut, boolean registerInToolbar) {
|
---|
53 | super(name, iconName, tooltip, shortcut, registerInToolbar);
|
---|
54 | }
|
---|
55 |
|
---|
56 | protected final void setSelected(boolean selected) {
|
---|
57 | putValue(SELECTED_KEY, selected);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Determines if this action is currently being selected.
|
---|
62 | * @return {@code true} if this action is currently being selected, {@code false} otherwise
|
---|
63 | */
|
---|
64 | public final boolean isSelected() {
|
---|
65 | Object selected = getValue(SELECTED_KEY);
|
---|
66 | if (selected instanceof Boolean) {
|
---|
67 | return (Boolean) selected;
|
---|
68 | } else {
|
---|
69 | return false;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Adds a button model
|
---|
75 | * @param model The button model to add
|
---|
76 | */
|
---|
77 | public final void addButtonModel(ButtonModel model) {
|
---|
78 | if (model != null && !buttonModels.contains(model)) {
|
---|
79 | buttonModels.add(model);
|
---|
80 | model.setSelected(isSelected());
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Removes a button model
|
---|
86 | * @param model The button model to remove
|
---|
87 | */
|
---|
88 | public final void removeButtonModel(ButtonModel model) {
|
---|
89 | if (model != null && buttonModels.contains(model)) {
|
---|
90 | buttonModels.remove(model);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | protected void notifySelectedState() {
|
---|
95 | boolean selected = isSelected();
|
---|
96 | for (ButtonModel model: buttonModels) {
|
---|
97 | if (model.isSelected() != selected) {
|
---|
98 | model.setSelected(selected);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected final void toggleSelectedState() {
|
---|
104 | setSelected(!isSelected());
|
---|
105 | }
|
---|
106 | }
|
---|