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.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import org.xnap.commons.settings.BooleanSetting;
25
26 /***
27 * Provides a default implementation for a toggle action that monitors
28 * a {@link org.xnap.commons.settings.BooleanSetting}.
29 */
30 public abstract class AbstractToggleSettingAction extends AbstractToggleAction
31 implements PropertyChangeListener {
32
33 BooleanSetting setting;
34
35 /***
36 * Constructs a toggle action that monitors a boolean preference key.
37 *
38 * @param setting the setting to monitor
39 */
40 public AbstractToggleSettingAction(BooleanSetting setting)
41 {
42 if (setting == null) {
43 throw new IllegalArgumentException();
44 }
45
46 this.setting = setting;
47
48 setting.addPropertyChangeListener(this);
49
50
51 boolean value = setting.getValue();
52 setSelected(value);
53 toggled(value);
54 }
55
56 public void propertyChange(PropertyChangeEvent e)
57 {
58 boolean newValue = setting.getValue();
59 setSelected(newValue);
60 }
61
62 /***
63 * Updates the setting.
64 */
65 @Override
66 public void toggled(boolean selected)
67 {
68 setting.setValue(selected);
69 }
70
71 }