1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-11-16 07:33:09 -08:00
Abacus/src/org/nwapw/abacus/window/Window.java

199 lines
6.1 KiB
Java
Raw Normal View History

2017-07-25 14:52:57 -07:00
package org.nwapw.abacus.window;
import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeNode;
2017-07-25 14:52:57 -07:00
import javax.swing.*;
import java.awt.*;
2017-07-26 14:48:43 -07:00
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionListener;
2017-07-26 14:48:43 -07:00
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
2017-07-25 14:52:57 -07:00
2017-07-26 10:10:37 -07:00
/**
* The main UI window for the calculator.
*/
2017-07-25 14:52:57 -07:00
public class Window extends JFrame {
private static final String CALC_STRING = "Calculate";
private static final String SELECT_STRING = "Select";
private static final String SYNTAX_ERR_STRING = "Syntax Error";
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
private static final String FUNCTION_LABEL = "Functions:";
/**
* The plugin manager used to retrieve functions.
*/
private PluginManager manager;
/**
* The reducer used to evaluate the tree.
*/
private NumberReducer reducer;
2017-07-26 10:10:37 -07:00
/**
* A collection of outputs from the calculator.
*/
2017-07-25 14:52:57 -07:00
private String history;
2017-07-26 10:10:37 -07:00
/**
* The last output by the calculator.
*/
2017-07-25 14:52:57 -07:00
private String lastOutput;
2017-07-26 10:10:37 -07:00
/**
* The panel where the output occurs.
*/
private JPanel outputPanel;
2017-07-26 10:10:37 -07:00
/**
* The text area reserved for the last output.
*/
private JTextArea lastOutputArea;
2017-07-26 10:10:37 -07:00
/**
* The table used for storing history results.
2017-07-26 10:10:37 -07:00
*/
private JTable historyTable;
/**
* The table model used for managing history.
*/
private HistoryTableModel historyModel;
2017-07-26 10:10:37 -07:00
/**
* The scroll pane for the history area.
*/
private JScrollPane historyScroll;
2017-07-26 10:10:37 -07:00
/**
* The panel where the input occurs.
*/
private JPanel inputPanel;
2017-07-26 10:10:37 -07:00
/**
* The input text field.
*/
private JTextField inputField;
2017-07-26 10:10:37 -07:00
/**
* The "submit" button.
*/
private JButton inputEnterButton;
2017-07-26 10:10:37 -07:00
/**
* The side panel for separate configuration.
*/
private JPanel sidePanel;
2017-07-26 10:10:37 -07:00
/**
* Panel for elements relating to number
* system selection.
*/
private JPanel numberSystemPanel;
2017-07-26 10:10:37 -07:00
/**
* The possible list of number systems.
*/
private JComboBox<String> numberSystemList;
2017-07-26 10:10:37 -07:00
/**
* The panel for elements relating to
* function selection.
*/
private JPanel functionSelectPanel;
2017-07-26 10:10:37 -07:00
/**
* The list of functions available to the user.
*/
private JComboBox<String> functionList;
2017-07-26 10:10:37 -07:00
/**
* The button used to select a function.
*/
private JButton functionSelectButton;
/**
* Creates a new window with the given manager.
* @param manager the manager to use.
*/
public Window(PluginManager manager){
this();
this.manager = manager;
reducer = new NumberReducer(manager);
}
/**
* Creates a new window.
*/
private Window() {
2017-07-25 14:52:57 -07:00
super();
history = "";
lastOutput = "";
setSize(640, 480);
inputField = new JTextField();
inputEnterButton = new JButton(CALC_STRING);
inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout());
2017-07-25 22:08:12 -07:00
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(inputEnterButton, BorderLayout.SOUTH);
historyModel = new HistoryTableModel();
historyTable = new JTable(historyModel);
historyScroll = new JScrollPane(historyTable);
lastOutputArea = new JTextArea(lastOutput);
lastOutputArea.setEditable(false);
outputPanel = new JPanel();
outputPanel.setLayout(new BorderLayout());
outputPanel.add(historyScroll, BorderLayout.CENTER);
2017-07-25 22:08:12 -07:00
outputPanel.add(lastOutputArea, BorderLayout.SOUTH);
numberSystemList = new JComboBox<>();
numberSystemPanel = new JPanel();
numberSystemPanel.setLayout(new BorderLayout());
numberSystemPanel.add(new JLabel(NUMBER_SYSTEM_LABEL), BorderLayout.NORTH);
2017-07-25 22:08:12 -07:00
numberSystemPanel.add(numberSystemList, BorderLayout.CENTER);
functionList = new JComboBox<>();
functionSelectButton = new JButton(SELECT_STRING);
functionSelectPanel = new JPanel();
functionSelectPanel.setLayout(new BorderLayout());
functionSelectPanel.add(new JLabel(FUNCTION_LABEL), BorderLayout.NORTH);
2017-07-25 22:08:12 -07:00
functionSelectPanel.add(functionList, BorderLayout.CENTER);
functionSelectPanel.add(functionSelectButton, BorderLayout.SOUTH);
sidePanel = new JPanel();
sidePanel.setLayout(new BorderLayout());
2017-07-25 22:08:12 -07:00
sidePanel.add(numberSystemPanel, BorderLayout.NORTH);
sidePanel.add(functionSelectPanel, BorderLayout.SOUTH);
add(outputPanel, BorderLayout.CENTER);
add(sidePanel, BorderLayout.EAST);
add(inputPanel, BorderLayout.SOUTH);
ActionListener actionListener = (event) -> {
TreeNode parsedExpression = TreeNode.fromString(inputField.getText());
if(parsedExpression == null){
lastOutputArea.setText(SYNTAX_ERR_STRING);
return;
}
lastOutput = parsedExpression.reduce(reducer).toString();
history += (history.length() == 0) ? "" : "\n\n";
history += lastOutput;
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
historyTable.invalidate();
lastOutputArea.setText(lastOutput);
};
inputEnterButton.addActionListener(actionListener);
inputField.addActionListener(actionListener);
2017-07-26 14:48:43 -07:00
historyTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Point clickPoint = e.getPoint();
if(e.getClickCount() == 2){
int row = historyTable.rowAtPoint(clickPoint);
int column = historyTable.columnAtPoint(clickPoint);
String toCopy = historyTable.getValueAt(row, column).toString();
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(toCopy), null);
}
}
});
2017-07-25 14:52:57 -07:00
}
}