Skip to content

Commit

Permalink
Merge pull request #97 from ciaoosuuu/export-pdf
Browse files Browse the repository at this point in the history
Export pdf v0.2
  • Loading branch information
ciaoosuuu authored Oct 25, 2022
2 parents d21dc47 + 01fd509 commit 73c421f
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 21 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ dependencies {
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'mac'
implementation group: 'org.openjfx', name: 'javafx-graphics', version: javaFxVersion, classifier: 'linux'

implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.27'

implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.7.0'
implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.7.4'

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/seedu/waddle/logic/StageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ public void setWishStage(Itinerary selectedItinerary) throws NullPointerExceptio
this.selectedItinerary = selectedItinerary;
}

public void setScheduleStage(Itinerary selectedItinerary) throws NullPointerException {
requireNonNull(selectedItinerary);
this.currentStage = Stages.SCHEDULE;
this.selectedItinerary = selectedItinerary;
}

public void switchStage(Stages selectedStage) {
this.currentStage = selectedStage;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/seedu/waddle/logic/Stages.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
public enum Stages {
NONE, // used in CommandResult to indicate that no state change occurred
HOME, //home page
WISH, //wishlist stage
SCHEDULE //scheduling stage
WISH //wishlist stage
}
8 changes: 0 additions & 8 deletions src/main/java/seedu/waddle/logic/commands/AddItemCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ public CommandResult execute(Model model) throws CommandException {

StageManager stageManager = StageManager.getInstance();

// if not at wish stage, throw exception and change to wish stage
/*
if (!stageManager.isCurrentStage(Stages.WISH)) {
return new CommandResult(MESSAGE_WRONG_STAGE);
}
stageManager.setHomeStage();
*/
Itinerary itinerary = stageManager.getSelectedItinerary();

if (itinerary.hasItem(toAdd)) {
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/seedu/waddle/logic/commands/ExportCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.waddle.logic.commands;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import seedu.waddle.logic.StageManager;
import seedu.waddle.logic.Stages;
import seedu.waddle.logic.commands.exceptions.CommandException;
import seedu.waddle.model.Model;
import seedu.waddle.model.itinerary.Itinerary;

/**
* Export an itinerary into pdf format.
*/
public class ExportCommand extends Command {

public static final String COMMAND_WORD = "export";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": export current itinerary to PDF\n"
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_EXPORT_SUCCESS = "PDF created!";
public static final String MESSAGE_EXPORT_FAILURE = "Failed to export!";

public static final String MESSAGE_EXPORT_WRONG_STAGE = "Please select an itinerary before exporting.";

@Override
public CommandResult execute(Model model) throws CommandException {
StageManager stageManager = StageManager.getInstance();

if (stageManager.isCurrentStage(Stages.HOME)) {
return new CommandResult(MESSAGE_EXPORT_WRONG_STAGE);
}

Itinerary itinerary = stageManager.getSelectedItinerary();

try {
PDDocument doc = new PDDocument();
PDPage myPage = new PDPage();
doc.addPage(myPage);
PDPage page = doc.getPage(0);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.setFont(PDType1Font.TIMES_BOLD_ITALIC, 14);
contentStream.newLineAtOffset(0, 700);
contentStream.showText(itinerary.toString());
contentStream.endText();
contentStream.close();
doc.save("./data/" + itinerary.getName().fullName + ".pdf");
doc.close();
} catch (IOException e) {
return new CommandResult(MESSAGE_EXPORT_FAILURE);
}

return new CommandResult(MESSAGE_EXPORT_SUCCESS);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
return other instanceof ExportCommand;

}
}
9 changes: 7 additions & 2 deletions src/main/java/seedu/waddle/logic/parser/WaddleParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.waddle.logic.commands.EditCommand;
import seedu.waddle.logic.commands.EditItemCommand;
import seedu.waddle.logic.commands.ExitCommand;
import seedu.waddle.logic.commands.ExportCommand;
import seedu.waddle.logic.commands.FindCommand;
import seedu.waddle.logic.commands.HelpCommand;
import seedu.waddle.logic.commands.HomeCommand;
Expand Down Expand Up @@ -61,8 +62,6 @@ public Command parseCommand(String userInput) throws ParseException {
return parseHomeCommand(commandWord, arguments);
case WISH:
return parseWishCommand(commandWord, arguments);
case SCHEDULE:
return parseScheduleCommand(commandWord, arguments);
default:
throw new ParseException(MESSAGE_UNKNOWN_STAGE);
}
Expand Down Expand Up @@ -109,6 +108,9 @@ public Command parseHomeCommand(String commandWord, String arguments) throws Par
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ExportCommand.COMMAND_WORD:
return new ExportCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down Expand Up @@ -155,6 +157,9 @@ public Command parseWishCommand(String commandWord, String arguments) throws Par
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ExportCommand.COMMAND_WORD:
return new ExportCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/waddle/model/itinerary/Itinerary.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private void sortUnscheduledItemList() {
public Item unplanItem(MultiIndex index) {
Day day = this.days.get(index.getDayIndex().getZeroBased());
Item unplannedItem = day.removeItem(index.getTaskIndex());
unplannedItem.resetStartTime();
addItem(unplannedItem);
sortUnscheduledItemList();
this.budget.updateSpending(-unplannedItem.getCost().getValue());
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/seedu/waddle/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,6 @@ private CommandResult executeCommand(String commandText) throws CommandException
.getSelectedItinerary().getUnmodifiableItemGroups();
setListPanel(new ItemGroupListPanel(itemGroups));
break;
case SCHEDULE:
//TODO: create a ListPanel for Schedule page
break;
default:
break;
}
Expand Down

0 comments on commit 73c421f

Please sign in to comment.