summaryrefslogtreecommitdiff
path: root/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils
diff options
context:
space:
mode:
authorJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-09 12:18:22 +0200
committerJakob Heher <jakob.heher@iaik.tugraz.at>2022-08-09 12:18:22 +0200
commit82c7b5dcc102b28277e4d880fb2c18612fa3c807 (patch)
tree20511bde9c97dba9857183c666ba30ffabe2085b /pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils
parent1599d8abfb17c8b700845d020866d0dc5e9acba7 (diff)
downloadpdf-over-82c7b5dcc102b28277e4d880fb2c18612fa3c807.tar.gz
pdf-over-82c7b5dcc102b28277e4d880fb2c18612fa3c807.tar.bz2
pdf-over-82c7b5dcc102b28277e4d880fb2c18612fa3c807.zip
functional interfaces for readability
Diffstat (limited to 'pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils')
-rw-r--r--pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTUtils.java18
1 files changed, 18 insertions, 0 deletions
diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTUtils.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTUtils.java
index c89f12b8..ebeda252 100644
--- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTUtils.java
+++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/SWTUtils.java
@@ -2,7 +2,10 @@ package at.asit.pdfover.gui.utils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.util.function.Consumer;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FormAttachment;
@@ -120,4 +123,19 @@ public final class SWTUtils {
}
public static AnchorSetter anchor(Control c) { return new AnchorSetter(c); }
+ /**
+ * functional-interface wrapper around swtObj.addSelectionListener
+ * @param swtObj SWT widget supporting addSelectionListener
+ * @param callback widgetSelected method
+ */
+ public static void addSelectionListener(Object swtObj, Consumer<SelectionEvent> callback) {
+ try {
+ Method m = swtObj.getClass().getMethod("addSelectionListener", SelectionAdapter.class);
+ m.invoke(swtObj, new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { callback.accept(e); } });
+ } catch (NoSuchMethodException | IllegalAccessException e) {
+ log.error("Attempted to pass object of type {} to onSelectionChanged; object does not have an accessible addSelectionListener method", swtObj.getClass().getSimpleName(), e);
+ } catch (InvocationTargetException e) {
+ log.error("Failed to add selection listener on object of type {}", swtObj.getClass().getSimpleName(), e);
+ }
+ }
}