1 package org.catacomb.druid.gui.edit;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.druid.swing.DCheckbox;
5 import org.catacomb.interlish.content.BooleanValue;
6 import org.catacomb.interlish.structure.*;
7 import org.catacomb.report.E;
8
9
10 import java.awt.Color;
11 import java.util.ArrayList;
12
13
14 public class DruCheckbox extends DruGCPanel
15 implements LabelActor, Toggle, Ablable, BooleanValueEditor, ValueWatcher {
16
17 static final long serialVersionUID = 1001;
18
19 ArrayList<Effect> effects;
20 DCheckbox dCheckbox;
21
22 BooleanValue booleanValue;
23
24
25 public DruCheckbox() {
26 this(null, null);
27 }
28
29
30 public DruCheckbox(String lab, String mn) {
31 super();
32 dCheckbox = new DCheckbox(lab);
33 setActionMethod(mn);
34
35 addSingleDComponent(dCheckbox);
36 dCheckbox.setLabelActor(this);
37
38 setBooleanValue(new BooleanValue());
39 }
40
41
42 public void setBooleanValue(BooleanValue bv) {
43 if (booleanValue != null) {
44 booleanValue.removeValueWatcher(this);
45 }
46 booleanValue = bv;
47 if (booleanValue == null) {
48 dCheckbox.setEnabled(false);
49 } else {
50 dCheckbox.setSelected(booleanValue.getBoolean());
51 booleanValue.addValueWatcher(this);
52 }
53
54 }
55
56
57
58 public void valueChangedBy(Value pv, Object src) {
59 if (src == this) {
60 valueChange(booleanValue.getBoolean());
61
62 } else {
63 if (booleanValue == pv) {
64 if (booleanValue == null) {
65 dCheckbox.setEnabled(false);
66 } else {
67 dCheckbox.setSelected(booleanValue.getBoolean());
68 }
69 } else {
70 E.error("value changed by called with mismatched value");
71 }
72 syncEffects();
73 }
74 }
75
76
77
78 public void postApply() {
79 dCheckbox.setMouseActor(this);
80 }
81
82
83 public void setBg(Color c) {
84 dCheckbox.setBg(c);
85 super.setBg(c);
86 }
87
88
89 public void able(boolean b) {
90 dCheckbox.setEnabled(b);
91 }
92
93
94
95 public void setInitialValue(boolean b) {
96 if (booleanValue != null) {
97 booleanValue.reportableSetBoolean(b, null);
98
99 } else {
100 E.warning("cant set state - no value holder");
101 }
102 }
103
104
105
106 public void applyState() {
107 boolean b = dCheckbox.isSelected();
108 booleanValue.reportableSetBoolean(b, this);
109
110 syncEffects();
111 }
112
113 private void syncEffects() {
114 if (effects != null) {
115 boolean b = booleanValue.getBoolean();
116
117 for (Effect eff : effects) {
118 eff.apply(b);
119 }
120 }
121
122 }
123
124
125
126
127 public void labelAction(String s, boolean b) {
128 applyState();
129 }
130
131
132 public void setEffects(ArrayList<Effect> arl) {
133 effects = arl;
134 }
135
136
137 public boolean isSelected() {
138 return booleanValue.getBoolean();
139 }
140
141
142 public void setState(boolean b) {
143 booleanValue.reportableSetBoolean(b, null);
144
145
146 }
147
148 }