1 package org.catacomb.druid.gui.edit;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.druid.event.OptionsSource;
5 import org.catacomb.druid.swing.DBorderLayout;
6 import org.catacomb.druid.swing.DChoice;
7 import org.catacomb.druid.swing.DLabel;
8 import org.catacomb.interlish.content.StringValue;
9 import org.catacomb.interlish.structure.*;
10 import org.catacomb.report.E;
11
12
13 import java.awt.Color;
14 import java.util.ArrayList;
15
16
17 public class DruChoice extends DruGCPanel
18 implements LabelActor, Ablable, Choice, StringValueEditor, ValueWatcher {
19
20 static final long serialVersionUID = 1001;
21
22 DLabel dLabel;
23 DChoice dChoice;
24
25
26 StringValue stringValue;
27
28 ArrayList<Effect> effects;
29
30
31 public DruChoice(String[] opts, String mnm) {
32 this(null, opts, opts, mnm);
33 }
34
35
36
37
38 public DruChoice(String label, String mnm) {
39 this(label, null, null, mnm);
40 }
41
42
43
44
45 public DruChoice(String label, String[] opts, String[] labs, String mnm) {
46 setActionMethod(mnm);
47
48
49 dChoice = new DChoice(opts, labs);
50
51
52 if (label != null && label.length() > 0) {
53 setBorderLayout(4, 4);
54 dLabel = new DLabel(label);
55
56 addDComponent(dLabel, DBorderLayout.WEST);
57 addDComponent(dChoice, DBorderLayout.CENTER);
58
59 } else {
60
61 addSingleDComponent(dChoice);
62 }
63
64 setStringValue(new StringValue());
65 dChoice.setLabelActor(this);
66 }
67
68 public void setEffects(ArrayList<Effect> arl) {
69 effects = arl;
70 }
71
72
73 public void setStringValue(StringValue bv) {
74 if (stringValue != null) {
75 stringValue.removeValueWatcher(this);
76 }
77 stringValue = bv;
78 if (stringValue == null) {
79 dChoice.setEnabled(false);
80 applyState(null);
81 } else {
82 String s = stringValue.getString();
83 dChoice.setSelected(s);
84 applyState(s);
85 dChoice.setEnabled(stringValue.isAble());
86 stringValue.addValueWatcher(this);
87 }
88 }
89
90 private void applyState(String s) {
91 if (effects != null) {
92 for (Effect eff : effects) {
93 eff.apply(s);
94 }
95 }
96 }
97
98
99 public void valueChangedBy(Value pv, Object src) {
100 if (src == this) {
101 valueChange(stringValue.getString());
102
103 } else {
104 if (stringValue == pv) {
105 if (stringValue == null) {
106 able(false);
107 } else {
108 dChoice.setSelected(stringValue.getString());
109 able(stringValue.isAble());
110 }
111 } else {
112 E.error("value changed by called with mismatched value");
113 }
114 }
115 }
116
117
118
119
120 public void unselect() {
121 stringValue.reportableSetString(null, this);
122 }
123
124 public void setUpdatable(Updatable u) {
125 dChoice.setUpdatable(u);
126 }
127
128
129 public void setAutoSelect(int ias) {
130 dChoice.setAutoSelect(ias);
131 }
132
133
134
135 public void setOptionsSource(OptionsSource os) {
136 dChoice.setOptionsSource(os);
137 }
138
139
140 public void setSelected(String s) {
141 dChoice.setSelected(s);
142 }
143
144
145 public String getSelected() {
146 return dChoice.getSelected();
147 }
148
149
150 public void setBg(Color c) {
151 dChoice.setBg(c);
152 super.setBg(c);
153 }
154
155
156
157 public void able(boolean b) {
158 dChoice.setEnabled(b);
159 if (dLabel != null) {
160 dLabel.setEnabled(b);
161 }
162 }
163
164
165
166 public void setTooltip(String s) {
167 dChoice.setTooltip(s);
168 }
169
170
171 public void setOptions(String[] sa) {
172 setOptions(sa, sa);
173 }
174
175
176 public void setOptions(String[] sa, String[] sl) {
177 dChoice.setOptions(sa, sl);
178 }
179
180
181 public void updateOptions() {
182 dChoice.checkOptions();
183 }
184
185
186 public void labelAction(String s, boolean b) {
187 applyState(s);
188 if (stringValue != null) {
189 stringValue.reportableSetString(dChoice.getSelected(), this);
190 }
191 }
192
193
194
195
196 public void clearSelection() {
197 dChoice.clearSelection();
198
199 }
200
201
202
203 }