aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/layout/Layout.java
blob: ba9f6043647596560b9b5b33dce1f501ffff5fba (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package model.layout;

import model.html.ElementNode;
import model.html.Node;

import java.awt.*;
import java.util.*;

public abstract class Layout {
    private Point location;
    private Dimension dimension;

    private Node associatedNode;
    private Layout parent;
    private Optional<Layout> previousSibling;
    private Optional<Layout> nextSibling;
    private ArrayList<Layout> children;

    public static final int DEFAULT_WIDTH = 1920;
    public static final int DEFAULT_HEIGHT = 1080;

    // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
    public static final Set<String> BLOCK_ELEMENTS = new HashSet<>(
            Arrays.asList("address", "article", "aside", "blockquote",
                    "details", "dialog", "dd", "div", "dl", "dt",
                    "fieldset", "figcaption", "figure", "footer", "form",
                    "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr",
                    "li", "main", "nav", "ol", "p", "pre", "section", "table", "ul"));

    // the big function
    public abstract void layout();

    public Layout(Node node, Layout parent) {
        this.associatedNode = node;

        this.location = new Point();
        this.dimension = new Dimension();

        this.parent = parent;
        this.previousSibling = Optional.empty();
        this.nextSibling = Optional.empty();
        this.children = new ArrayList<>();
    }

    // eh, probably the best place to put this
    // parent MAY BE nil: a handy call to Optional.ofNullable allows this
    public static ArrayList<Layout> constructTree(ArrayList<Node> html, Layout parent) {
        ArrayDeque<Layout> result = new ArrayDeque<>();
        for (Node node : html) {
            Layout layout;
            if (node instanceof ElementNode) {
                if (BLOCK_ELEMENTS.contains(((ElementNode) node).getTag())) {
                    layout = new BlockLayout(node, parent);
                } else {
                    layout = new InlineLayout(node, parent);
                }
                layout.setChildren(constructTree(((ElementNode) node).getChildren(), layout));
            } else {
                layout = new InlineLayout(node, parent);
            }

            if (result.size() > 0) {
                layout.setPreviousSibling(result.getLast());
                result.getLast().setNextSibling(layout);
            }

            result.add(layout);
        }
        return new ArrayList<>(result); // haha
    }

    public static DocumentLayout constructTree(ArrayList<Node> html) {
        DocumentLayout result = new DocumentLayout();
        result.setChildren(constructTree(html, result));
        return result;
    }

    // man, fuck design patterns, this is so much goddamn code

    public void setLocation(Point point) {
        this.location = point;
    }

    public void setX(double x) {
        this.location.setLocation(x, this.getLocation().getY());
    }

    public void setY(double y) {
        this.location.setLocation(this.getLocation().getX(), y);
    }

    public void setDimension(Dimension dimension) {
        this.dimension = dimension;
    }

    public void setDimension(double x, double y) {
        this.dimension.setSize(x, y);
    }

    public void setWidth(double width) {
        this.dimension.setSize(width, this.getDimension().getWidth());
    }

    public void setHeight(double height) {
        this.dimension.setSize(this.getDimension().getHeight(), height);
    }

    public void setPreviousSibling(Layout sibling) {
        this.previousSibling = Optional.ofNullable(sibling);
    }

    public void setNextSibling(Layout parent) {
        this.nextSibling = Optional.ofNullable(parent);
    }

    public void setChildren(ArrayList<Layout> children) {
        this.children = children;
    }

    public void addChild(Layout child) {
        this.children.add(child);
    }

    public Node getAssociatedNode() {
        return this.associatedNode;
    }

    public Point getLocation() {
        return this.location;
    }

    public double getX() {
        return this.location.getX();
    }

    public double getY() {
        return this.location.getY();
    }

    public Dimension getDimension() {
        return this.dimension;
    }

    public double getWidth() {
        return this.dimension.getWidth();
    }

    public double getHeight() {
        return this.dimension.getHeight();
    }

    public Layout getParent() {
        return this.parent;
    }

    public Optional<Layout> getPreviousSibling() {
        return this.previousSibling;
    }

    public Optional<Layout> getNextSibling() {
        return this.nextSibling;
    }

    public ArrayList<Layout> getChildren() {
        return this.children;
    }
}