SceneFactory.java
package edu.ntnu.idi.idatt.view;
import edu.ntnu.idi.idatt.model.market.Stock;
import edu.ntnu.idi.idatt.session.UserSession;
import edu.ntnu.idi.idatt.storage.SessionManager;
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 edu.ntnu.idi.idatt.view.primary.newspaper.NewspaperController;
import edu.ntnu.idi.idatt.view.primary.newspaper.NewspaperModel;
import edu.ntnu.idi.idatt.view.primary.newspaper.NewspaperView;
import edu.ntnu.idi.idatt.view.primary.portfolio.PortfolioController;
import edu.ntnu.idi.idatt.view.primary.portfolio.PortfolioModel;
import edu.ntnu.idi.idatt.view.primary.portfolio.PortfolioView;
import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeController;
import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeModel;
import edu.ntnu.idi.idatt.view.primary.exchange.ExchangeView;
import edu.ntnu.idi.idatt.view.primary.finish.FinishController;
import edu.ntnu.idi.idatt.view.primary.finish.FinishModel;
import edu.ntnu.idi.idatt.view.primary.finish.FinishView;
import edu.ntnu.idi.idatt.view.primary.stock.StockController;
import edu.ntnu.idi.idatt.view.primary.stock.StockModel;
import edu.ntnu.idi.idatt.view.primary.stock.StockView;
import edu.ntnu.idi.idatt.view.primary.transactions.TransactionController;
import edu.ntnu.idi.idatt.view.primary.transactions.TransactionModel;
import edu.ntnu.idi.idatt.view.primary.transactions.TransactionView;
import javafx.scene.Parent;
import javafx.util.Pair;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Factory class for creating MVC scenes.
*
* <p>
* Initializes MVC objects to bind together
* MVC models.
* </p>
*/
public class SceneFactory {
/**
* Functional interface for saving factory methods.
*/
@FunctionalInterface
public interface MVCInitInterface {
Parent execute();
}
private static Deque<MVCInitInterface> navigation = new ArrayDeque<>();
private static boolean navigatingBack = false;
/**
* Method for going back to previous MVC instance.
*/
public static void goBack() {
if (navigation.size() > 1) {
navigation.pop();
navigatingBack = true;
SceneManager.switchTo(navigation.peek().execute());
navigatingBack = false;
}
}
/**
* Method for refreshing current MVC instance.
* <p>
* Used for refreshing static components after
* updating game model.
* </p>
*/
public static void reloadCurrent() {
navigatingBack = true;
SceneManager.switchTo(navigation.peek().execute());
navigatingBack = false;
}
/**
* Method to save current MVC instance
*/
private static void mark(MVCInitInterface initializer) {
if (!navigatingBack) {
navigation.push(initializer);
}
}
/**
* Method for checking if current MVC instance is final.
*
* @return boolean;
*/
public static boolean isFinal() {
return navigation.size() == 1;
}
/**
* Initialization method for Start View.
*
* @return View's root.
*/
public static Parent createStartView() {
navigation.clear();
StartModel model = new StartModel();
StartView view = new StartView();
StartController controller = new StartController(model);
view.setModel(model);
view.setController(controller);
return view.getInstance();
}
/**
* Initialization method for all Portfolio views.
*
* @return View's root and controller.
*/
private static Pair<Parent, PortfolioController> createDefaultPortfolioView() {
PortfolioModel model = new PortfolioModel();
PortfolioView view = new PortfolioView();
PortfolioController controller = new PortfolioController(model);
view.setModel(model);
view.setController(controller);
return new Pair<Parent, PortfolioController>(view.getInstance(), controller);
}
/**
* Wrapper for Portfolio View initialization method.
*
* <p>
* Initializes a default portfolio.
* </p>
*
* @return View's root.
*/
public static Parent createPortfolioView() {
mark(() -> createPortfolioView());
Pair<Parent, PortfolioController> vc = createDefaultPortfolioView();
return vc.getKey(); // View
}
/**
* Wrapper for Portfolio View initialization method.
*
* <p>
* Initializes a portfolio with a search query.
* </p>
*
* @param initialSearch - Initial search query
* @return View's root
*/
public static Parent createPortfolioView(String initialSearch) {
mark(() -> createPortfolioView(initialSearch));
Pair<Parent, PortfolioController> vc = createDefaultPortfolioView();
Parent view = vc.getKey();
if (initialSearch == null || initialSearch.isEmpty()) {
return view;
}
PortfolioController controller = vc.getValue();
controller.handleSearchQuery(initialSearch);
return view;
}
/**
* Initialization method for Exchange View.
*
* @return View's root.
*/
private static Parent createDefaultExchangeView(boolean clean) {
if (clean) {
navigation.clear();
}
mark(() -> createDefaultExchangeView(false));
ExchangeModel model = new ExchangeModel();
ExchangeView view = new ExchangeView();
ExchangeController controller = new ExchangeController(model);
view.setModel(model);
view.setController(controller);
return view.getInstance();
}
/**
* Wrapper for Exchange View initialization method.
*
* <p>
* Initializes a default exchange view.
* </p>
*
* @return View's root
*/
public static Parent createExchangeView() {
return createDefaultExchangeView(false);
}
/**
* Wrapper for Exchange View initialization method.
*
* <p>
* Initializes a default exchange view with the clean parameter.
* </p>
*
* @param clean - should it reset the page stack.
* @return View's root
*/
public static Parent createExchangeView(boolean clean) {
return createDefaultExchangeView(clean);
}
/**
* Initialization method for Stock View.
*
* @param symbol - Symbol of stock for the view.
* @return View's root.
*/
public static Parent createStockView(String symbol) {
mark(() -> createStockView(symbol));
Stock stock = UserSession.getInstance().getExchange().getStock(symbol);
StockModel model = new StockModel();
StockView view = new StockView();
StockController controller = new StockController(model, stock);
view.setModel(model);
view.setController(controller);
return view.getInstance();
}
/**
* Initialization method for Transaction View.
*
* @return View's root.
*/
public static Parent createTransactionView() {
mark(() -> createTransactionView());
TransactionModel model = new TransactionModel();
TransactionView view = new TransactionView();
TransactionController controller = new TransactionController(model);
view.setModel(model);
view.setController(controller);
return view.getInstance();
}
/**
* Initialization method for Newspaper View.
*
* @param symbol - Symbol of stock for view.
* @return View's root.
*/
public static Parent createNewspaperView(String symbol) {
mark(() -> createNewspaperView(symbol));
Stock stock = UserSession.getInstance().getExchange().getStock(symbol);
NewspaperModel model = new NewspaperModel();
NewspaperView view = new NewspaperView();
NewspaperController controller = new NewspaperController(model, stock);
view.setModel(model);
view.setController(controller);
return view.getInstance();
}
/**
* Initialization method for Finish View.
*
* <p>
* Deletes current session on visit,
* working as a destructor from controller.
* </p>
*
* @return View's root.
*/
public static Parent createFinishView() {
FinishModel model = new FinishModel();
FinishView view = new FinishView();
FinishController controller = new FinishController(model);
view.setController(controller);
SessionManager.removeSession();
return view.getInstance();
}
}