Skip to content

Commit

Permalink
use Stream for FxProject children
Browse files Browse the repository at this point in the history
* adapt tests
  • Loading branch information
hansi-b committed Sep 8, 2024
1 parent 98e71b7 commit d371342
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.awt.GraphicsEnvironment;
import java.awt.SystemTray;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -64,25 +62,14 @@ static TrayIconMenu create(Stage primaryStage, ObservableData data) {
.addTitleItem(true) //
.separator();

for (MenuItem p : createProjectMenus(data))
menuBuilder.menuItem(p);
data.fxProjectTree().children().map(project -> createMenu(project)).forEach(menuBuilder::menuItem);

tim.iconWithMenu = menuBuilder //
.separator().addExitMenuItem(MenuItems.Exit.fmt()) //
.show().build();
return tim;
}

private static List<MenuItem> createProjectMenus(ObservableData data) {
FxProject projectTree = data.fxProjectTree();

List<MenuItem> menus = new ArrayList<>();
for (FxProject project : projectTree.children()) {
menus.add(createMenu(project));
}
return menus;
}

/*
* Create menu items of all project leaves in this project.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ public boolean hasChildren() {
}

@Override
public Iterable<FxProject> children() {
return project.children().stream().map(fxByProject::get).toList();
public Stream<FxProject> children() {
return project.children().stream().map(fxByProject::get);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.hansib.simplertimes.fx.tree;

import java.util.Comparator;
import java.util.stream.Stream;

import javafx.scene.control.TreeItem;

Expand All @@ -45,7 +46,7 @@ interface PreRemovalCallback<T> {

T addChild(String childText);

Iterable<T> children();
Stream<T> children();

/**
* Sorts the children of this node by their texts according to the argument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class FxProjectSpec extends Specification {

when:
def fx = FxProject.root(root)
def pFx = fx.children()[0]
def s2Fx = pFx.children()[0].children()[0]
def pFx = children(fx)[0]
def s2Fx = children(children(pFx)[0])[0]

then:
s2Fx.formatName(pFx, " - ") == "hello - world"
Expand All @@ -77,11 +77,11 @@ public class FxProjectSpec extends Specification {
def fx = FxProject.root(root)

then:
def children = fx.children()
def c = children(fx)
fx.flatList() == [
children[0],
children[0].children()[0],
children[1]
c[0],
children(c[0])[0],
c[1]
]
}

Expand All @@ -90,16 +90,16 @@ public class FxProjectSpec extends Specification {
given:
def c1 = root.add('1')
c1.add('1b')
root.add('2')
root.add('2x')

when:
def fx = FxProject.root(root)

then:
def children = fx.children()
def c = children(fx)
fx.leafChildren().toList() == [
children[0].children()[0],
children[1]
children(c[0])[0], // 1b
c[1] // 2x
]
}

Expand All @@ -112,7 +112,10 @@ public class FxProjectSpec extends Specification {
def fx = FxProject.root(root)

then:
def children = fx.children()
children[0].leafChildren().toList() == []
children(fx)[0].leafChildren().toList() == []
}

static List<FxProject> children(FxProject p) {
p.children().toList()
}
}

0 comments on commit d371342

Please sign in to comment.