mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-15 11:25:20 +00:00
Merge branch 'stoppable-new'
# Conflicts: # src/main/java/org/nwapw/abacus/fx/AbacusController.java # src/main/java/org/nwapw/abacus/plugin/StandardPlugin.java
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
@@ -45,7 +46,10 @@ public class AbacusController implements PluginListener {
|
||||
* Constant string that is displayed if the tree could not be reduced.
|
||||
*/
|
||||
private static final String ERR_EVAL = "Evaluation Error";
|
||||
|
||||
/**
|
||||
* Constant string that is displayed if the calculations are stopped before they are done.
|
||||
*/
|
||||
private static final String ERR_STOP = "Stopped";
|
||||
@FXML
|
||||
private TabPane coreTabPane;
|
||||
@FXML
|
||||
@@ -83,6 +87,7 @@ public class AbacusController implements PluginListener {
|
||||
private ObservableList<String> numberImplementationOptions;
|
||||
|
||||
/**
|
||||
<<<<<<< HEAD
|
||||
* The list of plugin objects that can be toggled on and off,
|
||||
* and, when reloaded, get added to the plugin manager's black list.
|
||||
*/
|
||||
@@ -92,6 +97,20 @@ public class AbacusController implements PluginListener {
|
||||
* The abacus instance used for changing the plugin configuration.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
/**
|
||||
* Thread used for calculating.
|
||||
*/
|
||||
private Thread calcThread;
|
||||
|
||||
/**
|
||||
* Checks whether the calculator is calculating.
|
||||
*/
|
||||
private boolean calculating;
|
||||
|
||||
/**
|
||||
* Seconds delayed for timer;
|
||||
*/
|
||||
private double delay = 0;
|
||||
|
||||
/**
|
||||
* Boolean which represents whether changes were made to the configuration.
|
||||
@@ -167,25 +186,63 @@ public class AbacusController implements PluginListener {
|
||||
}
|
||||
|
||||
@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()));
|
||||
private void performCalculation(){
|
||||
Runnable calculator = new Runnable(){
|
||||
public void run() {
|
||||
if(delay>0) {
|
||||
Runnable timer = new Runnable() {
|
||||
public void run() {
|
||||
long gap = (long) (delay * 1000);
|
||||
long startTime = System.currentTimeMillis();
|
||||
while (System.currentTimeMillis() - startTime <= gap) {
|
||||
}
|
||||
stopCalculation();
|
||||
}
|
||||
};
|
||||
Thread maxTime = new Thread(timer);
|
||||
maxTime.setName("maxTime");
|
||||
maxTime.start();
|
||||
}
|
||||
calculating = true;
|
||||
Platform.runLater(() -> inputButton.setDisable(true));
|
||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
||||
if (constructedTree == null) {
|
||||
Platform.runLater(() ->outputText.setText(ERR_SYNTAX));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
//return;
|
||||
}else {
|
||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
||||
if (evaluatedNumber == null) {
|
||||
if(Thread.currentThread().isInterrupted()){
|
||||
Platform.runLater(() -> outputText.setText(ERR_STOP));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
}else {
|
||||
Platform.runLater(() -> outputText.setText(ERR_EVAL));
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
//return;
|
||||
}
|
||||
} else {
|
||||
Platform.runLater(() -> outputText.setText(evaluatedNumber.toString()));
|
||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
||||
|
||||
inputButton.setDisable(false);
|
||||
inputField.setText("");
|
||||
Platform.runLater(() -> inputButton.setDisable(false));
|
||||
Platform.runLater(() -> inputField.setText(""));
|
||||
}
|
||||
}
|
||||
calculating = false;
|
||||
}
|
||||
};
|
||||
if(!calculating) {
|
||||
calcThread = new Thread(calculator);
|
||||
calcThread.setName("calcThread");
|
||||
calcThread.start();
|
||||
}
|
||||
}
|
||||
@FXML
|
||||
private void stopCalculation(){
|
||||
calcThread.interrupt();
|
||||
calculating = false;
|
||||
//Platform.runLater(() ->inputButton.setDisable(false));
|
||||
}
|
||||
|
||||
@FXML
|
||||
|
||||
Reference in New Issue
Block a user