From d8cd813ddc54c455eb3071795038b97b72dc1ee9 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 9 Jun 2015 09:13:55 +0200 Subject: Add working version of UpnFx --- .../in/inf/upnfx/view/MainViewController.java | 385 +++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100644 src/de/fhswf/in/inf/upnfx/view/MainViewController.java (limited to 'src/de/fhswf/in/inf/upnfx/view/MainViewController.java') diff --git a/src/de/fhswf/in/inf/upnfx/view/MainViewController.java b/src/de/fhswf/in/inf/upnfx/view/MainViewController.java new file mode 100644 index 0000000..6a9a115 --- /dev/null +++ b/src/de/fhswf/in/inf/upnfx/view/MainViewController.java @@ -0,0 +1,385 @@ +/** + * + */ +package de.fhswf.in.inf.upnfx.view; + +import java.util.HashMap; +import java.util.Map; + +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; +import javafx.fxml.FXML; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import de.fhswf.in.inf.upnfx.util.Addition; +import de.fhswf.in.inf.upnfx.util.BinaryTemplate; +import de.fhswf.in.inf.upnfx.util.Division; +import de.fhswf.in.inf.upnfx.util.Modulo; +import de.fhswf.in.inf.upnfx.util.Multiplication; +import de.fhswf.in.inf.upnfx.util.ObservableDoubleStack; +import de.fhswf.in.inf.upnfx.util.Operator; +import de.fhswf.in.inf.upnfx.util.Subtraction; +import de.fhswf.in.inf.upnfx.util.UPN; +import de.fhswf.in.inf.upnfx.util.UnaryTemplate; + +/** + * The controller for the MainView.fxml. + * + * @author Stefan Suhren + * @version 1.0 + */ +public class MainViewController +{ + @FXML + private TextField txtField; + + @FXML + private Button btnComma; + + @FXML + private Button btnZero; + + @FXML + private Button btnPlus; + + @FXML + private Button btnMinus; + + @FXML + private Button btnMul; + + @FXML + private Button btnDiv; + + @FXML + private Button btnLn; + + @FXML + private Button btnSin; + + @FXML + private Button btnCos; + + @FXML + private Button btnTan; + + @FXML + private Button btn1DivX; + + @FXML + private Button btnPow; + + @FXML + private Button btnSqr; + + @FXML + private Button btnXY; + + private UPN upnClass = new UPN(); + + private ObservableDoubleStack upnStack = upnClass.getStack(); + + private StringProperty newNumber = new SimpleStringProperty(""); + + private static Map operators = new HashMap<>(); + + static + { + operators.put("+", new Addition()); + operators.put("-", new Subtraction()); + operators.put("*", new Multiplication()); + operators.put("/", new Division()); + operators.put("%", new Modulo()); + operators.put("sin", new UnaryTemplate("sin")); + operators.put("cos", new UnaryTemplate("cos")); + operators.put("tan", new UnaryTemplate("tan")); + operators.put("log", new UnaryTemplate("log")); + operators.put("sqrt", new UnaryTemplate("sqrt")); + operators.put("pow", new BinaryTemplate("pow")); + } + + @FXML + private void initialize() + { + txtField.textProperty().addListener(e -> { + btnComma.setDisable(!newNumber.isEmpty().get() && newNumber.get().contains(".")); + btnZero.setDisable(!newNumber.isEmpty().get() && !newNumber.get().contains(".") && newNumber.get().contains("0")); + }); + + newNumber.addListener(e -> { + if(upnStack.isEmpty()) + { + txtField.clear(); + } + if(!newNumber.isEmpty().get()) + { + txtField.setText(newNumber.get()); + + btnPlus.setDisable(upnStack.size() < 1); + btnMinus.setDisable(upnStack.size() < 1); + btnMul.setDisable(upnStack.size() < 1); + btnDiv.setDisable(upnStack.size() < 1); + btnPow.setDisable(upnStack.size() < 1); + btnXY.setDisable(upnStack.size() < 1); + + btnLn.setDisable(upnStack.size() < 0); + btnSin.setDisable(upnStack.size() < 0); + btnCos.setDisable(upnStack.size() < 0); + btnTan.setDisable(upnStack.size() < 0); + btn1DivX.setDisable(upnStack.size() < 0); + btnSqr.setDisable(upnStack.size() < 0); + } + }); + + upnStack.sizeProperty().addListener(e -> { + txtField.clear(); + if(upnStack.size() > 0) + { + txtField.setText(upnStack.peek().toString()); + } + + btnPlus.setDisable(upnStack.size() < 2); + btnMinus.setDisable(upnStack.size() < 2); + btnMul.setDisable(upnStack.size() < 2); + btnDiv.setDisable(upnStack.size() < 2); + btnPow.setDisable(upnStack.size() < 2); + btnXY.setDisable(upnStack.size() < 2); + + btnLn.setDisable(upnStack.size() < 1); + btnSin.setDisable(upnStack.size() < 1); + btnCos.setDisable(upnStack.size() < 1); + btnTan.setDisable(upnStack.size() < 1); + btn1DivX.setDisable(upnStack.size() < 1); + btnSqr.setDisable(upnStack.size() < 1); + + }); + } + + @FXML + private void handleInvert() + { + if (!newNumber.isEmpty().get()) + { + if(newNumber.get().contains("-")) + { + newNumber.setValue(newNumber.get().replaceAll("-", "")); + } + else + { + newNumber.setValue("-".concat(newNumber.get())); + } + } + else + { + if(upnStack.size() > 0) + { + upnClass.addDouble(-1.0); + upnClass.calculate(operators.get("*")); + } + } + } + + @FXML + private void handleClr() + { + newNumber.setValue(""); + upnStack.clear(); + } + + @FXML + private void handleClx() + { + if (newNumber.isEmpty().get()) + { + if(upnStack.size() > 0) + { + upnStack.pop(); + } + } + else + { + newNumber.setValue(""); + } + } + + @FXML + private void handleLstX() + { + upnClass.setLastX(); + } + + @FXML + private void handleXY() + { + if(!newNumber.isEmpty().get()) + { + handleEnt(); + } + + upnClass.swapXY(); + } + + @FXML + private void handleEnt() + { + if(!newNumber.isEmpty().get()) + { + upnClass.addDouble(Double.parseDouble(newNumber.get().trim())); + newNumber.setValue(""); + } + } + + private void handleNumber(String number) + { + newNumber.set(newNumber.concat(number).get()); + } + + @FXML + private void handleNumber0() + { + handleNumber("0"); + } + + @FXML + private void handleNumberComma() + { + handleNumber("."); + } + + @FXML + private void handleNumber1() + { + handleNumber("1"); + } + + @FXML + private void handleNumber2() + { + handleNumber("2"); + } + + @FXML + private void handleNumber3() + { + handleNumber("3"); + } + + @FXML + private void handleNumber4() + { + handleNumber("4"); + } + + @FXML + private void handleNumber5() + { + handleNumber("5"); + } + + @FXML + private void handleNumber6() + { + handleNumber("6"); + } + + @FXML + private void handleNumber7() + { + handleNumber("7"); + } + + @FXML + private void handleNumber8() + { + handleNumber("8"); + } + + @FXML + private void handleNumber9() + { + handleNumber("9"); + } + + @FXML + private void handle1DivX() + { + if (!newNumber.isEmpty().get()) + { + handleEnt(); + } + upnClass.addDouble(1.0); + handleXY(); + upnClass.calculate(operators.get("/")); + } + + + private void handleOp(Operator operator) + { + if (!newNumber.isEmpty().get()) + { + handleEnt(); + } + upnClass.calculate(operator); + } + + @FXML + private void handleOpPlus() + { + handleOp(operators.get("+")); + } + + @FXML + private void handleOpMinus() + { + handleOp(operators.get("-")); + } + + @FXML + private void handleOpMult() + { + handleOp(operators.get("*")); + } + + @FXML + private void handleOpDiv() + { + handleOp(operators.get("/")); + } + + @FXML + private void handleOpTan() + { + handleOp(operators.get("tan")); + } + + @FXML + private void handleOpCos() + { + handleOp(operators.get("cos")); + } + + @FXML + private void handleOpSin() + { + handleOp(operators.get("sin")); + } + + @FXML + private void handleOpLn() + { + handleOp(operators.get("log")); + } + + @FXML + private void handleOpSqr() + { + handleOp(operators.get("sqrt")); + } + + @FXML + private void handleOpPow() + { + handleOp(operators.get("pow")); + } + +} -- cgit v1.2.3-70-g09d2