CleanUp: Review UIAccess
diff --git a/ecommons/org.eclipse.statet.ecommons.runtime.ui/src/org/eclipse/statet/ecommons/ts/ui/workbench/WorkbenchToolRegistry.java b/ecommons/org.eclipse.statet.ecommons.runtime.ui/src/org/eclipse/statet/ecommons/ts/ui/workbench/WorkbenchToolRegistry.java
index e772789..bb24e24 100644
--- a/ecommons/org.eclipse.statet.ecommons.runtime.ui/src/org/eclipse/statet/ecommons/ts/ui/workbench/WorkbenchToolRegistry.java
+++ b/ecommons/org.eclipse.statet.ecommons.runtime.ui/src/org/eclipse/statet/ecommons/ts/ui/workbench/WorkbenchToolRegistry.java
@@ -18,6 +18,7 @@
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
+import org.eclipse.statet.jcommons.ts.core.Tool;
/**
@@ -64,6 +65,6 @@
*
* @return best workbench page, never <code>null</code>
*/
- IWorkbenchPage findWorkbenchPage(final org.eclipse.statet.jcommons.ts.core.Tool process);
+ IWorkbenchPage findWorkbenchPage(final Tool process);
}
diff --git a/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/util/UIAccess.java b/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/util/UIAccess.java
index f4e36fc..5cf82cf 100644
--- a/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/util/UIAccess.java
+++ b/ecommons/org.eclipse.statet.ecommons.uimisc/src/org/eclipse/statet/ecommons/ui/util/UIAccess.java
@@ -16,7 +16,6 @@
import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.PARAMETER;
-import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.eclipse.core.runtime.CoreException;
@@ -30,11 +29,13 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
/**
* Access to UI resources from other threads.
*/
+@NonNullByDefault
public class UIAccess {
/**
@@ -54,47 +55,40 @@
* @param shell optional shell
* @return display
*/
- public static Display getDisplay(final Shell shell) {
- Display display = null;
+ public static Display getDisplay(final @Nullable Shell shell) {
+ Display display= null;
if (shell != null) {
- display = shell.getDisplay();
+ display= shell.getDisplay();
}
if (display == null) {
- display = PlatformUI.getWorkbench().getDisplay();
+ display= getDisplay();
}
return display;
}
- public static IWorkbenchWindow getActiveWorkbenchWindow(final boolean inUIThread) {
+ public static @Nullable IWorkbenchWindow getActiveWorkbenchWindow(final boolean inUIThread) {
if (inUIThread) {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
- final Display display = getDisplay();
+ final Display display= getDisplay();
if (display.getThread() == Thread.currentThread()) {
return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
}
- final AtomicReference<IWorkbenchWindow> windowRef= new AtomicReference<>();
- display.syncExec(new Runnable() {
- @Override
- public void run() {
- windowRef.set(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
- }
- });
- return windowRef.get();
+ return syncExecGet(() -> PlatformUI.getWorkbench().getActiveWorkbenchWindow(), display);
}
- public static IWorkbenchPage getActiveWorkbenchPage(final boolean inUIThread) {
- final IWorkbenchWindow window = getActiveWorkbenchWindow(inUIThread);
+ public static @Nullable IWorkbenchPage getActiveWorkbenchPage(final boolean inUIThread) {
+ final IWorkbenchWindow window= getActiveWorkbenchWindow(inUIThread);
if (window != null) {
return window.getActivePage();
}
return null;
}
- public static IWorkbenchPart getActiveWorkbenchPart(final boolean inUIThread) {
- final IWorkbenchWindow window = getActiveWorkbenchWindow(inUIThread);
+ public static @Nullable IWorkbenchPart getActiveWorkbenchPart(final boolean inUIThread) {
+ final IWorkbenchWindow window= getActiveWorkbenchWindow(inUIThread);
if (window != null) {
- final IWorkbenchPage activePage = window.getActivePage();
+ final IWorkbenchPage activePage= window.getActivePage();
if (activePage != null) {
return activePage.getActivePart();
}
@@ -102,8 +96,8 @@
return null;
}
- public static Shell getActiveWorkbenchShell(final boolean inUIThread) {
- final IWorkbenchWindow window = getActiveWorkbenchWindow(inUIThread);
+ public static @Nullable Shell getActiveWorkbenchShell(final boolean inUIThread) {
+ final IWorkbenchWindow window= getActiveWorkbenchWindow(inUIThread);
if (window != null) {
return window.getShell();
}
@@ -116,39 +110,55 @@
public void run() throws CoreException;
}
-
- public static void checkedSyncExec(final CheckedRunnable runnable)
+ public static void checkedSyncExec(final CheckedRunnable runnable, final Display display)
throws CoreException {
- final AtomicReference<CoreException> error= new AtomicReference<>();
- UIAccess.getDisplay().syncExec(new Runnable() {
+ final var catchRunnable= new Runnable() {
+
+ private volatile @Nullable CoreException error;
@Override
public void run() {
try {
runnable.run();
- } catch (final CoreException e) {
- error.set(e);
+ }
+ catch (final CoreException e) {
+ this.error= e;
}
}
- });
- if (error.get() != null) {
- throw error.get();
+
+ };
+ display.syncExec(catchRunnable);
+ final var error= catchRunnable.error;
+ if (error != null) {
+ throw error;
}
}
+ public static void checkedSyncExec(final CheckedRunnable runnable)
+ throws CoreException {
+ checkedSyncExec(runnable, getDisplay());
+ }
+
@NonNullByDefault({ PARAMETER })
- public static <T> T syncExecGet(final Supplier<T> getter) {
- class GetRunnable implements Runnable {
+ public static <T> T syncExecGet(final Supplier<T> getter, final Display display) {
+ final var getRunnable= new Runnable() {
+
@SuppressWarnings("null")
- volatile T value;
+ private volatile T value;
+
@Override
public void run() {
this.value= getter.get();
}
- }
- final GetRunnable runnable= new GetRunnable();
- UIAccess.getDisplay().syncExec(runnable);
- return runnable.value;
+
+ };
+ display.syncExec(getRunnable);
+ return getRunnable.value;
+ }
+
+ @NonNullByDefault({ PARAMETER })
+ public static <T> T syncExecGet(final Supplier<T> getter) {
+ return syncExecGet(getter, getDisplay());
}
@@ -166,7 +176,7 @@
*/
public static final boolean isOkToUse(final Viewer viewer) {
Control control;
- return ((viewer != null) && ((control = viewer.getControl()) != null)
+ return ((viewer != null) && ((control= viewer.getControl()) != null)
&& !control.isDisposed() && (Display.getCurrent() != null));
}