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