Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hastebrot/cbca1128dd791966e3a6 to your computer and use it in GitHub Desktop.
Save hastebrot/cbca1128dd791966e3a6 to your computer and use it in GitHub Desktop.
import java.lang.reflect.Field;
import java.util.Objects;
import javafx.application.Application;
public class ToolkitApplicationLauncher {
//---------------------------------------------------------------------------------------------
// CONSTANTS.
//---------------------------------------------------------------------------------------------
private static final String PROPERTY_JAVAFX_MACOSX_EMBEDDED = "javafx.macosx.embedded";
private static final String PROPERTY_TESTFX_HEADLESS = "javafx.monocle.headless";
private static final String PLATFORM_FACTORY_CLASS =
"com.sun.glass.ui.PlatformFactory";
private static final String PLATFORM_FACTORY_MONOCLE_IMPL =
"com.sun.glass.ui.monocle.MonoclePlatformFactory";
private static final String NATIVE_PLATFORM_FACTORY_CLASS =
"com.sun.glass.ui.monocle.NativePlatformFactory";
private static final String NATIVE_PLATFORM_HEADLESS_IMPL =
"com.sun.glass.ui.monocle.headless.HeadlessPlatform";
//---------------------------------------------------------------------------------------------
// METHODS.
//---------------------------------------------------------------------------------------------
public void launch(Class<? extends Application> appClass,
String... appArgs) {
initMacosxEmbedded();
initMonocleHeadless();
Application.launch(appClass, appArgs);
}
//---------------------------------------------------------------------------------------------
// PRIVATE METHODS.
//---------------------------------------------------------------------------------------------
private void initMacosxEmbedded() {
if (checkSystemPropertyEquals(PROPERTY_JAVAFX_MACOSX_EMBEDDED, null)) {
System.setProperty(PROPERTY_JAVAFX_MACOSX_EMBEDDED, "true");
}
}
private void initMonocleHeadless() {
if (checkSystemPropertyEquals(PROPERTY_TESTFX_HEADLESS, "true")) {
try {
assignMonoclePlatform();
assignHeadlessPlatform();
}
catch (Exception exception) {
throw new RuntimeException(exception);
}
}
}
private boolean checkSystemPropertyEquals(String propertyName,
String valueOrNull) {
return Objects.equals(System.getProperty(propertyName, null), valueOrNull);
}
private void assignMonoclePlatform()
throws Exception {
Class<?> platformFactoryClass = Class.forName(PLATFORM_FACTORY_CLASS);
Object platformFactoryImpl = Class.forName(PLATFORM_FACTORY_MONOCLE_IMPL).newInstance();
assignPrivateStaticField(platformFactoryClass, "instance", platformFactoryImpl);
}
private void assignHeadlessPlatform()
throws Exception {
Class<?> nativePlatformFactoryClass = Class.forName(NATIVE_PLATFORM_FACTORY_CLASS);
Object nativePlatformImpl = Class.forName(NATIVE_PLATFORM_HEADLESS_IMPL).newInstance();
assignPrivateStaticField(nativePlatformFactoryClass, "platform", nativePlatformImpl);
}
private void assignPrivateStaticField(Class<?> cls,
String name,
Object value)
throws Exception {
Field field = cls.getDeclaredField(name);
field.setAccessible(true);
field.set(cls, value);
field.setAccessible(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment