source: src/main/java/genius/gui/panels/BooleanModel.java@ 345

Last change on this file since 345 was 93, checked in by Wouter Pasman, 6 years ago

#28 fixed listener structures. Added Lockable interface, boolean model and integer model are now lockable. Slider now reflects state of lockable. Added test for that.

File size: 1.3 KB
Line 
1package genius.gui.panels;
2
3import genius.core.listener.DefaultListenable;
4
5/**
6 * Stores a listen-able boolean value. Listeners always hear the current value,
7 * not the lock setting.
8 *
9 */
10public class BooleanModel extends DefaultListenable<Boolean>
11 implements Lockable {
12 private boolean value = false;
13 private boolean lock = false;
14
15 /**
16 *
17 * @param b
18 * the initial value for the boolean
19 */
20 public BooleanModel(boolean b) {
21 this.value = b;
22 }
23
24 /**
25 * @param newValue
26 * the new value. Check {@link #isLocked()} before attempting
27 * this
28 * @throws IllegalStateException
29 * if object is locked.
30 */
31 public void setValue(boolean newValue) {
32 if (lock) {
33 throw new IllegalStateException("Value is locked");
34 }
35 if (value != newValue) {
36 value = newValue;
37 notifyChange(value);
38 }
39 }
40
41 public boolean getValue() {
42 return value;
43 }
44
45 /**
46 *
47 * @param isLock
48 * if true, the value can not be changed. The GUI should also
49 * reflect this setting eg by greying out the item.
50 */
51 @Override
52 public void setLock(boolean isLock) {
53 lock = isLock;
54 notifyChange(value);
55 }
56
57 /**
58 * @return true if the value is locked and can't be changed.
59 */
60 @Override
61 public boolean isLocked() {
62 return lock;
63 }
64}
Note: See TracBrowser for help on using the repository browser.