aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/layout/InlineLayout.java
blob: c4c51b5ce78b369ec3669eee8975a1718fdb8c11 (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 model.layout;

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

import java.awt.*;

public class InlineLayout extends Layout {

    private Point cursor;

    public InlineLayout(Node node, Layout parent) {
        super(node, parent);
        cursor = new Point();
    }

    // recursively construct the layout tree
    public void layout() {
        this.setLocation(this.getParent().getLocation());
        this.getPreviousSibling().ifPresent(
                sibling -> this.setY(sibling.getY() + sibling.getHeight()));

        this.setWidth(this.getParent().getWidth());
        this.setCursor(this.getX(), this.getY());

        Node node = this.getAssociatedNode();
        switch (node) {
            case ElementNode e -> {
                if (e.getTag().equals("a")) {
                    this.setX(this.getX() + this.getParent().getWidth());
                }
            }
            default -> {
                if (node.getData().length() > 5) {
                    this.setHeight(20);
//                    this.setWidth(this.getWidth() + node.getData().length());
                }
            }
        }

        for (Layout child : this.getChildren()) {
            child.layout();
            this.setHeight(this.getHeight() + child.getHeight()); // fixme
        }

        // todo: recurse to calculate cursor
//        this.setHeight(cursor.getY() - this.getY());
//        System.out.println(this.getAssociatedNode().getData() + this.getLocation());
    }

    public void setCursor(Point cursor) {
        this.cursor = cursor;
    }

    public void setCursor(double x, double y) {
        this.cursor.setLocation(x, y);
    }
}