From d06cc66f318b1f6504135d33310a601c1c460c44 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Wed, 19 Aug 2020 21:23:10 +0200 Subject: [PATCH] Adds a WebServer class as a fallback when native browsing is unavailable --- .../userinterface/WebBrowser.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java new file mode 100644 index 0000000..c4d53ee --- /dev/null +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/userinterface/WebBrowser.java @@ -0,0 +1,55 @@ +package net.knarcraft.minecraftserverlauncher.userinterface; + +import javax.swing.*; +import javax.swing.event.HyperlinkEvent; +import java.io.IOException; + +/** + * This class allows for displaying web pages + */ +public class WebBrowser { + + private static JFrame browserFrame; + private static JTextPane editorPane; + + private WebBrowser() {} + + /** + * Instantiates a new web browser + */ + private static void instantiate() { + editorPane = new JTextPane(); + editorPane.setEditable(false); + browserFrame = new JFrame("Web Browser"); + JScrollPane scrollPane = new JScrollPane(editorPane); + browserFrame.add(scrollPane); + + browserFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + browserFrame.setBounds(100, 100, 800, 600); + } + + /** + * Displays the web page with the given URL + * + * @param url

The URL to open

+ */ + public static void displayPage(String url) { + if (browserFrame == null) { + instantiate(); + } + browserFrame.setVisible(true); + try { + browserFrame.setTitle("Browsing: " + url); + editorPane.setPage(url); + editorPane.setContentType("text/html"); + editorPane.addHyperlinkListener(hyperlinkEvent -> { + if (hyperlinkEvent.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) { + displayPage(hyperlinkEvent.getURL().toString()); } + }); + } catch (IOException e) { + editorPane.setContentType("text/html"); + editorPane.setText("Could not load"); + } + } + +}