Skip to content

Commit

Permalink
Updating control example by adding option to make Table/Tree editable
Browse files Browse the repository at this point in the history
This commit is part of testing the custom controls for multi-monitor support.
  • Loading branch information
ShahzaibIbrahim authored and HeikoKlare committed Jul 5, 2024
1 parent 80086c9 commit d95a444
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Header_Visible = Header Visible
Sort_Indicator = Sort Indicator
Header_Images = Header Images
Sub_Images = Sub Images
Editable = Editable
Lines_Visible = Lines Visible
Moveable_Columns = Moveable Columns
Resizable_Columns = Resizable Columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Item;
Expand All @@ -35,6 +38,7 @@
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;

class TableTab extends ScrollableTab {
Expand All @@ -49,7 +53,8 @@ class TableTab extends ScrollableTab {
Button noScrollButton, checkButton, fullSelectionButton, hideSelectionButton;

/* Other widgets added to the "Other" group */
Button multipleColumns, moveableColumns, resizableColumns, headerVisibleButton, sortIndicatorButton, headerImagesButton, linesVisibleButton, subImagesButton;
Button multipleColumns, moveableColumns, resizableColumns, headerVisibleButton, sortIndicatorButton,
headerImagesButton, linesVisibleButton, subImagesButton, editableButton;

/* Controls and resources added to the "Colors and Fonts" group */
static final int ITEM_FOREGROUND_COLOR = 3;
Expand Down Expand Up @@ -248,6 +253,8 @@ void createOtherGroup () {
headerImagesButton.setText (ControlExample.getResourceString("Header_Images"));
subImagesButton = new Button (otherGroup, SWT.CHECK);
subImagesButton.setText (ControlExample.getResourceString("Sub_Images"));
editableButton = new Button(otherGroup, SWT.CHECK);
editableButton.setText(ControlExample.getResourceString("Editable"));

/* Add the listeners */
linesVisibleButton.addSelectionListener (widgetSelectedAdapter(event -> setWidgetLinesVisible ()));
Expand All @@ -258,6 +265,72 @@ void createOtherGroup () {
resizableColumns.addSelectionListener (widgetSelectedAdapter(event -> setColumnsResizable ()));
headerImagesButton.addSelectionListener (widgetSelectedAdapter(event -> recreateExampleWidgets ()));
subImagesButton.addSelectionListener (widgetSelectedAdapter(event -> recreateExampleWidgets ()));
editableButton.addSelectionListener(widgetSelectedAdapter(event -> makeTableContentEditable()));
}

void makeTableContentEditable() {
// Create a Text editor
final TableEditor editor = new TableEditor(table1);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// Listener to activate editor
table1.addListener(SWT.MouseDoubleClick, event -> {
if (!editableButton.getSelection()) {
return;
}
Control oldEditor = editor.getEditor();
if (oldEditor != null) {
oldEditor.dispose();
}

Point point = new Point(event.x, event.y);
final TableItem item = table1.getItem(point);
if (item == null) {
return;
}

final int column = findColumnContainingPoint(item, point);

if (column == -1) {
return;
}

final Text newEditor = new Text(table1, SWT.NONE);
newEditor.setText(item.getText(column));
newEditor.addModifyListener(e -> {
Text text = (Text) editor.getEditor();
item.setText(column, text.getText());
});
newEditor.selectAll();
newEditor.setFocus();

// Add a focus out listener to commit changes on focus lost
newEditor.addListener(SWT.FocusOut, e -> {
item.setText(newEditor.getText());
newEditor.dispose(); // Dispose the text field after editing
});

// Add a key listener to commit changes on Enter key pressed
newEditor.addListener(SWT.KeyDown, e -> {
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
item.setText(newEditor.getText());
newEditor.dispose(); // Dispose the text field after editing
}
});

editor.setEditor(newEditor, item, column);
});

}

private static int findColumnContainingPoint(TableItem item, Point point) {
for (int i = 0; i < item.getParent().getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point)) {
return i;
}
}
return -1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
Expand All @@ -33,6 +34,7 @@
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
Expand All @@ -51,7 +53,7 @@ class TreeTab extends ScrollableTab {
Button noScrollButton, checkButton, fullSelectionButton;

/* Other widgets added to the "Other" group */
Button multipleColumns, moveableColumns, resizableColumns, headerVisibleButton, sortIndicatorButton, headerImagesButton, subImagesButton, linesVisibleButton;
Button multipleColumns, moveableColumns, resizableColumns, headerVisibleButton, sortIndicatorButton, headerImagesButton, subImagesButton, linesVisibleButton, editableButton;

/* Controls and resources added to the "Colors and Fonts" group */
static final int ITEM_FOREGROUND_COLOR = 3;
Expand Down Expand Up @@ -248,6 +250,8 @@ void createOtherGroup () {
headerImagesButton.setText (ControlExample.getResourceString("Header_Images"));
subImagesButton = new Button (otherGroup, SWT.CHECK);
subImagesButton.setText (ControlExample.getResourceString("Sub_Images"));
editableButton = new Button(otherGroup, SWT.CHECK);
editableButton.setText(ControlExample.getResourceString("Editable"));

/* Add the listeners */
linesVisibleButton.addSelectionListener (widgetSelectedAdapter(event -> setWidgetLinesVisible ()));
Expand All @@ -258,6 +262,58 @@ void createOtherGroup () {
resizableColumns.addSelectionListener (widgetSelectedAdapter(event -> setColumnsResizable ()));
headerImagesButton.addSelectionListener (widgetSelectedAdapter(event -> recreateExampleWidgets ()));
subImagesButton.addSelectionListener (widgetSelectedAdapter(event -> recreateExampleWidgets ()));
editableButton.addSelectionListener(widgetSelectedAdapter(event -> makeTreeContentEditable()));
}

void makeTreeContentEditable() {
makeTreeEditable(tree1);
makeTreeEditable(tree2);
}

private void makeTreeEditable(Tree tree) {
final TreeEditor editor = new TreeEditor(tree);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;

tree.addListener(SWT.MouseDoubleClick, event -> {
treeDoubleClickListener(tree, editor, event);
});
}

private void treeDoubleClickListener(Tree tree, final TreeEditor editor, Event event) {
if (!editableButton.getSelection()) {
return;
}
Point point = new Point(event.x, event.y);
TreeItem item = tree.getItem(point);
if (item == null) {
return;
}
// Get the item text
final String oldText = item.getText();

// Create a text field to edit the item text
final Text text = new Text(tree, SWT.NONE);
text.setText(oldText);
text.selectAll();
text.setFocus();

// Add a focus out listener to commit changes on focus lost
text.addListener(SWT.FocusOut, e -> {
item.setText(text.getText());
text.dispose(); // Dispose the text field after editing
});

// Add a key listener to commit changes on Enter key pressed
text.addListener(SWT.KeyDown, e -> {
if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
item.setText(text.getText());
text.dispose(); // Dispose the text field after editing
}
});

// Edit the text field on double click
editor.setEditor(text, item);
}

/**
Expand Down

0 comments on commit d95a444

Please sign in to comment.