Skip to content

Commit

Permalink
Open button should open the output file in case of directory output but
Browse files Browse the repository at this point in the history
single output file (ex a rotate of a single file)
  • Loading branch information
torakiki committed Nov 21, 2017
1 parent 163f57a commit db09b5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pdfsam-fx/src/main/java/org/pdfsam/ui/module/OpenButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public OpenButton(String ownerModule, ModuleInputOutputType outputType) {
setPrefHeight(Double.MAX_VALUE);
setVisible(false);
setOnAction(e -> {
if (destination != null && destination.exists()) {
eventStudio().broadcast(new OpenFileRequest(destination));
if (latestOutput.size() != 1 || !openFile(latestOutput.get(0))) {
openFile(destination);
}

});
eventStudio().add(TaskExecutionRequestEvent.class, e -> {
if (e.getModuleId().equals(ownerModule)) {
Expand All @@ -102,6 +103,14 @@ public OpenButton(String ownerModule, ModuleInputOutputType outputType) {
eventStudio().addAnnotatedListeners(this);
}

private boolean openFile(File file) {
if (file != null && file.exists()) {
eventStudio().broadcast(new OpenFileRequest(file));
return true;
}
return false;
}

public void initModules(Collection<Module> modules) {
modules.forEach(m -> {
if (m.descriptor().hasInputType(outputType)) {
Expand Down
35 changes: 33 additions & 2 deletions pdfsam-fx/src/test/java/org/pdfsam/ui/module/OpenButtonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected Parent getRootNode() {
}

@Test
public void openClick() throws Exception {
public void openFileClick() throws Exception {
File file = temp.newFile();
FileTaskOutput output = new FileTaskOutput(file);
TestListener listener = new TestListener(file);
Expand All @@ -86,6 +86,36 @@ public void openClick() throws Exception {
FXTestUtils.invokeAndWait(() -> victim.dispatch(output), 1);
click(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}

@Test
public void openDirectoryClick() throws Exception {
File dir = temp.newFolder();
DirectoryTaskOutput output = new DirectoryTaskOutput(dir);
TestListener listener = new TestListener(dir);
OpenButton victim = find(".footer-open-button");
eventStudio().add(listener);
FXTestUtils.invokeAndWait(() -> victim.dispatch(output), 1);
click(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}

@Test
public void openSingleFileDirectoryDestinationClick() throws Exception {
File file = temp.newFile();
DirectoryTaskOutput output = new DirectoryTaskOutput(file.getParentFile());
TestListener listener = new TestListener(file);
OpenButton victim = find(".footer-open-button");
eventStudio().add(listener);
NotifiableTaskMetadata taskMetadata = new NotifiableTaskMetadata(mock(Task.class));
taskMetadata.addTaskOutput(file);
eventStudio().broadcast(new TaskExecutionCompletedEvent(12, taskMetadata), "moduleId");
FXTestUtils.invokeAndWait(() -> victim.dispatch(output), 1);
click(victim);
assertTrue(listener.isHit());
assertTrue(listener.equal);
}

@Test
Expand Down Expand Up @@ -140,6 +170,7 @@ public void sendToModuleClick() throws Exception {

private static class TestListener extends HitTestListener<OpenFileRequest> {
private File destination;
boolean equal = false;

private TestListener(File destination) {
this.destination = destination;
Expand All @@ -148,7 +179,7 @@ private TestListener(File destination) {
@Override
public void onEvent(OpenFileRequest event) {
super.onEvent(event);
assertEquals(destination, event.getFile());
equal = destination.equals(event.getFile());
}
}

Expand Down

0 comments on commit db09b5e

Please sign in to comment.