mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 00:25:20 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
051be8d49e | ||
|
|
f464bcff6f | ||
|
|
dd915136c6 | ||
|
|
699ba9e193 | ||
|
|
e43f223086 |
@@ -1,7 +1,6 @@
|
||||
package org.nwapw.abacus;
|
||||
|
||||
import org.nwapw.abacus.config.ConfigurationObject;
|
||||
import org.nwapw.abacus.fx.AbacusApplication;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||
@@ -12,7 +11,9 @@ import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||
import org.nwapw.abacus.tree.NumberReducer;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
import org.nwapw.abacus.window.Window;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -52,7 +53,10 @@ public class Abacus {
|
||||
* from a string.
|
||||
*/
|
||||
private TreeBuilder treeBuilder;
|
||||
|
||||
private Window window;
|
||||
public boolean getStop(){
|
||||
return window.getStop();
|
||||
}
|
||||
/**
|
||||
* Creates a new instance of the Abacus calculator.
|
||||
*/
|
||||
@@ -78,7 +82,13 @@ public class Abacus {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
AbacusApplication.launch(AbacusApplication.class, args);
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
new Window(new Abacus()).setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,7 +136,7 @@ public class Abacus {
|
||||
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
||||
*/
|
||||
public TreeNode parseString(String input) {
|
||||
return treeBuilder.fromString(input);
|
||||
return treeBuilder.fromString(input,this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,4 +168,8 @@ public class Abacus {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setWindow(Window window) {
|
||||
this.window = window;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class AbacusApplication extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
|
||||
Scene mainScene = new Scene(parent, 320, 480);
|
||||
primaryStage.setScene(mainScene);
|
||||
primaryStage.setTitle("Abacus");
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
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;
|
||||
|
||||
private ObservableList<HistoryModel> historyData;
|
||||
|
||||
private Abacus abacus;
|
||||
|
||||
@FXML
|
||||
public void initialize(){
|
||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> 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("");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.scene.control.TableCell;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.datatransfer.StringSelection;
|
||||
|
||||
public class CopyableCell<S, T> extends TableCell<S, T> {
|
||||
|
||||
public CopyableCell(){
|
||||
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
||||
if(event.getClickCount() == 2){
|
||||
Toolkit.getDefaultToolkit().getSystemClipboard()
|
||||
.setContents(new StringSelection(getText()), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateItem(T item, boolean empty) {
|
||||
super.updateItem(item, empty);
|
||||
setText((empty || item == null) ? null : item.toString());
|
||||
setGraphic(null);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package org.nwapw.abacus.fx;
|
||||
|
||||
import javafx.beans.property.SimpleStringProperty;
|
||||
import javafx.beans.property.StringProperty;
|
||||
|
||||
public class HistoryModel {
|
||||
|
||||
private final StringProperty input;
|
||||
private final StringProperty parsed;
|
||||
private final StringProperty output;
|
||||
|
||||
public HistoryModel(String input, String parsed, String output){
|
||||
this.input = new SimpleStringProperty();
|
||||
this.parsed = new SimpleStringProperty();
|
||||
this.output = new SimpleStringProperty();
|
||||
this.input.setValue(input);
|
||||
this.parsed.setValue(parsed);
|
||||
this.output.setValue(output);
|
||||
}
|
||||
|
||||
public StringProperty inputProperty() {
|
||||
return input;
|
||||
}
|
||||
public String getInput() {
|
||||
return input.get();
|
||||
}
|
||||
|
||||
public StringProperty parsedProperty() {
|
||||
return parsed;
|
||||
}
|
||||
public String getParsed() {
|
||||
return parsed.get();
|
||||
}
|
||||
|
||||
public StringProperty outputProperty() {
|
||||
return output;
|
||||
}
|
||||
public String getOutput() {
|
||||
return output.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nwapw.abacus.parsing;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import java.util.List;
|
||||
@@ -18,5 +19,5 @@ public interface Parser<T> {
|
||||
* @param tokens the tokens to construct a tree from.
|
||||
* @return the constructed tree, or null on error.
|
||||
*/
|
||||
public TreeNode constructTree(List<T> tokens);
|
||||
public TreeNode constructTree(List<T> tokens,Abacus trace);
|
||||
}
|
||||
|
||||
@@ -34,6 +34,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
*/
|
||||
private Map<String, OperatorType> typeMap;
|
||||
|
||||
//private Abacus trace;
|
||||
|
||||
/**
|
||||
* Creates a new Shunting Yard parser with the given Abacus instance.
|
||||
*
|
||||
@@ -116,7 +118,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
* @param matches the list of tokens from the source string.
|
||||
* @return the construct tree expression.
|
||||
*/
|
||||
public TreeNode constructRecursive(List<Match<TokenType>> matches) {
|
||||
public TreeNode constructRecursive(List<Match<TokenType>> matches,Abacus trace) {
|
||||
//this.trace = trace;
|
||||
if (matches.size() == 0) return null;
|
||||
Match<TokenType> match = matches.remove(0);
|
||||
TokenType matchType = match.getType();
|
||||
@@ -124,22 +127,22 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
String operator = match.getContent();
|
||||
OperatorType type = typeMap.get(operator);
|
||||
if (type == OperatorType.BINARY_INFIX) {
|
||||
TreeNode right = constructRecursive(matches);
|
||||
TreeNode left = constructRecursive(matches);
|
||||
TreeNode right = constructRecursive(matches,trace);
|
||||
TreeNode left = constructRecursive(matches,trace);
|
||||
if (left == null || right == null) return null;
|
||||
else return new BinaryInfixNode(operator, left, right);
|
||||
else return new BinaryInfixNode(operator, left, right,trace);
|
||||
} else {
|
||||
TreeNode applyTo = constructRecursive(matches);
|
||||
TreeNode applyTo = constructRecursive(matches,trace);
|
||||
if (applyTo == null) return null;
|
||||
else return new UnaryPrefixNode(operator, applyTo);
|
||||
else return new UnaryPrefixNode(operator, applyTo,trace);
|
||||
}
|
||||
} else if (matchType == TokenType.NUM) {
|
||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
String functionName = match.getContent();
|
||||
FunctionNode node = new FunctionNode(functionName);
|
||||
FunctionNode node = new FunctionNode(functionName,trace);
|
||||
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
|
||||
TreeNode argument = constructRecursive(matches);
|
||||
TreeNode argument = constructRecursive(matches,trace);
|
||||
if (argument == null) return null;
|
||||
node.prependChild(argument);
|
||||
}
|
||||
@@ -151,10 +154,10 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
}
|
||||
|
||||
@Override
|
||||
public TreeNode constructTree(List<Match<TokenType>> tokens) {
|
||||
public TreeNode constructTree(List<Match<TokenType>> tokens,Abacus trace) {
|
||||
tokens = intoPostfix(new ArrayList<>(tokens));
|
||||
Collections.reverse(tokens);
|
||||
return constructRecursive(tokens);
|
||||
return constructRecursive(tokens,trace);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nwapw.abacus.parsing;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import java.util.List;
|
||||
@@ -41,10 +42,10 @@ public class TreeBuilder<T> {
|
||||
* @param input the string to parse into a tree.
|
||||
* @return the resulting tree.
|
||||
*/
|
||||
public TreeNode fromString(String input) {
|
||||
public TreeNode fromString(String input,Abacus trace) {
|
||||
List<T> tokens = tokenizer.tokenizeString(input);
|
||||
if (tokens == null) return null;
|
||||
return parser.constructTree(tokens);
|
||||
return parser.constructTree(tokens,trace);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -203,11 +203,12 @@ public class StandardPlugin extends Plugin {
|
||||
private NumberInterface getLogPartialSum(NumberInterface x) {
|
||||
NumberInterface maxError = getMaxError(x);
|
||||
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
||||
NumberInterface currentTerm = x, sum = x;
|
||||
NumberInterface currentNumerator = x, currentTerm = x, sum = x;
|
||||
int n = 1;
|
||||
while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0) {
|
||||
n++;
|
||||
currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n - 1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate();
|
||||
currentNumerator = currentNumerator.multiply(x).negate();
|
||||
currentTerm = currentNumerator.divide(new NaiveNumber(n).promoteTo(x.getClass()));
|
||||
sum = sum.add(currentTerm);
|
||||
}
|
||||
return sum;
|
||||
@@ -273,7 +274,7 @@ public class StandardPlugin extends Plugin {
|
||||
*
|
||||
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||
* @param x where the function is evaluated.
|
||||
* @return the number of terms needed to evaluated the exponential function.
|
||||
* @return the number of terms needed to evaluate the exponential function.
|
||||
*/
|
||||
private static int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
|
||||
/**
|
||||
* A tree node that represents an operation being applied to two operands.
|
||||
*/
|
||||
@@ -17,6 +19,7 @@ public class BinaryInfixNode extends TreeNode {
|
||||
* The right node of the operation.
|
||||
*/
|
||||
private TreeNode right;
|
||||
private Abacus trace;
|
||||
|
||||
private BinaryInfixNode() {
|
||||
}
|
||||
@@ -27,8 +30,8 @@ public class BinaryInfixNode extends TreeNode {
|
||||
*
|
||||
* @param operation the operation.
|
||||
*/
|
||||
public BinaryInfixNode(String operation) {
|
||||
this(operation, null, null);
|
||||
public BinaryInfixNode(String operation,Abacus trace) {
|
||||
this(operation, null, null,trace);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,10 +42,11 @@ public class BinaryInfixNode extends TreeNode {
|
||||
* @param left the left node of the expression.
|
||||
* @param right the right node of the expression.
|
||||
*/
|
||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right) {
|
||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right,Abacus trace) {
|
||||
this.operation = operation;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
this.trace = trace;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,10 +96,15 @@ public class BinaryInfixNode extends TreeNode {
|
||||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
T leftReduce = left.reduce(reducer);
|
||||
T rightReduce = right.reduce(reducer);
|
||||
if (leftReduce == null || rightReduce == null) return null;
|
||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||
if(!trace.getStop()) {
|
||||
T leftReduce = left.reduce(reducer);
|
||||
|
||||
T rightReduce = right.reduce(reducer);
|
||||
if (leftReduce == null || rightReduce == null) return null;
|
||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -16,6 +18,7 @@ public class FunctionNode extends TreeNode {
|
||||
* The list of arguments to the function.
|
||||
*/
|
||||
private List<TreeNode> children;
|
||||
private Abacus trace;
|
||||
|
||||
/**
|
||||
* Creates a function node with no function.
|
||||
@@ -28,9 +31,10 @@ public class FunctionNode extends TreeNode {
|
||||
*
|
||||
* @param function the function name.
|
||||
*/
|
||||
public FunctionNode(String function) {
|
||||
public FunctionNode(String function,Abacus trace) {
|
||||
this.function = function;
|
||||
children = new ArrayList<>();
|
||||
this.trace = trace;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
|
||||
public class UnaryPrefixNode extends TreeNode {
|
||||
|
||||
/**
|
||||
@@ -10,14 +12,15 @@ public class UnaryPrefixNode extends TreeNode {
|
||||
* The tree node to apply the operation to.
|
||||
*/
|
||||
private TreeNode applyTo;
|
||||
private Abacus trace;
|
||||
|
||||
/**
|
||||
* Creates a new node with the given operation and no child.
|
||||
*
|
||||
* @param operation the operation for this node.
|
||||
*/
|
||||
public UnaryPrefixNode(String operation) {
|
||||
this(operation, null);
|
||||
public UnaryPrefixNode(String operation,Abacus trace) {
|
||||
this(operation, null,trace);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,16 +29,20 @@ public class UnaryPrefixNode extends TreeNode {
|
||||
* @param operation the operation for this node.
|
||||
* @param applyTo the node to apply the function to.
|
||||
*/
|
||||
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
||||
public UnaryPrefixNode(String operation, TreeNode applyTo,Abacus trace) {
|
||||
this.operation = operation;
|
||||
this.applyTo = applyTo;
|
||||
this.trace = trace;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
Object reducedChild = applyTo.reduce(reducer);
|
||||
if (reducedChild == null) return null;
|
||||
return reducer.reduceNode(this, reducedChild);
|
||||
if(!trace.getStop()) {
|
||||
Object reducedChild = applyTo.reduce(reducer);
|
||||
if (reducedChild == null) return null;
|
||||
return reducer.reduceNode(this, reducedChild);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -15,13 +15,14 @@ import java.awt.event.MouseEvent;
|
||||
* The main UI window for the calculator.
|
||||
*/
|
||||
public class Window extends JFrame {
|
||||
|
||||
|
||||
private static final String CALC_STRING = "Calculate";
|
||||
private static final String SYNTAX_ERR_STRING = "Syntax Error";
|
||||
private static final String EVAL_ERR_STRING = "Evaluation Error";
|
||||
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
||||
private static final String FUNCTION_LABEL = "Functions:";
|
||||
|
||||
private static final String STOP_STRING = "Stop";
|
||||
/**
|
||||
* Array of Strings to which the "calculate" button's text
|
||||
* changes. For instance, in the graph tab, the name will
|
||||
@@ -90,7 +91,10 @@ public class Window extends JFrame {
|
||||
* The "submit" button.
|
||||
*/
|
||||
private JButton inputEnterButton;
|
||||
|
||||
/**
|
||||
* The stop calculations button.
|
||||
*/
|
||||
private JButton inputStopButton;
|
||||
/**
|
||||
* The side panel for separate configuration.
|
||||
*/
|
||||
@@ -113,26 +117,80 @@ public class Window extends JFrame {
|
||||
* The list of functions available to the user.
|
||||
*/
|
||||
private JComboBox<String> functionList;
|
||||
/**
|
||||
* Thread used for calculations
|
||||
*/
|
||||
private Thread calculateThread;
|
||||
/**
|
||||
* Check if currently calculating.
|
||||
*/
|
||||
private boolean calculating;
|
||||
/**
|
||||
* Checks if thread should stop calculating.
|
||||
*/
|
||||
private boolean stopCalculating;
|
||||
/**
|
||||
* Test variable to check number of threads running in the calculator.
|
||||
*/
|
||||
private int count;
|
||||
/**
|
||||
* ActionListener that stops calculations.
|
||||
*/
|
||||
private ActionListener stopListener = (event) -> {
|
||||
//System.out.println(count++); //Shows about how many calculation threads are running with the calculating=false command
|
||||
//calculating = false; //Ignores result of calculation and allows for the start of a new calculation
|
||||
stopCalculating = true; //Stops calculation at the next node check
|
||||
//calculateThread.stop(); //Stops thread no matter what, Unsafe
|
||||
};
|
||||
|
||||
/**
|
||||
* Action listener that causes the input to be evaluated.
|
||||
* Node check to get whether nodes should continue calculating.
|
||||
* @return boolean stop calculations if true.
|
||||
*/
|
||||
public boolean getStop(){
|
||||
return stopCalculating;
|
||||
}
|
||||
|
||||
/**
|
||||
* ActionListener that runs all calculations.
|
||||
*/
|
||||
private ActionListener evaluateListener = (event) -> {
|
||||
TreeNode parsedExpression = abacus.parseString(inputField.getText());
|
||||
if (parsedExpression == null) {
|
||||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||
return;
|
||||
Runnable calculate = new Runnable() {
|
||||
public void run() {
|
||||
boolean skip = false;
|
||||
calculating = true;
|
||||
TreeNode parsedExpression = null;
|
||||
parsedExpression = abacus.parseString(inputField.getText());
|
||||
if (parsedExpression == null) {
|
||||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||
skip = true;
|
||||
}
|
||||
if(!skip){
|
||||
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
||||
if (numberInterface == null) {
|
||||
lastOutputArea.setText(EVAL_ERR_STRING);
|
||||
calculating = false;
|
||||
stopCalculating = false;
|
||||
|
||||
return;
|
||||
}
|
||||
if(calculateThread.equals(Thread.currentThread())) {
|
||||
lastOutput = numberInterface.toString();
|
||||
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
||||
historyTable.invalidate();
|
||||
lastOutputArea.setText(lastOutput);
|
||||
inputField.setText("");
|
||||
calculating = false;
|
||||
stopCalculating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
if(!calculating) {
|
||||
calculateThread = new Thread(calculate);
|
||||
calculateThread.setName("a-"+System.currentTimeMillis());
|
||||
calculateThread.start();
|
||||
}
|
||||
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
||||
if (numberInterface == null) {
|
||||
lastOutputArea.setText(EVAL_ERR_STRING);
|
||||
return;
|
||||
}
|
||||
lastOutput = numberInterface.toString();
|
||||
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
||||
historyTable.invalidate();
|
||||
lastOutputArea.setText(lastOutput);
|
||||
inputField.setText("");
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -143,6 +201,14 @@ public class Window extends JFrame {
|
||||
evaluateListener,
|
||||
null
|
||||
};
|
||||
/**
|
||||
* Array of listeners that tell the stop button how to behave
|
||||
* at a given input tab.
|
||||
*/
|
||||
private ActionListener[] stopListeners = {
|
||||
stopListener,
|
||||
null
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new window with the given manager.
|
||||
@@ -152,6 +218,7 @@ public class Window extends JFrame {
|
||||
public Window(Abacus abacus) {
|
||||
this();
|
||||
this.abacus = abacus;
|
||||
abacus.setWindow(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -160,25 +227,36 @@ public class Window extends JFrame {
|
||||
private Window() {
|
||||
super();
|
||||
|
||||
//variables related to when calculations occur
|
||||
stopCalculating = false;
|
||||
calculating = true;
|
||||
|
||||
lastOutput = "";
|
||||
|
||||
//default window properties
|
||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
setSize(320, 480);
|
||||
|
||||
//initiates input objects
|
||||
inputField = new JTextField();
|
||||
inputEnterButton = new JButton(CALC_STRING);
|
||||
inputStopButton = new JButton(STOP_STRING);
|
||||
|
||||
//adds input objects into Panel
|
||||
inputPanel = new JPanel();
|
||||
inputPanel.setLayout(new BorderLayout());
|
||||
inputPanel.add(inputField, BorderLayout.CENTER);
|
||||
inputPanel.add(inputEnterButton, BorderLayout.SOUTH);
|
||||
inputPanel.add(inputStopButton, BorderLayout.SOUTH);
|
||||
inputPanel.add(inputField, BorderLayout.NORTH);
|
||||
inputPanel.add(inputEnterButton, BorderLayout.CENTER);
|
||||
|
||||
//initiates output objects in calculator tab
|
||||
historyModel = new HistoryTableModel();
|
||||
historyTable = new JTable(historyModel);
|
||||
historyScroll = new JScrollPane(historyTable);
|
||||
lastOutputArea = new JTextArea(lastOutput);
|
||||
lastOutputArea.setEditable(false);
|
||||
|
||||
//adds output objects into Panel
|
||||
calculationPanel = new JPanel();
|
||||
calculationPanel.setLayout(new BorderLayout());
|
||||
calculationPanel.add(historyScroll, BorderLayout.CENTER);
|
||||
@@ -208,6 +286,8 @@ public class Window extends JFrame {
|
||||
settingsPanel.add(numberSystemPanel);
|
||||
settingsPanel.add(functionSelectPanel);
|
||||
|
||||
calculating = false;
|
||||
|
||||
pane = new JTabbedPane();
|
||||
pane.add("Calculator", calculationPanel);
|
||||
pane.add("Settings", settingsPanel);
|
||||
@@ -215,17 +295,23 @@ public class Window extends JFrame {
|
||||
int selectionIndex = pane.getSelectedIndex();
|
||||
boolean enabled = INPUT_ENABLED[selectionIndex];
|
||||
ActionListener listener = listeners[selectionIndex];
|
||||
ActionListener stopUsed = stopListeners[selectionIndex];
|
||||
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
|
||||
inputField.setEnabled(enabled);
|
||||
inputEnterButton.setEnabled(enabled);
|
||||
inputStopButton.setEnabled(enabled);
|
||||
|
||||
for (ActionListener removingListener : inputEnterButton.getActionListeners()) {
|
||||
inputEnterButton.removeActionListener(removingListener);
|
||||
inputField.removeActionListener(removingListener);
|
||||
}
|
||||
for(ActionListener removingListener : inputStopButton.getActionListeners()){
|
||||
inputStopButton.removeActionListener(removingListener);
|
||||
}
|
||||
if (listener != null) {
|
||||
inputEnterButton.addActionListener(listener);
|
||||
inputField.addActionListener(listener);
|
||||
inputStopButton.addActionListener(stopUsed);
|
||||
}
|
||||
});
|
||||
add(pane, BorderLayout.CENTER);
|
||||
@@ -233,6 +319,7 @@ public class Window extends JFrame {
|
||||
|
||||
inputEnterButton.addActionListener(evaluateListener);
|
||||
inputField.addActionListener(evaluateListener);
|
||||
inputStopButton.addActionListener(stopListener);
|
||||
historyTable.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.BorderPane?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
<BorderPane xmlns="http://javafx.com/javafx"
|
||||
xmlns:fx="http://javafx.com/fxml"
|
||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
||||
<center>
|
||||
<TabPane>
|
||||
<Tab text="Calculator" closable="false">
|
||||
<BorderPane>
|
||||
<center>
|
||||
<TableView fx:id="historyTable">
|
||||
<columnResizePolicy>
|
||||
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
|
||||
</columnResizePolicy>
|
||||
<columns>
|
||||
<TableColumn fx:id="inputColumn" text="Input" sortable="false"/>
|
||||
<TableColumn fx:id="parsedColumn" text="Parsed" sortable="false"/>
|
||||
<TableColumn fx:id="outputColumn" text="Output" sortable="false"/>
|
||||
</columns>
|
||||
</TableView>
|
||||
</center>
|
||||
<bottom>
|
||||
<VBox>
|
||||
<ScrollPane prefHeight="50" vbarPolicy="NEVER">
|
||||
<padding>
|
||||
<Insets top="10" bottom="10" left="10" right="10"/>
|
||||
</padding>
|
||||
<Text fx:id="outputText"/>
|
||||
</ScrollPane>
|
||||
<TextField fx:id="inputField" onAction="#performCalculation"/>
|
||||
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
|
||||
onAction="#performCalculation"/>
|
||||
</VBox>
|
||||
</bottom>
|
||||
</BorderPane>
|
||||
</Tab>
|
||||
<Tab text="Settings" closable="false"/>
|
||||
</TabPane>
|
||||
</center>
|
||||
|
||||
</BorderPane>
|
||||
Reference in New Issue
Block a user