algorithms-for-computing-li.../LinearRegressionTool/src/main/java/de/wwwu/awolf/view/controller/AlgorithmTabController.java

119 lines
3.6 KiB
Java

package de.wwwu.awolf.view.controller;
import de.wwwu.awolf.model.Line;
import de.wwwu.awolf.model.LineModel;
import de.wwwu.awolf.presenter.algorithms.Algorithm;
import de.wwwu.awolf.view.services.ButtonClickService;
import javafx.beans.property.BooleanProperty;
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 java.util.Map;
public class AlgorithmTabController {
private final XYChart.Series<Double, Double> dataSerie;
private final XYChart.Series<Double, Double> lineSerie;
private Algorithm.Type algorithmType;
private Map<String, String> properties;
private LineModel model;
@FXML
public LineChart<Double, Double> chart;
@FXML
public VBox vBox;
@FXML
public Button startButton;
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
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(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, 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 = pLine.calculateX1(model.getMIN());
Double y1 = pLine.calculateY1(model.getMIN());
Double x2 = pLine.calculateX2(model.getMAX());
Double y2 = pLine.calculateY2(model.getMAX());
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;
}
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
}