mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-22 15:30:09 -08:00
Implement a table as a history tracker.
This commit is contained in:
parent
50baf80433
commit
e41c25847b
97
src/org/nwapw/abacus/window/HistoryTableModel.java
Normal file
97
src/org/nwapw/abacus/window/HistoryTableModel.java
Normal file
|
@ -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<HistoryEntry> 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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -45,13 +45,17 @@ public class Window extends JFrame {
|
||||||
*/
|
*/
|
||||||
private JTextArea lastOutputArea;
|
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.
|
* The scroll pane for the history area.
|
||||||
*/
|
*/
|
||||||
private JScrollPane historyAreaScroll;
|
private JScrollPane historyScroll;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The panel where the input occurs.
|
* The panel where the input occurs.
|
||||||
|
@ -109,7 +113,6 @@ public class Window extends JFrame {
|
||||||
private Window() {
|
private Window() {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
|
||||||
history = "";
|
history = "";
|
||||||
lastOutput = "";
|
lastOutput = "";
|
||||||
|
|
||||||
|
@ -123,15 +126,15 @@ public class Window extends JFrame {
|
||||||
inputPanel.add(inputField, BorderLayout.CENTER);
|
inputPanel.add(inputField, BorderLayout.CENTER);
|
||||||
inputPanel.add(inputEnterButton, BorderLayout.EAST);
|
inputPanel.add(inputEnterButton, BorderLayout.EAST);
|
||||||
|
|
||||||
historyArea = new JTextArea(history);
|
historyModel = new HistoryTableModel();
|
||||||
historyAreaScroll = new JScrollPane(historyArea);
|
historyTable = new JTable(historyModel);
|
||||||
|
historyScroll = new JScrollPane(historyTable);
|
||||||
lastOutputArea = new JTextArea(lastOutput);
|
lastOutputArea = new JTextArea(lastOutput);
|
||||||
lastOutputArea.setEditable(false);
|
lastOutputArea.setEditable(false);
|
||||||
lastOutputArea.setText(":)");
|
|
||||||
|
|
||||||
outputPanel = new JPanel();
|
outputPanel = new JPanel();
|
||||||
outputPanel.setLayout(new BorderLayout());
|
outputPanel.setLayout(new BorderLayout());
|
||||||
outputPanel.add(historyAreaScroll, BorderLayout.CENTER);
|
outputPanel.add(historyScroll, BorderLayout.CENTER);
|
||||||
outputPanel.add(lastOutputArea, BorderLayout.SOUTH);
|
outputPanel.add(lastOutputArea, BorderLayout.SOUTH);
|
||||||
|
|
||||||
numberSystemList = new JComboBox<>();
|
numberSystemList = new JComboBox<>();
|
||||||
|
@ -158,5 +161,21 @@ public class Window extends JFrame {
|
||||||
add(outputPanel, BorderLayout.CENTER);
|
add(outputPanel, BorderLayout.CENTER);
|
||||||
add(sidePanel, BorderLayout.EAST);
|
add(sidePanel, BorderLayout.EAST);
|
||||||
add(inputPanel, BorderLayout.SOUTH);
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user