diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-06-09 11:43:05 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-06-09 11:43:05 +0300 |
commit | 802551195ba60f21c634081e9e694f93feed26a3 (patch) | |
tree | e06c02d769aa136cc110c2c1de54265e414d62f1 /src | |
parent | 48bdcca98a8d28208b452b030e08cc8e23fa8aed (diff) | |
download | LibGui-802551195ba60f21c634081e9e694f93feed26a3.tar.gz LibGui-802551195ba60f21c634081e9e694f93feed26a3.tar.bz2 LibGui-802551195ba60f21c634081e9e694f93feed26a3.zip |
Prevent adding widgets to themselves recursively
Also includes any parents.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 227376b..912c1c0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -9,6 +9,7 @@ import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.widget.data.Insets; import org.jetbrains.annotations.Nullable; +import java.util.AbstractList; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -23,7 +24,7 @@ public abstract class WPanel extends WWidget { * * <p>The list is mutable. */ - protected final List<WWidget> children = new ArrayList<>(); + protected final List<WWidget> children = new WidgetList(this, new ArrayList<>()); @Environment(EnvType.CLIENT) private BackgroundPainter backgroundPainter = null; @@ -250,4 +251,62 @@ public abstract class WPanel extends WWidget { public String toString() { return getClass().getSimpleName() + " {\n" + children.stream().map(Object::toString).map(x -> x + ",").flatMap(x -> Stream.of(x.split("\n")).filter(y -> !y.isEmpty()).map(y -> "\t" + y)).collect(Collectors.joining("\n")) + "\n}"; } + + private static final class WidgetList extends AbstractList<WWidget> { + private final WPanel owner; + private final List<WWidget> backing; + + private WidgetList(WPanel owner, List<WWidget> backing) { + this.owner = owner; + this.backing = backing; + } + + @Override + public WWidget get(int index) { + return backing.get(index); + } + + private void checkWidget(WWidget widget) { + if (widget == null) { + throw new NullPointerException("Adding null widget to " + owner); + } + + int n = 0; + WWidget parent = owner; + while (parent != null) { + if (widget == parent) { + if (n == 0) { + throw new IllegalArgumentException("Adding panel to itself: " + widget); + } else { + throw new IllegalArgumentException("Adding level " + n + " parent recursively to " + owner + ": " + widget); + } + } + + parent = parent.getParent(); + n++; + } + } + + @Override + public WWidget set(int index, WWidget element) { + checkWidget(element); + return backing.set(index, element); + } + + @Override + public void add(int index, WWidget element) { + checkWidget(element); + backing.add(index, element); + } + + @Override + public WWidget remove(int index) { + return backing.remove(index); + } + + @Override + public int size() { + return backing.size(); + } + } } |