From f4d87fa52a82d76ef8e1a7851af0ddda1e33c014 Mon Sep 17 00:00:00 2001 From: Jan Kozusznik <jan@kozusznik.cz> Date: Mon, 12 Feb 2018 13:16:10 +0100 Subject: [PATCH] moving methods from CloseableControl --- .../it4i/fiji/haas/ui/CloseableControl.java | 73 ----------------- .../java/cz/it4i/fiji/haas/ui/FXFrame.java | 2 +- .../cz/it4i/fiji/haas/ui/FXFrameNative.java | 4 +- .../java/cz/it4i/fiji/haas/ui/JFXPanel.java | 2 +- .../cz/it4i/fiji/haas/ui/JavaFXRoutines.java | 80 +++++++++++++++++++ .../core/FXFrameExecutorService.java | 4 +- .../ui/BenchmarkSPIMController.java | 7 +- .../ui/LogViewControl.java | 3 +- .../SPIMPipelineProgressViewController.java | 7 +- .../test/java/cz/it4i/fiji/haas/FXFrame.java | 3 +- .../SPIMPipelineProgressViewController.java | 7 +- 11 files changed, 102 insertions(+), 90 deletions(-) create mode 100644 haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JavaFXRoutines.java diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/CloseableControl.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/CloseableControl.java index b7ecbbf4..dc0fb201 100644 --- a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/CloseableControl.java +++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/CloseableControl.java @@ -1,83 +1,10 @@ package cz.it4i.fiji.haas.ui; import java.io.Closeable; -import java.io.IOException; -import java.util.concurrent.Callable; -import java.util.concurrent.Executor; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javafx.application.Platform; -import javafx.beans.value.ObservableValue; -import javafx.fxml.FXMLLoader; -import javafx.scene.Parent; -import javafx.scene.control.TableColumn; -import javafx.scene.control.TableView; public interface CloseableControl extends Closeable { - public static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas.ui.CloseableControl.class); - @Override void close(); - static void initRootAndController(String string, Parent parent) { - FXMLLoader fxmlLoader = new FXMLLoader(parent.getClass().getResource(string)); - fxmlLoader.setControllerFactory(c -> { - try { - if (c.equals(parent.getClass())) { - return parent; - } else { - return c.newInstance(); - } - } catch (InstantiationException | IllegalAccessException e) { - throw new RuntimeException(e); - } - }); - fxmlLoader.setRoot(parent); - Object explicitController = null; - do { - fxmlLoader.setController(explicitController); - try { - fxmlLoader.load(); - } catch (IOException exception) { - throw new RuntimeException(exception); - } - if(fxmlLoader.getController() == null) { - explicitController = parent; - } - } while(fxmlLoader.getController() == null); - } - - static public <V> void executeAsync(Executor executor, Callable<V> action, Consumer<V> postAction) { - executor.execute(() -> { - V result; - try { - result = action.call(); - postAction.accept(result); - } catch (Exception e) { - log.error(e.getMessage(), e); - } - }); - } - - static public void runOnFxThread(Runnable runnable) { - if (Platform.isFxApplicationThread()) { - runnable.run(); - } else { - Platform.runLater(runnable); - } - } - - @SuppressWarnings("unchecked") - static public <U, T extends ObservableValue<U>, V> void setCellValueFactory(TableView<T> tableView, int index, - Function<U, V> mapper) { - ((TableColumn<T, V>) tableView.getColumns().get(index)) - .setCellValueFactory(f -> new ObservableValueAdapter<U, V>(f.getValue(), mapper)); - - } - } diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrame.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrame.java index 3aa53cb4..7b981a33 100644 --- a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrame.java +++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrame.java @@ -57,7 +57,7 @@ public abstract class FXFrame<T extends Parent&CloseableControl> extends JDialog this.setLayout(new BorderLayout()); //JScrollPane scrollPane = new JScrollPane(this.fxPanel); this.add(fxPanel, BorderLayout.CENTER); - CloseableControl.runOnFxThread(() -> this.pack()); + JavaFXRoutines.runOnFxThread(() -> this.pack()); } diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrameNative.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrameNative.java index 7a61949f..2d6102fa 100644 --- a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrameNative.java +++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/FXFrameNative.java @@ -24,7 +24,7 @@ public abstract class FXFrameNative<T extends Parent&CloseableControl> { public FXFrameNative(Window applicationFrame, Supplier<T> fxSupplier) { new javafx.embed.swing.JFXPanel(); - CloseableControl.runOnFxThread(() -> { + JavaFXRoutines.runOnFxThread(() -> { stage = new Stage(); stage.setTitle("My New Stage Title"); stage.setScene(new Scene(fxSupplier.get(), 450, 450)); @@ -33,7 +33,7 @@ public abstract class FXFrameNative<T extends Parent&CloseableControl> { } public void setVisible(boolean b) { - CloseableControl.runOnFxThread(() -> { + JavaFXRoutines.runOnFxThread(() -> { if(b) { stage.show(); } else { diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JFXPanel.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JFXPanel.java index a26bbb67..47b5b563 100644 --- a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JFXPanel.java +++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JFXPanel.java @@ -26,7 +26,7 @@ public class JFXPanel<T extends Parent> extends javafx.embed.swing.JFXPanel { Platform.setImplicitExit(false); control = fxSupplier.get(); // The call to runLater() avoid a mix between JavaFX thread and Swing thread. - CloseableControl.runOnFxThread(() -> initFX()); + JavaFXRoutines.runOnFxThread(() -> initFX()); } private void initFX() { diff --git a/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JavaFXRoutines.java b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JavaFXRoutines.java new file mode 100644 index 00000000..bca20c8e --- /dev/null +++ b/haas-imagej-client/src/main/java/cz/it4i/fiji/haas/ui/JavaFXRoutines.java @@ -0,0 +1,80 @@ +package cz.it4i.fiji.haas.ui; + +import java.io.IOException; +import java.util.concurrent.Callable; +import java.util.concurrent.Executor; +import java.util.function.Consumer; +import java.util.function.Function; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javafx.application.Platform; +import javafx.beans.value.ObservableValue; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; + +public interface JavaFXRoutines { + + static void initRootAndController(String string, Parent parent) { + FXMLLoader fxmlLoader = new FXMLLoader(parent.getClass().getResource(string)); + fxmlLoader.setControllerFactory(c -> { + try { + if (c.equals(parent.getClass())) { + return parent; + } else { + return c.newInstance(); + } + } catch (InstantiationException | IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + fxmlLoader.setRoot(parent); + Object explicitController = null; + do { + fxmlLoader.setController(explicitController); + try { + fxmlLoader.load(); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + if(fxmlLoader.getController() == null) { + explicitController = parent; + } + } while(fxmlLoader.getController() == null); + } + + @SuppressWarnings("unchecked") + static public <U, T extends ObservableValue<U>, V> void setCellValueFactory(TableView<T> tableView, int index, + Function<U, V> mapper) { + ((TableColumn<T, V>) tableView.getColumns().get(index)) + .setCellValueFactory(f -> new ObservableValueAdapter<U, V>(f.getValue(), mapper)); + + } + + static public void runOnFxThread(Runnable runnable) { + if (Platform.isFxApplicationThread()) { + runnable.run(); + } else { + Platform.runLater(runnable); + } + } + + //TODO move to own class in the future + static public <V> void executeAsync(Executor executor, Callable<V> action, Consumer<V> postAction) { + executor.execute(() -> { + V result; + try { + result = action.call(); + postAction.accept(result); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + }); + } + + public static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas.ui.CloseableControl.class); + +} diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/FXFrameExecutorService.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/FXFrameExecutorService.java index 5b7d879d..18db0a77 100644 --- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/FXFrameExecutorService.java +++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/core/FXFrameExecutorService.java @@ -2,14 +2,14 @@ package cz.it4i.fiji.haas_spim_benchmark.core; import java.util.concurrent.Executor; -import cz.it4i.fiji.haas.ui.CloseableControl; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; public class FXFrameExecutorService implements Executor{ @Override public void execute(Runnable command) { - CloseableControl.runOnFxThread(() -> { + JavaFXRoutines.runOnFxThread(() -> { command.run(); }); } diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/BenchmarkSPIMController.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/BenchmarkSPIMController.java index b073fc29..7ab1546e 100644 --- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/BenchmarkSPIMController.java +++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/BenchmarkSPIMController.java @@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory; import cz.it4i.fiji.haas.ui.CloseableControl; import cz.it4i.fiji.haas.ui.DummyProgress; import cz.it4i.fiji.haas.ui.InitiableControl; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; import cz.it4i.fiji.haas.ui.ModalDialogs; import cz.it4i.fiji.haas.ui.ProgressDialog; import cz.it4i.fiji.haas.ui.TableViewContextMenu; @@ -68,7 +69,7 @@ public class BenchmarkSPIMController extends BorderPane implements CloseableCont public BenchmarkSPIMController(BenchmarkJobManager manager) { this.manager = manager; - CloseableControl.initRootAndController("BenchmarkSPIM.fxml", this); + JavaFXRoutines.initRootAndController("BenchmarkSPIM.fxml", this); } @@ -156,7 +157,7 @@ public class BenchmarkSPIMController extends BorderPane implements CloseableCont } private void executeWSCallAsync(String title, boolean update, P_JobAction action) { - CloseableControl.executeAsync(executorServiceWS, (Callable<Void>) () -> { + JavaFXRoutines.executeAsync(executorServiceWS, (Callable<Void>) () -> { ProgressDialog dialog = ModalDialogs.doModal(new ProgressDialog(root, title), WindowConstants.DO_NOTHING_ON_CLOSE); try { @@ -223,7 +224,7 @@ public class BenchmarkSPIMController extends BorderPane implements CloseableCont } private void setCellValueFactory(int index, Function<BenchmarkJob, String> mapper) { - CloseableControl.setCellValueFactory(jobs, index, mapper); + JavaFXRoutines.setCellValueFactory(jobs, index, mapper); } private interface P_JobAction { diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/LogViewControl.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/LogViewControl.java index 18498dc3..68b68620 100644 --- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/LogViewControl.java +++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/LogViewControl.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import cz.it4i.fiji.haas.ui.CloseableControl; import cz.it4i.fiji.haas.ui.FXFrameNative; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; import javafx.fxml.FXML; import javafx.scene.control.ScrollPane; import javafx.scene.control.TextArea; @@ -15,7 +16,7 @@ public class LogViewControl extends BorderPane implements CloseableControl { private static Logger log = LoggerFactory.getLogger(cz.it4i.fiji.haas_spim_benchmark.ui.LogViewControl.class); public LogViewControl() { - CloseableControl.initRootAndController("LogView.fxml", this); + JavaFXRoutines.initRootAndController("LogView.fxml", this); } public static void main(String[] args) { diff --git a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java index bbee8e05..6d1f5f4e 100644 --- a/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java +++ b/haas-spim-benchmark/src/main/java/cz/it4i/fiji/haas_spim_benchmark/ui/SPIMPipelineProgressViewController.java @@ -13,6 +13,7 @@ import java.util.function.Function; import java.util.stream.Collectors; import cz.it4i.fiji.haas.ui.CloseableControl; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; import cz.it4i.fiji.haas_java_client.JobState; import cz.it4i.fiji.haas_spim_benchmark.core.BenchmarkJobManager.BenchmarkJob; import cz.it4i.fiji.haas_spim_benchmark.core.Constants; @@ -83,7 +84,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl } private void init() { - CloseableControl.initRootAndController("SPIMPipelineProgressView.fxml", this); + JavaFXRoutines.initRootAndController("SPIMPipelineProgressView.fxml", this); tasks.setPrefWidth(PREFERRED_WIDTH); timer = new Timer(); registry = new ObservableTaskRegistry(task -> tasks.getItems().remove(registry.get(task))); @@ -113,7 +114,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl .collect(Collectors.toList())); executorFx.execute(() -> { int i = 0; - CloseableControl.setCellValueFactory(this.tasks, i++, + JavaFXRoutines.setCellValueFactory(this.tasks, i++, (Function<Task, String>) v -> v.getDescription()); for (TaskComputation tc : computations) { this.tasks.getColumns().add(new TableColumn<>(tc.getTimepoint() + "")); @@ -134,7 +135,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl @SuppressWarnings("unchecked") private void constructCellFactory(int index) { - CloseableControl.setCellValueFactory(this.tasks, index, (Function<Task, TaskComputation>) v -> { + JavaFXRoutines.setCellValueFactory(this.tasks, index, (Function<Task, TaskComputation>) v -> { if (v.getComputations().size() >= index) { return v.getComputations().get(index - 1); } else { diff --git a/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/FXFrame.java b/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/FXFrame.java index 38ec84ef..c0f3ce9b 100644 --- a/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/FXFrame.java +++ b/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/FXFrame.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import cz.it4i.fiji.haas.ui.CloseableControl; import cz.it4i.fiji.haas.ui.InitiableControl; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; import cz.it4i.fiji.haas.ui.ResizeableControl; import javafx.scene.Parent; @@ -64,7 +65,7 @@ public abstract class FXFrame<T extends Parent & CloseableControl> extends JDial this.setLayout(new BorderLayout()); // JScrollPane scrollPane = new JScrollPane(this.fxPanel); this.add(fxPanel, BorderLayout.CENTER); - CloseableControl.runOnFxThread(() -> this.pack()); + JavaFXRoutines.runOnFxThread(() -> this.pack()); } diff --git a/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/SPIMPipelineProgressViewController.java b/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/SPIMPipelineProgressViewController.java index 5e1475c8..d3bc5de9 100644 --- a/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/SPIMPipelineProgressViewController.java +++ b/haas-spim-benchmark/src/test/java/cz/it4i/fiji/haas/SPIMPipelineProgressViewController.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.function.Function; import cz.it4i.fiji.haas.ui.CloseableControl; +import cz.it4i.fiji.haas.ui.JavaFXRoutines; import cz.it4i.fiji.haas.ui.ResizeableControl; import cz.it4i.fiji.haas_java_client.JobState; import javafx.beans.value.ObservableValue; @@ -64,14 +65,14 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl } private void init() { - CloseableControl.initRootAndController("SPIMPipelineProgressView.fxml", this); + JavaFXRoutines.initRootAndController("SPIMPipelineProgressView.fxml", this); fillTable(); } private void fillTable() { - CloseableControl.setCellValueFactory(this.tasks, 0, (Function<String, String>) v -> v); + JavaFXRoutines.setCellValueFactory(this.tasks, 0, (Function<String, String>) v -> v); for (int i = 1; i <= 91; i++) { this.tasks.getColumns().add(new TableColumn<>(i + "")); constructCellFactory(i); @@ -91,7 +92,7 @@ public class SPIMPipelineProgressViewController extends BorderPane implements Cl @SuppressWarnings("unchecked") private void constructCellFactory(int index) { - CloseableControl.setCellValueFactory(this.tasks, index, (Function<String, String>) v -> { + JavaFXRoutines.setCellValueFactory(this.tasks, index, (Function<String, String>) v -> { return v; }); ((TableColumn<ObservableValue<String>, String>) this.tasks.getColumns().get(index)).setCellFactory(column -> { -- GitLab