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

org.eclipse.fx.ui.controls.tree.LazyTreeItem Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2014 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ui.controls.tree;

import java.util.List;
import java.util.function.Function;

import javafx.beans.binding.Bindings;
import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

/**
 * A tree item which is populated lazy
 *
 * @param 
 *            the domain value type
 * @since 1.2
 */
public class LazyTreeItem extends TreeItem {
	private boolean hasLoaded;

	/**
	 * Create a lazy tree item
	 *
	 * @param value
	 *            the domain value
	 * @param listCreator
	 *            the creator
	 */
	public LazyTreeItem(T value,
			Function, List>> listCreator) {
		super(value);
		getChildren().add(new TreeItem<>());

		expandedProperty().addListener((o) -> {
			if (isExpanded()) {
				if (!this.hasLoaded) {
					List> list = listCreator.apply(this);
					if( list instanceof ObservableList ) {
						getChildren().clear();
						Bindings.bindContent(getChildren(), (ObservableList>)list);
					} else {
						getChildren().setAll(list);
					}
					
				}
			}
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy