Small SWT refactor

This commit is contained in:
Robinson 2021-01-11 00:18:21 +01:00
parent 9ebc3499ea
commit f25ff4ddbf
2 changed files with 24 additions and 21 deletions

View File

@ -43,6 +43,8 @@ class Swt {
// Since this class is the place other code interacts with, we can use SWT stuff if necessary without loading/linking // Since this class is the place other code interacts with, we can use SWT stuff if necessary without loading/linking
// the SWT classes by accident // the SWT classes by accident
boolean isSwtLoadable_ = isLoadable(); boolean isSwtLoadable_ = isLoadable();
version = SWT.getVersion(); version = SWT.getVersion();
@ -76,7 +78,7 @@ class Swt {
} }
}); });
if (null != swtErrorClass) { if (swtErrorClass != null) {
try { try {
return org.eclipse.swt.SWT.isLoadable(); return org.eclipse.swt.SWT.isLoadable();
} catch (Exception ignored) { } catch (Exception ignored) {
@ -96,6 +98,7 @@ class Swt {
return false; return false;
} }
// required to use reflection, because this is an internal class
final String SWT_INTERNAL_CLASS = "org.eclipse.swt.internal.gtk.OS"; final String SWT_INTERNAL_CLASS = "org.eclipse.swt.internal.gtk.OS";
Class<?> osClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() { Class<?> osClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
@Override @Override
@ -148,24 +151,6 @@ class Swt {
} }
public static
class SwtOverride {
static
void onShutdown(final Display currentDisplay, final Runnable runnable) {
// currentDisplay.getShells() must only be called inside the event thread!
org.eclipse.swt.widgets.Shell shell = currentDisplay.getShells()[0];
shell.addListener(org.eclipse.swt.SWT.Close, new org.eclipse.swt.widgets.Listener() {
@Override
public
void handleEvent(final org.eclipse.swt.widgets.Event event) {
runnable.run();
}
});
}
}
public static public static
int getVersion() { int getVersion() {
return version; return version;
@ -185,13 +170,13 @@ class Swt {
void onShutdown(final Runnable runnable) { void onShutdown(final Runnable runnable) {
// currentDisplay.getShells() must only be called inside the event thread! // currentDisplay.getShells() must only be called inside the event thread!
if (isEventThread()) { if (isEventThread()) {
SwtOverride.onShutdown(currentDisplay, runnable); SwtAccess.onShutdown(currentDisplay, runnable);
} else { } else {
dispatch(new Runnable() { dispatch(new Runnable() {
@Override @Override
public public
void run() { void run() {
SwtOverride.onShutdown(currentDisplay, runnable); SwtAccess.onShutdown(currentDisplay, runnable);
} }
}); });
} }

View File

@ -0,0 +1,18 @@
package dorkbox.util.swt;
public
class SwtAccess {
static
void onShutdown(final org.eclipse.swt.widgets.Display currentDisplay, final Runnable runnable) {
// currentDisplay.getShells() must only be called inside the event thread!
org.eclipse.swt.widgets.Shell shell = currentDisplay.getShells()[0];
shell.addListener(org.eclipse.swt.SWT.Close, new org.eclipse.swt.widgets.Listener() {
@Override
public
void handleEvent(final org.eclipse.swt.widgets.Event event) {
runnable.run();
}
});
}
}