mirror of
https://github.com/DanilaFe/abacus
synced 2025-03-14 05:56:35 -07:00
94 lines
3.4 KiB
Java
94 lines
3.4 KiB
Java
package org.nwapw.abacus.fx;
|
|
|
|
import javafx.collections.FXCollections;
|
|
import javafx.collections.ObservableList;
|
|
import javafx.fxml.FXML;
|
|
import javafx.scene.control.*;
|
|
import javafx.scene.text.Text;
|
|
import javafx.util.Callback;
|
|
import org.nwapw.abacus.Abacus;
|
|
import org.nwapw.abacus.number.NumberInterface;
|
|
import org.nwapw.abacus.tree.TreeNode;
|
|
|
|
|
|
public class AbacusController {
|
|
|
|
private static final String ERR_SYNTAX = "Syntax Error";
|
|
private static final String ERR_EVAL = "Evaluation Error";
|
|
|
|
@FXML
|
|
private TableView<HistoryModel> historyTable;
|
|
@FXML
|
|
private TableColumn<HistoryModel, String> inputColumn;
|
|
@FXML
|
|
private TableColumn<HistoryModel, String> parsedColumn;
|
|
@FXML
|
|
private TableColumn<HistoryModel, String> outputColumn;
|
|
@FXML
|
|
private Text outputText;
|
|
@FXML
|
|
private TextField inputField;
|
|
@FXML
|
|
private Button inputButton;
|
|
@FXML
|
|
private ComboBox<String> numberImplementationBox;
|
|
|
|
private ObservableList<HistoryModel> historyData;
|
|
|
|
private ObservableList<String> numberImplementationOptions;
|
|
|
|
private Abacus abacus;
|
|
|
|
@FXML
|
|
public void initialize(){
|
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
|
param -> new CopyableCell<>();
|
|
|
|
historyData = FXCollections.observableArrayList();
|
|
historyTable.setItems(historyData);
|
|
numberImplementationOptions = FXCollections.observableArrayList();
|
|
numberImplementationBox.setItems(numberImplementationOptions);
|
|
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
|
-> {
|
|
abacus.getConfiguration().setNumberImplementation(newValue);
|
|
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
|
});
|
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
|
inputColumn.setCellFactory(cellFactory);
|
|
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
|
parsedColumn.setCellFactory(cellFactory);
|
|
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
|
outputColumn.setCellFactory(cellFactory);
|
|
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
|
|
|
abacus = new Abacus();
|
|
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
|
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
|
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
|
numberImplementationBox.getSelectionModel().select(toSelect);
|
|
}
|
|
|
|
@FXML
|
|
private void performCalculation(){
|
|
inputButton.setDisable(true);
|
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
|
if(constructedTree == null){
|
|
outputText.setText(ERR_SYNTAX);
|
|
inputButton.setDisable(false);
|
|
return;
|
|
}
|
|
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
|
if(evaluatedNumber == null){
|
|
outputText.setText(ERR_EVAL);
|
|
inputButton.setDisable(false);
|
|
return;
|
|
}
|
|
outputText.setText(evaluatedNumber.toString());
|
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
|
|
|
inputButton.setDisable(false);
|
|
inputField.setText("");
|
|
}
|
|
|
|
}
|