aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/BrowserCanvas.java
blob: ea20e81cf990b60c72027f30b9ba4b0daa2be0df (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
package ui;

import model.html.*;
import model.layout.*;

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

public class BrowserCanvas extends JPanel {
    private final DocumentLayout currentLayout;

    public BrowserCanvas(ArrayList<Node> html) {
        super();
        this.currentLayout = Layout.constructTree(html);
        printTree(this.currentLayout.children);
    }

    private void printTree(ArrayList<Layout> tree) {
        for (Layout node : tree) {
//            System.out.println(System.identityHashCode(node.location));
            printTree((node).children);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Point location = new Point(10, 20); // we need a mutable reference
        renderHtml(this.currentLayout.children, g, location);
    }

    private void renderHtml(ArrayList<Layout> tree, Graphics g, Point location) {
        for (Layout layout : tree) {
//            System.out.println(layout.location);
            g.drawRect(layout.location.x, layout.location.y, layout.dimension.width, layout.dimension.height);
            if (layout.associatedNode instanceof TextNode) {
                if (layout.associatedNode.data().length() > 5) {
//                    System.out.println(location);
                    g.drawString(layout.associatedNode.data(), layout.location.x, layout.location.y);
                    g.drawString("X", 10, 20);
                }
            } else {
                renderHtml(layout.children, g, location);
            }
        }
    }
}