diff --git a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableDirectoryField.java b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableDirectoryField.java index 604af533..66c3892c 100644 --- a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableDirectoryField.java +++ b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableDirectoryField.java @@ -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; @@ -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); } diff --git a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryField.java b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryField.java index 70155848..517fb547 100644 --- a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryField.java +++ b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryField.java @@ -18,6 +18,7 @@ */ 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; @@ -25,10 +26,11 @@ 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; @@ -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> { 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)))); } @@ -53,7 +59,7 @@ public void apply(MultipleOutputTaskParametersBuilder 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)); } diff --git a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryFieldTest.java b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryFieldTest.java index 93300bdd..77595a01 100644 --- a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryFieldTest.java +++ b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/BrowsableOutputDirectoryFieldTest.java @@ -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; @@ -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; @@ -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; @@ -52,16 +57,22 @@ public class BrowsableOutputDirectoryFieldTest { private MultipleOutputTaskParametersBuilder builder; private Consumer 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); @@ -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); @@ -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()); @@ -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); @@ -102,11 +113,22 @@ public void invalidFile(@TempDir Path folder) throws IOException { public void validNonExisting(@TempDir Path folder) throws IOException { HitTestListener 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()); + } }