1 package org.catacomb.druid.gui.edit;
2
3
4 import java.awt.Color;
5 import java.util.ArrayList;
6
7 import org.catacomb.druid.event.LabelActor;
8 import org.catacomb.druid.event.TextActor;
9 import org.catacomb.druid.swing.DBorderLayout;
10 import org.catacomb.druid.swing.DLabel;
11 import org.catacomb.druid.swing.DPanel;
12 import org.catacomb.druid.swing.DTextField;
13 import org.catacomb.druid.swing.DValueHistory;
14 import org.catacomb.interlish.content.StringValue;
15 import org.catacomb.interlish.structure.Ablable;
16 import org.catacomb.interlish.structure.TextField;
17 import org.catacomb.interlish.structure.UseWatcher;
18 import org.catacomb.interlish.structure.Value;
19 import org.catacomb.interlish.structure.ValueWatcher;
20 import org.catacomb.report.E;
21
22
23
24
25 public class DruLabelledTextField extends DruGCPanel
26 implements TextActor, Ablable, TextField, ValueWatcher, UseWatcher, LabelActor {
27
28 static final long serialVersionUID = 1001;
29
30 DLabel dLabel;
31 DTextField dTextField;
32 DPanel dpanel;
33 StringValue stringValue;
34
35 DValueHistory dValueHistory;
36
37 String focusAction;
38
39 ArrayList<Effect> effects;
40
41 public DruLabelledTextField() {
42 this("", null, 20, false);
43 }
44
45
46 public DruLabelledTextField(String s) {
47 this("", s, s.length(), false);
48 }
49
50
51 public DruLabelledTextField(String lbl, String mn, int width, boolean withHist) {
52 super();
53
54
55
56 dTextField = new DTextField("", width);
57 setActionMethod(mn);
58
59 dLabel = new DLabel(lbl);
60 dpanel = new DPanel();
61 dpanel.setBorderLayout(4, 0);
62 dpanel.add(dLabel, DBorderLayout.WEST);
63 dpanel.add(dTextField, DBorderLayout.CENTER);
64
65 if (withHist) {
66 dValueHistory = new DValueHistory();
67 dValueHistory.setLabelActor(this);
68 dpanel.add(dValueHistory, DBorderLayout.EAST);
69 }
70
71 addSingleDComponent(dpanel);
72 dTextField.setTextActor(this);
73
74 setLineBorder(0xc0c0c0);
75 }
76
77
78 public void setBg(Color c) {
79 dpanel.setBg(c);
80 dLabel.setBg(c);
81 dTextField.setBg(c);
82 if (dValueHistory != null) {
83 dValueHistory.setBg(c);
84 }
85 super.setBg(c);
86 }
87
88
89
90 public void valueChangedBy(Value pv, Object src) {
91 if (src == this) {
92
93 } else {
94 if (stringValue == pv) {
95 if (stringValue == null) {
96 dTextField.setText("");
97 able(false);
98 } else {
99 dTextField.setText(stringValue.getString());
100
101 able(stringValue.isAble());
102 }
103 } else {
104 E.error("value changed by called with mismatched value");
105 }
106
107 }
108 }
109
110
111 public void usedBy(Value pv, Object src) {
112 if (src != this) {
113 if (dValueHistory != null) {
114 dValueHistory.checkContains(((StringValue)pv).silentGetAsString());
115 }
116 }
117 }
118
119
120
121 public void labelAction(String s, boolean b) {
122 stringValue.reportableSetString(s, this);
123 dTextField.setText(s);
124 }
125
126
127
128 public void setEditable(boolean b) {
129 dTextField.setEditable(b);
130 dTextField.setEnabled(b);
131 }
132
133
134 public void able(boolean b) {
135 dTextField.setEnabled(b);
136 }
137
138
139
140
141
142 public void setStringValue(StringValue sv) {
143 if (stringValue != null) {
144 stringValue.removeValueWatcher(this);
145 stringValue.removeUseWatcher(this);
146
147 }
148 stringValue = sv;
149 if (stringValue == null) {
150 able(false);
151
152 } else {
153 dTextField.setText(stringValue.getString());
154 stringValue.addValueWatcher(this);
155 stringValue.addUseWatcher(this);
156 able(stringValue.isAble());
157 }
158 }
159
160
161
162 public void setLineBorder(int icol) {
163 dTextField.setLineBorder(icol);
164 }
165
166
167
168 public void textChanged(String s) {
169 stringValue.reportableSetString(dTextField.getText(), this);
170 syncEffects();
171 }
172
173 public void textEntered(String s) {
174 if (hasAction()) {
175 action();
176 }
177 syncEffects();
178 }
179
180 public void textEdited(String s) {
181 stringValue.editCompleted();
182 syncEffects();
183 }
184
185 private void syncEffects() {
186 if (effects != null) {
187 for (Effect eff : effects) {
188 eff.apply(true);
189 }
190 }
191 }
192
193
194 public void setReturnAction(String action) {
195 setAction(action);
196 dTextField.enableReturnEvents();
197 }
198
199 public void setFocusAction(String faction) {
200 focusAction = faction;
201 dTextField.enableFocusEvents();
202 }
203
204 public void focusGained() {
205 if (focusAction != null) {
206 performAction(focusAction, true);
207 }
208 }
209
210
211 public void focusLost() {
212 if (focusAction != null) {
213 performAction(focusAction, false);
214 }
215 }
216
217
218 public void setEffects(ArrayList<Effect> arl) {
219 effects = arl;
220 }
221
222
223
224 }