com.github.ktools1000.fx.LazyLoadingObjectProperty Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2018 Lablicate GmbH.
*
* All rights reserved.
*
* Contributors:
* Dr. Alexander Kerner - initial API and implementation
*
*******************************************************************************/
package com.github.ktools1000.fx;
import javafx.beans.property.SimpleObjectProperty;
import javafx.concurrent.Task;
/**
* A {@link SimpleObjectProperty} that lazily loads its value.
*
* @author Alexander Kerner
*
* @param
* type of the wrapped Object
*/
public abstract class LazyLoadingObjectProperty extends AlwaysLoadingObjectProperty {
public LazyLoadingObjectProperty() {
super();
}
public LazyLoadingObjectProperty(final Object bean, final String name, final T initialValue) {
super(bean, name, initialValue);
}
public LazyLoadingObjectProperty(final Object bean, final String name) {
super(bean, name);
}
public LazyLoadingObjectProperty(final T initialValue) {
super(initialValue);
}
private boolean loaded = false;
/**
* Starts the loading task asynchronously (if not already running) and returns
* {@link super#getValue()}.
*/
@Override
public T getValue() {
if (!loaded) {
startLoadingService();
}
return super.getValue();
}
public boolean isLoaded() {
return loaded;
}
public void setLoaded(final boolean loaded) {
this.loaded = loaded;
}
/**
* Starts the {@link Task} that will calculate this Property's value in the
* background.
*/
@Override
protected void startLoadingService() {
setLoaded(true);
final Task s = LazyLoadingObjectProperty.this.createTask();
exe.submit(s);
s.setOnFailed(e -> {
setValue(getFailedValue(e.getSource().getException()));
afterLoaded();
});
s.setOnSucceeded(e -> {
setValue(s.getValue());
afterLoaded();
});
}
}