01: package fr.aliacom.form.swt.ui;
02:
03: import java.beans.PropertyChangeEvent;
04: import java.beans.PropertyChangeListener;
05:
06: import org.eclipse.swt.SWT;
07: import org.eclipse.swt.events.SelectionAdapter;
08: import org.eclipse.swt.events.SelectionEvent;
09: import org.eclipse.swt.widgets.Button;
10: import org.eclipse.swt.widgets.Composite;
11:
12: import fr.aliacom.bean.BeanUtils;
13: import fr.aliacom.common.ui.ICheckBox;
14: import fr.aliacom.form.common.IFormComponent;
15:
16: /**
17: * @author tom
18: *
19: * (C) 2001, 2003 Thomas Cataldo
20: */
21: public final class SWTCheckBox implements ICheckBox,
22: PropertyChangeListener {
23:
24: private Button check;
25: private String property;
26: private Object bean;
27: IFormComponent fc;
28: private SelectionAdapter sl;
29: private boolean lock;
30:
31: public SWTCheckBox(IFormComponent fc, String property, String text) {
32: this .fc = fc;
33: check = new Button((Composite) fc.getNativeWidget(), SWT.CHECK);
34: check.setText(text);
35: this .property = property;
36: lock = false;
37: }
38:
39: /**
40: * @see fr.aliacom.form.common.IFormComponent#reset()
41: */
42: public void reset() {
43: }
44:
45: /**
46: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
47: */
48: public void setValueBean(Object javaBean) {
49: if (bean != null) {
50: check.removeSelectionListener(sl);
51: BeanUtils
52: .removePropertyChangeListener(bean, property, this );
53: }
54: bean = javaBean;
55: boolean value = ((Boolean) BeanUtils.getValue(bean, property))
56: .booleanValue();
57: check.setSelection(value);
58:
59: sl = new SelectionAdapter() {
60: public void widgetSelected(SelectionEvent se) {
61: if (!lock) {
62: boolean value = ((Boolean) BeanUtils.getValue(bean,
63: property)).booleanValue();
64: BeanUtils.setValue(bean, property, new Boolean(
65: !value));
66: }
67: }
68: };
69: check.addSelectionListener(sl);
70: BeanUtils.addPropertyChangeListener(bean, property, this );
71: }
72:
73: /**
74: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
75: */
76: public Object getNativeWidget() {
77: return check;
78: }
79:
80: public void propertyChange(PropertyChangeEvent evt) {
81: lock = true;
82: check.setSelection(!check.getSelection());
83: lock = false;
84: }
85:
86: }
|