mirror of
https://github.com/DanilaFe/abacus
synced 2025-01-09 23:58:09 -08:00
Add stop function by ignoring result of calculations
This commit is contained in:
parent
dd915136c6
commit
f464bcff6f
@ -24,7 +24,7 @@ public class Window extends JFrame {
|
|||||||
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
||||||
private static final String FUNCTION_LABEL = "Functions:";
|
private static final String FUNCTION_LABEL = "Functions:";
|
||||||
private static final String STOP_STRING = "Stop";
|
private static final String STOP_STRING = "Stop";
|
||||||
private static final String STOPPED_TEXT = "Stop";
|
private static final String STOPPED_TEXT = "Stopped";
|
||||||
/**
|
/**
|
||||||
* Array of Strings to which the "calculate" button's text
|
* Array of Strings to which the "calculate" button's text
|
||||||
* changes. For instance, in the graph tab, the name will
|
* changes. For instance, in the graph tab, the name will
|
||||||
@ -119,43 +119,85 @@ public class Window extends JFrame {
|
|||||||
* The list of functions available to the user.
|
* The list of functions available to the user.
|
||||||
*/
|
*/
|
||||||
private JComboBox<String> functionList;
|
private JComboBox<String> functionList;
|
||||||
|
/**
|
||||||
|
* Thread used for calculations
|
||||||
|
*/
|
||||||
|
private Thread calculateThread;
|
||||||
|
/**
|
||||||
|
* Check if currently calculating
|
||||||
|
*/
|
||||||
|
private boolean calculating;
|
||||||
|
private int count;
|
||||||
|
//private Object monitor;
|
||||||
/**
|
/**
|
||||||
* Action listener that causes the input to be evaluated.
|
* Action listener that causes the input to be evaluated.
|
||||||
*/
|
*/
|
||||||
|
//*
|
||||||
private ActionListener stopListener = (event) -> {
|
private ActionListener stopListener = (event) -> {
|
||||||
contComputation=false;
|
//contComputation=false;
|
||||||
};
|
//Long pause = Long.MAX_VALUE;
|
||||||
|
//System.out.println(Thread.currentThread().getName());
|
||||||
|
//System.out.println(calculateThread.getName());
|
||||||
|
//calculateThread.suspend();
|
||||||
|
System.out.println(count++);
|
||||||
|
calculating = false;
|
||||||
|
/*
|
||||||
|
synchronized(calculateThread) {
|
||||||
|
try {
|
||||||
|
calculateThread.wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//*/
|
||||||
|
};//*/
|
||||||
private ActionListener evaluateListener = (event) -> {
|
private ActionListener evaluateListener = (event) -> {
|
||||||
contComputation = true;
|
//contComputation = true;
|
||||||
TreeNode parsedExpression = abacus.parseString(inputField.getText());
|
Runnable calculate = new Runnable() {
|
||||||
if(contComputation) {
|
public void run() {
|
||||||
|
boolean skip = false;
|
||||||
|
calculating = true;
|
||||||
|
TreeNode parsedExpression = null;
|
||||||
|
|
||||||
|
|
||||||
|
parsedExpression = abacus.parseString(inputField.getText());
|
||||||
if (parsedExpression == null) {
|
if (parsedExpression == null) {
|
||||||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||||
return;
|
skip = true;
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
try {
|
||||||
|
Thread.currentThread().sleep(9999);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalMonitorStateException e){
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
NumberInterface numberInterface = null;
|
//*/
|
||||||
if(contComputation)
|
//NumberInterface numberInterface = null;
|
||||||
numberInterface = abacus.evaluateTree(parsedExpression);
|
|
||||||
if(contComputation) {
|
if(!skip){
|
||||||
|
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
||||||
if (numberInterface == null) {
|
if (numberInterface == null) {
|
||||||
lastOutputArea.setText(EVAL_ERR_STRING);
|
lastOutputArea.setText(EVAL_ERR_STRING);
|
||||||
return;
|
return;
|
||||||
}}
|
}
|
||||||
if(contComputation)
|
if(calculateThread.equals(Thread.currentThread())) {
|
||||||
lastOutput = numberInterface.toString();
|
lastOutput = numberInterface.toString();
|
||||||
if(contComputation)
|
|
||||||
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
||||||
if(contComputation)
|
|
||||||
historyTable.invalidate();
|
historyTable.invalidate();
|
||||||
if(contComputation)
|
|
||||||
lastOutputArea.setText(lastOutput);
|
lastOutputArea.setText(lastOutput);
|
||||||
else
|
|
||||||
lastOutputArea.setText(STOPPED_TEXT);
|
|
||||||
inputField.setText("");
|
inputField.setText("");
|
||||||
|
calculating = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if(!calculating) {
|
||||||
|
calculateThread = new Thread(calculate);
|
||||||
|
calculateThread.setName("a-"+System.currentTimeMillis());
|
||||||
|
calculateThread.start();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,6 +225,8 @@ public class Window extends JFrame {
|
|||||||
private Window() {
|
private Window() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
contComputation = true;
|
||||||
|
|
||||||
lastOutput = "";
|
lastOutput = "";
|
||||||
|
|
||||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
@ -194,8 +238,9 @@ public class Window extends JFrame {
|
|||||||
|
|
||||||
inputPanel = new JPanel();
|
inputPanel = new JPanel();
|
||||||
inputPanel.setLayout(new BorderLayout());
|
inputPanel.setLayout(new BorderLayout());
|
||||||
inputPanel.add(inputField, BorderLayout.CENTER);
|
inputPanel.add(inputStopButton, BorderLayout.SOUTH);
|
||||||
inputPanel.add(inputEnterButton, BorderLayout.SOUTH);
|
inputPanel.add(inputField, BorderLayout.NORTH);
|
||||||
|
inputPanel.add(inputEnterButton, BorderLayout.CENTER);
|
||||||
|
|
||||||
historyModel = new HistoryTableModel();
|
historyModel = new HistoryTableModel();
|
||||||
historyTable = new JTable(historyModel);
|
historyTable = new JTable(historyModel);
|
||||||
@ -232,6 +277,8 @@ public class Window extends JFrame {
|
|||||||
settingsPanel.add(numberSystemPanel);
|
settingsPanel.add(numberSystemPanel);
|
||||||
settingsPanel.add(functionSelectPanel);
|
settingsPanel.add(functionSelectPanel);
|
||||||
|
|
||||||
|
calculating = false;
|
||||||
|
|
||||||
pane = new JTabbedPane();
|
pane = new JTabbedPane();
|
||||||
pane.add("Calculator", calculationPanel);
|
pane.add("Calculator", calculationPanel);
|
||||||
pane.add("Settings", settingsPanel);
|
pane.add("Settings", settingsPanel);
|
||||||
@ -246,10 +293,12 @@ public class Window extends JFrame {
|
|||||||
for (ActionListener removingListener : inputEnterButton.getActionListeners()) {
|
for (ActionListener removingListener : inputEnterButton.getActionListeners()) {
|
||||||
inputEnterButton.removeActionListener(removingListener);
|
inputEnterButton.removeActionListener(removingListener);
|
||||||
inputField.removeActionListener(removingListener);
|
inputField.removeActionListener(removingListener);
|
||||||
|
inputStopButton.removeActionListener(removingListener);
|
||||||
}
|
}
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
inputEnterButton.addActionListener(listener);
|
inputEnterButton.addActionListener(listener);
|
||||||
inputField.addActionListener(listener);
|
inputField.addActionListener(listener);
|
||||||
|
inputStopButton.addActionListener(listener);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
add(pane, BorderLayout.CENTER);
|
add(pane, BorderLayout.CENTER);
|
||||||
@ -257,6 +306,7 @@ public class Window extends JFrame {
|
|||||||
|
|
||||||
inputEnterButton.addActionListener(evaluateListener);
|
inputEnterButton.addActionListener(evaluateListener);
|
||||||
inputField.addActionListener(evaluateListener);
|
inputField.addActionListener(evaluateListener);
|
||||||
|
inputStopButton.addActionListener(stopListener);
|
||||||
historyTable.addMouseListener(new MouseAdapter() {
|
historyTable.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user