1 package org.catacomb.druid.swing;
2
3
4 import org.catacomb.druid.event.LabelActor;
5 import org.catacomb.druid.event.TextActor;
6 import org.catacomb.interlish.interact.DComponent;
7 import org.catacomb.report.E;
8
9 import java.awt.BorderLayout;
10 import java.awt.Color;
11
12 import javax.swing.JPanel;
13
14
15 public class DInt extends JPanel implements DComponent, LabelActor, TextActor {
16
17 static final long serialVersionUID = 1001;
18
19 public final static int LOG = 1;
20 public final static int LIN = 2;
21
22 public int scale;
23
24 int min0;
25 int max0;
26
27
28 DTextField dTextField;
29
30
31 LabelActor labelActor;
32
33 int value;
34
35 boolean rangeEditable;
36
37
38 public DInt(int val, int mn, int mx, String scl) {
39 setScale(scl);
40 rangeEditable = true;
41 setRange(mn, mx);
42
43 String sval = "" + val;
44 setLayout(new BorderLayout(4, 0));
45
46 dTextField = new DTextField(sval, 8);
47 add("West", dTextField);
48 dTextField.setLineBorder(0xc0c0c0);
49 dTextField.setTextActor(this);
50 dTextField.enableReturnEvents();
51
52
53
54
55
56 setRange(mn, mx);
57 setValue(val);
58 }
59
60
61 public void setProperties(int val, int min, int max,
62 String scale, boolean strictLims) {
63 setScale(scale);
64 setRange(min, max);
65 setValue(val);
66 if (strictLims) {
67 rangeEditable = false;
68 }
69 }
70
71 public void setTooltip(String s) {
72 setToolTipText(s);
73 }
74
75
76 private void setScale(String scl) {
77 if (scl == null || scl.startsWith("log")) {
78 scale = LOG;
79 } else if (scl.startsWith("lin")) {
80 scale = LIN;
81 } else {
82 E.error(" - unrecognized scale in DFloat " + scl);
83 scale = LOG;
84 }
85 }
86
87
88 public void setRange(int min, int max) {
89 if (rangeEditable) {
90 min0 = min;
91 max0 = max;
92 } else {
93 E.warning("set range called, but range not editable");
94 }
95 }
96
97 public void setLabel(String s) {
98
99 }
100
101
102 public void setBg(Color c) {
103
104 setBackground(c);
105 }
106
107
108 public int getValue() {
109 return value;
110 }
111
112
113 public void setValue(int i) {
114 setValue(i, null);
115 }
116
117 public void setValue(int i, Object src) {
118 value = i;
119
120 showValue();
121 }
122
123
124 public void setFromSlider(int i) {
125 value = i;
126 showValue();
127 notifyValueChange();
128 }
129
130
131 private void showValue() {
132 String stxt ="" + value;
133 dTextField.setText(stxt);
134 }
135
136
137 public void notifyValueChange() {
138 if (labelActor != null) {
139 labelActor.labelAction("value_changed", true);
140 }
141 }
142
143
144
145 public void setLabelActor(LabelActor lact) {
146 labelActor = lact;
147 }
148
149
150
151 public void setEditable(boolean b) {
152 dTextField.setEditable(b);
153 }
154
155
156 public void labelAction(String s, boolean b) {
157 String sval = dTextField.getText();
158 if (s.equals("accept_text")) {
159
160 try {
161 int i = (new Integer(sval)).intValue();
162 setValue(i);
163
164
165 notifyValueChange();
166 } catch (Exception ex) {
167 E.error(" - not a number " + sval);
168 }
169
170 } else if (s.equals("accept_double")) {
171
172
173
174
175
176 notifyValueChange();
177
178
179 } else {
180 E.error("unhandled label action in DFloat " + s);
181 }
182 }
183
184
185
186
187 public void textChanged(String stxt) {
188
189 }
190
191
192
193 public void textEntered(String txt) {
194 exportFromText();
195 }
196
197
198 public void textEdited(String txt) {
199 exportFromText();
200 }
201
202 private void exportFromText() {
203 String sval = dTextField.getText();
204 try {
205 int i = (new Integer(sval)).intValue();
206 setValue(i);
207
208 notifyValueChange();
209
210 } catch (Exception ex) {
211 E.error(" - not a number " + sval);
212 }
213
214
215 }
216
217
218 public void focusGained() {
219
220
221 }
222
223
224 public void focusLost() {
225
226
227 }
228
229 }