1 package org.catacomb.druid.swing;
2
3 import org.catacomb.druid.dialog.Dialoguer;
4 import org.catacomb.druid.event.LabelActor;
5 import org.catacomb.druid.event.OptionsSource;
6 import org.catacomb.interlish.interact.DComponent;
7 import org.catacomb.interlish.lang.U;
8 import org.catacomb.interlish.structure.SpecialStrings;
9 import org.catacomb.interlish.structure.Updatable;
10
11 import java.awt.Color;
12 import java.awt.Component;
13 import java.awt.Font;
14 import java.awt.Graphics;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.MouseListener;
18 import java.awt.event.MouseEvent;
19
20 import javax.swing.Icon;
21 import javax.swing.JButton;
22 import javax.swing.JComponent;
23 import javax.swing.JPopupMenu;
24 import javax.swing.event.ChangeEvent;
25
26
27
28 public class DChoice extends JButton implements DComponent,
29 ActionListener, LabelActor, Icon, MouseListener {
30
31 static final long serialVersionUID = 1001;
32
33 LabelActor lact;
34
35 JPopupMenu menu;
36
37 String[] options;
38 String[] labels;
39
40 String selected;
41
42 RolloverEffect rollover;
43
44 OptionsSource optionsSource;
45 Updatable updatable;
46
47 int autoSelect;
48
49
50 String prefTooltip = null;
51
52
53 public DChoice(String[] opts, String[] labs) {
54 super("none");
55 setIcon(this);
56
57
58 setFont(new Font("sansserif", Font.PLAIN, 12));
59
60
61 rollover = new RolloverEffect(this);
62 addMouseListener(rollover);
63
64
65
66 addActionListener(this);
67 addMouseListener(this);
68 setFocusPainted(false);
69
70
71 setOptions(opts, labs);
72 autoSelect = -1;
73 }
74
75
76 public void setTooltip(String s) {
77 prefTooltip = s;
78 setToolTipText(s);
79 }
80
81
82 public void setAutoSelect(int ias) {
83 autoSelect = ias;
84 progSelect();
85 }
86
87
88 public void setBg(Color c) {
89 setBackground(c);
90 menu.setBackground(c);
91 rollover.setBg(c);
92 }
93
94
95 public void setOptionsSource(OptionsSource os) {
96 optionsSource = os;
97 }
98
99
100 public void setOptions(String[] optsin, String[] labsin) {
101 String[] opts = optsin;
102 String[] labs = labsin;
103
104
105 if (opts == null) {
106 opts = new String[0];
107 labs = new String[0];
108 }
109
110 if (labs == null || labs.length == 0) {
111 labs = new String[opts.length];
112 } else if (labs.length < opts.length) {
113 String[] olabs = labs;
114 labs = new String[opts.length];
115 for (int i = 0; i < olabs.length; i++) {
116 labs[i] = olabs[i];
117 }
118 }
119
120 for (int i = 0; i < opts.length; i++) {
121 if (labs[i] == null) {
122 labs[i] = opts[i];
123 }
124 }
125
126
127 boolean stillIn = false;
128 options = opts;
129 labels = labs;
130 menu = new JPopupMenu();
131
132 DMenuItem dminone = new DMenuItem(SpecialStrings.NONE_STRING);
133 dminone.setLabelActor(this);
134 menu.add(dminone);
135 menu.addSeparator();
136
137 for (int i = 0; i < opts.length; i++) {
138 if (opts[i].equals(selected)) {
139 stillIn = true;
140 }
141
142 DMenuItem dmi = new DMenuItem(labs[i], opts[i]);
143 dmi.setLabelActor(this);
144 menu.add(dmi);
145
146 }
147
148
149
150 if (selected != null && stillIn) {
151
152
153
154 } else {
155
156 progSelect();
157 }
158 }
159
160
161
162 private void progSelect() {
163 if (autoSelect >= 0 && options != null && options.length > autoSelect) {
164 labelAction(options[autoSelect], true);
165
166 } else {
167
168 setSelected(null);
169 }
170 }
171
172
173
174 public void checkOptions() {
175 if (updatable != null) {
176 updatable.update(0);
177 }
178
179 if (optionsSource != null) {
180 String[] sa = optionsSource.getOptions();
181 if (sa == null) {
182 sa = new String[0];
183 }
184 String[] sb = optionsSource.getLabels();
185 boolean neednew = false;
186 if (options == null || options.length != sa.length) {
187 neednew = true;
188 } else {
189 for (int i = 0; i < options.length; i++) {
190 if (options[i].equals(sa[i])) {
191
192 } else {
193 neednew = true;
194 break;
195 }
196 }
197 }
198
199 if (neednew) {
200 setOptions(sa, sb);
201 }
202 }
203 }
204
205
206
207 public void setSelected(String s) {
208
209 if (selectOne(s)) {
210
211
212 } else if (s == null || s.equals("") || s.equals("null") ||
213 s.equals(SpecialStrings.NONE_STRING)) {
214 setText(SpecialStrings.NONE_STRING);
215 selected = SpecialStrings.NONE_STRING;
216
217
218 } else {
219 checkOptions();
220 if (selectOne(s)) {
221
222 } else {
223 String msg = "Warning the selection " + s + "\n" +
224 "is no longer available. The selection will be unset -b-" +
225 "The possible values are: -b-";
226
227 for (String sopt : options) {
228 msg += sopt + "-b-";
229 }
230 Dialoguer.message(msg);
231 }
232 }
233 }
234
235
236
237 private boolean selectOne(String s) {
238 boolean ok = false;
239
240 for (int i = 0; i < options.length; i++) {
241 if (options[i].equals(s)) {
242 ok = true;
243 selected = s;
244
245 String otxt = getText();
246
247 if (U.differ(otxt, labels[i])) {
248 String sl = labels[i];
249 int sll = sl.length();
250 if (sll > 22) {
251
252 setToolTipText(sl);
253 sl = "..." + sl.substring(sll-19, sll);
254 setText(sl);
255 } else {
256
257 setToolTipText(prefTooltip);
258 setText(sl);
259 }
260 }
261
262
263 break;
264 }
265 }
266 return ok;
267 }
268
269
270
271 public String getSelected() {
272 return selected;
273 }
274
275
276
277 public void labelAction(String s, boolean b) {
278 setSelected(s);
279 deliverAction(s, true);
280 }
281
282
283
284 public void actionPerformed(ActionEvent aev) {
285
286
287 }
288
289 public void stateChanged(ChangeEvent cev) {
290
291 }
292
293
294 public void showMenu() {
295 checkOptions();
296 menu.show(this, 0, 18);
297 }
298
299
300 public String toString() {
301 String sret = "DChoice";
302 if (options != null && options.length > 0) {
303 sret += " first opt=" + options[0];
304 }
305 return sret;
306 }
307
308
309
310 public void setLabelActor(LabelActor bl) {
311 lact = bl;
312 }
313
314
315
316 public void deliverAction(String s, boolean b) {
317 if (lact != null) {
318 lact.labelAction(s, b);
319 }
320 }
321
322
323
324
325 public void paintIcon(Component c, Graphics g, int x, int y) {
326 JComponent component = (JComponent)c;
327 int iconWidth = getIconWidth();
328
329 g.translate(x, y);
330
331 g.setColor(component.isEnabled() ? Color.gray : Color.blue);
332
333 g.drawLine(2, 0, iconWidth - 1, 0);
334 g.drawLine(3, 1, 1 + (iconWidth - 3), 1);
335 g.drawLine(5, 3, 3 + (iconWidth - 7), 3);
336 g.drawLine(6, 4, 4 + (iconWidth - 9), 4);
337
338 g.translate(-x, -y);
339 }
340
341
342 public int getIconWidth() {
343 return 12;
344 }
345
346
347 public int getIconHeight() {
348 return 5;
349 }
350
351
352 public void setUpdatable(Updatable u) {
353 updatable = u;
354 }
355
356
357
358
359 public void mouseClicked(MouseEvent e) {
360 }
361
362 public void mouseReleased(MouseEvent e) {
363 }
364
365 public void mouseEntered(MouseEvent e) {
366 }
367
368 public void mouseExited(MouseEvent e) {
369 }
370
371
372 public void mousePressed(MouseEvent e) {
373 showMenu();
374 }
375
376
377 public void clearSelection() {
378 setSelected(null);
379
380 }
381
382 }