package de.wwwu.awolf.view.controller; import de.wwwu.awolf.model.dao.Line; import de.wwwu.awolf.model.dao.LineModel; import de.wwwu.awolf.presenter.algorithms.Algorithm; import de.wwwu.awolf.view.services.ButtonClickService; import java.util.HashMap; import java.util.Map; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleObjectProperty; import javafx.fxml.FXML; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.chart.LineChart; import javafx.scene.chart.XYChart; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.VBox; import javax.annotation.Nullable; public class AlgorithmTabController { private final XYChart.Series dataSerie; private final XYChart.Series lineSerie; @FXML private LineChart chart; @FXML private VBox vBox; @FXML private Button startButton; private Algorithm.Type algorithmType; private Map> properties; private LineModel model; public AlgorithmTabController() { dataSerie = new XYChart.Series<>(); dataSerie.setName("Datapoints"); lineSerie = new XYChart.Series<>(); lineSerie.setName("Estimated Line"); } public LineModel getModel() { return model; } public void setModel(LineModel model) { this.model = model; } public void init() { // add GUI elements to maintain the parameters if (properties != null) { properties.forEach((key, value) -> { //Pane component for the layout BorderPane borderPane = new BorderPane(); borderPane.setPadding(new Insets(10, 0, 10, 0)); //text field for the input TextField textField = new TextField(String.valueOf(value.get())); textField.textProperty().bindBidirectional(value); //label Label label = new Label(key); label.setAlignment(Pos.BASELINE_LEFT); label.setLabelFor(textField); label.setPadding(new Insets(2, 10, 0, 0)); //add components borderPane.setLeft(label); borderPane.setRight(textField); vBox.getChildren().add(borderPane); }); } BooleanProperty booleanProperty = startButton.disableProperty(); startButton.setOnAction(event -> { ButtonClickService buttonClickService = new ButtonClickService(algorithmType, getProperties(), booleanProperty); buttonClickService.start(); } ); updatePlot(this.model, null); chart.getData().add(dataSerie); chart.getData().add(lineSerie); } public void updatePlot(final LineModel model, final Line pLine) { dataSerie.getData().clear(); model.getLines() .forEach( line -> dataSerie.getData().add(new XYChart.Data<>(line.getM(), line.getB()))); if (pLine != null) { lineSerie.getData().clear(); Double x1 = model.getMin(); Double y1 = model.getMin() * pLine.getM() + pLine.getB(); Double x2 = model.getMax(); Double y2 = model.getMax() * pLine.getM() + pLine.getB(); lineSerie.getData().add(new XYChart.Data<>(x1, y1)); lineSerie.getData().add(new XYChart.Data<>(x2, y2)); } } public Algorithm.Type getAlgorithmType() { return algorithmType; } public void setAlgorithmType(Algorithm.Type algorithmType) { this.algorithmType = algorithmType; } @Nullable public Map getProperties() { if (properties == null) { return null; } else { HashMap props = new HashMap<>(); properties.forEach((key, value) -> { props.put(key, value.get()); }); return props; } } public void setProperties(Map props) { if (props != null && !props.isEmpty()) { properties = new HashMap<>(); props.forEach((key, value) -> { properties.put(key, new SimpleObjectProperty<>(value)); }); } } public Button getStartButton() { return startButton; } }