Skip to content

Commit

Permalink
make digit butons work
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 20, 2024
1 parent 65b8da1 commit c529a37
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
import mscalc.engine.commands.Command;
import mscalc.engine.commands.IExpressionCommand;
import mscalc.engine.resource.JavaBundleResourceProvider;
import mscalc.gui.views.scientific.ScientificView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ScientificCalculatorViewModel {
private static final Logger logger = LogManager.getLogger(ScientificCalculatorViewModel.class);



private final List<InputViewModel> allInputs = new ArrayList<>();

private final CalculatorManager calculatorManager = new CalculatorManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Text?>

<fx:root xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
type="javafx.scene.layout.VBox"
stylesheets="@ScientificView.css" styleClass="scientificView">
type="javafx.scene.layout.VBox"
stylesheets="@ScientificView.css" styleClass="scientificView">
<children>
<TextField fx:id="display" disable="true" styleClass="display" />
<HBox VBox.vgrow="NEVER" styleClass="selectorsContainer">
<children>
<HBox styleClass="selectBaseArea" HBox.hgrow="ALWAYS">
<children>
<fx:define>
<ToggleGroup fx:id="baseToggleGroup" />
<ToggleGroup fx:id="radixToggleGroup"/>
</fx:define>
<RadioButton mnemonicParsing="false" toggleGroup="${baseToggleGroup}" text="Hex" />
<RadioButton mnemonicParsing="false" toggleGroup="${baseToggleGroup}" text="Dec" />
<RadioButton mnemonicParsing="false" toggleGroup="${baseToggleGroup}" text="Oct" />
<RadioButton mnemonicParsing="false" toggleGroup="${baseToggleGroup}" text="Bin" />
<RadioButton mnemonicParsing="false" toggleGroup="${radixToggleGroup}" text="Hex" fx:id="radioRadixHex" />
<RadioButton mnemonicParsing="false" toggleGroup="${radixToggleGroup}" text="Dec" fx:id="radioRadixDec" />
<RadioButton mnemonicParsing="false" toggleGroup="${radixToggleGroup}" text="Oct" fx:id="radioRadixOct" />
<RadioButton mnemonicParsing="false" toggleGroup="${radixToggleGroup}" text="Bin" fx:id="radioRadixBin" />
</children>
</HBox>
<HBox styleClass="selectAngleTypeArea">
Expand Down
49 changes: 47 additions & 2 deletions gui/src/main/java/mscalc/gui/views/scientific/ScientificView.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package mscalc.gui.views.scientific;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import mscalc.engine.RadixType;
import mscalc.gui.App;
import mscalc.gui.viewmodel.ScientificCalculatorViewModel;
import mscalc.gui.views.CalculatorView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;

public class ScientificView extends VBox implements CalculatorView {
private static final Logger logger = LogManager.getLogger(ScientificView.class);

private final ScientificCalculatorViewModel viewModel = new ScientificCalculatorViewModel();

public ScientificView() {
Expand All @@ -35,6 +43,20 @@ public ScientificView() {
@FXML
private TextField display;

@FXML
private ToggleGroup radixToggleGroup;
@FXML
private RadioButton radioRadixHex;
@FXML
private RadioButton radioRadixDec;
@FXML
private RadioButton radioRadixOct;
@FXML
private RadioButton radioRadixBin;

@FXML
private Button bDigit0;

@FXML
private Button bDigit1;

Expand Down Expand Up @@ -89,6 +111,29 @@ public ScientificView() {
public void install(Scene scene) {
display.textProperty().bind(viewModel.displayProperty);

radixToggleGroup.selectToggle(radioRadixDec);
viewModel.radixProperty.addListener((observable, oldValue, newValue) -> {
logger.info("Selected radix from ViewModel: {}", newValue);
radixToggleGroup.selectToggle(switch (newValue) {
case Hex -> radioRadixHex;
case Decimal -> radioRadixDec;
case Octal -> radioRadixOct;
case Binary -> radioRadixBin;
default -> null;
});
});
radixToggleGroup.selectedToggleProperty().addListener((observableValue, oldValue, newValue) -> {
RadixType radix =
(newValue == radioRadixHex) ? RadixType.Hex :
(newValue == radioRadixDec) ? RadixType.Decimal :
(newValue == radioRadixOct) ? RadixType.Octal :
(newValue == radioRadixBin) ? RadixType.Binary :
null;
logger.info("Selected radix from UI: {}", radix);
viewModel.radixProperty.set(radix);
});

bindButton(bDigit0, viewModel.digit0Button);
bindButton(bDigit1, viewModel.digit1Button);
bindButton(bDigit2, viewModel.digit2Button);
bindButton(bDigit3, viewModel.digit3Button);
Expand Down

0 comments on commit c529a37

Please sign in to comment.