17
17
import static org .eclipse .swt .events .SelectionListener .widgetSelectedAdapter ;
18
18
19
19
import org .eclipse .swt .SWT ;
20
+ import org .eclipse .swt .custom .TableEditor ;
20
21
import org .eclipse .swt .events .SelectionListener ;
21
22
import org .eclipse .swt .graphics .Color ;
22
23
import org .eclipse .swt .graphics .Font ;
23
24
import org .eclipse .swt .graphics .FontData ;
24
25
import org .eclipse .swt .graphics .Image ;
25
26
import org .eclipse .swt .graphics .Point ;
26
27
import org .eclipse .swt .graphics .RGB ;
28
+ import org .eclipse .swt .graphics .Rectangle ;
27
29
import org .eclipse .swt .layout .GridData ;
28
30
import org .eclipse .swt .layout .GridLayout ;
29
31
import org .eclipse .swt .widgets .Button ;
32
+ import org .eclipse .swt .widgets .Control ;
30
33
import org .eclipse .swt .widgets .Event ;
31
34
import org .eclipse .swt .widgets .Group ;
32
35
import org .eclipse .swt .widgets .Item ;
35
38
import org .eclipse .swt .widgets .Table ;
36
39
import org .eclipse .swt .widgets .TableColumn ;
37
40
import org .eclipse .swt .widgets .TableItem ;
41
+ import org .eclipse .swt .widgets .Text ;
38
42
import org .eclipse .swt .widgets .Widget ;
39
43
40
44
class TableTab extends ScrollableTab {
@@ -49,7 +53,8 @@ class TableTab extends ScrollableTab {
49
53
Button noScrollButton , checkButton , fullSelectionButton , hideSelectionButton ;
50
54
51
55
/* Other widgets added to the "Other" group */
52
- Button multipleColumns , moveableColumns , resizableColumns , headerVisibleButton , sortIndicatorButton , headerImagesButton , linesVisibleButton , subImagesButton ;
56
+ Button multipleColumns , moveableColumns , resizableColumns , headerVisibleButton , sortIndicatorButton ,
57
+ headerImagesButton , linesVisibleButton , subImagesButton , editableButton ;
53
58
54
59
/* Controls and resources added to the "Colors and Fonts" group */
55
60
static final int ITEM_FOREGROUND_COLOR = 3 ;
@@ -248,6 +253,8 @@ void createOtherGroup () {
248
253
headerImagesButton .setText (ControlExample .getResourceString ("Header_Images" ));
249
254
subImagesButton = new Button (otherGroup , SWT .CHECK );
250
255
subImagesButton .setText (ControlExample .getResourceString ("Sub_Images" ));
256
+ editableButton = new Button (otherGroup , SWT .CHECK );
257
+ editableButton .setText (ControlExample .getResourceString ("Editable" ));
251
258
252
259
/* Add the listeners */
253
260
linesVisibleButton .addSelectionListener (widgetSelectedAdapter (event -> setWidgetLinesVisible ()));
@@ -258,6 +265,72 @@ void createOtherGroup () {
258
265
resizableColumns .addSelectionListener (widgetSelectedAdapter (event -> setColumnsResizable ()));
259
266
headerImagesButton .addSelectionListener (widgetSelectedAdapter (event -> recreateExampleWidgets ()));
260
267
subImagesButton .addSelectionListener (widgetSelectedAdapter (event -> recreateExampleWidgets ()));
268
+ editableButton .addSelectionListener (widgetSelectedAdapter (event -> makeTableContentEditable ()));
269
+ }
270
+
271
+ void makeTableContentEditable () {
272
+ // Create a Text editor
273
+ final TableEditor editor = new TableEditor (table1 );
274
+ editor .horizontalAlignment = SWT .LEFT ;
275
+ editor .grabHorizontal = true ;
276
+ // Listener to activate editor
277
+ table1 .addListener (SWT .MouseDoubleClick , event -> {
278
+ if (!editableButton .getSelection ()) {
279
+ return ;
280
+ }
281
+ Control oldEditor = editor .getEditor ();
282
+ if (oldEditor != null ) {
283
+ oldEditor .dispose ();
284
+ }
285
+
286
+ Point point = new Point (event .x , event .y );
287
+ final TableItem item = table1 .getItem (point );
288
+ if (item == null ) {
289
+ return ;
290
+ }
291
+
292
+ final int column = findColumnContainingPoint (item , point );
293
+
294
+ if (column == -1 ) {
295
+ return ;
296
+ }
297
+
298
+ final Text newEditor = new Text (table1 , SWT .NONE );
299
+ newEditor .setText (item .getText (column ));
300
+ newEditor .addModifyListener (e -> {
301
+ Text text = (Text ) editor .getEditor ();
302
+ item .setText (column , text .getText ());
303
+ });
304
+ newEditor .selectAll ();
305
+ newEditor .setFocus ();
306
+
307
+ // Add a focus out listener to commit changes on focus lost
308
+ newEditor .addListener (SWT .FocusOut , e -> {
309
+ item .setText (newEditor .getText ());
310
+ newEditor .dispose (); // Dispose the text field after editing
311
+ });
312
+
313
+ // Add a key listener to commit changes on Enter key pressed
314
+ newEditor .addListener (SWT .KeyDown , e -> {
315
+ if (e .keyCode == SWT .CR || e .keyCode == SWT .KEYPAD_CR ) {
316
+ item .setText (newEditor .getText ());
317
+ newEditor .dispose (); // Dispose the text field after editing
318
+ }
319
+ });
320
+
321
+ editor .setEditor (newEditor , item , column );
322
+ });
323
+
324
+ }
325
+
326
+ private static int findColumnContainingPoint (TableItem item , Point point ) {
327
+ for (int i = 0 ; i < item .getParent ().getColumnCount (); i ++) {
328
+ Rectangle rect = item .getBounds (i );
329
+ if (rect .contains (point )) {
330
+ return i ;
331
+ }
332
+ }
333
+ return -1 ;
261
334
}
262
335
263
336
/**
0 commit comments