1 package org.catacomb.druid.swing;
2
3 import org.catacomb.druid.event.LabelActor;
4 import org.catacomb.druid.event.TextActor;
5 import org.catacomb.interlish.interact.DComponent;
6
7 import java.awt.Color;
8 import java.awt.event.FocusEvent;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.KeyListener;
11 import java.awt.event.FocusListener;
12
13 import javax.swing.BorderFactory;
14 import javax.swing.JTextField;
15 import javax.swing.event.DocumentEvent;
16 import javax.swing.event.DocumentListener;
17
18
19 public class DTextField extends DPanel implements DComponent,
20 DocumentListener, KeyListener, FocusListener {
21
22 static final long serialVersionUID = 1001;
23
24 LabelActor labelActor;
25 TextActor textActor;
26
27 String tag;
28 boolean ignoreEvents = false;
29
30 JTextField jTextField;
31
32 boolean returnEvents;
33
34 boolean reportFocus = false;
35
36 boolean changedWhileFocused;
37
38 public DTextField(String sid, int w) {
39 super();
40 tag = sid;
41 jTextField = new JTextField(w);
42
43 jTextField.setBorder(BorderUtil.makeEmptyBorder(2));
44
45 setSingle();
46 add(jTextField);
47
48
49 jTextField.getDocument().addDocumentListener(this);
50
51 returnEvents = false;
52 jTextField.addFocusListener(this);
53 }
54
55
56 public void enableReturnEvents() {
57 if (returnEvents) {
58
59 } else {
60 returnEvents = true;
61 jTextField.addKeyListener(this);
62 }
63 }
64
65
66 public void enableFocusEvents() {
67 reportFocus = true;
68 }
69
70 protected JTextField getJTextField() {
71 return jTextField;
72 }
73
74
75 public String getTag() {
76 return tag;
77 }
78
79
80 public void setText(String s) {
81
82 ignoreEvents = true;
83 jTextField.setText(s);
84
85 ignoreEvents = false;
86 }
87
88
89 public void setEditable(boolean b) {
90 jTextField.setEditable(b);
91 }
92
93 public void setEnabled(boolean b) {
94 jTextField.setEnabled(b);
95 if (b) {
96 jTextField.setBackground(Color.white);
97 } else {
98 jTextField.setBackground(new Color(236, 236, 236));
99 }
100 }
101
102
103 public void setLabelActor(LabelActor bl) {
104 labelActor = bl;
105 }
106
107
108 public void setTextActor(TextActor tl) {
109 textActor = tl;
110 }
111
112
113 public void changedUpdate(DocumentEvent d) {
114 flagChange();
115 }
116
117
118 public void insertUpdate(DocumentEvent d) {
119 flagChange();
120 }
121
122
123 public void removeUpdate(DocumentEvent d) {
124 flagChange();
125 }
126
127
128 public void flagChange() {
129 if (ignoreEvents) {
130
131 } else {
132 changedWhileFocused = true;
133
134 if (textActor != null) {
135 textActor.textChanged(getText());
136 }
137 }
138 }
139
140
141
142 public String getText() {
143 return jTextField.getText();
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 protected void reportAction() {
161 if (labelActor != null) {
162 labelActor.labelAction(getText(), true);
163 }
164 }
165
166
167 public void keyTyped(KeyEvent e) {
168 }
169
170
171 public void keyPressed(KeyEvent e) {
172 if (e.getKeyCode() == KeyEvent.VK_ENTER) {
173 if (labelActor != null) {
174 labelActor.labelAction("return_pressed", true);
175 }
176
177 if (textActor != null) {
178 textActor.textEntered(getText());
179 }
180 }
181 }
182
183
184 public void keyReleased(KeyEvent e) {
185 }
186
187
188 public void setLineBorder(int icol) {
189 setBorder(BorderFactory.createLineBorder(new Color(icol)));
190 }
191
192
193 public void focusGained(FocusEvent e) {
194 changedWhileFocused = false;
195 if (reportFocus) {
196 textActor.focusGained();
197 }
198 }
199
200
201 public void focusLost(FocusEvent e) {
202 if (changedWhileFocused) {
203 if (ignoreEvents) {
204
205 } else if (textActor != null) {
206 textActor.textEdited(getText());
207
208 }
209 }
210 if (reportFocus) {
211 textActor.focusLost();
212 }
213
214 }
215
216
217 }