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.

Contributes to #62 and #127
  • Loading branch information
ShahzaibIbrahim committed Jul 1, 2024
1 parent e02dd67 commit aac6dbb
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,31 @@
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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 +56,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 All @@ -60,9 +68,12 @@ class TableTab extends ScrollableTab {
static final int CELL_FONT = 8;
static final int HEADER_FOREGROUND_COLOR = 9;
static final int HEADER_BACKGROUND_COLOR = 10;
Color itemForegroundColor, itemBackgroundColor, cellForegroundColor, cellBackgroundColor, headerForegroundColor, headerBackgroundColor;
Color itemForegroundColor, itemBackgroundColor, cellForegroundColor, cellBackgroundColor, headerForegroundColor,
headerBackgroundColor;
Font itemFont, cellFont;

static int column = -1;

static String [] columnTitles = {ControlExample.getResourceString("TableTitle_0"),
ControlExample.getResourceString("TableTitle_1"),
ControlExample.getResourceString("TableTitle_2"),
Expand Down Expand Up @@ -248,6 +259,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("Editable");

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

/**
* Make Tree Editable
*/
void makeTableContentEditable() {
// Create a Text editor
final TableEditor editor = new TableEditor(table1);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// Listener to activate editor
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent event) {
if (editableButton.getSelection()) {
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;

for (int i = 0; i < table1.getColumnCount(); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(point)) {
column = i;
break;
}
}

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, new Listener() {
@Override
public void handleEvent(Event 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, new Listener() {
@Override
public void handleEvent(Event 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);
}
}
});
}

/**
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 @@ -30,9 +31,11 @@
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Listener;
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 +54,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 +251,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("Editable");

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

/**
* Make Tree Editable
*/
void makeTreeContentEditable() {
// Tree 1
final TreeEditor editor = new TreeEditor(tree1);
editor.horizontalAlignment = SWT.LEFT;
editor.grabHorizontal = true;
// Add double click listener to the tree
tree1.addListener(SWT.MouseDoubleClick, new Listener() {
@Override
public void handleEvent(Event event) {
if (editableButton.getSelection()) {
Point point = new Point(event.x, event.y);
TreeItem item = tree1.getItem(point);
if (item != null) {
// Get the item text
final String oldText = item.getText();

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

// Add a focus out listener to commit changes on focus lost
text.addListener(SWT.FocusOut, new Listener() {
@Override
public void handleEvent(Event 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, new Listener() {
@Override
public void handleEvent(Event 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);
}
}
}
});

// Tree 2
final TreeEditor editor2 = new TreeEditor(tree2);
editor2.horizontalAlignment = SWT.LEFT;
editor2.grabHorizontal = true;
// Add double click listener to the tree
tree2.addListener(SWT.MouseDoubleClick, new Listener() {
@Override
public void handleEvent(Event event) {
if (editableButton.getSelection()) {
Point point = new Point(event.x, event.y);
TreeItem item = tree2.getItem(point);
if (item != null) {
// Get the item text
final String oldText = item.getText();

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

// Add a focus out listener to commit changes on focus lost
text.addListener(SWT.FocusOut, new Listener() {
@Override
public void handleEvent(Event 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, new Listener() {
@Override
public void handleEvent(Event 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 aac6dbb

Please sign in to comment.