Skip to content

Commit

Permalink
fix(UICompositor): growWithAlignment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
danieskj committed May 14, 2026
1 parent 0af42f9 commit 98b3f06
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt.view.components.elements;

import edu.ntnu.idi.idatt.model.portfolio.Portfolio;
import edu.ntnu.idi.idatt.session.UserSession;
import edu.ntnu.idi.idatt.view.components.ui.UICompositor;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
Expand All @@ -15,11 +16,14 @@ public class PlayerPortfolioComponent extends HBox {
public PlayerPortfolioComponent(Portfolio portfolio){
this.setMaxSize(Double.MAX_VALUE, 500);
this.getStyleClass().add("light");

Label userTitle = new Label("Name Title");
Label invisibleTitle = new Label(" ");
Label netWorth = new Label("Total net worth: "+portfolio.getNetWorth().toString());
Label netWorth = new Label();
netWorth.textProperty().bind(
UserSession.getInstance().netWorthProperty().asString("Net Worth: %.2f $"));
Label percentageChange = new Label("Percentage change: ");
Label playerStatus = new Label("Player status: ");
Label playerStatus = new Label("Player status: "+ UserSession.getInstance().getPlayer().getStatus());
Label portfolioWorth = new Label("Portfolio net worth: ");
Label totalShares = new Label("Total shares owned: ");

Expand All @@ -29,15 +33,17 @@ public PlayerPortfolioComponent(Portfolio portfolio){
userTitle.getStyleClass().add("portfolio-box-title");
invisibleTitle.getStyleClass().add("portfolio-box-title");

VBox leftColumn = new VBox(userTitle, netWorth, percentageChange, playerStatus);
VBox rightColumn = new VBox(invisibleTitle, portfolioWorth, totalShares);

UICompositor playerPortfolioComponent = new UICompositor.Builder()
.parent(new HBox())
.growWithAlignment(Pos.CENTER)
.addContent(leftColumn)
.wrap(new VBox())
.addAllContent(userTitle, netWorth, percentageChange, playerStatus)
.unwrap()
.filler()
.addContent(rightColumn)
.wrap(new VBox())
.growWithAlignment(Pos.CENTER)
.addAllContent(portfolioWorth, totalShares)
.unwrap()
.build();

this.getChildren().add(playerPortfolioComponent.makeUI());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ public ShareComponent(Share share){
this.setMaxSize(Double.MAX_VALUE, 300);
this.setPadding(new Insets(30));
this.getStyleClass().add("rowBox");

Label name = new Label(share.getStock().getCompany());
Label quantity = new Label("Owned shares: "+share.getQuantity().toString());
Label ticker = new Label("("+share.getStock().getSymbol()+")");
Label latestPrice = new Label("Latest price: "+share.getStock().getSalesPrice().toString());
Button sellButton = new Button("Sell");

ArrayList<Label> labels = new ArrayList<>();
Collections.addAll(labels, name, quantity, ticker, latestPrice);
labels.forEach(e -> e.getStyleClass().add("portfolio-box-text"));
sellButton.getStyleClass().add("button");
String color = CssUtils.generateValueColors(share.getProfit().doubleValue());
CssUtils.apply(latestPrice, color);

UICompositor shareComponent = new UICompositor.Builder()
.parent(new HBox())
.growWithAlignment(Pos.CENTER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public Builder parent(Pane parent) {
}

public Builder growWithAlignment(Pos position) {
if (parent instanceof HBox) {
((HBox) parent).setAlignment(position);
parent.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(parent, Priority.ALWAYS);
Pane root = getRoot();
if (root instanceof HBox) {
((HBox) root).setAlignment(position);
root.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(root, Priority.ALWAYS);
}
if (parent instanceof VBox) {
((VBox) parent).setAlignment(position);
parent.setMaxHeight(Double.MAX_VALUE);
VBox.setVgrow(parent, Priority.ALWAYS);
if (root instanceof VBox) {
((VBox) root).setAlignment(position);
root.setMaxHeight(Double.MAX_VALUE);
VBox.setVgrow(root, Priority.ALWAYS);
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public Parent createContent() {

VBox.setVgrow(userBox, Priority.ALWAYS);
userBox.setPadding(new Insets(20));
root.setTop(userBox);

ScrollPane stockBox = new ScrollPane();
stockBox.setPadding(new Insets(10, 10, 20, 20));
root.setCenter(stockBox);
shareBox = new VBox(20);

stockBox.setFitToWidth(true);
stockBox.getStyleClass().add("viewport-colour");
stockBox.getStyleClass().add("primary");
shareBox = new VBox(20);
root.setTop(userBox);
root.setCenter(stockBox);
stockBox.setContent(shareBox);
return root;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public Parent createToolbar() {
public Parent createMenu() {
return UIFactory.createMenu("Account",
List.of(" • Portfolio", " • Transactions"),
() -> System.out.println("Portfolio clicked!"),
() -> SceneManager.switchTo(SceneFactory.createPortfolioView()),
() -> SceneManager.switchTo(SceneFactory.createTransactionView()));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Parent createToolbar() {
public Parent createMenu() {
return UIFactory.createMenu("Account",
List.of(" • Portfolio", " • Transactions"),
() -> System.out.println("Portfolio clicked!"),
() -> SceneManager.switchTo(SceneFactory.createPortfolioView()),
() -> {
});

Expand Down

0 comments on commit 98b3f06

Please sign in to comment.