diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/LinkPopUp.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/LinkPopUp.java index 00247c3ce451807c90630830162f3b1d558e6d97..f54b91b3956f220f937a3071c03057efa5bc6bcd 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/LinkPopUp.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/LinkPopUp.java @@ -3,30 +3,35 @@ package edu.ntnu.idatt2001.group_30.paths.view.components.pop_up; import edu.ntnu.idatt2001.group_30.paths.model.Link; import edu.ntnu.idatt2001.group_30.paths.model.Passage; import edu.ntnu.idatt2001.group_30.paths.model.actions.Action; +import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Pos; import javafx.scene.control.*; import javafx.scene.layout.VBox; +import java.util.HashMap; + public class LinkPopUp { private TextField textField; - private TextField referenceField; private Button saveButton; private ObservableList<Passage> passages; private ObservableList<Link> links; - + private HashMap<String, Passage> passageHashMap; private Link link; public LinkPopUp(ObservableList<Passage> passages, ObservableList<Link> links) { this.passages = passages; this.links = links; + this.passageHashMap = new HashMap<>(); textField = new TextField(); textField.setPromptText("Enter the text of the link"); - referenceField = new TextField(); - referenceField.setPromptText("Enter the reference of the link"); + + passages.forEach(passage -> passageHashMap.put(passage.getTitle(), passage)); + ComboBox<String> reference = new ComboBox<>(FXCollections.observableArrayList(passageHashMap.keySet())); + reference.setPromptText("Select reference passage of the link"); saveButton = new Button("Save"); @@ -43,7 +48,7 @@ public class LinkPopUp { new Label("Link Text:"), textField, new Label("Link Reference:"), - referenceField, + reference, new Label("Actions:"), actionComboBox, addActionButton, @@ -61,11 +66,10 @@ public class LinkPopUp { .withDialogSize(400, 500); saveButton.setOnAction(e -> { - if (textField.getText().isBlank() || referenceField.getText().isBlank()) { + if (textField.getText().isBlank() || reference.getValue() == null) { AlertDialog.showWarning("The text or reference cannot be blank."); } else { - link = new Link(textField.getText(), referenceField.getText()); - + link = new Link(textField.getText(), passageHashMap.get(reference.getValue()).getTitle()); popUp.close(); } }); @@ -73,6 +77,10 @@ public class LinkPopUp { popUp.showAndWait(); } + /** + * This method retrieves the link created in the pop-up. + * @return Link created in pop-up, given as a Link object. + */ public Link getLink() { return link; } diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/PassagePopUp.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/PassagePopUp.java index f549f821e9be8d3d624e70519ee4f42a7d3b6c64..86ff9746a0bcb33e1a17a30be9e60e2b802e735f 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/PassagePopUp.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/components/pop_up/PassagePopUp.java @@ -2,9 +2,7 @@ package edu.ntnu.idatt2001.group_30.paths.view.components.pop_up; import edu.ntnu.idatt2001.group_30.paths.model.Passage; import edu.ntnu.idatt2001.group_30.paths.model.Link; -import edu.ntnu.idatt2001.group_30.paths.model.actions.Action; import edu.ntnu.idatt2001.group_30.paths.view.components.table.LinkTable; -import edu.ntnu.idatt2001.group_30.paths.view.components.table.PassageTable; import edu.ntnu.idatt2001.group_30.paths.view.components.table.TableDisplay; import javafx.collections.FXCollections; import javafx.collections.ObservableList; @@ -13,8 +11,6 @@ import javafx.scene.control.*; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import java.util.stream.Collectors; - /** * This class contains a pop-up for creating a new passage. * @@ -22,19 +18,45 @@ import java.util.stream.Collectors; */ public class PassagePopUp { - private final TextField titleField; - private final TextArea contentArea; - private final Button saveButton; - private final Button removeLinkButton; - private final LinkTable<Link> linkTable; + private TextField titleField; + private TextArea contentArea; + private Button saveButton; + private Button removeLinkButton; + private Button addLinkButton; + private VBox content; + private LinkTable<Link> linkTable; private final ObservableList<Passage> passages; private final ObservableList<Link> links; private Passage passage; + private PopUp<VBox, ?> popUp; public PassagePopUp(ObservableList<Passage> passages) { this.passages = passages; this.links = FXCollections.observableArrayList(); + initialize(); + createPopUp(); + } + + /** + * This constructor allows a pre-existing passage to be edited. + * @param passages Other passages in the story, given as an ObservableList of passages. + * @param passage Passage to edit, given as a Passage object. + */ + public PassagePopUp(ObservableList<Passage> passages, Passage passage) { + this.passages = passages; + this.passage = passage; + this.links = FXCollections.observableArrayList(passage.getLinks()); + initialize(); + loadPassage(passage); + createPopUp(); + } + private void initialize() { + setupUiComponents(); + setupBehavior(); + } + + private void setupUiComponents() { titleField = new TextField(); titleField.setPromptText("Enter the title of the passage"); @@ -53,17 +75,13 @@ public class PassagePopUp { removeLinkButton = new Button("Remove Link"); removeLinkButton.setDisable(true); - removeLinkButton.setOnAction(e -> links.remove(linkTable.getSelectionModel().getSelectedItem())); - linkTable.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> - removeLinkButton.setDisable(newSelection == null)); + addLinkButton = new Button("Add Link"); + if(passages.isEmpty()) addLinkButton.setDisable(true); - Button addLinkButton = new Button("Add Link"); HBox linkTableButtonHBox = new HBox(addLinkButton, removeLinkButton); linkTableButtonHBox.setAlignment(Pos.CENTER); - addLinkButton.setOnAction(e -> this.links.add(new LinkPopUp(passages, links).getLink())); - - VBox content = new VBox( + content = new VBox( new Label("Passage Title:"), titleField, new Label("Passage Content:"), @@ -76,14 +94,22 @@ public class PassagePopUp { content.setAlignment(Pos.CENTER); content.setSpacing(20); + } + + private void setupBehavior() { + removeLinkButton.setOnAction(e -> links.remove(linkTable.getSelectionModel().getSelectedItem())); + linkTable.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> + removeLinkButton.setDisable(newSelection == null)); + + + + addLinkButton.setOnAction(e -> { + Link newLink = new LinkPopUp(this.passages, links).getLink(); + if(newLink != null) { + this.links.add(newLink); + } + }); - PopUp<VBox, ?> popUp = PopUp - .<VBox>create() - .withTitle("Create a Passage") - .withoutCloseButton() - .withContent(content) - .withDialogSize(400, 500); - ; saveButton.setOnAction(e -> { if (titleField.getText().isBlank() || contentArea.getText().isBlank()) { @@ -91,19 +117,34 @@ public class PassagePopUp { } else { this.passage = new Passage(titleField.getText(), contentArea.getText()); + this.links.forEach(link -> this.passage.addLink(link)); //TODO: save the new passage //TODO: add links from the table to the new passage popUp.close(); } }); + } + + private void createPopUp() { + popUp = PopUp + .<VBox>create() + .withTitle("Create a Passage") + .withoutCloseButton() + .withContent(content) + .withDialogSize(400, 500); popUp.showAndWait(); } + private void loadPassage(Passage passage) { + titleField.setText(passage.getTitle()); + contentArea.setText(passage.getContent()); + } + /** * This method retrieves the passages created from the pop-up. - * @return Passags created, given as a List of Passage objects. + * @return Passages created, given as a List of Passage objects. */ public Passage getPassage() { return passage; diff --git a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java index 6fe9c962a56e492fefc728d0513f408bd1d1804e..bdb7cd3d2bf182beba7143dfa59bbc1935824a65 100644 --- a/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java +++ b/src/main/java/edu/ntnu/idatt2001/group_30/paths/view/views/NewStoryView.java @@ -8,6 +8,7 @@ import edu.ntnu.idatt2001.group_30.paths.model.Story; import static edu.ntnu.idatt2001.group_30.paths.PathsSingleton.INSTANCE; +import edu.ntnu.idatt2001.group_30.paths.view.components.common.DefaultText; import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.AlertDialog; import edu.ntnu.idatt2001.group_30.paths.view.components.pop_up.PassagePopUp; import edu.ntnu.idatt2001.group_30.paths.view.components.table.PassageTable; @@ -35,6 +36,7 @@ public class NewStoryView extends View<BorderPane> { private Story story; private final ObservableList<Passage> passages; private final Button removePassageButton; + private final Button editPassageButton; public NewStoryView() { @@ -51,7 +53,7 @@ public class NewStoryView extends View<BorderPane> { passages = story == null ? FXCollections.observableArrayList() : FXCollections.observableArrayList(story.getPassages()); - Text titleText = new Text("Create a new/edit a Story"); + Text titleText = DefaultText.big("Create a new/edit a Story"); Text labelText = new Text("Story Title: "); TextField textField = new TextField(title); @@ -59,6 +61,7 @@ public class NewStoryView extends View<BorderPane> { HBox titleBox = new HBox(labelText, textField); + titleBox.setSpacing(20); textField.setOnKeyTyped(event -> { title = textField.getText(); @@ -85,8 +88,24 @@ public class NewStoryView extends View<BorderPane> { removePassageButton = new Button("Remove Passage"); removePassageButton.setDisable(true); removePassageButton.setOnAction(e -> passages.remove(passageTable.getSelectionModel().getSelectedItem())); - passageTable.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> - removePassageButton.setDisable(newSelection == null)); + + editPassageButton = new Button("Edit Passage"); + editPassageButton.setDisable(true); + editPassageButton.setOnAction(e -> { + Passage selectedPassage = passageTable.getSelectionModel().getSelectedItem(); + if (selectedPassage != null) { + Passage updatedPassage = new PassagePopUp(passages, selectedPassage).getPassage(); + if(updatedPassage != null) { + passages.remove(selectedPassage); + passages.add(updatedPassage); + } + } + }); + + passageTable.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> { + removePassageButton.setDisable(newSelection == null); + editPassageButton.setDisable(newSelection == null); + }); Button addPassageButton = new Button(); @@ -100,17 +119,20 @@ public class NewStoryView extends View<BorderPane> { System.err.println("Something is wrong with the trash image resource link"); } + VBox editTableButtons = new VBox(addPassageButton, removePassageButton, editPassageButton); + editTableButtons.setAlignment(Pos.CENTER); + editTableButtons.setSpacing(20); + addPassageButton.setOnAction(event -> { if(passages.isEmpty()) { AlertDialog.showInformation("Every story needs an opening passage.", "The opening passage" + " will by default be the first passage added."); } - PassagePopUp passagePopUp = new PassagePopUp(INSTANCE.getStory() == null ? - FXCollections.observableArrayList() : FXCollections.observableArrayList(INSTANCE.getStory().getPassages())); + PassagePopUp passagePopUp = new PassagePopUp(passages); if(passagePopUp.getPassage() != null) this.passages.addAll(passagePopUp.getPassage()); }); - Button saveButton = new Button("Save"); + Button saveButton = new Button("Save Story"); saveButton.setOnAction(event -> { try { newStoryController.addStory(title, passages); @@ -121,7 +143,7 @@ public class NewStoryView extends View<BorderPane> { } }); - VBox display = new VBox(titleText, titleBox, passageTable, addPassageButton, removePassageButton, saveButton); + VBox display = new VBox(titleText, titleBox, passageTable, saveButton); display.setAlignment(Pos.CENTER); display.setSpacing(10); display.setPrefWidth(500); @@ -131,6 +153,8 @@ public class NewStoryView extends View<BorderPane> { getParentPane().setCenter(display); getParentPane().setBottom(backButton); + getParentPane().setRight(editTableButtons); + getParentPane().getRight().setTranslateX(-50); } }