-
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.
Feat: Added base classes representing View and Controller in the MVC
- Loading branch information
Showing
5 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/utils/ConfigValues.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,16 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.utils; | ||
|
|
||
| public enum ConfigValues { | ||
| VIEWPORT_WIDTH(600), | ||
| VIEWPORT_HEIGHT(600); | ||
|
|
||
| private int value; | ||
|
|
||
| ConfigValues(final int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/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,77 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.view; | ||
|
|
||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventManager; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventPublisher; | ||
|
|
||
| /** | ||
| * Handles logic and event publishing for {@link ViewElement} objects. | ||
| * | ||
| * <p>Implements {@link EventPublisher}</p> | ||
| * | ||
| * @param <T1> The type of {@link ViewElement} this controller is attached to. We use generics | ||
| * because we want to call methods that may be specific for certain view element | ||
| * objects, and we want to reduce casting. | ||
| * @see ViewElement | ||
| * @see EventPublisher | ||
| * @see EventManager | ||
| * | ||
| */ | ||
| public abstract class ViewController<T1 extends ViewElement<?>> | ||
| implements EventPublisher { | ||
|
|
||
| /** | ||
| * The {@link ViewElement} object this controller is associated with. | ||
| * | ||
| */ | ||
| private final T1 viewElement; | ||
| /** | ||
| * The {@link EventManager} object to send events to. | ||
| * | ||
| */ | ||
| private final EventManager eventManager; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param viewElement the instance of type T1 this controller is attached to. | ||
| * @param eventManager the {@link EventManager} object this controller communicates with. | ||
| * | ||
| */ | ||
| protected ViewController(final T1 viewElement, final EventManager eventManager) { | ||
| this.viewElement = viewElement; | ||
| this.eventManager = eventManager; | ||
| initInteractions(); | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the event manager. | ||
| * | ||
| * @return the {@link EventManager} object. | ||
| * | ||
| */ | ||
| protected EventManager getEventManager() { | ||
| return eventManager; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the current view element. | ||
| * | ||
| * @return the {@link ViewElement} object. | ||
| * | ||
| */ | ||
| public T1 getViewElement() { | ||
| return viewElement; | ||
| } | ||
|
|
||
| /** | ||
| * Abstract method to initialize logic for the view element. | ||
| * | ||
| */ | ||
| protected abstract void initInteractions(); | ||
|
|
||
| @Override | ||
| public final <T> void invoke(final EventData<T> data, final EventManager eventManager) { | ||
| eventManager.invokeEvent(data); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewData.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,39 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.view; | ||
|
|
||
| /** | ||
| * Object containing data about {@link ViewElement} objects. | ||
| * | ||
| * <p>Sent in {@link edu.ntnu.idi.idatt2003.g40.mappe.service.event.EventData} | ||
| * objects of type SCENE_CHANGE</p> | ||
| * | ||
| * <p>Handled by the {@link ViewManager} to update and change views</p> | ||
| * | ||
| */ | ||
| public class ViewData { | ||
|
|
||
| /** | ||
| * Name of the scene. | ||
| * | ||
| */ | ||
| private final String sceneName; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param sceneName the name of the scene. | ||
| * | ||
| */ | ||
| public ViewData(final String sceneName) { | ||
| this.sceneName = sceneName; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the scene name. | ||
| * | ||
| * @return scene name. | ||
| * | ||
| */ | ||
| public String getSceneName() { | ||
| return sceneName; | ||
| } | ||
| } |
155 changes: 155 additions & 0 deletions
155
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/view/ViewElement.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,155 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.view; | ||
|
|
||
|
|
||
| import javafx.scene.control.Button; | ||
| import javafx.scene.layout.Pane; | ||
| import java.util.ArrayList; | ||
|
|
||
| /** | ||
| * Base class for view elements, such as the main menu, or list items. | ||
| * | ||
| * <p>Extends {@link Pane}</p> | ||
| * | ||
| * @param <T> the type of pane this view element represents. | ||
| * | ||
| */ | ||
| public abstract class ViewElement<T extends Pane> { | ||
|
|
||
| /** | ||
| * List of buttons in this view. | ||
| * | ||
| */ | ||
| private final ArrayList<Button> buttons = new ArrayList<>(); | ||
| /** | ||
| * The {@link Pane} element the view element has as a root. | ||
| * | ||
| * <p>This allows different view elements to have different layouts.</p> | ||
| * | ||
| */ | ||
| private T rootPane; | ||
| /** | ||
| * The name of the view. | ||
| * | ||
| * <p>Used as identification by the {@link ViewManager}.</p> | ||
| * | ||
| */ | ||
| private String viewName; | ||
|
|
||
| /** | ||
| * Constructor with a name. | ||
| * | ||
| * @param rootPane an instance of type T (defined in the class). | ||
| * @param viewName The name of the view. | ||
| * | ||
| */ | ||
| protected ViewElement(final T rootPane, final String viewName) { | ||
| this.viewName = viewName; | ||
| this(rootPane, true); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor with a name and initLoading. | ||
| * | ||
| * @param rootPane an instance of type T (defined in the class). | ||
| * @param viewName The name of the view. | ||
| * @param initLoading boolean value representing if the view should be initialized. Default is | ||
| * true. | ||
| * | ||
| */ | ||
| protected ViewElement(final T rootPane, final String viewName, boolean initLoading) { | ||
| this.viewName = viewName; | ||
| this(rootPane, initLoading); | ||
| } | ||
|
|
||
| /** | ||
| * Constructor without a specific name. | ||
| * | ||
| * @param rootPane an instance of type T (defined in the class). | ||
| * | ||
| */ | ||
| protected ViewElement(final T rootPane, boolean isInit) { | ||
| setRootPane(rootPane); | ||
| if (isInit) { | ||
| initLayout(); | ||
| } | ||
| initStyling(); | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for view name. | ||
| * | ||
| * @return the name of this view. | ||
| * | ||
| */ | ||
| public String getViewName() { | ||
| return viewName; | ||
| } | ||
|
|
||
| /** | ||
| * Setter method for the view name. | ||
| * | ||
| * @param name the new name to set this view element to. | ||
| * | ||
| */ | ||
| protected void setViewName(final String name) { | ||
| viewName = name; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for the root pane. | ||
| * | ||
| * @return the root pane of this view element. | ||
| * | ||
| */ | ||
| public T getRootPane() { | ||
| return rootPane; | ||
| } | ||
|
|
||
| /** | ||
| * Setter method for the root pane. | ||
| * | ||
| * @param pane the pane to set the root pane to. | ||
| * | ||
| */ | ||
| protected void setRootPane(final T pane) { | ||
| rootPane = pane; | ||
| } | ||
|
|
||
| /** | ||
| * Getter method for buttons in this view. | ||
| * | ||
| * <p>Gotten by the associated {@link ViewController} object that hooks | ||
| * the buttons to functionality.</p> | ||
| * | ||
| * @return {@link ArrayList<Button>} object containing all buttons in this view element. | ||
| * | ||
| */ | ||
| public ArrayList<Button> getButtons() { | ||
| return buttons; | ||
| } | ||
|
|
||
| /** | ||
| * Method that initializes the layout for the view element. | ||
| * | ||
| */ | ||
| protected abstract void initLayout(); | ||
|
|
||
| /** | ||
| * Method that initializes the styling for the view element. | ||
| * | ||
| */ | ||
| protected abstract void initStyling(); | ||
|
|
||
| /** | ||
| * Method that defines how view elements set data. | ||
| * | ||
| * @param <T2> The type of data to set. | ||
| * @param data the data to set. | ||
| * | ||
| */ | ||
| public <T2 extends ViewData> void setData(final T2 data) { | ||
| setViewName(data.getSceneName()); | ||
| } | ||
|
|
||
| public abstract void onUpdate(); | ||
| } |
Oops, something went wrong.