Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/edu/ntnu/idi/idatt/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import edu.ntnu.idi.idatt.storage.CSVtoJSON;
import edu.ntnu.idi.idatt.view.SceneManager;
import edu.ntnu.idi.idatt.view.primary.PrimaryController;
import edu.ntnu.idi.idatt.view.primary.PrimaryModel;
import edu.ntnu.idi.idatt.view.primary.PrimaryView;
import edu.ntnu.idi.idatt.view.entry.StartController;
import edu.ntnu.idi.idatt.view.entry.StartModel;
import edu.ntnu.idi.idatt.view.entry.StartView;
import javafx.application.Application;
import javafx.stage.Stage;

Expand All @@ -25,9 +25,9 @@ public void start(Stage stage) {
stage.setHeight(700);
stage.setTitle("Stock Game");

PrimaryModel model = new PrimaryModel();
PrimaryView view = new PrimaryView();
PrimaryController controller = new PrimaryController(model);
StartModel model = new StartModel();
StartView view = new StartView();
StartController controller = new StartController(model);

view.setModel(model);
view.setController(controller);
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/session/UserSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.ntnu.idi.idatt.session;

import edu.ntnu.idi.idatt.model.player.Player;

public class UserSession {

// Singleton instance
private static UserSession INSTANCE;

// Disable constructor initialization.
private UserSession() {
}

public static UserSession getInstance() {
if (INSTANCE == null) {
INSTANCE = new UserSession();
}
return INSTANCE;
}

private Player player;

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ public abstract class AbstractView<T extends Pane> {
/**
* Constructor for AbstractView.
*/
public AbstractView(T type) {
protected AbstractView(T type) {
instance = type;
instance.getChildren().add(this.createContent());
}

/**
* Constructor for AbstractView without initialization.
*/
protected AbstractView() {

}

/**
* Getter for current view instance.
*
Expand All @@ -50,6 +57,13 @@ public T getInstance() {
return instance;
}

/**
* Setter for current view instance.
*/
public void setInstance(T instance) {
this.instance = instance;
}

/**
* Abstract method for creating view content.
*/
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/view/components/AbstractViewUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package edu.ntnu.idi.idatt.view.components;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public abstract class AbstractViewUI extends AbstractView<StackPane> {

private VBox navigation;
private HBox header;
private HBox toolbar;
private SimpleBooleanProperty isMenuVisible = new SimpleBooleanProperty(false);

public AbstractViewUI() {
HBox wrapper = new HBox();
BorderPane layout = new BorderPane();

createUIComponents();
navigation.getChildren().add(createNavigation());
header.getChildren().add(createHeader());
toolbar.getChildren().add(createToolbar());

layout.setTop(header);
layout.setBottom(toolbar);
layout.setCenter(createContent());

HBox.setHgrow(layout, Priority.ALWAYS);
wrapper.getChildren().addAll(navigation, layout);

this.setInstance(new StackPane());

Parent menu = createMenu();
StackPane.setAlignment(menu, Pos.CENTER_RIGHT);
menu.setVisible(false);

Region disableMenu = new Region();
disableMenu.setVisible(false);

disableMenu.setOnMouseClicked(e -> {
menu.setVisible(false);
disableMenu.setVisible(false);
});

isMenuVisible.addListener((observer, oldVal, newVal) -> {
menu.setVisible(true);
disableMenu.setVisible(true);
});

this.getInstance().getChildren().addAll(wrapper, disableMenu, menu);
}

public void createUIComponents() {
navigation = new VBox();
navigation.setMaxHeight(Double.MAX_VALUE);
navigation.getStyleClass().add("dark");
navigation.setPrefWidth(150);

header = new HBox();
header.getStyleClass().add("light");
header.setPrefHeight(80);

toolbar = new HBox();
toolbar.getStyleClass().add("light");
HBox.setHgrow(toolbar, Priority.ALWAYS);
toolbar.setPrefHeight(80);
}

public abstract Parent createContent();

public abstract Parent createNavigation();

public abstract Parent createHeader();

public abstract Parent createToolbar();

public abstract Parent createMenu();

public VBox getNavigation() {
return navigation;
}

public HBox getHeader() {
return header;
}

public HBox getToolbar() {
return toolbar;
}

public void toggleMenu() {
isMenuVisible.set(!isMenuVisible.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package edu.ntnu.idi.idatt.view.components.elements;

import edu.ntnu.idi.idatt.view.components.primitives.ActionEventHandler;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;

public class IconComponent extends StackPane {

private Button icon;

public IconComponent(Image image, String description, int size) {

ImageView iv = new ImageView();
iv.setImage(image);
iv.setFitHeight(size); // TODO: Fix?
iv.setFitWidth(size);

icon = new Button(description, iv);
icon.getStyleClass().clear(); // Remove parent buffers in case.
icon.getStyleClass().add("icon");
icon.setMaxHeight(Double.MAX_VALUE);

this.getChildren().add(icon);
}

public void onIconClick(ActionEventHandler handler) {
this.icon.setOnAction(e -> handler.handle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package edu.ntnu.idi.idatt.view.components.elements;

import edu.ntnu.idi.idatt.view.components.primitives.ActionEventHandler;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;

public class SearchBarComponent extends HBox {

private final StringProperty query = new SimpleStringProperty();
private final IconComponent searchIcon;

public SearchBarComponent(String placeholder) {

HBox wrapper = new HBox();
wrapper.getStyleClass().add("searchbar");
wrapper.setAlignment(Pos.CENTER);

TextField searchBar = new TextField();
searchBar.getStyleClass().add("searchbar-field");
searchBar.setPromptText(placeholder);
searchBar.setMaxHeight(Double.MAX_VALUE);
searchBar.textProperty().bindBidirectional(query);
searchBar.setFocusTraversable(false);

Image image = new Image(this.getClass().getResource("/icons/search.png/").toExternalForm());
searchIcon = new IconComponent(image, null, 32);

wrapper.getChildren().addAll(searchBar, searchIcon);

this.getChildren().addAll(wrapper);
}

public String getQuery() {
return query.get();
}

public void onSearchQuery(ActionEventHandler handler) {
this.searchIcon.onIconClick(() -> handler.handle());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package edu.ntnu.idi.idatt.view.components.primitives;

@FunctionalInterface
public interface ActionEventHandler {
void handle();
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package edu.ntnu.idi.idatt.view.primary;
package edu.ntnu.idi.idatt.view.entry;

import java.io.File;
import java.math.BigDecimal;

import edu.ntnu.idi.idatt.model.player.Player;
import edu.ntnu.idi.idatt.session.UserSession;
import edu.ntnu.idi.idatt.view.SceneManager;
import edu.ntnu.idi.idatt.view.components.AbstractController;
import edu.ntnu.idi.idatt.view.primary.MainView;
import javafx.stage.FileChooser;

public class PrimaryController extends AbstractController<PrimaryModel> {
public class StartController extends AbstractController<StartModel> {

private File csv;

public PrimaryController(PrimaryModel model) {
public StartController(StartModel model) {
super(model);
}

Expand All @@ -35,8 +39,9 @@ public void initializeGame() {
return;
}

BigDecimal balance = null;
try {
BigDecimal balance = new BigDecimal(model.getBalance().get());
balance = new BigDecimal(model.getBalance().get());
} catch (NumberFormatException e) {
model.getError().set("The balance must be a number format!");
return;
Expand All @@ -46,6 +51,10 @@ public void initializeGame() {
model.getError().set("Specify the stocks .csv file!");
return;
}

UserSession.getInstance().setPlayer(new Player(model.getName().get(), balance));
SceneManager.switchTo(new MainView().getInstance());

}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package edu.ntnu.idi.idatt.view.primary;
package edu.ntnu.idi.idatt.view.entry;

import edu.ntnu.idi.idatt.view.components.Model;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class PrimaryModel implements Model {
public class StartModel implements Model {

private final StringProperty name = new SimpleStringProperty();
private final StringProperty balance = new SimpleStringProperty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package edu.ntnu.idi.idatt.view.primary;
package edu.ntnu.idi.idatt.view.entry;

import java.util.List;

Expand All @@ -15,7 +15,7 @@
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public class PrimaryView extends AbstractView<StackPane> {
public class StartView extends AbstractView<StackPane> {

// Globlal variables for model implementation
private TextField nameField;
Expand All @@ -27,7 +27,7 @@ public class PrimaryView extends AbstractView<StackPane> {
private Button csvButton;
private Button startButton;

public PrimaryView() {
public StartView() {
super(new StackPane());
this.getInstance().getStyleClass().add("dark");
}
Expand Down Expand Up @@ -106,14 +106,14 @@ public Parent createContent() {
return root;
}

public void setModel(PrimaryModel model) {
public void setModel(StartModel model) {
this.nameField.textProperty().bindBidirectional(model.getName());
this.balanceField.textProperty().bindBidirectional(model.getBalance());
this.errorLabel.textProperty().bind(model.getError());
this.fileLabel.textProperty().bind(model.getFileName());
}

public void setController(PrimaryController controller) {
public void setController(StartController controller) {
this.csvButton.setOnAction(e -> controller.obtainCSVFile());
this.startButton.setOnAction(e -> controller.initializeGame());
}
Expand Down
Loading
Loading