From 5231b1c3fe40cd6143dd901355f5c7d9c9d05f67 Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Fri, 2 Apr 2021 16:07:50 +0200 Subject: [PATCH] Fix more editor bugs --- .../app/editor/EditorCollectorNode.java | 13 +++++++++++++ .../pdx_unlimiter/app/editor/EditorNode.java | 5 +++++ .../pdx_unlimiter/app/editor/EditorSimpleNode.java | 12 +++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorCollectorNode.java b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorCollectorNode.java index ba93b50c..903ef5d5 100644 --- a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorCollectorNode.java +++ b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorCollectorNode.java @@ -18,6 +18,19 @@ public EditorCollectorNode(EditorNode directParent, String keyName, int parentIn this.nodes = nodes; } + @Override + public void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) { + getRealParent().updateNodeAtIndex(replacementValue, keyName, firstNodeIndex + index); + } + + @Override + public void replacePart(ArrayNode toInsert, int beginIndex, int length) { + getRealParent().replacePart( + ArrayNode.sameKeyArray(keyName, toInsert.getNodeArray()), + firstNodeIndex + beginIndex, + length); + } + @Override public void delete() { getRealParent().replacePart( diff --git a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorNode.java b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorNode.java index fd90fce8..5b6d0714 100644 --- a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorNode.java +++ b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorNode.java @@ -1,6 +1,7 @@ package com.crschnick.pdx_unlimiter.app.editor; import com.crschnick.pdx_unlimiter.core.node.ArrayNode; +import com.crschnick.pdx_unlimiter.core.node.Node; import com.crschnick.pdx_unlimiter.core.node.NodeMatcher; import java.util.ArrayList; @@ -72,6 +73,10 @@ public static List create(EditorNode parent, ArrayNode ar) { return result; } + public abstract void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index); + + public abstract void replacePart(ArrayNode toInsert, int beginIndex, int length); + public abstract void delete(); public abstract boolean filterKey(Predicate filter); diff --git a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorSimpleNode.java b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorSimpleNode.java index bf4828d3..c5e2106c 100644 --- a/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorSimpleNode.java +++ b/app/src/main/java/com/crschnick/pdx_unlimiter/app/editor/EditorSimpleNode.java @@ -30,14 +30,14 @@ public void updateColor(Color c) { cn.set(newColorNode); } - private void updateParentNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) { + public void updateNodeAtIndex(Node replacementValue, String toInsertKeyName, int index) { ArrayNode ar = (ArrayNode) backingNode; var replacement = toInsertKeyName != null ? ArrayNode.singleKeyNode(toInsertKeyName, replacementValue) : ArrayNode.array(List.of(replacementValue)); this.backingNode = ar.replacePart(replacement, index, 1); if (getDirectParent() != null) { - getRealParent().updateParentNodeAtIndex(this.backingNode, keyName, getKeyIndex()); + getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex()); } } @@ -47,14 +47,14 @@ public void replacePart(ArrayNode toInsert, int beginIndex, int length) { // Update parent node to reflect change if (getDirectParent() != null) { - getRealParent().updateParentNodeAtIndex(this.backingNode, keyName, getKeyIndex()); + getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex()); } } @Override public void delete() { if (getDirectParent() != null) { - getRealParent().replacePart(ArrayNode.emptyArray(), getKeyIndex(), 1); + getDirectParent().replacePart(ArrayNode.emptyArray(), getKeyIndex(), 1); } } @@ -108,9 +108,7 @@ public void update(ArrayNode newNode) { // Update parent node to reflect change if (getDirectParent() != null) { - var replacement = getKeyName().map(k -> ArrayNode.singleKeyNode(k, this.backingNode)) - .orElse(ArrayNode.array(this.backingNode.getNodeArray())); - getRealParent().replacePart(replacement, getKeyIndex(), 1); + getDirectParent().updateNodeAtIndex(this.backingNode, keyName, getKeyIndex()); } } else { var nodeToUse = newNode.getNodeArray().get(0);