package org.openautonomousconnection.webclient.ui; import java.util.Objects; import java.util.function.Consumer; /** * A logical browser tab consisting of a key (tab id), a TabView (WebView), * and navigation helpers. */ public class BrowserTab { private final String key; private final TabView view; /** * Creates a browser tab. * * @param initialUrl initial URL (used for initial location value) * @param onLocationChange callback invoked on URL changes */ public BrowserTab(String initialUrl, Consumer onLocationChange) { this.key = Objects.requireNonNull(initialUrl, "initialUrl"); // placeholder key overwritten by BrowserUI this.view = new TabView(onLocationChange, initialUrl); } /** * Sets the key (tab id) used by the UI host. * * @param key tab key * @return this */ public BrowserTab withKey(String key) { // The BrowserUI uses titles as keys; keep logic simple. return new BrowserTabKeyed(key, view); } /** * Loads a URL. * * @param url URL */ public void loadUrl(String url) { view.loadUrl(url); } /** * Goes back in history if possible. */ public void goBack() { view.back(); } /** * Goes forward in history if possible. */ public void goForward() { view.forward(); } /** * Reloads the page. */ public void reload() { view.reload(); } /** * Returns current location if known. * * @return URL or null */ public String getLocation() { return view.getEngineLocation(); } /** * Releases resources. */ public void dispose() { view.dispose(); } /** * Returns the tab key. * * @return key */ public String getKey() { return key; } /** * Returns the Swing component that renders the web content. * * @return tab view */ public TabView getView() { return view; } /** * Internal keyed wrapper so BrowserUI can store a stable key without re-creating the WebView. */ private static final class BrowserTabKeyed extends BrowserTab { private final String fixedKey; private final TabView fixedView; private BrowserTabKeyed(String fixedKey, TabView fixedView) { super("about:blank", s -> { }); this.fixedKey = Objects.requireNonNull(fixedKey, "fixedKey"); this.fixedView = Objects.requireNonNull(fixedView, "fixedView"); } @Override public String getKey() { return fixedKey; } @Override public TabView getView() { return fixedView; } @Override public void loadUrl(String url) { fixedView.loadUrl(url); } @Override public void goBack() { fixedView.back(); } @Override public void goForward() { fixedView.forward(); } @Override public void reload() { fixedView.reload(); } @Override public String getLocation() { return fixedView.getEngineLocation(); } @Override public void dispose() { fixedView.dispose(); } } }