Skip to content

Commit

Permalink
Merge pull request #137 from jomcruz93/master
Browse files Browse the repository at this point in the history
Created a class to handle displaying of note prompt window when adding (and editing - v1.3) notes.
  • Loading branch information
m-aslam-mj2 committed Oct 20, 2018
2 parents c01e05e + cd36c48 commit bf36635
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 65 deletions.
50 changes: 4 additions & 46 deletions src/main/java/seedu/address/logic/commands/NoteAddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NOTE_DATE;

import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.note.Note;
import seedu.address.model.note.NoteManager;
import seedu.address.ui.NoteEntryPrompt;
import seedu.address.ui.NoteTextEditWindow;

/**
* Adds a note to Trajectory.
Expand Down Expand Up @@ -47,9 +39,10 @@ public NoteAddCommand(Note note) {
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
NoteManager noteManager = new NoteManager();

boolean isCancelled = showTextPrompt();
NoteTextEditWindow noteTextEditWindow = new NoteTextEditWindow(noteToAdd);
noteTextEditWindow.showAndWait();

if (!isCancelled) {
if (!noteTextEditWindow.isCancelled()) {
noteManager.addNote(noteToAdd);
noteManager.saveNoteList();

Expand All @@ -58,39 +51,4 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
return new CommandResult(MESSAGE_CANCEL);
}
}

/**
* Displays a pop-up window for the user to enter input.
*
* @return true if the user cancelled, otherwise false
*/
public boolean showTextPrompt() {

try {
NoteEntryPrompt controller = new NoteEntryPrompt();

FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/NoteEntryPromptWindow.fxml"));
loader.setController(controller);
BorderPane layout = loader.load();
Scene scene = new Scene(layout);

Stage notePromptStage = new Stage();
notePromptStage.setMinHeight(380);
notePromptStage.setMinWidth(350);
notePromptStage.setTitle("Add/Edit note");
notePromptStage.initModality(Modality.APPLICATION_MODAL);
notePromptStage.setScene(scene);

controller.setDialogStage(notePromptStage);
controller.setNote(noteToAdd);

notePromptStage.showAndWait();

return controller.isCancelled();

} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public CommandResult execute(Model model, CommandHistory history) throws Command

NoteManager noteManager = new NoteManager();

System.out.println("ArrayList size: " + noteManager.getNotes().size());

if (index > noteManager.getNotes().size() || index < 1) {
return new CommandResult(MESSAGE_INVALID_INDEX);
} else {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/seedu/address/logic/commands/NoteListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
import seedu.address.model.note.Note;
import seedu.address.model.note.NoteManager;


/**
* Lists notes based on given predicate.
*/
public class NoteListCommand extends Command {

public static final String COMMAND_WORD = "note list";

public static final String MESSAGE_SUCCESS = "Listed %1$s note(s).\n";
public static final String MESSAGE_SUCCESS = "Listed %1$s note(s).";

public static final String MESSAGE_NOT_FOUND = "No notes were found.";

Expand All @@ -43,20 +42,23 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
StringBuilder sb = new StringBuilder();

int listId = 1;
int size = filteredNotes.size();

for (Note n: filteredNotes) {
sb.append(listId++ + ":\n");
sb.append(listId + ":\n");
sb.append("Module Code: ");
sb.append(n.getModuleCode() + "\n");
sb.append("Date: ");
sb.append(n.getDate() + "\n");
sb.append("Note:\n");
sb.append(n.getNoteText() + "\n\n");
sb.append(n.getNoteText() + ((listId < size) ? "\n\n" : "\n"));
listId++;
}

if (sb.length() > 0) {
return new CommandResult(
String.format(MESSAGE_SUCCESS, filteredNotes.size(), "s")
+ "\n" + sb.toString());
String.format(MESSAGE_SUCCESS, filteredNotes.size())
+ "\n\n" + sb.toString());
} else {
return new CommandResult(String.format(MESSAGE_NOT_FOUND));
}
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/seedu/address/ui/NoteEntryPrompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import seedu.address.model.note.Note;

/**
* The window to add/edit a note. It provides a Text Area
* to allow the user to type notes in multiple line.
* This is the controller for the NoteEntryPromptWindow FXML.
* It consists of a TextArea for the user to enter input in multiple lines.
*/
public class NoteEntryPrompt {

Expand Down Expand Up @@ -41,7 +41,8 @@ public void setDialogStage(Stage dialogStage) {
}

/**
* Populate the Text Area containing the editable note
* Populates the TextArea with the editable note.
*
* @param note
*/
public void setNote(Note note) {
Expand All @@ -57,23 +58,37 @@ public void setNote(Note note) {
@FXML
private void handleKeyPress(KeyEvent keyEvent) {
if (keyCombinationSave.match(keyEvent)) {
String tempNote = noteContent.getText();

if (tempNote.trim().length() > 0) {
note.setNoteText(tempNote);
dialogStage.close();
}
saveAndCloseWindow();
}

if (keyCombinationCancel.match(keyEvent)) {
isCancelled = true;
closeWindow();
}
}

/**
* Saves the note and closes the window.
*/
public void saveAndCloseWindow() {
String tempNote = noteContent.getText();

if (tempNote.trim().length() > 0) {
note.setNoteText(tempNote);
dialogStage.close();
}
}

/**
* Closes the window without saving the note data.
*/
public void closeWindow() {
isCancelled = true;
dialogStage.close();
}

/**
* Returns true if the user pressed the key combination to cancel.
* Otherwise, return false
* Otherwise, return false.
*
* @return value of isCancelled.
*/
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/seedu/address/ui/NoteTextEditWindow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.address.ui;

import java.io.IOException;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import seedu.address.model.note.Note;

/**
* Handles the displaying of a TextArea window for adding/editing a note.
*/
public class NoteTextEditWindow {

private static final String PATH = "/view/NoteEntryPromptWindow.fxml";

private BorderPane layout;
private FXMLLoader loader;
private Scene scene;
private Stage notePromptStage;

private NoteEntryPrompt controller;

private Note note;

public NoteTextEditWindow(Note note) {
this.note = note;
setUpWindow();
}

/**
* This method sets up the window to be loaded and
* handles its configuration and behaviour.
*/
private void setUpWindow() {
try {
controller = new NoteEntryPrompt();

loader = new FXMLLoader(getClass().getResource(PATH));
loader.setController(controller);
layout = loader.load();

scene = new Scene(layout);

notePromptStage = new Stage();
notePromptStage.setMinHeight(380);
notePromptStage.setMinWidth(350);
notePromptStage.setTitle("Add/Edit note");
notePromptStage.initModality(Modality.APPLICATION_MODAL);
notePromptStage.setScene(scene);
notePromptStage.setOnCloseRequest(e -> {
e.consume();
controller.closeWindow();
});

controller.setDialogStage(notePromptStage);
controller.setNote(note);
} catch (IOException e) {
e.printStackTrace();
}
}

public void showAndWait() {
notePromptStage.showAndWait();
}

public boolean isCancelled() {
return controller.isCancelled();
}
}

0 comments on commit bf36635

Please sign in to comment.