Skip to content

Commit

Permalink
Feat: Refreshing, combobox
Browse files Browse the repository at this point in the history
Implemented a refresh method that is called when changing to the transactions view. This method ensures that the view is updated automatically. Also changed the week selection text field to a combobox that only gives a selection range of weeks where transactions have taken place.
  • Loading branch information
tommyah committed May 19, 2026
1 parent 41f1125 commit 2700002
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void start(final Stage stage) throws Exception {
stocksInFile);

TransactionsView transactionsView = new TransactionsView();
new TransactionsController(
TransactionsController transactionsController = new TransactionsController(
transactionsView,
eventManager,
player.getTransactionArchive());
Expand All @@ -133,7 +133,8 @@ public void start(final Stage stage) throws Exception {
inGameView::changeCenterView,
dashBoardView.getRootPane(),
marketView.getRootPane(),
transactionsView.getRootPane()
transactionsView.getRootPane(),
transactionsController::refresh
);

// Register all views
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public List<Transaction> getTransactions(final int week) {
return result;
}

/**
* Returns all transactions.
*
* @return list of transactions from the given week
*/
public List<Transaction> getTransactions() {
return transactions;
}

/**
* Returns all purchase transactions from a given week.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventSubscriber;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventType;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import javafx.scene.Scene;
import javafx.stage.Stage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ protected void initInteractions() {
* {@code inGameView::changeCenterView}).
* @param dashboardCenter root pane of the dashboard widget.
* @param marketCenter root pane of the market widget.
* @param transactionsCenter root pane of the transactions widget.
* @param transactionsCenter root pane of the transactions' widget.
* */
public void setMarketIntegration(final Consumer<Node> centerSwitcher,
final Node dashboardCenter,
final Node marketCenter,
final Node transactionsCenter) {
final Node transactionsCenter, final Runnable onTransactionUpdate) {
getViewElement().setOnAction(TopBarActions.EXIT, () -> {
if (inMarketView || inTransactionsView) {
centerSwitcher.accept(dashboardCenter);
Expand Down Expand Up @@ -95,6 +95,7 @@ public void setMarketIntegration(final Consumer<Node> centerSwitcher,

getViewElement().setOnAction(TopBarActions.TRANSACTIONS, () -> {
centerSwitcher.accept(transactionsCenter);
onTransactionUpdate.run();
getViewElement().setQuitText("Back");
inMarketView = false;
inTransactionsView = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.transactions;

/**
* Enum representing actions to be done in a {@link TransactionsView} element.
* */
public enum TransactionsActions {
// Empty, no interactable buttons in transactions page.
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import edu.ntnu.idi.idatt2003.g40.mappe.engine.TransactionArchive;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Sale;
import edu.ntnu.idi.idatt2003.g40.mappe.model.Transaction;
import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager;
import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewController;
import javafx.scene.control.TextFormatter;
import java.util.List;

/**
* Controller for a {@link TransactionsView} element.
*
* <p>extends {@link ViewController}</p>
* */
public class TransactionsController extends ViewController<TransactionsView> {

private final TransactionArchive transactionArchive;
Expand All @@ -20,25 +26,27 @@ public TransactionsController(final TransactionsView viewElement,
@Override
protected void initInteractions() {

getViewElement().getWeekField().setTextFormatter(new TextFormatter<>(change -> {
if (change.getControlNewText().matches("([0-9]*(\\.[0-9]*)?)?")) {
return change;
}
return null;
}));

getViewElement().getWeekField().focusedProperty().addListener((observable, wasFocused, isNowFocused) -> {
if (!isNowFocused && getViewElement().getWeekField().getText().trim().isEmpty()) {
getViewElement().getWeekField().setText("1");
getViewElement().getWeekSelectBox().setOnAction(event -> {
try {
Integer selectedWeek = Integer.parseInt(getViewElement().getWeekSelectBox().getValue());
filterData(getViewElement().getSearchField().getText(), selectedWeek);
} catch (NumberFormatException _) {
filterData(getViewElement().getSearchField().getText(), 1);
}
filterData(getViewElement().getSearchField().getText(), Integer.parseInt(getViewElement().getWeekField().getText()));
});

getViewElement().getSearchField().textProperty().addListener((observable, oldValue, newValue) -> {
filterData(newValue, Integer.parseInt(getViewElement().getWeekField().getText()));
filterData(newValue, Integer.parseInt(getViewElement().getWeekSelectBox().getValue()));
});
}

/**
* Method used for updating the list of transactions based on a search and target week.
*
* @param searchKeyword the keyword to search for stocks with.
* @param weekTarget the week used when searching for transactions.
* */
private void filterData(final String searchKeyword, final Integer weekTarget) {
getViewElement().clearCards();
transactionArchive.getTransactions(weekTarget).forEach(t -> {
Expand All @@ -64,4 +72,28 @@ private void filterData(final String searchKeyword, final Integer weekTarget) {
}
});
}

private List<Integer> getUniqueTransactionWeeks(final List<Transaction> transactionList) {
return transactionList.stream()
.map(Transaction::getWeek)
.distinct()
.sorted()
.toList();
}

public void refresh() {
List<Integer> activeWeeks = getUniqueTransactionWeeks(transactionArchive.getTransactions());
getViewElement().setWeekSelectBoxOptions(activeWeeks);

if (activeWeeks.contains(Integer.parseInt(getViewElement().getWeekSelectBox().getValue()))) {
getViewElement().getWeekSelectBox().setValue(getViewElement().getWeekSelectBox().getValue());
} else if (!activeWeeks.isEmpty()) {
getViewElement().getWeekSelectBox().setValue(activeWeeks.getFirst().toString());
}

filterData(
getViewElement().getSearchField().getText(),
Integer.parseInt(getViewElement().getWeekSelectBox().getValue())
);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package edu.ntnu.idi.idatt2003.g40.mappe.view.widgets.transactions;

import edu.ntnu.idi.idatt2003.g40.mappe.view.ViewElement;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import java.util.List;

/**
* View for displaying a history of all transactions for the active player.
Expand All @@ -25,7 +27,7 @@ public final class TransactionsView
/**
* Week input field.
* */
private TextField weekField;
private ComboBox<String> weekSelectBox;

/**
* Container element for cards.
Expand Down Expand Up @@ -70,13 +72,14 @@ protected void initLayout() {
HBox.setHgrow(searchField, Priority.ALWAYS);

weekLabel = new Label("Week:");
weekField = new TextField("2");
weekSelectBox = new ComboBox<>();
weekSelectBox.setPromptText("All");

searchBar.getChildren().addAll(
searchLabel,
searchField,
weekLabel,
weekField
weekSelectBox
);

// Horizontal scroll pane:
Expand Down Expand Up @@ -149,6 +152,23 @@ public void addTransactionCard(final String type,
cardWeekLabel.getStyleClass().add("transactions-cardText");
}

/**
* Helper method for setting the options for the week selection combo box.
*
* @param options a list of integers representing the different options.
* */
public void setWeekSelectBoxOptions(final List<Integer> options) {
weekSelectBox.getItems().clear();
options.forEach(i -> weekSelectBox.getItems().add(i.toString()));

if (!options.isEmpty()) {
weekSelectBox.setValue(options.getFirst().toString());
} else {
weekSelectBox.getItems().add("1");
weekSelectBox.setValue("1");
}
}

/**
* Clears the displayed list of cards.
* */
Expand All @@ -166,12 +186,12 @@ public TextField getSearchField() {
}

/**
* Getter method for the week filter field.
* Getter method for the week selection box.
*
* @return week filter field.
* @return week selection box.
* */
public TextField getWeekField() {
return weekField;
public ComboBox<String> getWeekSelectBox() {
return weekSelectBox;
}

@Override
Expand All @@ -181,7 +201,7 @@ protected void initStyling() {
searchLabel.getStyleClass().add("transactions-searchLabel");
searchField.getStyleClass().add("transactions-searchField");
weekLabel.getStyleClass().add("transactions-searchLabel");
weekField.getStyleClass().add("transactions-weekField");
weekSelectBox.getStyleClass().add("transactions-weekField");
scrollPane.getStyleClass().add("transactions-scrollPane");
cardsContainer.getStyleClass().add("transactions-cardsContainer");
}
Expand Down
13 changes: 10 additions & 3 deletions src/main/resources/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,18 @@
}

.transactions-weekField {
-fx-pref-width: 50;
-fx-alignment: CENTER;
-fx-pref-width: 75px;
-fx-min-width: 75px;
-fx-background-color: white;
-fx-border-color: #000000;
-fx-border-width: 1.5;
-fx-border-width: 1.5px;
-fx-alignment: CENTER;
}

.transactions-weekField .list-cell {
-fx-text-fill: black;
-fx-font-weight: bold;
-fx-alignment: center;
}

.transactions-transactionCard {
Expand Down

0 comments on commit 2700002

Please sign in to comment.