Skip to content

Commit

Permalink
ref #532: output folder field automatically set to the default workin…
Browse files Browse the repository at this point in the history
…g directory value if there is one.
  • Loading branch information
torakiki committed Oct 25, 2023
1 parent a6444c0 commit d00af48
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -67,7 +66,7 @@ public void handle(ActionEvent event) {
var directoryChooser = Choosers.directoryChooser(getBrowseWindowTitle());
String currentSelection = getTextField().getText();
if (isNotBlank(currentSelection)) {
Path path = Paths.get(currentSelection);
var path = Paths.get(currentSelection);
if (Files.exists(path)) {
directoryChooser.setInitialDirectory(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
*/
package org.pdfsam.ui.components.io;

import org.pdfsam.core.context.ApplicationContext;
import org.pdfsam.core.support.params.MultipleOutputTaskParametersBuilder;
import org.pdfsam.core.support.params.TaskParametersBuildStep;
import org.pdfsam.model.ui.NonExistingOutputDirectoryEvent;
import org.pdfsam.ui.components.support.FXValidationSupport;
import org.sejda.model.parameter.base.SingleOrMultipleOutputTaskParameters;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;

import static org.pdfsam.core.context.ApplicationContext.app;
import static org.pdfsam.core.context.StringPersistentProperty.WORKING_PATH;
import static org.pdfsam.core.support.validation.Validators.and;
import static org.pdfsam.core.support.validation.Validators.nonBlank;
import static org.pdfsam.eventstudio.StaticStudio.eventStudio;
Expand All @@ -37,14 +39,18 @@

/**
* A {@link BrowsableDirectoryField} letting the user select a directory as output for a {@link MultipleOutputTaskParametersBuilder}.
*
* @author Andrea Vacondio
*
* @author Andrea Vacondio
*/
public class BrowsableOutputDirectoryField extends BrowsableDirectoryField
implements TaskParametersBuildStep<MultipleOutputTaskParametersBuilder<?>> {

public BrowsableOutputDirectoryField() {
this(app());
}

BrowsableOutputDirectoryField(ApplicationContext context) {
context.persistentSettings().get(WORKING_PATH).ifPresent(getTextField()::setText);
getTextField().setValidator(and(nonBlank(), v -> !Files.isRegularFile(Paths.get(v))));
}

Expand All @@ -53,7 +59,7 @@ public void apply(MultipleOutputTaskParametersBuilder<? extends SingleOrMultiple
Consumer<String> onError) {
getTextField().validate();
if (getTextField().getValidationState() == FXValidationSupport.ValidationState.VALID) {
Path output = Paths.get(getTextField().getText());
var output = Paths.get(getTextField().getText());
if (!Files.exists(output)) {
eventStudio().broadcast(new NonExistingOutputDirectoryEvent(output));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.pdfsam.core.context.ApplicationContext;
import org.pdfsam.core.context.ApplicationPersistentSettings;
import org.pdfsam.core.context.StringPersistentProperty;
import org.pdfsam.core.support.params.MultipleOutputTaskParametersBuilder;
import org.pdfsam.model.ui.NonExistingOutputDirectoryEvent;
import org.pdfsam.test.HitTestListener;
Expand All @@ -32,6 +35,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.equalTo;
Expand All @@ -42,6 +46,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.hamcrest.MockitoHamcrest.argThat;
import static org.pdfsam.eventstudio.StaticStudio.eventStudio;

Expand All @@ -52,16 +57,22 @@
public class BrowsableOutputDirectoryFieldTest {
private MultipleOutputTaskParametersBuilder<? extends MultipleOutputTaskParameters> builder;
private Consumer<String> onError;
private ApplicationPersistentSettings persistentSettings;
private ApplicationContext context;

@BeforeEach
public void setUp() {
onError = mock(Consumer.class);
builder = mock(MultipleOutputTaskParametersBuilder.class);
context = mock(ApplicationContext.class);
persistentSettings = mock(ApplicationPersistentSettings.class);
when(context.persistentSettings()).thenReturn(persistentSettings);
when(persistentSettings.get(StringPersistentProperty.WORKING_PATH)).thenReturn(Optional.empty());
}

@Test
public void valid(@TempDir Path folder) throws IOException {
var victim = new BrowsableOutputDirectoryField();
var victim = new BrowsableOutputDirectoryField(context);
var value = Files.createTempDirectory(folder, null).toFile();
victim.getTextField().setText(value.getAbsolutePath());
victim.apply(builder, onError);
Expand All @@ -71,7 +82,7 @@ public void valid(@TempDir Path folder) throws IOException {

@Test
public void validUTFSpecialChars(@TempDir Path folder) throws IOException {
BrowsableOutputDirectoryField victim = new BrowsableOutputDirectoryField();
var victim = new BrowsableOutputDirectoryField(context);
File value = Files.createTempDirectory(folder, "संसकरण_test").toFile();
victim.getTextField().setText(value.getAbsolutePath());
victim.apply(builder, onError);
Expand All @@ -81,7 +92,7 @@ public void validUTFSpecialChars(@TempDir Path folder) throws IOException {

@Test
public void invalidBlank() {
BrowsableOutputDirectoryField victim = new BrowsableOutputDirectoryField();
var victim = new BrowsableOutputDirectoryField(context);
victim.getTextField().setText(" ");
victim.apply(builder, onError);
verify(builder, never()).output(any());
Expand All @@ -90,7 +101,7 @@ public void invalidBlank() {

@Test
public void invalidFile(@TempDir Path folder) throws IOException {
BrowsableOutputDirectoryField victim = new BrowsableOutputDirectoryField();
var victim = new BrowsableOutputDirectoryField(context);
File value = Files.createTempFile(folder, null, null).toFile();
victim.getTextField().setText(value.getAbsolutePath());
victim.apply(builder, onError);
Expand All @@ -102,11 +113,22 @@ public void invalidFile(@TempDir Path folder) throws IOException {
public void validNonExisting(@TempDir Path folder) throws IOException {
HitTestListener<NonExistingOutputDirectoryEvent> listener = new HitTestListener<>();
eventStudio().add(NonExistingOutputDirectoryEvent.class, listener);
BrowsableOutputDirectoryField victim = new BrowsableOutputDirectoryField();
var victim = new BrowsableOutputDirectoryField(context);
var value = Files.createTempDirectory(folder, null);
victim.getTextField().setText(value.toAbsolutePath().toString());
Files.delete(value);
victim.apply(builder, onError);
assertTrue(listener.isHit());
}

@Test
public void validDefaultWorkspace(@TempDir Path folder) throws IOException {
var value = Files.createTempDirectory(folder, null).toFile();
when(persistentSettings.get(StringPersistentProperty.WORKING_PATH)).thenReturn(
Optional.of(value.getAbsolutePath()));
var victim = new BrowsableOutputDirectoryField(context);
victim.apply(builder, onError);
verify(builder).output(argThat(hasProperty("destination", equalTo(value))));
verify(onError, never()).accept(anyString());
}
}

0 comments on commit d00af48

Please sign in to comment.