-
-
Save kevenli/e1c385919f1316b902fe398662a78057 to your computer and use it in GitHub Desktop.
A main class wrapper for deck2pdf that runs the application headless using Monocle from jfxtras.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.champeau.deck2pdf; | |
import java.lang.reflect.Field; | |
import java.util.Objects; | |
import javafx.application.Application; | |
/** | |
* Adapted from https://gist.github.com/hastebrot/cbca1128dd791966e3a6 | |
*/ | |
public class HeadlessMain { | |
//--------------------------------------------------------------------------------------------- | |
// 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 static void main(String[] args) { | |
new HeadlessMain().launch(Main.class, args); | |
} | |
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