package org.openautonomousconnection.luascript.hosts; import javafx.scene.web.WebEngine; import org.openautonomousconnection.luascript.fx.FxDomHost; import org.openautonomousconnection.luascript.fx.FxEventHost; import org.openautonomousconnection.luascript.fx.FxUiHost; import org.openautonomousconnection.luascript.fx.FxWebViewResourceHost; import org.openautonomousconnection.luascript.runtime.LuaRuntime; import java.util.Objects; import java.util.Optional; /** * Service container holding optional host capabilities. * *

This avoids one huge "bridge" interface.

*/ public final class HostServices { private final ConsoleHost console; private final DomHost dom; private final EventHost events; private final ResourceHost resources; private final UiHost ui; private HostServices(Builder b) { this.console = b.console; this.ui = b.ui; this.dom = b.dom; this.events = b.events; this.resources = b.resources; } /** * Builds a JavaFX WebView preset of HostServices (DOM + events + resources + UI). * *

Important: This method creates the {@link EventHost} using the provided {@link LuaRuntime}'s * {@link org.openautonomousconnection.luascript.runtime.LuaEventRouter}. Therefore, the runtime must already * be constructed before calling this method.

* *

Note: You should call {@link FxDomHost#ensureAllElementsHaveId()} after the WebEngine finished loading.

* * @param engine JavaFX WebEngine * @param runtime Lua runtime used for event routing * @param console optional console host (may be null) * @return HostServices preset for JavaFX WebView */ public static HostServices fxPreset(WebEngine engine, LuaRuntime runtime, ConsoleHost console) { Objects.requireNonNull(engine, "engine"); Objects.requireNonNull(runtime, "runtime"); FxDomHost dom = new FxDomHost(engine); FxWebViewResourceHost resources = new FxWebViewResourceHost(engine); FxUiHost ui = new FxUiHost(engine, dom); FxEventHost events = new FxEventHost(dom, runtime.eventRouter()); Builder b = builder().dom(dom).events(events).resources(resources).ui(ui); if (console != null) { b.console(console); } return b.build(); } /** * @return builder */ public static Builder builder() { return new Builder(); } /** * @return optional DomHost capability */ public Optional dom() { return Optional.ofNullable(dom); } /** * @return optional EventHost capability */ public Optional events() { return Optional.ofNullable(events); } /** * @return optional ResourceHost capability */ public Optional resources() { return Optional.ofNullable(resources); } /** * @return optional console host */ public Optional console() { return Optional.ofNullable(console); } /** * @return optional ui host */ public Optional ui() { return Optional.ofNullable(ui); } /** * Builder for HostServices. */ public static final class Builder { private ConsoleHost console; private DomHost dom; private EventHost events; private ResourceHost resources; private UiHost ui; public Builder console(ConsoleHost console) { this.console = Objects.requireNonNull(console, "console"); return this; } public Builder ui(UiHost ui) { this.ui = Objects.requireNonNull(ui, "ui"); return this; } /** * Provides dom capability. * * @param dom dom host * @return this */ public Builder dom(DomHost dom) { this.dom = Objects.requireNonNull(dom, "dom"); return this; } /** * Provides event subscription capability. * * @param events event host * @return this */ public Builder events(EventHost events) { this.events = Objects.requireNonNull(events, "events"); return this; } /** * Provides resource loading capability. * * @param resources resource host * @return this */ public Builder resources(ResourceHost resources) { this.resources = Objects.requireNonNull(resources, "resources"); return this; } public HostServices build() { return new HostServices(this); } } }