From 196ead4ee87b148ee9fad5c0a9fc84348dcf4aff Mon Sep 17 00:00:00 2001 From: "shahzaib.ibrahim" Date: Fri, 17 May 2024 17:11:19 +0200 Subject: [PATCH] Provide HiDPI Support for Multi-Monitor Environments for custom controls 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 #62 and #127 --- .../controlexample/CustomControlExample.java | 2 + .../swt/examples/controlexample/TableTab.java | 83 ++++++++++++- .../swt/examples/controlexample/TreeTab.java | 115 +++++++++++++++++- 3 files changed, 196 insertions(+), 4 deletions(-) diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CustomControlExample.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CustomControlExample.java index d30378e8c4f..876102e544c 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CustomControlExample.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CustomControlExample.java @@ -42,6 +42,8 @@ Tab[] createTabs() { new CTabFolderTab (this), new SashFormTab (this), new StyledTextTab (this), + new TreeTab (this), + new TableTab (this) }; } diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java index 3a0c15d3d3b..b707d4e36bf 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TableTab.java @@ -17,6 +17,9 @@ 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; @@ -24,17 +27,21 @@ 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 { @@ -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; @@ -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 ())); @@ -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); + } + } + }); } /** @@ -274,6 +351,8 @@ void createExampleGroup () { tableGroup.setText ("Table"); } + static int column = -1; + /** * Creates the "Example" widgets. */ @@ -717,4 +796,4 @@ protected void specialPopupMenuItems(Menu menu, Event event) { eventConsole.append ("\n"); })); } -} +} \ No newline at end of file diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java index 57e511154db..2222c460b6e 100644 --- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java +++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/TreeTab.java @@ -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; @@ -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; @@ -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; @@ -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 ())); @@ -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); + } + } + } + }); + } /** @@ -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. @@ -819,4 +930,4 @@ protected void specialPopupMenuItems(Menu menu, Event event) { eventConsole.append ("\n"); })); } -} +} \ No newline at end of file