Adds a WebServer class as a fallback when native browsing is unavailable

This commit is contained in:
Kristian Knarvik 2020-08-19 21:23:10 +02:00
parent 3d6476b1ef
commit d06cc66f31

View File

@ -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 <p>The URL to open</p>
*/
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("<html>Could not load</html>");
}
}
}