01: package fr.aliacom.form.swt.ui;
02:
03: import java.util.ArrayList;
04:
05: import org.eclipse.swt.SWT;
06: import org.eclipse.swt.events.SelectionEvent;
07: import org.eclipse.swt.events.SelectionListener;
08: import org.eclipse.swt.widgets.Combo;
09: import org.eclipse.swt.widgets.Composite;
10:
11: import fr.aliacom.bean.BeanUtils;
12: import fr.aliacom.commands.Command;
13: import fr.aliacom.common.ui.IComboBox;
14:
15: /**
16: * @author tom
17: *
18: * (C) 2001, 2003 Thomas Cataldo
19: */
20: public final class SWTComboBox implements IComboBox {
21:
22: private String valueProperty;
23: private String displayProperty;
24: private Combo combo;
25: private ArrayList al;
26: private boolean eventLock;
27: private int oldSelection;
28:
29: public SWTComboBox(Composite parent, String valueProperty,
30: String displayProperty) {
31: this .valueProperty = valueProperty;
32: this .displayProperty = displayProperty;
33: this .combo = new Combo(parent, SWT.READ_ONLY);
34: eventLock = true;
35: }
36:
37: /**
38: * @see fr.aliacom.form.common.IFormComponent#reset()
39: */
40: public void reset() {
41: }
42:
43: /**
44: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
45: */
46: public void setValueBean(Object bean) {
47: al = (ArrayList) bean;
48: String[] items = new String[al.size()];
49: for (int i = 0, n = items.length; i < n; i++) {
50: String val = (String) BeanUtils.getValue(al.get(i),
51: displayProperty);
52: items[i] = (val == null ? "Null" : val);
53: }
54: combo.setItems(items);
55: combo.select(0);
56: oldSelection = 0;
57: eventLock = false;
58: }
59:
60: /**
61: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
62: */
63: public Object getNativeWidget() {
64: return combo;
65: }
66:
67: /**
68: * @see fr.aliacom.common.ui.IComboBox#getSelectedValue()
69: */
70: public Object getSelectedValue() {
71: return BeanUtils.getValue(al.get(combo.getSelectionIndex()),
72: valueProperty);
73: }
74:
75: public void setOnSelectAction(final Command c) {
76: combo.addSelectionListener(new SelectionListener() {
77: public void widgetDefaultSelected(SelectionEvent ev) {
78: }
79:
80: public void widgetSelected(SelectionEvent ev) {
81: if (!eventLock
82: && combo.getSelectionIndex() != oldSelection) {
83: c.run();
84: oldSelection = combo.getSelectionIndex();
85: }
86: }
87: });
88: }
89: }
|