Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Vogel2016-11-10 16:44:59 +0000
committerLars Vogel2016-11-18 09:45:34 +0000
commitd9cc6d666000e568cb9d5e60caacccec14b058e6 (patch)
treebe8bbd863857f4be6ccf24e58a9f8490134b902b /examples
parentf6823895eca075b75e694a593097103ddd6d7bb0 (diff)
downloadeclipse.platform.swt-d9cc6d666000e568cb9d5e60caacccec14b058e6.tar.gz
eclipse.platform.swt-d9cc6d666000e568cb9d5e60caacccec14b058e6.tar.xz
eclipse.platform.swt-d9cc6d666000e568cb9d5e60caacccec14b058e6.zip
Bug 507339 - Update SWT Snippets KeyListener with Lambda expressions
Change-Id: I752d1b34f40551da90aa057f779339ae3841a926 Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet334.java14
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java58
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java54
3 files changed, 58 insertions, 68 deletions
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet334.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet334.java
index ac6af3ec5f..a67ce702ab 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet334.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet334.java
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 507339
*******************************************************************************/
package org.eclipse.swt.snippets;
@@ -18,6 +19,9 @@ package org.eclipse.swt.snippets;
*
* @since 3.6
*/
+
+import static org.eclipse.swt.events.KeyListener.*;
+
import org.eclipse.swt.*;
import org.eclipse.swt.accessibility.*;
import org.eclipse.swt.events.*;
@@ -56,12 +60,10 @@ public static void main(String[] arg) {
break;
}
});
- canvas.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- // key listener enables traversal out
- }
- });
+
+ // key listener enables traversal out)
+ canvas.addKeyListener(keyPressedAdapter(e-> {}));
+
canvas.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java
index 667822f1b4..7bb8d28443 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet360.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011, 2013 IBM Corporation and others.
+ * Copyright (c) 2011, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 507185
*******************************************************************************/
package org.eclipse.swt.snippets;
@@ -20,7 +21,9 @@ package org.eclipse.swt.snippets;
*
* @since 3.8
*/
-import org.eclipse.swt.SWT;
+import static org.eclipse.swt.events.KeyListener.*;
+
+import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
@@ -76,45 +79,38 @@ public static void main(String[] args) {
// when the user hits "ENTER" in the TreeCursor, pop up a text
// editor so that they can change the text of the cell
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
- final Text text = new Text(cursor, SWT.NONE);
- TreeItem row = cursor.getRow();
- int column = cursor.getColumn();
- text.setText(row.getText(column));
- text.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ final Text text = new Text(cursor, SWT.NONE);
+ TreeItem row = cursor.getRow();
+ int column = cursor.getColumn();
+ text.setText(row.getText(column));
+ text.addKeyListener(keyPressedAdapter(event -> {
// close the text editor and copy the data over
// when the user hits "ENTER"
- if (e.character == SWT.CR) {
- TreeItem row = cursor.getRow();
- int column = cursor.getColumn();
- row.setText(column, text.getText());
+ if (event.character == SWT.CR) {
+ TreeItem localRow = cursor.getRow();
+ int localColumn = cursor.getColumn();
+ localRow.setText(localColumn, text.getText());
text.dispose();
}
// close the text editor when the user hits "ESC"
- if (e.character == SWT.ESC) {
+ if (event.character == SWT.ESC) {
text.dispose();
}
- }
- });
- editor.setEditor(text);
- text.setFocus();
- }
- });
- // Hide the TreeCursor when the user hits the "MOD1" or "MOD2" key.
- // This allows the user to select multiple items in the tree.
- cursor.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2
- || (e.stateMask & SWT.MOD1) != 0
+ }));
+ editor.setEditor(text);
+ text.setFocus();
+ }
+ });
+ // Hide the TreeCursor when the user hits the "MOD1" or "MOD2" key.
+ // This allows the user to select multiple items in the tree.
+ cursor.addKeyListener(keyPressedAdapter(e -> {
+ if (e.keyCode == SWT.MOD1 || e.keyCode == SWT.MOD2 || (e.stateMask & SWT.MOD1) != 0
|| (e.stateMask & SWT.MOD2) != 0) {
cursor.setVisible(false);
}
- }
- });
+ }));
// Show the TreeCursor when the user releases the "MOD2" or "MOD1" key.
// This signals the end of the multiple selection task.
tree.addKeyListener(new KeyListener() {
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java
index 76d6c2b25b..399c1e68f2 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet96.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 507185
*******************************************************************************/
package org.eclipse.swt.snippets;
@@ -18,6 +19,9 @@ package org.eclipse.swt.snippets;
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*/
+
+import static org.eclipse.swt.events.KeyListener.*;
+
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
@@ -68,23 +72,20 @@ public static void main(String[] args) {
TableItem row = cursor.getRow();
int column = cursor.getColumn();
text.setText(row.getText(column));
- text.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
+ text.addKeyListener(keyPressedAdapter(event -> {
// close the text editor and copy the data over
// when the user hits "ENTER"
- if (e.character == SWT.CR) {
- TableItem row = cursor.getRow();
- int column = cursor.getColumn();
- row.setText(column, text.getText());
+ if (event.character == SWT.CR) {
+ TableItem localRow = cursor.getRow();
+ int localColumn = cursor.getColumn();
+ localRow.setText(localColumn, text.getText());
text.dispose();
}
// close the text editor when the user hits "ESC"
- if (e.character == SWT.ESC) {
+ if (event.character == SWT.ESC) {
text.dispose();
}
- }
- });
+ }));
// close the text editor when the user tabs away
text.addFocusListener(new FocusAdapter() {
@Override
@@ -98,17 +99,14 @@ public static void main(String[] args) {
});
// Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key.
// This allows the user to select multiple items in the table.
- cursor.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
+ cursor.addKeyListener(keyPressedAdapter(e-> {
if (e.keyCode == SWT.CTRL
|| e.keyCode == SWT.SHIFT
|| (e.stateMask & SWT.CONTROL) != 0
|| (e.stateMask & SWT.SHIFT) != 0) {
cursor.setVisible(false);
}
- }
- });
+ }));
// When the user double clicks in the TableCursor, pop up a text editor so that
// they can change the text of the cell.
cursor.addMouseListener(new MouseAdapter() {
@@ -118,23 +116,20 @@ public static void main(String[] args) {
TableItem row = cursor.getRow();
int column = cursor.getColumn();
text.setText(row.getText(column));
- text.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
+ text.addKeyListener(keyPressedAdapter(event -> {
// close the text editor and copy the data over
// when the user hits "ENTER"
- if (e.character == SWT.CR) {
- TableItem row = cursor.getRow();
- int column = cursor.getColumn();
- row.setText(column, text.getText());
+ if (event.character == SWT.CR) {
+ TableItem localRow = cursor.getRow();
+ int localColumn = cursor.getColumn();
+ localRow.setText(localColumn, text.getText());
text.dispose();
}
// close the text editor when the user hits "ESC"
- if (e.character == SWT.ESC) {
+ if (event.character == SWT.ESC) {
text.dispose();
}
- }
- });
+ }));
// close the text editor when the user clicks away
text.addFocusListener(new FocusAdapter() {
@Override
@@ -149,9 +144,7 @@ public static void main(String[] args) {
// Show the TableCursor when the user releases the "SHIFT" or "CTRL" key.
// This signals the end of the multiple selection task.
- table.addKeyListener(new KeyAdapter() {
- @Override
- public void keyReleased(KeyEvent e) {
+ table.addKeyListener(keyReleasedAdapter(e-> {
if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) return;
if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) return;
if (e.keyCode != SWT.CONTROL && (e.stateMask & SWT.CONTROL) != 0) return;
@@ -163,8 +156,7 @@ public static void main(String[] args) {
cursor.setSelection(row, cursor.getColumn());
cursor.setVisible(true);
cursor.setFocus();
- }
- });
+ }));
shell.open();
while (!shell.isDisposed()) {

Back to the top