Skip to content

Commit

Permalink
Document queueing (#35)
Browse files Browse the repository at this point in the history
Allows multiple documents to be specified in CLI (or by drag & drop) and
processes them in sequence. Closes #34.
  • Loading branch information
iaik-jheher authored Jan 11, 2023
1 parent 4ec73be commit 4ed8b71
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public int handleArgument(String[] args, int argOffset,
throw new FileNotFoundException(signatureDocument);
}

getStatus().document = signatureDocumentFile;
var status = getStatus();
if (status.document == null)
status.document = signatureDocumentFile;
else
status.pendingDocuments.add(signatureDocumentFile);

return argOffset + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// Imports
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
Expand Down Expand Up @@ -54,19 +56,13 @@ public class DataSourceSelectComposite extends StateComposite {
*/
public void openFileDialog() {
FileDialog dialog = new FileDialog(
DataSourceSelectComposite.this.getShell(), SWT.OPEN);
DataSourceSelectComposite.this.getShell(), SWT.OPEN | SWT.MULTI);
dialog.setFilterExtensions(new String[] { "*.pdf", "*" });
dialog.setFilterNames(new String[] {
Messages.getString("common.PDFExtension_Description"),
Messages.getString("common.AllExtension_Description") });
String fileName = dialog.open();
File file = null;
if (fileName != null) {
file = new File(fileName);
if (file.exists()) {
DataSourceSelectComposite.this.setSelected(file);
}
}
dialog.open();
this.setSelected(dialog.getFilterPath(), dialog.getFileNames());
}

/**
Expand All @@ -93,15 +89,18 @@ public void widgetSelected(SelectionEvent e) {
/**
* Set this value through the setter method!!
*/
private File selected = null;

/**
* Sets the selected file and calls update to the workflow
*
* @param selected
*/
protected void setSelected(File selected) {
this.selected = selected;
final private List<File> selected = new ArrayList<>();

public void setSelected(String basePath, String[] fileNames) {
this.selected.clear();
if (fileNames != null) {
for (String fileName : fileNames) {
File file = new File(basePath, fileName);
if (file.exists()) {
this.selected.add(file);
}
}
}
this.state.updateStateMachine();
}

Expand All @@ -110,7 +109,7 @@ protected void setSelected(File selected) {
*
* @return the selected file
*/
public File getSelected() {
public List<File> getSelected() {
return this.selected;
}

Expand Down Expand Up @@ -200,16 +199,7 @@ public void drop(DropTargetEvent event) {
log.error("Dropped file name was null");
return;
}
String[] files = (String[]) event.data;
if (files.length > 0) {
// Only taking first file ...
File file = new File(files[0]);
if (!file.exists()) {
log.error(Messages.formatString("error.FileNotExist", files[0]));
return;
}
DataSourceSelectComposite.this.setSelected(file);
}
DataSourceSelectComposite.this.setSelected("", (String[])event.data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ public void saveDocument() {
* @param target the filename to save the document as
*
* @return saved File (or null if unsuccessful)
* @throws FileNotFoundException
* @throws IOException
*/
private File saveResultAsFile(File inputFolder, String target) {
if (target == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

// Imports
import java.io.File;
import java.util.ArrayDeque;
import java.util.Queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,6 +34,7 @@ public class Status {
private static final Logger log = LoggerFactory.getLogger(Status.class);

public File document = null;
public Queue<File> pendingDocuments = new ArrayDeque<>();

public SignaturePosition signaturePosition = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ public void run() {
if (!(status.getPreviousState() instanceof PrepareConfigurationState)
&& !(status.getPreviousState() instanceof OpenState)) {
status.bku = config.getDefaultBKU();
status.document = null;

if (status.getPreviousState() instanceof OutputState) {
status.document = status.pendingDocuments.poll();
} else {
status.document = null;
status.pendingDocuments.clear();
}

status.signaturePosition = ((config.getSignatureProfile() == Profile.INVISIBLE) || config.getAutoPositionSignature()) ? (new SignaturePosition()) : null;

/* ensure that files get closed */
Expand All @@ -104,7 +111,15 @@ public void run() {
getStateMachine().display(selection);
selection.layout();

status.document = selection.getSelected();
List<File> selectedFiles = selection.getSelected();
status.document = null;
status.pendingDocuments.clear();
for (File file : selectedFiles) {
if (status.document == null)
status.document = file;
else
status.pendingDocuments.add(file);
}

if (status.document == null) {
// Not selected yet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ private OutputComposite getOutputComposite() {

// Save signed document
this.outputComposite.saveDocument();

if (config.getSkipFinish() && this.outputComposite.getSaveSuccessful()) {
getStateMachine().exit();
}
}

return this.outputComposite;
Expand All @@ -94,6 +90,14 @@ public void run() {

OutputComposite outputComposite = this.getOutputComposite();

if (outputComposite.getSaveSuccessful()) {
if (!getStateMachine().status.pendingDocuments.isEmpty()) {
this.setNextState(new OpenState(getStateMachine()));
return;
}
if (getConfig().getSkipFinish())
getStateMachine().exit();
}
// Display dialog
getStateMachine().display(outputComposite);
}
Expand Down

0 comments on commit 4ed8b71

Please sign in to comment.