|
| 1 | +package edu.wpi.grip.ui; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.common.base.Throwables; |
| 5 | +import com.google.inject.Inject; |
| 6 | +import edu.wpi.grip.ui.annotations.ParametrizedController; |
| 7 | +import edu.wpi.grip.ui.components.StartStoppableButton; |
| 8 | +import edu.wpi.grip.ui.deployment.DeploymentOptionsController; |
| 9 | +import edu.wpi.grip.ui.deployment.DeploymentOptionsControllersFactory; |
| 10 | +import edu.wpi.grip.ui.util.deployment.DeployedInstanceManager; |
| 11 | +import javafx.application.Platform; |
| 12 | +import javafx.fxml.FXML; |
| 13 | +import javafx.scene.control.Accordion; |
| 14 | +import javafx.scene.control.DialogPane; |
| 15 | +import javafx.scene.control.ProgressBar; |
| 16 | +import javafx.scene.control.TextArea; |
| 17 | +import javafx.scene.layout.HBox; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.OutputStream; |
| 21 | +import java.io.PrintStream; |
| 22 | +import java.util.function.Supplier; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +@ParametrizedController(url = "DeployerPane.fxml") |
| 26 | +public class DeployerController { |
| 27 | + |
| 28 | + @FXML |
| 29 | + private DialogPane root; |
| 30 | + |
| 31 | + @FXML |
| 32 | + private HBox controlsBox; |
| 33 | + |
| 34 | + @FXML |
| 35 | + private Accordion deploymentMethods; |
| 36 | + |
| 37 | + @FXML |
| 38 | + private TextArea stdOutStreamTextArea; |
| 39 | + |
| 40 | + @FXML |
| 41 | + private TextArea stdErrStreamTextArea; |
| 42 | + |
| 43 | + @FXML |
| 44 | + private ProgressBar progressIndicator; |
| 45 | + |
| 46 | + private final StartStoppableButton.Factory startStopButtonFactory; |
| 47 | + private final DeploymentOptionsControllersFactory optionsControllersFactory; |
| 48 | + |
| 49 | + private class StreamToTextArea extends OutputStream { |
| 50 | + private final TextArea outputArea; |
| 51 | + |
| 52 | + public StreamToTextArea(TextArea outputArea) { |
| 53 | + super(); |
| 54 | + this.outputArea = outputArea; |
| 55 | + } |
| 56 | + |
| 57 | + public StreamToTextArea reset() { |
| 58 | + outputArea.clear(); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void write(int i) throws IOException { |
| 64 | + outputArea.appendText(String.valueOf((char) i)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + public interface Factory { |
| 70 | + DeployerController create(); |
| 71 | + } |
| 72 | + |
| 73 | + @Inject |
| 74 | + DeployerController(StartStoppableButton.Factory startStopButtonFactory, DeploymentOptionsControllersFactory optionsControllersFactory) { |
| 75 | + this.startStopButtonFactory = startStopButtonFactory; |
| 76 | + this.optionsControllersFactory = optionsControllersFactory; |
| 77 | + } |
| 78 | + |
| 79 | + @FXML |
| 80 | + @SuppressWarnings("PMD.UnusedPrivateMethod") |
| 81 | + private void initialize() { |
| 82 | + final Supplier<OutputStream> out = () -> |
| 83 | + new PrintStream(new StreamToTextArea(stdOutStreamTextArea).reset(), false); |
| 84 | + final Supplier<OutputStream> err = () -> |
| 85 | + new PrintStream(new StreamToTextArea(stdErrStreamTextArea).reset(), false); |
| 86 | + deploymentMethods.getPanes().addAll( |
| 87 | + optionsControllersFactory |
| 88 | + .createControllers(this::onDeploy, out, err) |
| 89 | + .stream() |
| 90 | + .map(DeploymentOptionsController::getRoot) |
| 91 | + .collect(Collectors.toList())); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Calls {@link DeployedInstanceManager#deploy()} and displays the result to the UI. |
| 96 | + * @param manager The manager to call deploy on |
| 97 | + */ |
| 98 | + private void onDeploy(DeployedInstanceManager manager) { |
| 99 | + Platform.runLater(() -> { |
| 100 | + progressIndicator.setProgress(0); |
| 101 | + deploymentMethods.setDisable(true); |
| 102 | + }); |
| 103 | + manager.deploy() |
| 104 | + .fail(throwable -> { |
| 105 | + Platform.runLater(() -> { |
| 106 | + stdErrStreamTextArea.setText("Failed to deploy\n" + |
| 107 | + Throwables.getStackTraceAsString(throwable) |
| 108 | + ); |
| 109 | + deploymentMethods.setDisable(false); |
| 110 | + }); |
| 111 | + }) |
| 112 | + .progress(percent -> { |
| 113 | + Platform.runLater(() -> progressIndicator.setProgress(percent)); |
| 114 | + }) |
| 115 | + .done(deployedManager -> { |
| 116 | + Platform.runLater(() -> { |
| 117 | + controlsBox.getChildren().add(startStopButtonFactory.create(deployedManager)); |
| 118 | + deploymentMethods.setDisable(true); |
| 119 | + progressIndicator.setProgress(-1); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + public DialogPane getRoot() { |
| 126 | + return root; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
0 commit comments