STILEntryTreeItem.java

package ui.stilview;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;
import libsidutils.stil.STIL.STILEntry;
import libsidutils.stil.STIL.TuneEntry;

import java.util.ArrayList;
import java.util.Collection;

public class STILEntryTreeItem extends TreeItem<Object> {

    private boolean hasLoadedChildren;

    public STILEntryTreeItem(STILEntry entry) {
        super(entry);
    }

    @Override
    public boolean isLeaf() {
        STILEntry stilEntry = (STILEntry) getValue();
        return stilEntry.getSubTunes().isEmpty();
    }

    @Override
    public ObservableList<TreeItem<Object>> getChildren() {
        if (!hasLoadedChildren) {
            hasLoadedChildren = true;
            Collection<TuneEntryTreeItem> children = new ArrayList<>();
            STILEntry stilEntry = (STILEntry) getValue();
            for (TuneEntry tuneEntry : stilEntry.getSubTunes()) {
                children.add(new TuneEntryTreeItem(tuneEntry));
            }
            super.getChildren().setAll(children);
        }
        return super.getChildren();
    }
}