Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import javax.swing.event.HyperlinkEvent;
- import javax.swing.event.HyperlinkListener;
- import javax.swing.text.html.HTMLDocument;
- import javax.swing.text.html.HTMLFrameHyperlinkEvent;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Stack;
- public class SimpleBrowser extends JFrame implements HyperlinkListener
- {
- private JTextField addressBar = new JTextField(40);
- private JEditorPane webPane = new JEditorPane();
- private URL currentURL = null;
- private static Stack<URL> historyBack = new Stack<>();;
- private static Stack<URL> historyForward = new Stack<>();;
- public SimpleBrowser()
- {
- setSize(800, 600);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- JPanel navigationPanel = new JPanel();
- navigationPanel.setLayout(new BorderLayout());
- addressBar.addActionListener((ActionEvent e) -> {
- if (addressBar.getText().equals(""))
- return;
- search();
- });
- navigationPanel.add(addressBar, BorderLayout.CENTER);
- JButton backButton = new JButton("<");
- backButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent b) {
- back();
- }
- });
- JButton forwardButton = new JButton(">");
- forwardButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent f) {
- forward();
- }
- });
- navigationPanel.add(backButton, BorderLayout.NORTH);
- navigationPanel.add(forwardButton, BorderLayout.SOUTH);
- JButton goButton = new JButton("Suche");
- goButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- search();
- }
- });
- navigationPanel.add(goButton, BorderLayout.EAST);
- webPane.setContentType("text/html");
- webPane.setEditable(false);
- webPane.addHyperlinkListener(this);
- getContentPane().setLayout(new BorderLayout());
- getContentPane().add(navigationPanel, BorderLayout.NORTH);
- getContentPane().add(new JScrollPane(webPane), BorderLayout.CENTER);
- }
- public void back()
- {
- if (historyBack.empty())
- {
- System.out.println("Cannot got back. Stack is empty");
- return;
- }
- URL poppedURL = historyBack.pop();
- historyForward.push(currentURL);
- showPage(poppedURL);
- }
- public void forward()
- {
- if (historyForward.empty())
- {
- System.out.println("Cannot got forward. Stack is empty");
- return;
- }
- URL poppedURL = historyForward.pop();
- historyBack.push(currentURL);
- showPage(poppedURL);
- }
- public void search()
- {
- URL verifiedURL = checkURL(addressBar.getText());
- if (verifiedURL != null)
- {
- showPage(verifiedURL);
- historyForward.clear();
- }
- else
- {
- System.out.println("Fehler in der URL");
- }
- }
- public void showPage(URL url)
- {
- if (currentURL!=null)
- historyBack.push(currentURL);
- currentURL = url;
- String content = null;
- URLConnection connection;
- try
- {
- connection = url.openConnection();
- boolean textSite = connection.getHeaderField("Content-Type").contains("text/html") || connection.getHeaderField("Content-Type").contains("text/plain");
- webPane.setContentType(connection.getHeaderField("Content-Type"));
- if (textSite)
- {
- try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())))
- {
- String inputLine;
- StringBuilder stringBuilder = new StringBuilder();
- while ((inputLine = bufferedReader.readLine()) != null)
- {
- stringBuilder.append(inputLine);
- }
- content = stringBuilder.toString();
- }
- }
- }
- catch (Exception e)
- {
- }
- System.out.println(content);
- webPane.setText(content);
- }
- public URL checkURL(String urlString)
- {
- if (urlString.toLowerCase().startsWith("https://"))
- {
- return null;
- }
- if (!urlString.toLowerCase().startsWith("http://"))
- {
- urlString = "http://" + urlString;
- }
- URL validURL;
- try
- {
- validURL = new URL(urlString);
- }
- catch (MalformedURLException e)
- {
- return null;
- }
- return validURL;
- }
- public void hyperlinkUpdate(HyperlinkEvent e)
- {
- HyperlinkEvent.EventType eventType = e.getEventType();
- if (eventType == HyperlinkEvent.EventType.ACTIVATED)
- {
- if (e instanceof HTMLFrameHyperlinkEvent)
- {
- HTMLFrameHyperlinkEvent linkEvent = (HTMLFrameHyperlinkEvent) e;
- HTMLDocument document = (HTMLDocument) webPane.getDocument();
- document.processHTMLFrameHyperlinkEvent(linkEvent);
- }
- else
- {
- showPage(e.getURL());
- }
- }
- }
- public static void main(String[] args)
- {
- SimpleBrowser browser = new SimpleBrowser();
- browser.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement