Files
WebClient/src/main/java/org/openautonomousconnection/webclient/ui/BrowserTab.java

156 lines
3.4 KiB
Java
Raw Normal View History

package org.openautonomousconnection.webclient.ui;
2026-02-08 22:36:42 +01:00
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<String> onLocationChange) {
this.key = Objects.requireNonNull(initialUrl, "initialUrl"); // placeholder key overwritten by BrowserUI
this.view = new TabView(onLocationChange, initialUrl);
2026-02-08 22:36:42 +01:00
}
/**
* 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);
2026-02-08 22:36:42 +01:00
}
/**
* 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) {
2026-02-11 23:31:49 +01:00
super("about:blank", s -> {
});
2026-02-08 22:36:42 +01:00
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);
2026-02-08 22:36:42 +01:00
}
@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();
}
}
}