1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.ui.table.editor;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.FocusEvent;
15 import org.eclipse.swt.events.FocusListener;
16 import org.eclipse.swt.events.KeyEvent;
17 import org.eclipse.swt.events.KeyListener;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Text;
21
22 import de.jaret.util.ui.table.JaretTable;
23 import de.jaret.util.ui.table.model.IColumn;
24 import de.jaret.util.ui.table.model.IRow;
25
26 /***
27 * Cell Editor for editing strings. Supports single and multiple line edits. For multiple line usage there are several
28 * options:
29 * <ul>
30 * <li>resize: when true the input will be growing with the input text up to maxRows rows</li>
31 * <li>maxRows: max height of input filed when resizing</li>
32 * </ul>
33 * <p>
34 * Key bindings: CR, TAB: accept input and leave, ALT+CR insert CR, ESC leave and reset to value when starting editing
35 * </p>
36 *
37 * @author Peter Kliem
38 * @version $Id: TextCellEditor.java 394 2007-05-01 10:51:25Z olk $
39 */
40 public class TextCellEditor extends CellEditorBase implements ICellEditor, FocusListener {
41 protected boolean _multi = true;
42
43 /*** control used for editing. */
44 protected Text _text;
45
46 private String _oldVal;
47
48 private int _maxrows = 6;
49
50 public TextCellEditor(boolean multi) {
51 _multi = multi;
52 }
53
54 protected String convertValue(IRow row, IColumn column) {
55 Object value = column.getValue(row);
56 return value != null ? value.toString() : null;
57 }
58
59 protected void storeValue(IRow row, IColumn column) {
60 String value = _text.getText();
61 _column.setValue(_row, value);
62 }
63
64 /***
65 * Create the control to be used when editing.
66 *
67 * @param table table is the parent control
68 */
69 private void createControl(JaretTable table) {
70 if (_text == null) {
71 _table = table;
72 if (!_multi) {
73 _text = new Text(table, SWT.BORDER);
74 } else {
75 _text = new Text(table, SWT.BORDER | SWT.MULTI | SWT.WRAP);
76 }
77 _text.addKeyListener(new KeyListener() {
78
79 public void keyPressed(KeyEvent event) {
80 if ((event.stateMask & SWT.ALT) != 0 && event.keyCode == SWT.CR) {
81 event.doit = false;
82 _text.insert("\n");
83 } else if (event.keyCode == SWT.TAB) {
84 event.doit = false;
85 stopEditing(true);
86 _table.forceFocus();
87 _table.focusRight();
88 } else if (event.keyCode == SWT.CR) {
89 event.doit = false;
90 stopEditing(true);
91 _table.forceFocus();
92 _table.focusDown();
93 } else if (event.keyCode == SWT.ESC) {
94 event.doit = false;
95 stopEditing(false);
96 _column.setValue(_row, _oldVal);
97 _table.forceFocus();
98 } else {
99 if (_multi) {
100
101
102
103
104
105 if (true || _text.getLineCount() * _text.getLineHeight() < _text.getSize().y) {
106 Point newSize = new Point(_text.getSize().x, getPreferredHeight());
107 _text.setSize(newSize);
108 }
109 }
110 }
111
112 }
113
114 public void keyReleased(KeyEvent arg0) {
115 }
116
117 });
118
119 _text.addFocusListener(this);
120 }
121 }
122
123 /***
124 * {@inheritDoc}
125 */
126 public Control getEditorControl(JaretTable table, IRow row, IColumn column, char typedKey) {
127 super.getEditorControl(table, row, column, typedKey);
128 createControl(table);
129 _oldVal = (String) column.getValue(row);
130 if (typedKey != 0) {
131 _text.setText("" + typedKey);
132 _text.setSelection(1);
133 } else {
134 String value = convertValue(row, column);
135 _text.setText(value != null ? value : "");
136 _text.selectAll();
137 }
138 return _text;
139 }
140
141 /***
142 * {@inheritDoc}
143 */
144 public int getPreferredHeight() {
145 if (_text == null) {
146 return -1;
147 }
148 int lheight = _text.getLineHeight();
149 int lcount = _text.getLineCount();
150 if (lcount > _maxrows + 1) {
151 lcount = _maxrows;
152 }
153 return (lcount + 1) * lheight;
154
155 }
156
157 public void focusGained(FocusEvent arg0) {
158 }
159
160 public void focusLost(FocusEvent arg0) {
161 _table.stopEditing(true);
162 }
163
164 public void dispose() {
165 super.dispose();
166 if (_text != null && !_text.isDisposed()) {
167 _text.removeFocusListener(this);
168 _text.dispose();
169 }
170
171 }
172
173 /***
174 * {@inheritDoc}
175 */
176 public void stopEditing(boolean storeInput) {
177 if (storeInput) {
178 storeValue(_row, _column);
179 }
180 _text.setVisible(false);
181 }
182
183 /***
184 * @return the maxrows
185 */
186 public int getMaxrows() {
187 return _maxrows;
188 }
189
190 /***
191 * @param maxrows the maxrows to set
192 */
193 public void setMaxrows(int maxrows) {
194 _maxrows = maxrows;
195 }
196
197 }