-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from IDATT2003-Gruppe-53/32-view-manager
Added a class for changing views/stages
- Loading branch information
Showing
4 changed files
with
95 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,25 @@ | ||
| package no.ntnu.gruppe53; | ||
|
|
||
| /** | ||
| * Hello world! | ||
| */ | ||
| public class App { | ||
| import javafx.application.Application; | ||
| import javafx.stage.Stage; | ||
| import no.ntnu.gruppe53.controller.ViewController; | ||
| import no.ntnu.gruppe53.views.StartView; | ||
|
|
||
|
|
||
| public class App extends Application { | ||
| @Override | ||
| public void start(Stage stage) { | ||
| ViewController vm = new ViewController(stage); | ||
|
|
||
| vm.addView("start", new StartView(vm)); | ||
|
|
||
| vm.switchView("start"); | ||
|
|
||
| stage.setTitle("Millions - the Stock Game"); | ||
| stage.show(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hello World!"); | ||
| launch(); | ||
| } | ||
| } |
61 changes: 61 additions & 0 deletions
61
millions/src/main/java/no/ntnu/gruppe53/controller/ViewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package no.ntnu.gruppe53.controller; | ||
|
|
||
| import javafx.scene.Parent; | ||
| import javafx.scene.Scene; | ||
| import javafx.stage.Stage; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Manages application views and handles switching between them. | ||
| * <p> | ||
| * Stores a mapping of view names to JavaFX {@link Parent} nodes and updates | ||
| * the current Scene by replacing its root node when switching views. | ||
| * </p> | ||
| */ | ||
| public class ViewController { | ||
| /** Primary application window used to display the Scene. */ | ||
| private Stage stage; | ||
| /** Scene containing the currently active view. */ | ||
| private Scene scene; | ||
| /** Map of view identifiers to their corresponding UI roots. */ | ||
| private Map<String, Parent> views = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Constructs a ViewController and initializes a new Scene. | ||
| * | ||
| * @param stage the primary application stage where the scene will be set | ||
| */ | ||
| public ViewController(Stage stage) { | ||
| this.stage = stage; | ||
| this.scene = new Scene(new javafx.scene.layout.StackPane(), 800, 600); | ||
| stage.setScene(scene); | ||
| } | ||
|
|
||
| /** | ||
| * Registers a view in the controller. | ||
| * | ||
| * @param name unique identifier used to reference the view later | ||
| * @param view the JavaFX root node representing the view | ||
| */ | ||
| public void addView(String name, Parent view) { | ||
| views.put(name, view); | ||
| } | ||
|
|
||
| /** | ||
| * Switches the currently displayed view by replacing the Scene root. | ||
| * | ||
| * @param name the identifier of the view to display | ||
| * @throws IllegalArgumentException if no view is registered under the given name | ||
| */ | ||
| public void switchView(String name) { | ||
| Parent view = views.get(name); | ||
|
|
||
| if (view == null) { | ||
| throw new IllegalArgumentException("No views found matching: " + name); | ||
| } | ||
|
|
||
| scene.setRoot(view); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
millions/src/main/java/no/ntnu/gruppe53/views/StartView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package no.ntnu.gruppe53.views; | ||
|
|
||
| import javafx.scene.layout.VBox; | ||
| import no.ntnu.gruppe53.controller.ViewController; | ||
|
|
||
| public class StartView extends VBox { | ||
|
|
||
| public StartView(ViewController vm) { | ||
|
|
||
| } | ||
| } |