package scenegraphdemo; import javafx.beans.binding.Bindings; import javafx.beans.binding.ObjectBinding; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.scene.control.TreeCell; import javafx.scene.control.TreeView; import javafx.util.Callback; /** * A TreeCell implementation that searches for templates to create * nodes for a cell. * * @author Mr. Java * */ @SuppressWarnings("rawtypes") public class TemplatedTreeCell extends TreeCell { public static Callback createCellFactoryCallback() { return new Callback() { @Override public TreeCell call(TreeView arg0) { TreeCell tc = new TemplatedTreeCell(); return tc; } }; } public TemplatedTreeCell() { @SuppressWarnings("rawtypes") ObjectBinding binding = Bindings.select(this, "item", "value"); binding.addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Object arg1, Object arg2) { if (arg2 != null) { Object tmp = ScenegraphHelper.findResource(arg2.getClass(), TemplatedTreeCell.this); DataTemplate dt = (DataTemplate) tmp; if (dt != null) { dt.applyTo(TemplatedTreeCell.this, "item", "value"); return; } dt = createDefaultTemplate(); dt.applyTo(TemplatedTreeCell.this, "item", "value"); } } protected DataTemplate createDefaultTemplate() { return new DataTemplate(Object.class); } }); } }