2026-01-16 21:47:04 +01:00
|
|
|
package org.openautonomousconnection.luascript.hosts;
|
|
|
|
|
|
2026-01-19 22:23:49 +01:00
|
|
|
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;
|
|
|
|
|
|
2026-01-16 21:47:04 +01:00
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Service container holding optional host capabilities.
|
|
|
|
|
*
|
|
|
|
|
* <p>This avoids one huge "bridge" interface.</p>
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-19 22:23:49 +01:00
|
|
|
/**
|
|
|
|
|
* Builds a JavaFX WebView preset of HostServices (DOM + events + resources + UI).
|
|
|
|
|
*
|
|
|
|
|
* <p>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.</p>
|
|
|
|
|
*
|
|
|
|
|
* <p>Note: You should call {@link FxDomHost#ensureAllElementsHaveId()} after the WebEngine finished loading.</p>
|
|
|
|
|
*
|
|
|
|
|
* @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();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 21:53:46 +01:00
|
|
|
/**
|
|
|
|
|
* @return builder
|
|
|
|
|
*/
|
|
|
|
|
public static Builder builder() {
|
|
|
|
|
return new Builder();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 21:47:04 +01:00
|
|
|
/**
|
|
|
|
|
* @return optional DomHost capability
|
|
|
|
|
*/
|
|
|
|
|
public Optional<DomHost> dom() {
|
|
|
|
|
return Optional.ofNullable(dom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return optional EventHost capability
|
|
|
|
|
*/
|
|
|
|
|
public Optional<EventHost> events() {
|
|
|
|
|
return Optional.ofNullable(events);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return optional ResourceHost capability
|
|
|
|
|
*/
|
|
|
|
|
public Optional<ResourceHost> resources() {
|
|
|
|
|
return Optional.ofNullable(resources);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 21:53:46 +01:00
|
|
|
/**
|
|
|
|
|
* @return optional console host
|
|
|
|
|
*/
|
2026-01-16 21:47:04 +01:00
|
|
|
public Optional<ConsoleHost> console() {
|
|
|
|
|
return Optional.ofNullable(console);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 21:53:46 +01:00
|
|
|
/**
|
|
|
|
|
* @return optional ui host
|
|
|
|
|
*/
|
2026-01-16 21:47:04 +01:00
|
|
|
public Optional<UiHost> 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|