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; /** * The controller for the abacus FX UI, responsible * for all the user interaction. */ public class AbacusController { /** * Constant string that is displayed if the text could not be lexed or parsed. */ private static final String ERR_SYNTAX = "Syntax Error"; /** * Constant string that is displayed if the tree could not be reduced. */ private static final String ERR_EVAL = "Evaluation Error"; @FXML private TableView historyTable; @FXML private TableColumn inputColumn; @FXML private TableColumn parsedColumn; @FXML private TableColumn outputColumn; @FXML private Text outputText; @FXML private TextField inputField; @FXML private Button inputButton; /** * The list of history entries, created by the users. */ private ObservableList historyData; /** * The abacus instance used for calculations and all * other main processing code. */ private Abacus abacus; @FXML public void initialize(){ Callback, TableCell> cellFactory = param -> new CopyableCell<>(); abacus = new Abacus(); historyData = FXCollections.observableArrayList(); historyTable.setItems(historyData); 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()); } @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(""); } }