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.KeyAdapter;
07: import org.eclipse.swt.events.KeyEvent;
08: import org.eclipse.swt.widgets.Composite;
09: import org.eclipse.swt.widgets.Text;
10:
11: import fr.aliacom.common.ui.ITextField;
12: import fr.aliacom.common.ui.table.ICellEditor;
13: import fr.aliacom.common.ui.table.ICellEditorListener;
14: import fr.aliacom.form.common.IFormComponent;
15:
16: /**
17: * @author tom
18: *
19: * (C) 2001, 2003 Thomas Cataldo
20: */
21: public final class SWTText extends SWTAbstractText implements
22: ITextField, ICellEditor {
23:
24: private ArrayList listeners;
25: private IFormComponent fc;
26:
27: public SWTText(IFormComponent fc, String property, int length) {
28: this .property = property;
29: Composite parent = (Composite) fc.getNativeWidget();
30: if (fc instanceof SWTTable) {
31: this .fc = fc;
32: initAsEditor(parent);
33: } else {
34: initAsControl(parent, length);
35: }
36: }
37:
38: private void initAsEditor(Composite parent) {
39: text = new Text(parent, SWT.SINGLE);
40: listeners = new ArrayList(1);
41: text.addKeyListener(new KeyAdapter() {
42: public void keyPressed(KeyEvent e) {
43: if (e.character == SWT.CR) {
44: fireEditingStopped();
45: text.dispose();
46: }
47: }
48: });
49: }
50:
51: private void initAsControl(Composite parent, int length) {
52: text = new Text(parent, SWT.BORDER | SWT.SINGLE);
53: sizeControl(length);
54: }
55:
56: /**
57: * Used by SWTTableCursor to start cell edition
58: */
59: public Object clone() {
60: return new SWTText(fc, property, 10);
61: }
62:
63: /**
64: * @see fr.aliacom.common.ui.table.ICellEditor#addCellEditorListener(fr.aliacom.common.ui.table.ICellEditorListener)
65: */
66: public void addCellEditorListener(ICellEditorListener icel) {
67: listeners.add(icel);
68: }
69:
70: /**
71: * @see fr.aliacom.common.ui.table.ICellEditor#removeCellEditorListener(fr.aliacom.common.ui.table.ICellEditorListener)
72: */
73: public void removeCellEditorListener(ICellEditorListener icel) {
74: listeners.remove(icel);
75: }
76:
77: private void fireEditingStopped() {
78: for (int i = 0, n = listeners.size(); i < n; i++) {
79: ICellEditorListener icel = (ICellEditorListener) listeners
80: .get(i);
81: icel.editingStopped();
82: }
83: }
84:
85: }
|