2017-07-31 16:48:04 -07:00
|
|
|
package org.nwapw.abacus.fx;
|
|
|
|
|
2017-07-31 17:49:57 -07:00
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableList;
|
2017-07-31 16:52:34 -07:00
|
|
|
import javafx.fxml.FXML;
|
2017-07-31 17:17:56 -07:00
|
|
|
import javafx.scene.control.Button;
|
|
|
|
import javafx.scene.control.TextField;
|
|
|
|
import javafx.scene.text.Text;
|
2017-07-31 16:52:34 -07:00
|
|
|
import org.nwapw.abacus.Abacus;
|
2017-07-31 17:17:56 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
|
|
|
import org.nwapw.abacus.tree.TreeNode;
|
2017-07-31 16:52:34 -07:00
|
|
|
|
2017-07-31 16:48:04 -07:00
|
|
|
public class AbacusController {
|
|
|
|
|
2017-07-31 17:17:56 -07:00
|
|
|
private static final String ERR_SYNTAX = "Syntax Error";
|
|
|
|
private static final String ERR_EVAL = "Evaluation Error";
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
private Text outputText;
|
|
|
|
@FXML
|
|
|
|
private TextField inputField;
|
|
|
|
@FXML
|
|
|
|
private Button inputButton;
|
|
|
|
|
2017-07-31 17:49:57 -07:00
|
|
|
private ObservableList<HistoryModel> historyData;
|
|
|
|
|
2017-07-31 16:52:34 -07:00
|
|
|
private Abacus abacus;
|
|
|
|
|
|
|
|
@FXML
|
|
|
|
public void initialize(){
|
|
|
|
abacus = new Abacus();
|
2017-07-31 17:49:57 -07:00
|
|
|
historyData = FXCollections.observableArrayList();
|
2017-07-31 16:52:34 -07:00
|
|
|
}
|
|
|
|
|
2017-07-31 17:17:56 -07:00
|
|
|
@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);
|
2017-07-31 17:49:57 -07:00
|
|
|
if(evaluatedNumber == null){
|
2017-07-31 17:17:56 -07:00
|
|
|
outputText.setText(ERR_EVAL);
|
|
|
|
inputButton.setDisable(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inputButton.setDisable(false);
|
|
|
|
outputText.setText(evaluatedNumber.toString());
|
|
|
|
}
|
|
|
|
|
2017-07-31 16:48:04 -07:00
|
|
|
}
|