1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.action;
21
22 import java.awt.event.ActionEvent;
23 import javax.swing.AbstractButton;
24
25 /***
26 * Provides a default implementation for toggeable <code>Action</code> objects.
27 */
28 public abstract class AbstractToggleAction extends AbstractXNapAction
29 implements ToggleAction {
30
31 public AbstractToggleAction(boolean selected)
32 {
33 putValue(ToggleAction.SELECTED, new Boolean(selected));
34 }
35
36 /***
37 * Constructor an AbstractToggleAction that is not selected by default.
38 */
39 public AbstractToggleAction()
40 {
41 this(false);
42 }
43
44 /***
45 * Returns the state of the action.
46 *
47 * @return true, if the action is currently in active state, e.g. selected
48 * in case of a {@link javax.swing.JToggleButton}
49 */
50 public boolean isSelected()
51 {
52 return ((Boolean)getValue(ToggleAction.SELECTED)).booleanValue();
53 }
54
55 public void actionPerformed(ActionEvent event)
56 {
57 if (event.getSource() instanceof AbstractButton) {
58 AbstractButton ab = (AbstractButton)event.getSource();
59 setSelected(ab.isSelected());
60 }
61 }
62
63 /***
64 * Invoked when the selection changes.
65 */
66 public abstract void toggled(boolean selected);
67
68 /***
69 * Sets the state of the action.
70 *
71 * @param newValue if true, the action is set to active state
72 */
73 public void setSelected(boolean newValue)
74 {
75 if (newValue != isSelected()) {
76 putValue(ToggleAction.SELECTED, new Boolean(newValue));
77 toggled(newValue);
78 }
79 }
80
81 }