All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fge.grappa.debugger.javafx.parsetree.ParseTreeNodeCell Maven / Gradle / Ivy

The newest version!
package com.github.fge.grappa.debugger.javafx.parsetree;

import com.github.fge.grappa.debugger.csvtrace.tabs.tree.TreeTabDisplay;
import com.github.fge.grappa.debugger.model.ParseTreeNode;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TreeCell;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import org.fxmisc.easybind.EasyBind;

public final class ParseTreeNodeCell
    extends TreeCell
{
    private final Text text = new Text();
    private final ProgressBar progressBar = new ProgressBar();
    private final HBox hBox = new HBox(text);

    public ParseTreeNodeCell(final TreeTabDisplay display)
    {
        setEditable(false);

        selectedProperty().addListener(new ChangeListener()
        {
            @SuppressWarnings("AutoUnboxing")
            @Override
            public void changed(
                final ObservableValue observable,
                final Boolean oldValue, final Boolean newValue)
            {
                if (!newValue)
                    return;
                final ParseTreeNode node = getItem();
                if (node != null)
                    display.parseTreeNodeShowEvent(node);
            }
        });

        final ObservableValue loading
            = EasyBind.select(treeItemProperty())
            .selectObject(item -> ((ParseTreeItem) item).loadingProperty());

        loading.addListener(new ChangeListener()
        {
            @Override
            public void changed(
                final ObservableValue observable,
                final Boolean oldValue, final Boolean newValue)
            {
                final ObservableList children = hBox.getChildren();
                if (newValue == null || !newValue.booleanValue()) {
                    children.remove(progressBar);
                    return;
                }

                if (!children.contains(progressBar))
                    children.add(progressBar);
            }
        });

        itemProperty().addListener(new ChangeListener()
        {
            @Override
            public void changed(
                final ObservableValue observable,
                final ParseTreeNode oldValue, final ParseTreeNode newValue)
            {
                if (newValue == null) {
                    setGraphic(null);
                    return;
                }
                text.setText(String.format("%s (%s)",
                    newValue.getRuleInfo().getName(),
                    newValue.isSuccess() ? "SUCCESS" : "FAILURE"));
                setGraphic(hBox);
            }
        });
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy