Skip to content
Snippets Groups Projects

Async table

Closed Jan Kožusznik requested to merge asyncTable into master
All threads resolved!
8 files
+ 236
65
Compare changes
  • Side-by-side
  • Inline
Files
8
@@ -2,6 +2,7 @@ package cz.it4i.fiji.haas.ui;
@@ -2,6 +2,7 @@ package cz.it4i.fiji.haas.ui;
import java.io.IOException;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.Callable;
 
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Function;
@@ -9,18 +10,75 @@ import java.util.function.Function;
@@ -9,18 +10,75 @@ import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactory;
 
import cz.it4i.fiji.haas.ui.JavaFXRoutines.TableCellAdapter.TableCellUpdater;
import javafx.application.Platform;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.fxml.FXMLLoader;
 
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView;
public interface JavaFXRoutines {
public interface JavaFXRoutines {
 
Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas.ui.CloseableControl.class);
 
 
static public class TableCellAdapter<S, T> extends TableCell<S, T> {
 
public interface TableCellUpdater<A, B> {
 
void accept(TableCell<?, ?> cell, B value, boolean empty);
 
}
 
 
private TableCellUpdater<S, T> updater;
 
 
public TableCellAdapter(TableCellUpdater<S, T> updater) {
 
this.updater = updater;
 
}
 
 
@Override
 
protected void updateItem(T item, boolean empty) {
 
updater.accept(this, item, empty);
 
}
 
}
 
 
static public class FutureValueUpdater<S, T, U extends CompletableFuture<T>> implements TableCellUpdater<S, U> {
 
 
private TableCellUpdater<S, T> inner;
 
private Executor executor;
 
 
public FutureValueUpdater(TableCellUpdater<S, T> inner, Executor exec) {
 
this.inner = inner;
 
this.executor = exec;
 
}
 
 
@Override
 
public void accept(TableCell<?, ?> cell, U value, boolean empty) {
 
if (value != null) {
 
if (!value.isDone()) {
 
inner.accept(cell, null, empty);
 
}
 
value.thenAcceptAsync(val -> {
 
inner.accept(cell, val, empty);
 
}, executor);
 
} else {
 
inner.accept(cell, null, empty);
 
}
 
}
 
}
 
 
static public class StringValueUpdater<S> implements TableCellUpdater<S, String> {
 
@Override
 
public void accept(TableCell<?, ?> cell, String value, boolean empty) {
 
if (value != null) {
 
cell.setText(value);
 
} else if (!empty) {
 
cell.setText("N/A");
 
}
 
}
 
}
 
static void initRootAndController(String string, Object parent) {
static void initRootAndController(String string, Object parent) {
initRootAndController(string, parent, false);
initRootAndController(string, parent, false);
}
}
static void initRootAndController(String string, Object parent, boolean setController) {
static void initRootAndController(String string, Object parent, boolean setController) {
FXMLLoader fxmlLoader = new FXMLLoader(parent.getClass().getResource(string));
FXMLLoader fxmlLoader = new FXMLLoader(parent.getClass().getResource(string));
fxmlLoader.setControllerFactory(c -> {
fxmlLoader.setControllerFactory(c -> {
@@ -35,7 +93,7 @@ public interface JavaFXRoutines {
@@ -35,7 +93,7 @@ public interface JavaFXRoutines {
}
}
});
});
fxmlLoader.setRoot(parent);
fxmlLoader.setRoot(parent);
if(setController) {
if (setController) {
fxmlLoader.setController(parent);
fxmlLoader.setController(parent);
}
}
try {
try {
@@ -78,6 +136,4 @@ public interface JavaFXRoutines {
@@ -78,6 +136,4 @@ public interface JavaFXRoutines {
});
});
}
}
public static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas.ui.CloseableControl.class);
}
}
Loading