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.ListCell;
import javafx.scene.control.ListView;
import javafx.util.Callback;
/**
* A TreeCell
implementation that searches for templates to create
* nodes for a cell.
*
* @author Mr. Java
*
*/
@SuppressWarnings("rawtypes")
public class TemplatedListCell extends ListCell {
public static Callback createCellFactoryCallback() {
return new Callback() {
@Override
public ListCell call(ListView arg0) {
return new TemplatedListCell();
}
};
}
public TemplatedListCell() {
@SuppressWarnings("rawtypes")
ObjectBinding binding = Bindings.select(this, "item");
binding.addListener(new ChangeListener() {
@Override
public void changed(ObservableValue arg0, Object arg1, Object arg2) {
if (arg2 != null) {
Object tmp = ScenegraphHelper.findResource(arg2.getClass(),
TemplatedListCell.this);
DataTemplate dt = (DataTemplate) tmp;
if (dt != null) {
dt.applyTo(TemplatedListCell.this);
return;
}
dt = createDefaultTemplate();
dt.applyTo(TemplatedListCell.this);
}
}
protected DataTemplate createDefaultTemplate() {
return new DataTemplate(Object.class);
}
});
}
}