From 53b9b56039050fab1b9d69b7456e7e71dd872449 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 26 Jul 2017 13:31:05 -0700 Subject: [PATCH] Add a new constructor to the UI, and move strings into constants. --- src/org/nwapw/abacus/Abacus.java | 2 +- src/org/nwapw/abacus/tree/NumberReducer.java | 26 ++++++++++++ src/org/nwapw/abacus/window/Window.java | 42 +++++++++++++++++--- 3 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 src/org/nwapw/abacus/tree/NumberReducer.java diff --git a/src/org/nwapw/abacus/Abacus.java b/src/org/nwapw/abacus/Abacus.java index 7150c4e..6397157 100644 --- a/src/org/nwapw/abacus/Abacus.java +++ b/src/org/nwapw/abacus/Abacus.java @@ -23,7 +23,7 @@ public class Abacus { } manager = new PluginManager(); manager.addInstantiated(new StandardPlugin(manager)); - mainUi = new Window(); + mainUi = new Window(manager); mainUi.setVisible(true); } diff --git a/src/org/nwapw/abacus/tree/NumberReducer.java b/src/org/nwapw/abacus/tree/NumberReducer.java new file mode 100644 index 0000000..fed6b59 --- /dev/null +++ b/src/org/nwapw/abacus/tree/NumberReducer.java @@ -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 { + + 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; + } + +} diff --git a/src/org/nwapw/abacus/window/Window.java b/src/org/nwapw/abacus/window/Window.java index e8e32ff..f1f6710 100644 --- a/src/org/nwapw/abacus/window/Window.java +++ b/src/org/nwapw/abacus/window/Window.java @@ -1,5 +1,9 @@ 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 java.awt.*; @@ -8,6 +12,21 @@ import java.awt.*; */ 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. */ @@ -74,7 +93,20 @@ public class Window extends JFrame { */ 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(); @@ -84,7 +116,7 @@ public class Window extends JFrame { setSize(640, 480); inputField = new JTextField(); - inputEnterButton = new JButton("Calculate"); + inputEnterButton = new JButton(CALC_STRING); inputPanel = new JPanel(); inputPanel.setLayout(new BorderLayout()); @@ -106,15 +138,15 @@ public class Window extends JFrame { numberSystemPanel = new JPanel(); 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); functionList = new JComboBox<>(); - functionSelectButton = new JButton("Select"); + functionSelectButton = new JButton(SELECT_STRING); functionSelectPanel = new JPanel(); 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(functionSelectButton, BorderLayout.SOUTH);