Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexander Kurtakov2017-08-11 02:50:44 -0400
committerAlexander Kurtakov2017-08-11 03:01:54 -0400
commit9583bb220837807a23b917ad8280e1afa17b1b22 (patch)
tree3715bfc9932efca2b39c18a8ab5262b2c7660133 /examples
parent1eabb1b2b0abefd1965ad52fc4515d6b99dfa361 (diff)
downloadeclipse.platform.swt-9583bb220837807a23b917ad8280e1afa17b1b22.tar.gz
eclipse.platform.swt-9583bb220837807a23b917ad8280e1afa17b1b22.tar.xz
eclipse.platform.swt-9583bb220837807a23b917ad8280e1afa17b1b22.zip
Bug 520822 - [api] Provide helpers to use lambda expressions for
MenuListener Use the new API in examples and snippets. Change-Id: I0ce58b739e3b6f293b5895b97bc43a2fd95eacad Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java70
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CoolBarTab.java12
-rw-r--r--examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java31
-rw-r--r--examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java31
4 files changed, 59 insertions, 85 deletions
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java
index 84d8b2eac3..769e1606cf 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/addressbook/AddressBook.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
@@ -10,11 +10,10 @@
*******************************************************************************/
package org.eclipse.swt.examples.addressbook;
-
+import static org.eclipse.swt.events.MenuListener.menuShownAdapter;
import static org.eclipse.swt.events.SelectionListener.widgetDefaultSelectedAdapter;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
-/* Imports */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
@@ -26,8 +25,6 @@ import java.util.Comparator;
import java.util.ResourceBundle;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.MenuAdapter;
-import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Cursor;
@@ -470,16 +467,13 @@ private void createFileMenu(Menu menuBar) {
* Adds a listener to handle enabling and disabling
* some items in the Edit submenu.
*/
- menu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent e) {
- Menu menu = (Menu)e.widget;
- MenuItem[] items = menu.getItems();
- items[1].setEnabled(table.getSelectionCount() != 0); // edit contact
- items[5].setEnabled((file != null) && isModified); // save
- items[6].setEnabled(table.getItemCount() != 0); // save as
- }
- });
+ menu.addMenuListener(menuShownAdapter(e -> {
+ Menu menu1 = (Menu) e.widget;
+ MenuItem[] items = menu1.getItems();
+ items[1].setEnabled(table.getSelectionCount() != 0); // edit contact
+ items[5].setEnabled((file != null) && isModified); // save
+ items[6].setEnabled(table.getItemCount() != 0); // save as
+ }));
//File -> New Contact
@@ -562,19 +556,16 @@ private MenuItem createEditMenu(Menu menuBar) {
* Add a listener to handle enabling and disabling
* some items in the Edit submenu.
*/
- menu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent e) {
- Menu menu = (Menu)e.widget;
- MenuItem[] items = menu.getItems();
- int count = table.getSelectionCount();
- items[0].setEnabled(count != 0); // edit
- items[1].setEnabled(count != 0); // copy
- items[2].setEnabled(copyBuffer != null); // paste
- items[3].setEnabled(count != 0); // delete
- items[5].setEnabled(table.getItemCount() != 0); // sort
- }
- });
+ menu.addMenuListener(menuShownAdapter(e -> {
+ Menu menu1 = (Menu) e.widget;
+ MenuItem[] items = menu1.getItems();
+ int count = table.getSelectionCount();
+ items[0].setEnabled(count != 0); // edit
+ items[1].setEnabled(count != 0); // copy
+ items[2].setEnabled(copyBuffer != null); // paste
+ items[3].setEnabled(count != 0); // delete
+ items[5].setEnabled(table.getItemCount() != 0); // sort
+ }));
//Edit -> Edit
MenuItem subItem = new MenuItem(menu, SWT.PUSH);
@@ -704,19 +695,16 @@ private Menu createPopUpMenu() {
* Adds a listener to handle enabling and disabling
* some items in the Edit submenu.
*/
- popUpMenu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent e) {
- Menu menu = (Menu)e.widget;
- MenuItem[] items = menu.getItems();
- int count = table.getSelectionCount();
- items[2].setEnabled(count != 0); // edit
- items[3].setEnabled(count != 0); // copy
- items[4].setEnabled(copyBuffer != null); // paste
- items[5].setEnabled(count != 0); // delete
- items[7].setEnabled(table.getItemCount() != 0); // find
- }
- });
+ popUpMenu.addMenuListener(menuShownAdapter(e -> {
+ Menu menu = (Menu) e.widget;
+ MenuItem[] items = menu.getItems();
+ int count = table.getSelectionCount();
+ items[2].setEnabled(count != 0); // edit
+ items[3].setEnabled(count != 0); // copy
+ items[4].setEnabled(copyBuffer != null); // paste
+ items[5].setEnabled(count != 0); // delete
+ items[7].setEnabled(table.getItemCount() != 0); // find
+ }));
//New
MenuItem item = new MenuItem(popUpMenu, SWT.PUSH);
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CoolBarTab.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CoolBarTab.java
index 0dd151baea..0a3989cd26 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CoolBarTab.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/CoolBarTab.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
@@ -11,11 +11,10 @@
package org.eclipse.swt.examples.controlexample;
+import static org.eclipse.swt.events.MenuListener.menuHiddenAdapter;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.MenuAdapter;
-import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
@@ -346,12 +345,7 @@ class CoolBarTab extends Tab {
if (menu == null) {
// Lazy create the menu.
menu = new Menu(shell, SWT.POP_UP | (coolBar.getStyle() & (SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT)));
- menu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuHidden(MenuEvent e) {
- visible = false;
- }
- });
+ menu.addMenuListener(menuHiddenAdapter(e -> visible = false));
for (int i = 0; i < 9; ++i) {
final String text = ControlExample.getResourceString("DropDownData_" + i);
if (text.length() != 0) {
diff --git a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java
index 1a7b25e15d..f9d18aa296 100644
--- a/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java
+++ b/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.swt.examples.texteditor;
+import static org.eclipse.swt.events.MenuListener.menuShownAdapter;
import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
import java.io.BufferedReader;
@@ -317,15 +318,12 @@ public class TextEditor {
selectAllItem.setAccelerator(SWT.MOD1 | 'a');
selectAllItem.addSelectionListener(widgetSelectedAdapter(event -> styledText.selectAll()));
- editMenu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent event) {
- int selectionCount = styledText.getSelectionCount();
- cutItem.setEnabled(selectionCount > 0);
- copyItem.setEnabled(selectionCount > 0);
- selectAllItem.setEnabled(selectionCount < styledText.getCharCount());
- }
- });
+ editMenu.addMenuListener(menuShownAdapter(event -> {
+ int selectionCount = styledText.getSelectionCount();
+ cutItem.setEnabled(selectionCount > 0);
+ copyItem.setEnabled(selectionCount > 0);
+ selectAllItem.setEnabled(selectionCount < styledText.getCharCount());
+ }));
MenuItem wrapItem = new MenuItem(editMenu, SWT.CHECK);
wrapItem.setText(getResourceString("Wrap_menuitem")); //$NON-NLS-1$
@@ -499,15 +497,12 @@ public class TextEditor {
final MenuItem selectAllItem = new MenuItem (menu, SWT.PUSH);
selectAllItem.setText (getResourceString("SelectAll_menuitem")); //$NON-NLS-1$
selectAllItem.addListener (SWT.Selection, event -> styledText.selectAll());
- menu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent event) {
- int selectionCount = styledText.getSelectionCount();
- cutItem.setEnabled(selectionCount > 0);
- copyItem.setEnabled(selectionCount > 0);
- selectAllItem.setEnabled(selectionCount < styledText.getCharCount());
- }
- });
+ menu.addMenuListener(menuShownAdapter(event -> {
+ int selectionCount = styledText.getSelectionCount();
+ cutItem.setEnabled(selectionCount > 0);
+ copyItem.setEnabled(selectionCount > 0);
+ selectAllItem.setEnabled(selectionCount < styledText.getCharCount());
+ }));
styledText.setMenu(menu);
}
diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java
index 6b0bd77b5c..2de25cc76e 100644
--- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java
+++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet122.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2016 IBM Corporation and others.
+ * Copyright (c) 2000, 2017 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
@@ -57,24 +57,21 @@ public static void main(String[] args) {
text.insert(string);
}
}));
- menu.addMenuListener(new MenuAdapter() {
- @Override
- public void menuShown(MenuEvent e) {
- // is copy valid?
- String selection = text.getSelectionText();
- copyItem.setEnabled(selection.length() > 0);
- // is paste valid?
- TransferData[] available = cb.getAvailableTypes();
- boolean enabled = false;
- for (int i = 0; i < available.length; i++) {
- if (TextTransfer.getInstance().isSupportedType(available[i])) {
- enabled = true;
- break;
- }
+ menu.addMenuListener(MenuListener.menuShownAdapter(e -> {
+ // is copy valid?
+ String selection = text.getSelectionText();
+ copyItem.setEnabled(selection.length() > 0);
+ // is paste valid?
+ TransferData[] available = cb.getAvailableTypes();
+ boolean enabled = false;
+ for (int i = 0; i < available.length; i++) {
+ if (TextTransfer.getInstance().isSupportedType(available[i])) {
+ enabled = true;
+ break;
}
- pasteItem.setEnabled(enabled);
}
- });
+ pasteItem.setEnabled(enabled);
+ }));
text.setMenu (menu);
shell.setSize(200, 200);

Back to the top