1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 16:00:09 -08:00

Add a new constructor to the UI, and move strings into constants.

This commit is contained in:
Danila Fedorin 2017-07-26 13:31:05 -07:00
parent 9ce35aad54
commit 53b9b56039
3 changed files with 64 additions and 6 deletions

View File

@ -23,7 +23,7 @@ public class Abacus {
} }
manager = new PluginManager(); manager = new PluginManager();
manager.addInstantiated(new StandardPlugin(manager)); manager.addInstantiated(new StandardPlugin(manager));
mainUi = new Window(); mainUi = new Window(manager);
mainUi.setVisible(true); mainUi.setVisible(true);
} }

View File

@ -0,0 +1,26 @@
package org.nwapw.abacus.tree;
import org.nwapw.abacus.number.NumberInterface;
import org.nwapw.abacus.plugin.PluginManager;
public class NumberReducer implements Reducer<NumberInterface> {
private PluginManager manager;
public NumberReducer(PluginManager manager){
this.manager = manager;
}
@Override
public NumberInterface reduceNode(TreeNode node, Object... children) {
if(node instanceof NumberNode) {
return ((NumberNode) node).getNumber();
} else if(node instanceof OpNode){
NumberInterface left = (NumberInterface) children[0];
NumberInterface right = (NumberInterface) children[1];
return manager.functionFor(((OpNode) node).getOperation()).apply(left, right);
}
return null;
}
}

View File

@ -1,5 +1,9 @@
package org.nwapw.abacus.window; package org.nwapw.abacus.window;
import org.nwapw.abacus.plugin.PluginManager;
import org.nwapw.abacus.tree.NumberReducer;
import org.nwapw.abacus.tree.TreeNode;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -8,6 +12,21 @@ import java.awt.*;
*/ */
public class Window extends JFrame { 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;
/** /**
* A collection of outputs from the calculator. * A collection of outputs from the calculator.
*/ */
@ -74,7 +93,20 @@ public class Window extends JFrame {
*/ */
private JButton functionSelectButton; private JButton functionSelectButton;
public Window() { /**
* 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() {
super(); super();
@ -84,7 +116,7 @@ public class Window extends JFrame {
setSize(640, 480); setSize(640, 480);
inputField = new JTextField(); inputField = new JTextField();
inputEnterButton = new JButton("Calculate"); inputEnterButton = new JButton(CALC_STRING);
inputPanel = new JPanel(); inputPanel = new JPanel();
inputPanel.setLayout(new BorderLayout()); inputPanel.setLayout(new BorderLayout());
@ -106,15 +138,15 @@ public class Window extends JFrame {
numberSystemPanel = new JPanel(); numberSystemPanel = new JPanel();
numberSystemPanel.setLayout(new BorderLayout()); numberSystemPanel.setLayout(new BorderLayout());
numberSystemPanel.add(new JLabel("Number Type:"), BorderLayout.NORTH); numberSystemPanel.add(new JLabel(NUMBER_SYSTEM_LABEL), BorderLayout.NORTH);
numberSystemPanel.add(numberSystemList, BorderLayout.CENTER); numberSystemPanel.add(numberSystemList, BorderLayout.CENTER);
functionList = new JComboBox<>(); functionList = new JComboBox<>();
functionSelectButton = new JButton("Select"); functionSelectButton = new JButton(SELECT_STRING);
functionSelectPanel = new JPanel(); functionSelectPanel = new JPanel();
functionSelectPanel.setLayout(new BorderLayout()); functionSelectPanel.setLayout(new BorderLayout());
functionSelectPanel.add(new JLabel("Functions:"), BorderLayout.NORTH); functionSelectPanel.add(new JLabel(FUNCTION_LABEL), BorderLayout.NORTH);
functionSelectPanel.add(functionList, BorderLayout.CENTER); functionSelectPanel.add(functionList, BorderLayout.CENTER);
functionSelectPanel.add(functionSelectButton, BorderLayout.SOUTH); functionSelectPanel.add(functionSelectButton, BorderLayout.SOUTH);