1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 15:30:09 -08:00

Separate UI into tabbed panes, and change layout of side panel.

This commit is contained in:
Danila Fedorin 2017-07-26 17:17:21 -07:00
parent 626a2cb514
commit e06feaa581

View File

@ -5,6 +5,8 @@ import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeNode; import org.nwapw.abacus.tree.TreeNode;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; import java.awt.*;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -22,6 +24,16 @@ 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[] BUTTON_NAMES = {
CALC_STRING,
CALC_STRING
};
private static boolean[] BUTTON_ENABLED = {
true,
false
};
/** /**
* The plugin manager used to retrieve functions. * The plugin manager used to retrieve functions.
*/ */
@ -31,15 +43,16 @@ public class Window extends JFrame {
*/ */
private NumberReducer reducer; private NumberReducer reducer;
/**
* A collection of outputs from the calculator.
*/
private String history;
/** /**
* The last output by the calculator. * The last output by the calculator.
*/ */
private String lastOutput; private String lastOutput;
/**
* The tabbed pane that separates calculator contexts.
*/
private JTabbedPane pane;
/** /**
* The panel where the output occurs. * The panel where the output occurs.
*/ */
@ -96,10 +109,27 @@ 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;
/** /**
* The button used to select a function. * Action listener that causes the input to be evaluated.
*/ */
private JButton functionSelectButton; private ActionListener evaluateListener = (event) -> {
TreeNode parsedExpression = TreeNode.fromString(inputField.getText());
if(parsedExpression == null){
lastOutputArea.setText(SYNTAX_ERR_STRING);
return;
}
lastOutput = parsedExpression.reduce(reducer).toString();
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
historyTable.invalidate();
lastOutputArea.setText(lastOutput);
inputField.setText("");
};
private ActionListener[] listeners = {
evaluateListener,
null
};
/** /**
* Creates a new window with the given manager. * Creates a new window with the given manager.
@ -117,7 +147,6 @@ public class Window extends JFrame {
private Window() { private Window() {
super(); super();
history = "";
lastOutput = ""; lastOutput = "";
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@ -145,46 +174,52 @@ public class Window extends JFrame {
numberSystemList = new JComboBox<>(); numberSystemList = new JComboBox<>();
numberSystemPanel = new JPanel(); numberSystemPanel = new JPanel();
numberSystemPanel.setLayout(new BorderLayout()); numberSystemPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
numberSystemPanel.add(new JLabel(NUMBER_SYSTEM_LABEL), BorderLayout.NORTH); numberSystemPanel.setLayout(new FlowLayout());
numberSystemPanel.add(numberSystemList, BorderLayout.CENTER); numberSystemPanel.add(new JLabel(NUMBER_SYSTEM_LABEL));
numberSystemPanel.add(numberSystemList);
numberSystemPanel.setMaximumSize(numberSystemPanel.getPreferredSize());
functionList = new JComboBox<>(); functionList = new JComboBox<>();
functionSelectButton = new JButton(SELECT_STRING);
functionSelectPanel = new JPanel(); functionSelectPanel = new JPanel();
functionSelectPanel.setLayout(new BorderLayout()); functionSelectPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
functionSelectPanel.add(new JLabel(FUNCTION_LABEL), BorderLayout.NORTH); functionSelectPanel.setLayout(new FlowLayout());
functionSelectPanel.add(functionList, BorderLayout.CENTER); functionSelectPanel.add(new JLabel(FUNCTION_LABEL));
functionSelectPanel.add(functionSelectButton, BorderLayout.SOUTH); functionSelectPanel.add(functionList);
functionSelectPanel.setMaximumSize(functionSelectPanel.getPreferredSize());
sidePanel = new JPanel(); sidePanel = new JPanel();
sidePanel.setLayout(new BorderLayout()); sidePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
sidePanel.add(numberSystemPanel, BorderLayout.NORTH); sidePanel.setLayout(new BoxLayout(sidePanel, BoxLayout.PAGE_AXIS));
sidePanel.add(functionSelectPanel, BorderLayout.SOUTH); sidePanel.add(numberSystemPanel);
sidePanel.add(functionSelectPanel);
JTabbedPane pane = new JTabbedPane(); pane = new JTabbedPane();
pane.add("Calculator", outputPanel); pane.add("Calculator", outputPanel);
pane.add("Settings", sidePanel); pane.add("Settings", sidePanel);
pane.addChangeListener(e -> {
int selectionIndex = pane.getSelectedIndex();
boolean enabled = BUTTON_ENABLED[selectionIndex];
ActionListener listener = listeners[selectionIndex];
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
inputField.setEnabled(enabled);
inputEnterButton.setEnabled(enabled);
for(ActionListener removingListener : inputEnterButton.getActionListeners()){
inputEnterButton.removeActionListener(removingListener);
inputField.removeActionListener(removingListener);
}
if(listener != null){
inputEnterButton.addActionListener(listener);
inputField.addActionListener(listener);
}
});
add(pane, BorderLayout.CENTER); add(pane, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH); add(inputPanel, BorderLayout.SOUTH);
ActionListener actionListener = (event) -> { inputEnterButton.addActionListener(evaluateListener);
TreeNode parsedExpression = TreeNode.fromString(inputField.getText()); inputField.addActionListener(evaluateListener);
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);
historyTable.addMouseListener(new MouseAdapter() { historyTable.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {