Skip to content

Commit

Permalink
Provide HiDPI Support for Multi-Monitor Environments for custom controls
Browse files Browse the repository at this point in the history
Updating the example by adding option to make Table and Tree Widgets editable. This was part of testing the custom controls for multi-monitor support.

Contributes to eclipse-platform#62 and eclipse-platform#127
  • Loading branch information
ShahzaibIbrahim committed May 17, 2024
1 parent 7479681 commit 196ead4
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ Tab[] createTabs() {
new CTabFolderTab (this),
new SashFormTab (this),
new StyledTextTab (this),
new TreeTab (this),
new TableTab (this)
};
}

Expand Down
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,7 @@ 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 +255,9 @@ 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 +268,73 @@ 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 All @@ -274,6 +351,8 @@ void createExampleGroup () {
tableGroup.setText ("Table");
}

static int column = -1;

/**
* Creates the "Example" widgets.
*/
Expand Down Expand Up @@ -717,4 +796,4 @@ protected void specialPopupMenuItems(Menu menu, Event event) {
eventConsole.append ("\n");
}));
}
}
}
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,111 @@ 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 All @@ -279,6 +389,7 @@ void createExampleGroup () {
imageTreeGroup.setLayoutData (new GridData (SWT.FILL, SWT.FILL, true, true));
imageTreeGroup.setText (ControlExample.getResourceString("Tree_With_Images"));
}
static int column = -1;

/**
* Creates the "Example" widgets.
Expand Down Expand Up @@ -819,4 +930,4 @@ protected void specialPopupMenuItems(Menu menu, Event event) {
eventConsole.append ("\n");
}));
}
}
}

0 comments on commit 196ead4

Please sign in to comment.