Skip to content

Commit 52de733

Browse files
committed
Add toTreeString methods
1 parent d1ff3d5 commit 52de733

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

patternfx-core/src/main/java/com/techsenger/patternfx/core/AbstractParentComponent.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.techsenger.toolkit.fx.binding.ListBinder;
2020
import java.util.List;
21+
import java.util.function.BiConsumer;
2122
import javafx.collections.FXCollections;
2223
import javafx.collections.ListChangeListener;
2324
import javafx.collections.ObservableList;
@@ -67,6 +68,16 @@ List<ParentViewModel<?>> getChildren(ParentViewModel<?> parent) {
6768
};
6869
}
6970

71+
@Override
72+
public String toTreeString() {
73+
return component.toTreeString(depthFirstIterator(), (c, b) -> b.append(c.getMediator().getFullName()));
74+
}
75+
76+
@Override
77+
public String toTreeString(BiConsumer<ParentViewModel<?>, StringBuilder> componentAppender) {
78+
return component.toTreeString(depthFirstIterator(), componentAppender);
79+
}
80+
7081
@Override
7182
public ObservableList<ChildViewModel<?>> getChildren() {
7283
return children;
@@ -122,10 +133,34 @@ public ObservableList<ChildComponent<?>> getChildren() {
122133
return this.children;
123134
}
124135

136+
@Override
137+
public String toTreeString() {
138+
return toTreeString(depthFirstIterator(), (c, b) -> b.append(c.getFullName()));
139+
}
140+
141+
@Override
142+
public String toTreeString(BiConsumer<ParentComponent<?>, StringBuilder> componentAppender) {
143+
return toTreeString(depthFirstIterator(), componentAppender);
144+
}
145+
125146
protected ObservableList<ChildComponent<?>> getModifiableChildren() {
126147
return modifiableChildren;
127148
}
128149

129150
@Override
130151
protected abstract Mediator createMediator();
152+
153+
public <T> String toTreeString(SubtreeIterator<T> iterator, BiConsumer<T, StringBuilder> componentAppender) {
154+
var builder = new StringBuilder();
155+
var sep = System.lineSeparator();
156+
while (iterator.hasNext()) {
157+
var c = iterator.next();
158+
if (builder.length() > 0) {
159+
builder.append(sep);
160+
}
161+
builder.append(" ".repeat(iterator.getDepth()));
162+
componentAppender.accept(c, builder);
163+
}
164+
return builder.toString();
165+
}
131166
}

patternfx-core/src/main/java/com/techsenger/patternfx/core/ParentComponent.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.techsenger.patternfx.core;
1818

19+
import java.util.function.BiConsumer;
1920
import javafx.collections.ObservableList;
2021

2122
/**
@@ -44,4 +45,24 @@ public interface ParentComponent<T extends ParentView<?, ?>> extends Component<T
4445
* @return an {@link Iterator} that iterates over this component and all of its descendants
4546
*/
4647
SubtreeIterator<ParentComponent<?>> breadthFirstIterator();
48+
49+
/**
50+
* Returns a string representation of this component and all its descendants formatted as a tree.
51+
*
52+
* @return a tree-formatted string representation of this component
53+
*/
54+
String toTreeString();
55+
56+
/**
57+
* Returns a string representation of this component and all its descendants formatted as a tree, allowing the
58+
* caller to append an additional note for each component.
59+
*
60+
* The provided {@code componentAppender} is invoked for each component and is responsible for appending the
61+
* complete string representation of that component to the given {@link StringBuilder}. The tree structure and
62+
* line separation are handled by this method.
63+
*
64+
* @param componentAppender a callback used to append the full string representation of each component.
65+
* @return a tree-formatted string representation of this component
66+
*/
67+
String toTreeString(BiConsumer<ParentComponent<?>, StringBuilder> componentAppender);
4768
}

patternfx-core/src/main/java/com/techsenger/patternfx/core/ParentMediator.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.techsenger.patternfx.core;
1818

19+
import java.util.function.BiConsumer;
1920
import javafx.collections.ObservableList;
2021

2122
/**
@@ -44,4 +45,24 @@ public interface ParentMediator extends ComponentMediator {
4445
* @return an {@link Iterator} that iterates over this component and all of its descendants
4546
*/
4647
SubtreeIterator<ParentViewModel<?>> breadthFirstIterator();
48+
49+
/**
50+
* Returns a string representation of this component and all its descendants formatted as a tree.
51+
*
52+
* @return a tree-formatted string representation of this component
53+
*/
54+
String toTreeString();
55+
56+
/**
57+
* Returns a string representation of this component and all its descendants formatted as a tree, allowing the
58+
* caller to append an additional note for each component.
59+
*
60+
* <p>The provided {@code componentAppender} is invoked for each component and is responsible for appending the
61+
* complete string representation of that component to the given {@link StringBuilder}. The tree structure and
62+
* line separation are handled by this method.
63+
*
64+
* @param componentAppender a callback used to append the full string representation of each component.
65+
* @return a tree-formatted string representation of this component
66+
*/
67+
String toTreeString(BiConsumer<ParentViewModel<?>, StringBuilder> componentAppender);
4768
}

0 commit comments

Comments
 (0)