Skip to content

Commit

Permalink
Merge pull request #9 from pr0crustes/beta
Browse files Browse the repository at this point in the history
Beta - Bug fixes, code refactoration.
  • Loading branch information
pr0crustes authored Oct 2, 2018
2 parents fc3b6a3 + a1c8697 commit 82883de
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>pr0crustes</groupId>
<artifactId>PDFToolHelper</artifactId>
<version>1.0.0</version>
<version>1.4.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import me.pr0crustes.backend.enums.FileExtensions;
import me.pr0crustes.backend.exeptions.NullFileException;
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Callable;

Expand All @@ -26,4 +29,20 @@ protected Callable<File> getCallable(Stage stage, List<FileChooser.ExtensionFilt
return (() -> this.createFileWindow("Save as", filters).showSaveDialog(stage));
}

/**
* Method that asks the user and saves a given PDDocument.
* @param document the PDDocument to be saved.
* @throws IOException in case of an exception during file saving.
* @throws NullFileException in case the user selected file is null.
*/
public void savePDF(PDDocument document) throws IOException, NullFileException {
File saveAs = this.getSelection();

if (saveAs == null) {
throw new NullFileException();
}

document.save(saveAs);
}

}
63 changes: 41 additions & 22 deletions src/main/java/me/pr0crustes/backend/classes/number/RangeEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.pr0crustes.backend.exeptions.ArgumentException;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -66,30 +67,20 @@
* '*' can be used to match every number. In this case, all other operations will be ignored.
*
* Because of this, RangeEx allows the user to specifically select pages of a file, even not continuous ones.
*
*
* Extends HashSet of generic Integer, overriding a few methods.
*/
public class RangeEx {
public class RangeEx extends HashSet<Integer> {

private final Set<Integer> values;
private boolean isUniversal = false;

/**
* Constructor that setups RangeEx with a string.
* @param rangeString a valid RangeEx string.
*/
public RangeEx(String rangeString) throws ArgumentException {
this.values = this.getValues(rangeString);
}

/**
* Method that tests if value is in rangex.
* @param value the value to test.
* @return if the value is contained in this rangex.
*/
public boolean contains(int value) {
if (this.isUniversal) {
return true;
}
return this.values.contains(value);
this.addAll(this.getValues(rangeString));
}

/**
Expand All @@ -111,21 +102,18 @@ private Set<Integer> getValues(String rangeString) throws ArgumentException {
return parsedValues; // Empty set
}

Set<String> rangesParts = RangeEx.getMatches(rangeString, "\\d+_\\d+");
for (String str : rangesParts) {
for (String str : RangeEx.getMatches(rangeString, "\\d+_\\d+")) {
String[] parts = str.split("_");
for (int i = Integer.valueOf(parts[0]); i <= Integer.valueOf(parts[1]); i++) {
for (int i = RangeEx.stringAsUnsigned(parts[0]); i <= RangeEx.stringAsUnsigned(parts[1]); i++) {
parsedValues.add(i);
}
}

Set<String> addParts = RangeEx.getMatches(rangeString, "[+]\\d+");
for (String str : addParts) {
for (String str : RangeEx.getMatches(rangeString, "[+]\\d+")) {
parsedValues.add(RangeEx.stringAsUnsigned(str));
}

Set<String> subParts = RangeEx.getMatches(rangeString, "[-]\\d+");
for (String str : subParts) {
for (String str : RangeEx.getMatches(rangeString, "[-]\\d+")) {
parsedValues.remove(RangeEx.stringAsUnsigned(str));
}

Expand Down Expand Up @@ -163,4 +151,35 @@ private static Set<String> getMatches(String str, String regex) {
return matches;
}

/**
* Override of `contains`:
* If isUniversal, every value should return true.
* Else, check if the value is in the hashSet.
* @param o the object to check.
* @return if contains o.
*/
@Override
public boolean contains(Object o) {
if (!(o instanceof Integer)) {
return false;
}
return this.isUniversal || super.contains(o);
}

/**
* Override of `containsAll`:
* Just calls `contains` in every elem.
* @param c the Collection to check.
* @return if contains all the values of c.
*/
@Override
public boolean containsAll(Collection<?> c) {
for (Object e : c) {
if (!this.contains(e)) {
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.pr0crustes.backend.exeptions;

/**
* NullFileException is a custom exception that should be thrown when the save as file is null.
*/
public class NullFileException extends Exception {
public NullFileException() {
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.event.EventHandler;
import javafx.scene.layout.Pane;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;

import java.io.IOException;

Expand Down Expand Up @@ -61,13 +62,13 @@ public void run() {
private void runExecute() {
try {
this.execute();
} catch (NullFileException e) {
e.printStackTrace();
// Ignore, user didn't selected a file
// TODO: Handle this in some way (?)
} catch (ArgumentException e) {
e.printStackTrace();
AlertFactory.DefinedAlert.invalidArgument.sendAlert();
} catch (NullPointerException e) {
e.printStackTrace();
// Ignore, user didn't selected a file
//TODO: Handle this in some way (?)
} catch (IOException e) {
e.printStackTrace();
AlertFactory.DefinedAlert.fileError.sendAlert();
Expand All @@ -82,10 +83,10 @@ private void runExecute() {
* This method can throw exceptions and because of this is mainly called from runExecute.
* @throws ArgumentException in case of invalid arguments.
* @throws IOException in case a file related error.
* @throws NullPointerException in case the user did not selected a file.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @throws Exception in case of any other error.
*/
@SuppressWarnings("RedundantThrows")
protected abstract void execute() throws ArgumentException, IOException, NullPointerException, Exception;
protected abstract void execute() throws ArgumentException, IOException, NullFileException, Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.pr0crustes.backend.classes.file.SaveFileSelector;
import me.pr0crustes.backend.classes.pdf.PDFConverter;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;
import me.pr0crustes.frontend.gui.classes.ListController;
import me.pr0crustes.frontend.gui.classes.elements.FileListViewManagerFactory;
import me.pr0crustes.frontend.gui.classes.elements.ListViewManager;
Expand Down Expand Up @@ -37,21 +38,20 @@ public class ConvertController extends ListController {
* Implementation of execute, converts the files and saves.
* @throws IOException in case of file error.
* @throws ArgumentException in case of invalid arguments.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @see me.pr0crustes.frontend.gui.classes.ActionController
*/
public void execute() throws IOException, ArgumentException {
public void execute() throws IOException, ArgumentException, NullFileException {
List<File> fileList = this.listViewManager.getList();

if (fileList.size() == 0) {
throw new ArgumentException();
}

File saveAs = new SaveFileSelector().getSelection();

PDFConverter converter = new PDFConverter(fileList);

PDDocument document = converter.getDocumentFromImages();
document.save(saveAs);
new SaveFileSelector().savePDF(document);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import me.pr0crustes.backend.classes.pdf.PDFCropper;
import me.pr0crustes.backend.enums.FileExtensions;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;
import me.pr0crustes.frontend.gui.classes.ActionController;
import me.pr0crustes.frontend.gui.classes.internationalization.LocalizableStrings;
import me.pr0crustes.frontend.gui.classes.layout.NodeFactory;
Expand Down Expand Up @@ -55,20 +56,19 @@ private void onClickSearch() {
* Method that creates a PDFCropper, crops the pdf and saves.
* @throws ArgumentException in case of invalid args.
* @throws IOException in case of file error.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @see ActionController
* @see PDFCropper
*/
public void execute() throws ArgumentException, IOException {
public void execute() throws ArgumentException, IOException, NullFileException {
if (this.selectedFile == null) {
throw new ArgumentException();
}

File saveAs = new SaveFileSelector().getSelection();

PDFCropper cropper = new PDFCropper(this.selectedFile);

PDDocument document = cropper.subDocument(new RangeEx(this.textFieldRange.getText()));
document.save(saveAs);
new SaveFileSelector().savePDF(document);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import me.pr0crustes.backend.classes.pdf.PDFInsert;
import me.pr0crustes.backend.enums.FileExtensions;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;
import me.pr0crustes.frontend.gui.classes.ActionController;
import me.pr0crustes.frontend.gui.classes.internationalization.LocalizableStrings;
import me.pr0crustes.frontend.gui.classes.layout.NodeFactory;
Expand Down Expand Up @@ -52,10 +53,11 @@ public class InsertController extends ActionController {
* Method that creates a PDFInsert, inserts a pdf into other and saves.
* @throws ArgumentException in case of argument error.
* @throws IOException in case of file error.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @see ActionController
* @see PDFInsert
*/
public void execute() throws ArgumentException, IOException {
public void execute() throws ArgumentException, IOException, NullFileException {
if (this.insertFile == null || this.intoFile == null) {
throw new ArgumentException();
}
Expand All @@ -64,12 +66,10 @@ public void execute() throws ArgumentException, IOException {

int afterPage = Numbers.valueFromTextField(this.textFieldIntoAfterPage);

File saveAs = new SaveFileSelector().getSelection();

PDFInsert pdfInsert = new PDFInsert(this.insertFile, this.intoFile);

PDDocument document = pdfInsert.insertDocument(rangeEx, afterPage);
document.save(saveAs);
new SaveFileSelector().savePDF(document);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.pr0crustes.backend.classes.file.SaveFileSelector;
import me.pr0crustes.backend.classes.pdf.PDFMerger;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;
import me.pr0crustes.frontend.gui.classes.ListController;
import me.pr0crustes.frontend.gui.classes.elements.FileListViewManagerFactory;
import me.pr0crustes.frontend.gui.classes.elements.ListViewManager;
Expand Down Expand Up @@ -37,24 +38,22 @@ public class MergeController extends ListController {
* Implementation of execute, merges the files and saves.
* @throws IOException in case of file related error.
* @throws ArgumentException in case of invalid arguments.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @see me.pr0crustes.frontend.gui.classes.ActionController
*/
@Override
public void execute() throws IOException, ArgumentException {
public void execute() throws IOException, ArgumentException, NullFileException {

List<File> fileList = this.listViewManager.getList();

if (fileList.size() == 0) {
throw new ArgumentException();
}

File saveAs = new SaveFileSelector().getSelection();

PDFMerger merger = new PDFMerger(fileList);

PDDocument document = merger.mergeFiles();

document.save(saveAs);
new SaveFileSelector().savePDF(document);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import me.pr0crustes.backend.classes.number.Numbers;
import me.pr0crustes.backend.classes.pdf.PDFQualityModifier;
import me.pr0crustes.backend.exeptions.ArgumentException;
import me.pr0crustes.backend.exeptions.NullFileException;
import me.pr0crustes.frontend.gui.classes.ActionController;
import me.pr0crustes.frontend.gui.classes.internationalization.LocalizableStrings;
import me.pr0crustes.frontend.gui.classes.layout.NodeFactory;
Expand Down Expand Up @@ -55,21 +56,20 @@ private void onClickSearch() {
* Method that creates a PDFQualityModifier, changes the pdf dpi and saves.
* @throws ArgumentException in case of invalid args.
* @throws IOException in case of file related error.
* @throws NullFileException in case the file selected by the user to saveAs is null.
* @see ActionController
* @see PDFQualityModifier
*/
@Override
public void execute() throws ArgumentException, IOException {
public void execute() throws ArgumentException, IOException, NullFileException {
if (this.selectedFile == null) {
throw new ArgumentException();
}

File saveAs = new SaveFileSelector().getSelection();

PDFQualityModifier qualityModifier = new PDFQualityModifier(this.selectedFile);

PDDocument document = qualityModifier.getDocumentWithDPI(Numbers.valueFromTextField(this.textFieldDpi));
document.save(saveAs);
new SaveFileSelector().savePDF(document);
}

/**
Expand Down

0 comments on commit 82883de

Please sign in to comment.