From d746db8c9f794d8ac185cb72605bed0cdfe5161f Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 14:34:19 -0700 Subject: [PATCH] Implement a table as a history tracker. --- .../abacus/window/HistoryTableModel.java | 97 +++++++++++++++++++ src/org/nwapw/abacus/window/Window.java | 35 +++++-- 2 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 src/org/nwapw/abacus/window/HistoryTableModel.java diff --git a/src/org/nwapw/abacus/window/HistoryTableModel.java b/src/org/nwapw/abacus/window/HistoryTableModel.java new file mode 100644 index 0000000..934e699 --- /dev/null +++ b/src/org/nwapw/abacus/window/HistoryTableModel.java @@ -0,0 +1,97 @@ +package org.nwapw.abacus.window; + +import org.nwapw.abacus.tree.TreeNode; + +import javax.swing.event.TableModelListener; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.TableModel; +import java.util.ArrayList; + +public class HistoryTableModel extends AbstractTableModel { + + public static final String[] COLUMN_NAMES = { + "Input", + "Parsed Input", + "Output" + }; + + public static final Class[] CLASS_TYPES = { + String.class, + TreeNode.class, + String.class + }; + + public static class HistoryEntry { + public String input; + public TreeNode parsedInput; + public String output; + + public HistoryEntry(String input, TreeNode parsedInput, String output){ + this.input = input; + this.parsedInput = parsedInput; + this.output = output; + } + + Object nthValue(int n){ + if(n == 0) return input; + if(n == 1) return parsedInput; + if(n == 2) return output; + return null; + } + } + + ArrayList entries; + + public HistoryTableModel() { + entries = new ArrayList<>(); + } + + public void addEntry(HistoryEntry entry){ + entries.add(entry); + } + + @Override + public int getRowCount() { + return entries.size(); + } + + @Override + public int getColumnCount() { + return 3; + } + + @Override + public String getColumnName(int columnIndex) { + return COLUMN_NAMES[columnIndex]; + } + + @Override + public Class getColumnClass(int columnIndex) { + return CLASS_TYPES[columnIndex]; + } + + @Override + public boolean isCellEditable(int rowIndex, int columnIndex) { + return false; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + return entries.get(rowIndex).nthValue(columnIndex); + } + + @Override + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + return; + } + + @Override + public void addTableModelListener(TableModelListener l) { + + } + + @Override + public void removeTableModelListener(TableModelListener l) { + + } +} diff --git a/src/org/nwapw/abacus/window/Window.java b/src/org/nwapw/abacus/window/Window.java index f1f6710..62ab4ad 100644 --- a/src/org/nwapw/abacus/window/Window.java +++ b/src/org/nwapw/abacus/window/Window.java @@ -45,13 +45,17 @@ public class Window extends JFrame { */ private JTextArea lastOutputArea; /** - * The text area used for all history output. + * The table used for storing history results. */ - private JTextArea historyArea; + private JTable historyTable; + /** + * The table model used for managing history. + */ + private HistoryTableModel historyModel; /** * The scroll pane for the history area. */ - private JScrollPane historyAreaScroll; + private JScrollPane historyScroll; /** * The panel where the input occurs. @@ -109,7 +113,6 @@ public class Window extends JFrame { private Window() { super(); - history = ""; lastOutput = ""; @@ -123,15 +126,15 @@ public class Window extends JFrame { inputPanel.add(inputField, BorderLayout.CENTER); inputPanel.add(inputEnterButton, BorderLayout.EAST); - historyArea = new JTextArea(history); - historyAreaScroll = new JScrollPane(historyArea); + historyModel = new HistoryTableModel(); + historyTable = new JTable(historyModel); + historyScroll = new JScrollPane(historyTable); lastOutputArea = new JTextArea(lastOutput); lastOutputArea.setEditable(false); - lastOutputArea.setText(":)"); outputPanel = new JPanel(); outputPanel.setLayout(new BorderLayout()); - outputPanel.add(historyAreaScroll, BorderLayout.CENTER); + outputPanel.add(historyScroll, BorderLayout.CENTER); outputPanel.add(lastOutputArea, BorderLayout.SOUTH); numberSystemList = new JComboBox<>(); @@ -158,5 +161,21 @@ public class Window extends JFrame { add(outputPanel, BorderLayout.CENTER); add(sidePanel, BorderLayout.EAST); add(inputPanel, BorderLayout.SOUTH); + + inputEnterButton.addActionListener((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); + inputField.setText(lastOutput); + }); } }