aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserWindow.java
blob: d774a8f3de0afb2cb52041f0012ba239a984a8e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ui;

import model.BrowserState;
import model.html.HtmlParser;

import javax.swing.*;
import java.awt.*;
import java.nio.file.*;
import java.util.*;

// Broad JFrame usage taken from here: https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
public class BrowserWindow extends JFrame {
    public static final int WIDTH = 1500;
    public static final int HEIGHT = 500;

    private BrowserCanvas canvas;
    private final BrowserBar browserBar;
    private final BrowserState state;

    public BrowserWindow() {
        super("apus");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        state = new BrowserState(new ArrayDeque<>(), "");
        canvas = new BrowserCanvas(new ArrayList<>());
        browserBar = new BrowserBar(this);
        getContentPane().add(browserBar, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        render("/home/apropos/Projects/website/j-james/index.html");
//        browserBar.addTab("/home/apropos/Projects/website/j-james/index.html");
        setVisible(true);
    }

    public void render(String uri) {
        state.currentTab = uri;
        remove(canvas);
        try {
            String file = Files.readString(Path.of(state.currentTab));
            HtmlParser parser = new HtmlParser();
            canvas = new BrowserCanvas(parser.parseHtml(file));
        } catch (Exception e) {
            System.out.println("Could not read file, rendering empty page: " + e.getMessage());
            canvas = new BrowserCanvas(new ArrayList<>());
        }
        add(canvas);
        repaint();
        setVisible(true);
    }

    public BrowserState getBrowserState() {
        return this.state;
    }
}