From fc2bec60250c1cb7ab6ed023c0dbdab01a2b5eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Martinussen?= Date: Wed, 20 Oct 2021 14:10:53 +0000 Subject: [PATCH] Resolve "Split up FXML files and add communications between them (maybe add design)" --- .../core/domainlogic/FlashCardProvider.java | 16 +- .../gr2141/core/state/Observable.java | 17 ++ .../it1901/groups2021/gr2141/ui/Flashy.java | 2 + .../gr2141/ui/FlashyAppController.java | 150 +----------------- .../groups2021/gr2141/ui/FlashyModel.java | 27 ++++ .../controllers/FlashyCardAreaController.java | 68 ++++++++ .../FlashyCardNavigationController.java | 29 ++++ .../ui/controllers/FlashyMenuController.java | 52 ++++++ .../controllers/FlashyNavbarController.java | 64 ++++++++ .../groups2021/gr2141/ui/FlashyApp.fxml | 97 ++--------- .../gr2141/ui/components/CardArea.fxml | 22 +++ .../gr2141/ui/components/CardNavigation.fxml | 15 ++ .../groups2021/gr2141/ui/components/Menu.fxml | 35 ++++ .../gr2141/ui/components/Navbar.fxml | 54 +++++++ .../groups2021/gr2141/ui/styling/Flashy.css | 8 +- 15 files changed, 420 insertions(+), 236 deletions(-) create mode 100644 flashy/core/src/main/java/it1901/groups2021/gr2141/core/state/Observable.java create mode 100644 flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyModel.java create mode 100644 flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardAreaController.java create mode 100644 flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardNavigationController.java create mode 100644 flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyMenuController.java create mode 100644 flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyNavbarController.java create mode 100644 flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardArea.fxml create mode 100644 flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardNavigation.fxml create mode 100644 flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/Menu.fxml create mode 100644 flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/Navbar.fxml diff --git a/flashy/core/src/main/java/it1901/groups2021/gr2141/core/domainlogic/FlashCardProvider.java b/flashy/core/src/main/java/it1901/groups2021/gr2141/core/domainlogic/FlashCardProvider.java index 3adeaf2..ae325eb 100644 --- a/flashy/core/src/main/java/it1901/groups2021/gr2141/core/domainlogic/FlashCardProvider.java +++ b/flashy/core/src/main/java/it1901/groups2021/gr2141/core/domainlogic/FlashCardProvider.java @@ -1,14 +1,14 @@ package it1901.groups2021.gr2141.core.domainlogic; - import java.util.List; import java.util.Random; import java.util.prefs.Preferences; import it1901.groups2021.gr2141.core.models.CardContent; import it1901.groups2021.gr2141.core.models.CardDeck; +import it1901.groups2021.gr2141.core.state.Observable; -public class FlashCardProvider { +public class FlashCardProvider extends Observable { private static final Preferences prefs = Preferences.userRoot().node(FlashCardProvider.class.getName()); @@ -27,7 +27,10 @@ public class FlashCardProvider { this.showOrderIsFlippedMode = prefs.getBoolean("showOrderIsFlippedMode", false); this.cardOrderIsRandomMode = prefs.getBoolean("cardOrderIsRandomMode",false); this.firstLastWrapAroundMode = prefs.getBoolean("firstLastWrapAroundMode", true); + } + private void updateSubscribers() { + super.updateSubscribers(this); } public CardDeck getCardDeck() { @@ -36,6 +39,7 @@ public class FlashCardProvider { public void setCardDeck(CardDeck deck) { this.deck = deck; + updateSubscribers(); } public int getCardIndex() { @@ -75,8 +79,7 @@ public class FlashCardProvider { this.cardIndex %= this.deck.getNumberOfCards(); cardShowsFront = true; - - + updateSubscribers(); } // used for later iterations @@ -110,10 +113,12 @@ public class FlashCardProvider { int nextCard = randomGenerator.nextInt(deck.getNumberOfCards() - 1); nextCard = nextCard + ((nextCard >= cardIndex) ? 1 : 0); return nextCard; + } public void flipCard() { this.cardShowsFront = !this.cardShowsFront; + updateSubscribers(); } public boolean getShowOrderIsFlippedMode() { @@ -131,16 +136,19 @@ public class FlashCardProvider { public void toggleShowOrderIsFlippedMode() { this.showOrderIsFlippedMode = !this.showOrderIsFlippedMode; prefs.putBoolean("showOrderIsFlippedMode", this.showOrderIsFlippedMode); + updateSubscribers(); } public void toggleCardOrderIsRandomMode() { this.cardOrderIsRandomMode = !this.cardOrderIsRandomMode; prefs.putBoolean("cardOrderIsRandomMode", this.cardOrderIsRandomMode); + updateSubscribers(); } public void toggleFirstLastWrapAroundMode() { this.firstLastWrapAroundMode = !this.firstLastWrapAroundMode; prefs.putBoolean("firstLastWrapAroundMode", this.firstLastWrapAroundMode); + updateSubscribers(); } public void clearPreferences() { diff --git a/flashy/core/src/main/java/it1901/groups2021/gr2141/core/state/Observable.java b/flashy/core/src/main/java/it1901/groups2021/gr2141/core/state/Observable.java new file mode 100644 index 0000000..e02908e --- /dev/null +++ b/flashy/core/src/main/java/it1901/groups2021/gr2141/core/state/Observable.java @@ -0,0 +1,17 @@ +package it1901.groups2021.gr2141.core.state; + +import java.util.Collection; +import java.util.function.Consumer; +import java.util.ArrayList; + +public abstract class Observable { + private Collection> subscribers = new ArrayList<>(); + + public void subscribe(Consumer function) throws IllegalArgumentException { + this.subscribers.add(function); + } + + public void updateSubscribers(T newValue) { + this.subscribers.forEach((s) -> s.accept(newValue)); + } +} \ No newline at end of file diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/Flashy.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/Flashy.java index 1c06c40..5925dd9 100644 --- a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/Flashy.java +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/Flashy.java @@ -13,6 +13,8 @@ import javafx.scene.image.Image; */ public class Flashy extends Application { + public static final FlashyModel applicationState = new FlashyModel(); + public static void main(String[] args) { launch(args); } diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyAppController.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyAppController.java index 2579926..dccba4f 100644 --- a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyAppController.java +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyAppController.java @@ -1,148 +1,10 @@ -package it1901.groups2021.gr2141.ui; - -import java.net.URL; -import java.util.List; -import java.util.ResourceBundle; - -import it1901.groups2021.gr2141.core.models.CardDeck; -import it1901.groups2021.gr2141.core.models.Flashcard; -import it1901.groups2021.gr2141.core.storage.CardDeckStorage; -import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; -import javafx.event.ActionEvent; -import javafx.fxml.FXML; -import javafx.fxml.Initializable; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.control.ToggleButton; -import javafx.scene.input.MouseEvent; - -/** - * Root controller of the application UI. - */ -public class FlashyAppController implements Initializable { - - @FXML - private TextField writeFront; - - @FXML - private TextField writeBack; - - @FXML - private Label showCard; - - @FXML - private ToggleButton btnToggleFirstLastWrapAroundMode; - - @FXML - private ToggleButton btnToggleRandomMode; - - @FXML - private ToggleButton btnToggleShowOtherSide; - - private FlashCardProvider flashCardProvider = new FlashCardProvider(); - - /** - * Initializes the card deck. - * - * @param url - * @param resourceBundle - */ - @Override - public void initialize(URL url, ResourceBundle resourceBundle) { - - try { - flashCardProvider.setCardDeck(CardDeckStorage.readDeck(0)); - } catch (Exception e) { - flashCardProvider.setCardDeck(new CardDeck(List.of(Flashcard.of("Welcome to Flashy", "E N J O Y !")))); - } - setToggleButtonValues(); - showCurrentCardContent(); - } - - private void setToggleButtonValues() { - btnToggleRandomMode.setSelected(flashCardProvider.getCardOrderIsRandomMode()); - btnToggleShowOtherSide.setSelected(flashCardProvider.getShowOrderIsFlippedMode()); - btnToggleFirstLastWrapAroundMode.setSelected(flashCardProvider.getFirstLastWrapAroundMode()); - } - - public void showCurrentCardContent() { - var content = flashCardProvider.getCard(); - showCard.setText(listToString(content.getLines())); - } - - /** - * Converts the card content to String. - * - * @param lines - */ - private static String listToString(List lines) { - return String.join("\n", lines); - } +// package it1901.groups2021.gr2141.ui; - @FXML - public void handleFlipCardMouse(MouseEvent event) { - flashCardProvider.flipCard(); - showCurrentCardContent(); - } +// /** +// * Root controller of the application UI. +// */ +// public class FlashyAppController implements Initializable { - @FXML - public void handleFlipCardButton(ActionEvent event) { - flashCardProvider.flipCard(); - showCurrentCardContent(); - } - @FXML - public void handleTurnToNextCard(ActionEvent event) { - flashCardProvider.turnToNextCard(flashCardProvider.getCardIndex()); - showCurrentCardContent(); - } - - @FXML - public void handleTurnToPreviousCard(ActionEvent event) { - flashCardProvider.turnToPreviousCard(flashCardProvider.getCardIndex()); - showCurrentCardContent(); - } - - @FXML - public void handleToggleShowOtherSide(ActionEvent event) { - flashCardProvider.toggleShowOrderIsFlippedMode(); - showCurrentCardContent(); - } - - @FXML - public void handleToggleCardOrderIsRandom(ActionEvent event) { - flashCardProvider.toggleCardOrderIsRandomMode(); - } - - @FXML - public void handleToggleFirstLastWrapAroundMode(ActionEvent event) { - flashCardProvider.toggleFirstLastWrapAroundMode(); - } - - private void clearTextFields() { - writeFront.clear(); - writeBack.clear(); - } - - /** - * Creates a card and saves the card - * - * @param event - */ - @FXML - public void handleCreateFlashCard(ActionEvent event) { - if (writeFront.getText().isEmpty() || writeBack.getText().isEmpty()) - return; - - flashCardProvider.getCardDeck().add(Flashcard.of(writeFront.getText(), writeBack.getText())); - clearTextFields(); - - try { - CardDeckStorage.writeDeck(flashCardProvider.getCardDeck(), 0); - } catch (Exception e) { - System.err.println("Failed to write carddeck to file"); - } - } - -} +// } diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyModel.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyModel.java new file mode 100644 index 0000000..4dc56e5 --- /dev/null +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/FlashyModel.java @@ -0,0 +1,27 @@ +package it1901.groups2021.gr2141.ui; + +import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; + +public class FlashyModel { + + private FlashCardProvider flashCardProvider; + + public FlashyModel() { + this.flashCardProvider = new FlashCardProvider(); + } + + public FlashCardProvider getFlashCardProvider() { + return this.flashCardProvider; + } + + public void setFlashCardProvider(FlashCardProvider flashCardProvider) { + if (flashCardProvider == null) { + throw new IllegalArgumentException("FlashCardProvider can not be null"); + } + this.flashCardProvider = flashCardProvider; + } + + + + +} diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardAreaController.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardAreaController.java new file mode 100644 index 0000000..ca1eb86 --- /dev/null +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardAreaController.java @@ -0,0 +1,68 @@ +package it1901.groups2021.gr2141.ui.controllers; + +import java.util.List; +import java.net.URL; +import java.util.ResourceBundle; +import javafx.fxml.Initializable; + +import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; +import it1901.groups2021.gr2141.core.models.CardDeck; +import it1901.groups2021.gr2141.core.models.Flashcard; +import it1901.groups2021.gr2141.core.storage.CardDeckStorage; +import it1901.groups2021.gr2141.ui.Flashy; +import javafx.fxml.FXML; +import javafx.scene.control.Label; +import javafx.scene.input.MouseEvent; + + +/** + * CardArea controller of the application UI. + */ +public class FlashyCardAreaController implements Initializable { + + @FXML + private Label showCard; + + /** + * Initializes the card deck. + * + * @param url + * @param resourceBundle + */ + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + + try { + getFlashcardProvider().setCardDeck(CardDeckStorage.readDeck(0)); + } catch (Exception e) { + getFlashcardProvider().setCardDeck(new CardDeck(List.of(Flashcard.of("Welcome to Flashy", "E N J O Y !")))); + } + + showCurrentCardContent(); + getFlashcardProvider().subscribe((newFlashCardModel) -> showCurrentCardContent()); + } + + private FlashCardProvider getFlashcardProvider() { + return Flashy.applicationState.getFlashCardProvider(); + } + + public void showCurrentCardContent() { + var content = getFlashcardProvider().getCard(); + showCard.setText(listToString(content.getLines())); + } + + /** + * Converts the card content to String. + * + * @param lines + */ + private static String listToString(List lines) { + return String.join("\n", lines); + } + + @FXML + public void handleFlipCardMouse(MouseEvent event) { + getFlashcardProvider().flipCard(); + } + +} diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardNavigationController.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardNavigationController.java new file mode 100644 index 0000000..09272e6 --- /dev/null +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyCardNavigationController.java @@ -0,0 +1,29 @@ +package it1901.groups2021.gr2141.ui.controllers; + +import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; +import it1901.groups2021.gr2141.ui.Flashy; +import javafx.event.ActionEvent; +import javafx.fxml.FXML; + +public class FlashyCardNavigationController { + + + private FlashCardProvider getFlashcardProvider() { + return Flashy.applicationState.getFlashCardProvider(); + } + + @FXML + public void handleFlipCardButton(ActionEvent event) { + getFlashcardProvider().flipCard(); + } + + @FXML + public void handleTurnToNextCard(ActionEvent event) { + getFlashcardProvider().turnToNextCard(getFlashcardProvider().getCardIndex()); + } + + @FXML + public void handleTurnToPreviousCard(ActionEvent event) { + getFlashcardProvider().turnToPreviousCard(getFlashcardProvider().getCardIndex()); + } +} diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyMenuController.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyMenuController.java new file mode 100644 index 0000000..ef8a0c8 --- /dev/null +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyMenuController.java @@ -0,0 +1,52 @@ +package it1901.groups2021.gr2141.ui.controllers; + + +import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; +import it1901.groups2021.gr2141.core.models.Flashcard; +import it1901.groups2021.gr2141.core.storage.CardDeckStorage; +import it1901.groups2021.gr2141.ui.Flashy; +import javafx.fxml.FXML; +import javafx.scene.control.TextField; +import javafx.event.ActionEvent; + +/** + * Menu controller of the application UI. + */ +public class FlashyMenuController { + + @FXML + private TextField writeFront; + + @FXML + private TextField writeBack; + + + private FlashCardProvider getFlashcardProvider() { + return Flashy.applicationState.getFlashCardProvider(); + } + + /** + * Creates a card and saves the card + * + * @param event + */ + @FXML + public void handleCreateFlashCard(ActionEvent event) { + if (writeFront.getText().isEmpty() || writeBack.getText().isEmpty()) + return; + + getFlashcardProvider().getCardDeck().add(Flashcard.of(writeFront.getText(), writeBack.getText())); + clearTextFields(); + + try { + CardDeckStorage.writeDeck(getFlashcardProvider().getCardDeck(), 0); + } catch (Exception e) { + System.err.println("Failed to write carddeck to file"); + } + } + + private void clearTextFields() { + writeFront.clear(); + writeBack.clear(); + } +} diff --git a/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyNavbarController.java b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyNavbarController.java new file mode 100644 index 0000000..e51ea53 --- /dev/null +++ b/flashy/fxui/src/main/java/it1901/groups2021/gr2141/ui/controllers/FlashyNavbarController.java @@ -0,0 +1,64 @@ +package it1901.groups2021.gr2141.ui.controllers; + +import java.net.URL; +import java.util.ResourceBundle; + +import it1901.groups2021.gr2141.core.domainlogic.FlashCardProvider; +import it1901.groups2021.gr2141.ui.Flashy; +import javafx.fxml.Initializable; + +import javafx.fxml.FXML; +import javafx.scene.control.ToggleButton; +import javafx.event.ActionEvent; + +/** + * Navbar controller of the application UI. + */ +public class FlashyNavbarController implements Initializable { + + @FXML + private ToggleButton btnToggleFirstLastWrapAroundMode; + + @FXML + private ToggleButton btnToggleRandomMode; + + @FXML + private ToggleButton btnToggleShowOtherSide; + + /** + * Initializes the card deck. + * + * @param url + * @param resourceBundle + */ + @Override + public void initialize(URL url, ResourceBundle resourceBundle) { + setToggleButtonValues(); + } + + private FlashCardProvider getFlashcardProvider() { + return Flashy.applicationState.getFlashCardProvider(); + } + + private void setToggleButtonValues() { + btnToggleRandomMode.setSelected(getFlashcardProvider().getCardOrderIsRandomMode()); + btnToggleShowOtherSide.setSelected(getFlashcardProvider().getShowOrderIsFlippedMode()); + btnToggleFirstLastWrapAroundMode.setSelected(getFlashcardProvider().getFirstLastWrapAroundMode()); + } + + @FXML + public void handleToggleShowOtherSide(ActionEvent event) { + getFlashcardProvider().toggleShowOrderIsFlippedMode(); + } + + @FXML + public void handleToggleCardOrderIsRandom(ActionEvent event) { + getFlashcardProvider().toggleCardOrderIsRandomMode(); + } + + @FXML + public void handleToggleFirstLastWrapAroundMode(ActionEvent event) { + getFlashcardProvider().toggleFirstLastWrapAroundMode(); + } + +} diff --git a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/FlashyApp.fxml b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/FlashyApp.fxml index 3c697bf..53702ad 100644 --- a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/FlashyApp.fxml +++ b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/FlashyApp.fxml @@ -1,99 +1,34 @@ - - + - - - - - - + - - - - - - + -
- - - - - - + + + + - - - -
+
\ No newline at end of file diff --git a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardArea.fxml b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardArea.fxml new file mode 100644 index 0000000..d050328 --- /dev/null +++ b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardArea.fxml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardNavigation.fxml b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardNavigation.fxml new file mode 100644 index 0000000..7df0381 --- /dev/null +++ b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/CardNavigation.fxml @@ -0,0 +1,15 @@ + + + + + + + + + \ No newline at end of file diff --git a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/Navbar.fxml b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/Navbar.fxml new file mode 100644 index 0000000..1e8b2ba --- /dev/null +++ b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/components/Navbar.fxml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/styling/Flashy.css b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/styling/Flashy.css index 040bcd0..a517f84 100644 --- a/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/styling/Flashy.css +++ b/flashy/fxui/src/main/resources/it1901/groups2021/gr2141/ui/styling/Flashy.css @@ -20,8 +20,6 @@ -fx-font-family: "Roboto"; } - - /* --------------------------------- Navbar --------------------------------- */ #navbar { @@ -67,18 +65,14 @@ #menu .button:pressed { -fx-background-color: -main-black-color; - } - /* --------------------------------- FlashCard --------------------------------- */ -#flashcontainer { +#cardArea { -fx-background-color: -main-grey-color; - } - #showCard { -fx-background-color: -main-purple-menu-accent-frame-color; -fx-text-fill: -main-fg-color;