From 27c55d488396f6ea9f29a0c851959077c57a1d40 Mon Sep 17 00:00:00 2001 From: torakiki Date: Wed, 18 Oct 2023 09:39:33 +0200 Subject: [PATCH] close #555: Added a default output PDF version setting --- .../ApplicationPersistentSettings.java | 18 +++- .../context/StringPersistentProperty.java | 5 +- .../ApplicationPersistentSettingsTest.java | 16 ++++ .../context/StringPersistentPropertyTest.java | 2 + .../preference/PreferenceComboBox.java | 4 +- .../content/preference/PreferenceConfig.java | 19 +++++ .../preference/PreferenceOutputPane.java | 26 ++++-- .../PreferenceAppearencePaneTest.java | 9 +- .../preference/PreferenceComboBoxTest.java | 30 +++++-- .../preference/PreferenceOutputPaneTest.java | 21 +++-- .../model/ui/DefaultPdfVersionComboItem.java | 47 +++++++++++ .../pdfsam/model/ui/PdfVersionComboItem.java | 31 +++++++ .../ui/components/io/PdfDestinationPane.java | 6 +- .../ui/components/io/PdfVersionCombo.java | 83 ++++--------------- .../components/io/PdfDestinationPaneTest.java | 2 +- .../ui/components/io/PdfVersionComboTest.java | 27 +++++- 16 files changed, 243 insertions(+), 103 deletions(-) create mode 100644 pdfsam-model/src/main/java/org/pdfsam/model/ui/DefaultPdfVersionComboItem.java create mode 100644 pdfsam-model/src/main/java/org/pdfsam/model/ui/PdfVersionComboItem.java diff --git a/pdfsam-core/src/main/java/org/pdfsam/core/context/ApplicationPersistentSettings.java b/pdfsam-core/src/main/java/org/pdfsam/core/context/ApplicationPersistentSettings.java index 21c4cc49..a2d2639c 100644 --- a/pdfsam-core/src/main/java/org/pdfsam/core/context/ApplicationPersistentSettings.java +++ b/pdfsam-core/src/main/java/org/pdfsam/core/context/ApplicationPersistentSettings.java @@ -26,8 +26,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Arrays; import java.util.Optional; +import static java.util.Objects.nonNull; import static java.util.Optional.of; import static java.util.Optional.ofNullable; import static org.sejda.commons.util.RequireUtils.requireNotNullArg; @@ -132,6 +134,17 @@ public void set(BooleanPersistentProperty prop, boolean value) { } } + /** + * @param property + * @return true if there is a value stored for the given persistent property + */ + public boolean hasValueFor(PersistentProperty property) { + if (nonNull(property)) { + return Arrays.stream(this.repo.keys()).anyMatch(k -> k.equals(property.key())); + } + return false; + } + /** * @return an observable for changes to the given property */ @@ -153,7 +166,10 @@ public Observable> settingsChanges(BooleanPersistentProperty p return boolSettingsChanges.hide().filter(c -> c.property().equals(prop)).map(PersistentPropertyChange::value); } - void clean() { + /** + * Clears all the persistent settings + */ + public void clean() { try { this.repo.clean(); LOG.info("Persistent application settings deleted"); diff --git a/pdfsam-core/src/main/java/org/pdfsam/core/context/StringPersistentProperty.java b/pdfsam-core/src/main/java/org/pdfsam/core/context/StringPersistentProperty.java index 705d5436..3a554157 100644 --- a/pdfsam-core/src/main/java/org/pdfsam/core/context/StringPersistentProperty.java +++ b/pdfsam-core/src/main/java/org/pdfsam/core/context/StringPersistentProperty.java @@ -18,6 +18,8 @@ */ package org.pdfsam.core.context; +import org.sejda.model.pdf.PdfVersion; + import java.util.function.Supplier; import static org.pdfsam.core.ConfigurableSystemProperty.LOCALE_PROP; @@ -34,7 +36,8 @@ public enum StringPersistentProperty implements PersistentProperty { STARTUP_MODULE(() -> ""), LOCALE(() -> System.getProperty(LOCALE_PROP)), THEME(() -> System.getProperty(THEME_PROP)), - FONT_SIZE(() -> ""); + FONT_SIZE(() -> ""), + PDF_VERSION(PdfVersion.VERSION_1_5::name); private final Supplier defaultSupplier; diff --git a/pdfsam-core/src/test/java/org/pdfsam/core/context/ApplicationPersistentSettingsTest.java b/pdfsam-core/src/test/java/org/pdfsam/core/context/ApplicationPersistentSettingsTest.java index 3e98f582..e2644ed5 100644 --- a/pdfsam-core/src/test/java/org/pdfsam/core/context/ApplicationPersistentSettingsTest.java +++ b/pdfsam-core/src/test/java/org/pdfsam/core/context/ApplicationPersistentSettingsTest.java @@ -234,4 +234,20 @@ public void close() { booleanSettings.assertComplete(); } + @Test + void hasValueFor() { + when(repo.keys()).thenReturn(new String[] {}); + assertFalse(victim.hasValueFor(StringPersistentProperty.LOCALE)); + when(repo.keys()).thenReturn(new String[] { "theme" }); + assertFalse(victim.hasValueFor(StringPersistentProperty.LOCALE)); + when(repo.keys()).thenReturn(new String[] { "theme", "locale" }); + assertTrue(victim.hasValueFor(StringPersistentProperty.LOCALE)); + when(repo.keys()).thenReturn(new String[] { "locale" }); + assertTrue(victim.hasValueFor(StringPersistentProperty.LOCALE)); + } + + @Test + void hasValueForNullProperty() { + assertFalse(victim.hasValueFor(null)); + } } diff --git a/pdfsam-core/src/test/java/org/pdfsam/core/context/StringPersistentPropertyTest.java b/pdfsam-core/src/test/java/org/pdfsam/core/context/StringPersistentPropertyTest.java index c227b0ff..77645a31 100644 --- a/pdfsam-core/src/test/java/org/pdfsam/core/context/StringPersistentPropertyTest.java +++ b/pdfsam-core/src/test/java/org/pdfsam/core/context/StringPersistentPropertyTest.java @@ -23,6 +23,7 @@ import org.junitpioneer.jupiter.ClearSystemProperty; import org.junitpioneer.jupiter.SetSystemProperty; import org.pdfsam.core.ConfigurableSystemProperty; +import org.sejda.model.pdf.PdfVersion; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -56,5 +57,6 @@ public void defaultValue() { assertEquals("", StringPersistentProperty.WORKING_PATH.defaultSupplier().get()); assertEquals("", StringPersistentProperty.WORKSPACE_PATH.defaultSupplier().get()); assertEquals("", StringPersistentProperty.STARTUP_MODULE.defaultSupplier().get()); + assertEquals(PdfVersion.VERSION_1_5.name(), StringPersistentProperty.PDF_VERSION.defaultSupplier().get()); } } diff --git a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceComboBox.java b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceComboBox.java index b0057036..1be08e45 100644 --- a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceComboBox.java +++ b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceComboBox.java @@ -34,7 +34,7 @@ * @param the type of the elements in the combo * @author Andrea Vacondio */ -public class PreferenceComboBox> extends ComboBox { +public class PreferenceComboBox> extends ComboBox { private static final Logger LOG = LoggerFactory.getLogger(PreferenceComboBox.class); PreferenceComboBox(StringPersistentProperty property) { @@ -44,7 +44,7 @@ public class PreferenceComboBox> extends ComboBox PreferenceComboBox(StringPersistentProperty property, ApplicationContext context) { requireNotNullArg(property, "Preference cannot be null"); valueProperty().addListener((observable, oldValue, newValue) -> { - context.persistentSettings().set(property, newValue.key()); + context.persistentSettings().set(property, newValue.key().toString()); LOG.trace("Preference {} set to {}", property, newValue.key()); }); } diff --git a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceConfig.java b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceConfig.java index 1d23eaa5..d3543c0b 100644 --- a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceConfig.java +++ b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceConfig.java @@ -27,9 +27,12 @@ import org.pdfsam.model.io.FileType; import org.pdfsam.model.io.OpenType; import org.pdfsam.model.ui.ComboItem; +import org.pdfsam.model.ui.DefaultPdfVersionComboItem; import org.pdfsam.ui.components.support.FXValidationSupport; import org.pdfsam.ui.components.support.Style; +import org.sejda.model.pdf.PdfVersion; +import java.util.Arrays; import java.util.Comparator; import java.util.stream.IntStream; @@ -49,6 +52,7 @@ import static org.pdfsam.core.context.BooleanPersistentProperty.SMART_OUTPUT; import static org.pdfsam.core.context.IntegerPersistentProperty.LOGVIEW_ROWS_NUMBER; import static org.pdfsam.core.context.StringPersistentProperty.FONT_SIZE; +import static org.pdfsam.core.context.StringPersistentProperty.PDF_VERSION; import static org.pdfsam.core.context.StringPersistentProperty.STARTUP_MODULE; import static org.pdfsam.core.context.StringPersistentProperty.THEME; import static org.pdfsam.core.context.StringPersistentProperty.WORKING_PATH; @@ -111,6 +115,21 @@ public PreferenceComboBox> fontSizeCombo() { return fontSizeCombo; } + @Provides + @Named("pdfVersionCombo") + public PreferenceComboBox> pdfVersionCombo() { + PreferenceComboBox> pdfVersionCombo = new PreferenceComboBox<>(PDF_VERSION); + pdfVersionCombo.setId("pdfVersionCombo"); + pdfVersionCombo.getItems().addAll(Arrays.stream(PdfVersion.values()) + .filter(v -> v.getVersion() > PdfVersion.VERSION_1_2.getVersion()).map(DefaultPdfVersionComboItem::new) + .toList()); + //select if present in the settings + app().persistentSettings().get(StringPersistentProperty.PDF_VERSION).map(PdfVersion::valueOf) + .flatMap(v -> pdfVersionCombo.getItems().stream().filter(i -> i.key() == v).findFirst()) + .ifPresent(i -> pdfVersionCombo.getSelectionModel().select(i)); + return pdfVersionCombo; + } + @Provides @Named("checkForUpdates") public PreferenceCheckBox checkForUpdates() { diff --git a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPane.java b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPane.java index a18c71ac..063cb2dd 100644 --- a/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPane.java +++ b/pdfsam-gui/src/main/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPane.java @@ -20,10 +20,13 @@ import jakarta.inject.Inject; import jakarta.inject.Named; +import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; -import javafx.scene.layout.VBox; +import javafx.scene.layout.GridPane; +import org.pdfsam.model.ui.ComboItem; import org.pdfsam.ui.components.support.Style; +import org.sejda.model.pdf.PdfVersion; import static java.util.Objects.isNull; import static org.pdfsam.i18n.I18nContext.i18n; @@ -31,34 +34,45 @@ /** * Preference pane displaying the output section - * - * @author Andrea Vacondio * + * @author Andrea Vacondio */ -class PreferenceOutputPane extends VBox { +class PreferenceOutputPane extends GridPane { @Inject public PreferenceOutputPane(@Named("smartRadio") PreferenceRadioButton smartRadio, @Named("compressionEnabled") PreferenceCheckBox compressionEnabled, - @Named("overwriteOutput") PreferenceCheckBox overwriteOutput) { + @Named("overwriteOutput") PreferenceCheckBox overwriteOutput, + @Named("pdfVersionCombo") PreferenceComboBox> pdfVersionCombo) { + + add(new Label(i18n().tr("Default PDF version:")), 0, 1); + setFillWidth(pdfVersionCombo, true); + pdfVersionCombo.setMaxWidth(Double.POSITIVE_INFINITY); + add(pdfVersionCombo, 1, 1); + add(helpIcon(i18n().tr("Default PDF version for generated PDF files")), 2, 1); + ToggleGroup group = new ToggleGroup(); RadioButton manualRadio = new RadioButton(i18n().tr("Manually selected")); manualRadio.setToggleGroup(group); manualRadio.getStyleClass().addAll(Style.VITEM.css()); manualRadio.setId("manualRadio"); + add(manualRadio, 0, 2, 3, 1); smartRadio.getStyleClass().addAll(Style.VITEM.css()); smartRadio.setToggleGroup(group); smartRadio.setGraphic(helpIcon( i18n().tr("Automatically set the destination directory to the selected PDF document directory"))); smartRadio.getStyleClass().addAll(Style.WITH_HELP.css()); + add(smartRadio, 0, 3, 3, 1); if (isNull(group.getSelectedToggle())) { group.selectToggle(manualRadio); } - getChildren().addAll(manualRadio, smartRadio, compressionEnabled, overwriteOutput); + add(compressionEnabled, 0, 4, 3, 1); + add(overwriteOutput, 0, 5, 3, 1); getStyleClass().addAll(Style.CONTAINER.css()); + getStyleClass().addAll(Style.GRID.css()); } } diff --git a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceAppearencePaneTest.java b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceAppearencePaneTest.java index 11530fb8..19a1810c 100644 --- a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceAppearencePaneTest.java +++ b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceAppearencePaneTest.java @@ -59,11 +59,12 @@ public class PreferenceAppearencePaneTest { public void start(Stage stage) { Locale.setDefault(Locale.ENGLISH); when(appContext.persistentSettings()).thenReturn(persistentSettings); - var localeCombo = new PreferenceComboBox<>(StringPersistentProperty.LOCALE, appContext); + var localeCombo = new PreferenceComboBox>(StringPersistentProperty.LOCALE, appContext); localeCombo.setId("localeCombo"); - var startupModuleCombo = new PreferenceComboBox<>(StringPersistentProperty.STARTUP_MODULE, appContext); - var themeCombo = new PreferenceComboBox<>(StringPersistentProperty.THEME, appContext); - var fontSizeCombo = new PreferenceComboBox<>(StringPersistentProperty.FONT_SIZE, appContext); + var startupModuleCombo = new PreferenceComboBox>(StringPersistentProperty.STARTUP_MODULE, + appContext); + var themeCombo = new PreferenceComboBox>(StringPersistentProperty.THEME, appContext); + var fontSizeCombo = new PreferenceComboBox>(StringPersistentProperty.FONT_SIZE, appContext); PreferenceAppearencePane victim = new PreferenceAppearencePane(localeCombo, startupModuleCombo, themeCombo, fontSizeCombo); victim.setId("victim"); diff --git a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceComboBoxTest.java b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceComboBoxTest.java index 6172858b..dff53588 100644 --- a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceComboBoxTest.java +++ b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceComboBoxTest.java @@ -19,7 +19,7 @@ package org.pdfsam.gui.components.content.preference; import javafx.scene.Scene; -import javafx.scene.layout.HBox; +import javafx.scene.layout.VBox; import javafx.stage.Stage; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -28,6 +28,8 @@ import org.pdfsam.core.context.ApplicationPersistentSettings; import org.pdfsam.core.context.StringPersistentProperty; import org.pdfsam.model.ui.ComboItem; +import org.pdfsam.model.ui.DefaultPdfVersionComboItem; +import org.sejda.model.pdf.PdfVersion; import org.testfx.api.FxRobot; import org.testfx.framework.junit5.ApplicationExtension; import org.testfx.framework.junit5.Start; @@ -36,6 +38,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.pdfsam.i18n.I18nContext.i18n; /** * @author Andrea Vacondio @@ -51,21 +54,32 @@ public class PreferenceComboBoxTest { @Start public void start(Stage stage) { when(appContext.persistentSettings()).thenReturn(persistentSettings); - PreferenceComboBox> victim = new PreferenceComboBox<>(StringPersistentProperty.LOCALE, - appContext); - victim.setId("victim"); - victim.getItems().addAll(new ComboItem<>("key1", "value1"), new ComboItem<>("key2", "value2"), + var first = new PreferenceComboBox<>(StringPersistentProperty.LOCALE, appContext); + first.setId("first"); + first.getItems().addAll(new ComboItem<>("key1", "value1"), new ComboItem<>("key2", "value2"), new ComboItem<>("key3", "value3")); - Scene scene = new Scene(new HBox(victim)); + var second = new PreferenceComboBox<>(StringPersistentProperty.PDF_VERSION, appContext); + second.setId("second"); + second.getItems().addAll(new DefaultPdfVersionComboItem(PdfVersion.VERSION_1_3), + new DefaultPdfVersionComboItem(PdfVersion.VERSION_1_4), + new DefaultPdfVersionComboItem(PdfVersion.VERSION_1_5)); + Scene scene = new Scene(new VBox(first, second)); stage.setScene(scene); stage.show(); } @Test @Tag("NoHeadless") - public void preferenceSetOnClick() { - robot.clickOn("#victim").clickOn("value2"); + public void stringPreferenceSetOnClick() { + robot.clickOn("#first").clickOn("value2"); verify(persistentSettings).set(eq(StringPersistentProperty.LOCALE), eq("key2")); } + @Test + @Tag("NoHeadless") + public void preferenceSetOnClick() { + robot.clickOn("#second").clickOn(i18n().tr("Version {0}", PdfVersion.VERSION_1_4.getVersionString())); + verify(persistentSettings).set(eq(StringPersistentProperty.PDF_VERSION), eq(PdfVersion.VERSION_1_4.toString())); + } + } diff --git a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPaneTest.java b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPaneTest.java index 88dbb80a..3ab6b8a1 100644 --- a/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPaneTest.java +++ b/pdfsam-gui/src/test/java/org/pdfsam/gui/components/content/preference/PreferenceOutputPaneTest.java @@ -27,6 +27,9 @@ import org.pdfsam.core.context.ApplicationContext; import org.pdfsam.core.context.ApplicationPersistentSettings; import org.pdfsam.core.context.BooleanPersistentProperty; +import org.pdfsam.core.context.StringPersistentProperty; +import org.pdfsam.model.ui.ComboItem; +import org.sejda.model.pdf.PdfVersion; import org.testfx.api.FxRobot; import org.testfx.framework.junit5.ApplicationExtension; import org.testfx.framework.junit5.Start; @@ -49,16 +52,20 @@ public class PreferenceOutputPaneTest { @Start public void start(Stage stage) { when(appContext.persistentSettings()).thenReturn(persistentSettings); - PreferenceRadioButton smartRadio = new PreferenceRadioButton(BooleanPersistentProperty.SMART_OUTPUT, "radio", - false, appContext); + var smartRadio = new PreferenceRadioButton(BooleanPersistentProperty.SMART_OUTPUT, "radio", false, appContext); smartRadio.setId("smartRadio"); - PreferenceCheckBox compressionEnabled = new PreferenceCheckBox( - BooleanPersistentProperty.PDF_COMPRESSION_ENABLED, "compression", true, appContext); + var compressionEnabled = new PreferenceCheckBox(BooleanPersistentProperty.PDF_COMPRESSION_ENABLED, + "compression", true, appContext); compressionEnabled.setId("compressionEnabled"); - PreferenceCheckBox overwriteOutput = new PreferenceCheckBox(BooleanPersistentProperty.OVERWRITE_OUTPUT, - "overwrite", false, appContext); + var overwriteOutput = new PreferenceCheckBox(BooleanPersistentProperty.OVERWRITE_OUTPUT, "overwrite", false, + appContext); overwriteOutput.setId("overwriteOutput"); - PreferenceOutputPane victim = new PreferenceOutputPane(smartRadio, compressionEnabled, overwriteOutput); + var pdfVersionCombo = new PreferenceComboBox>(StringPersistentProperty.PDF_VERSION, + appContext); + pdfVersionCombo.setId("pdfVersionCombo"); + + PreferenceOutputPane victim = new PreferenceOutputPane(smartRadio, compressionEnabled, overwriteOutput, + pdfVersionCombo); victim.setId("victim"); Scene scene = new Scene(new HBox(victim)); stage.setScene(scene); diff --git a/pdfsam-model/src/main/java/org/pdfsam/model/ui/DefaultPdfVersionComboItem.java b/pdfsam-model/src/main/java/org/pdfsam/model/ui/DefaultPdfVersionComboItem.java new file mode 100644 index 00000000..5b2e388c --- /dev/null +++ b/pdfsam-model/src/main/java/org/pdfsam/model/ui/DefaultPdfVersionComboItem.java @@ -0,0 +1,47 @@ +package org.pdfsam.model.ui; +/* + * This file is part of the PDF Split And Merge source code + * Created on 17/10/23 + * Copyright 2023 by Sober Lemur S.r.l. (info@soberlemur.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import org.sejda.model.pdf.PdfVersion; + +import static org.pdfsam.i18n.I18nContext.i18n; +import static org.sejda.commons.util.RequireUtils.requireNotNullArg; + +/** + * Default implementation for a combo item representing a {@link PdfVersion} + * + * @author Andrea Vacondio + */ +public class DefaultPdfVersionComboItem extends ComboItem implements PdfVersionComboItem { + + public DefaultPdfVersionComboItem(PdfVersion version) { + super(version, i18n().tr("Version {0}", version.getVersionString())); + requireNotNullArg(version, "PDF version cannot be null"); + } + + @Override + public PdfVersion getVersion() { + return this.key(); + } + + @Override + public boolean isHigherOrEqual(PdfVersion version) { + return this.key().getVersion() >= version.getVersion(); + } +} diff --git a/pdfsam-model/src/main/java/org/pdfsam/model/ui/PdfVersionComboItem.java b/pdfsam-model/src/main/java/org/pdfsam/model/ui/PdfVersionComboItem.java new file mode 100644 index 00000000..5465495c --- /dev/null +++ b/pdfsam-model/src/main/java/org/pdfsam/model/ui/PdfVersionComboItem.java @@ -0,0 +1,31 @@ +package org.pdfsam.model.ui; +/* + * This file is part of the PDF Split And Merge source code + * Created on 16/10/23 + * Copyright 2023 by Sober Lemur S.r.l. (info@soberlemur.com). + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import org.sejda.model.pdf.PdfVersion; + +/** + * @author Andrea Vacondio + */ +public interface PdfVersionComboItem { + + PdfVersion getVersion(); + + boolean isHigherOrEqual(PdfVersion version); +} diff --git a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfDestinationPane.java b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfDestinationPane.java index 6abc6da3..42c8acb1 100644 --- a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfDestinationPane.java +++ b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfDestinationPane.java @@ -23,13 +23,13 @@ import javafx.scene.control.TitledPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import org.apache.commons.lang3.StringUtils; import org.pdfsam.core.context.BooleanPersistentProperty; import org.pdfsam.core.support.params.AbstractPdfOutputParametersBuilder; import org.pdfsam.core.support.params.TaskParametersBuildStep; import org.pdfsam.eventstudio.annotation.EventListener; import org.pdfsam.eventstudio.annotation.EventStation; import org.pdfsam.model.tool.ToolBound; +import org.pdfsam.model.ui.DefaultPdfVersionComboItem; import org.pdfsam.model.ui.ResettableView; import org.pdfsam.model.ui.SetDestinationRequest; import org.pdfsam.model.ui.workspace.RestorableView; @@ -64,7 +64,7 @@ public class PdfDestinationPane extends DestinationPane implements ToolBound, Re private final PdfVersionCombo version; private final PdfVersionConstrainedCheckBox compress; private Optional discardBookmarks = empty(); - private String toolBinding = StringUtils.EMPTY; + private final String toolBinding; public PdfDestinationPane(BrowsableField destination, String toolBinding, DestinationPanelFields... optionalFields) { @@ -168,7 +168,7 @@ public void restoreStateFrom(Map data) { compress.setSelected(Boolean.parseBoolean(data.get("compress"))); overwrite().setSelected(Boolean.parseBoolean(data.get("overwrite"))); discardBookmarks.ifPresent(d -> d.setSelected(Boolean.parseBoolean(data.get("discardBookmarks")))); - ofNullable(data.get("version")).map(PdfVersion::valueOf).map(PdfVersionCombo.DefaultPdfVersionComboItem::new) + ofNullable(data.get("version")).map(PdfVersion::valueOf).map(DefaultPdfVersionComboItem::new) .ifPresent(v -> this.version.getSelectionModel().select(v)); } diff --git a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfVersionCombo.java b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfVersionCombo.java index 64a43540..6c42ed03 100644 --- a/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfVersionCombo.java +++ b/pdfsam-ui-components/src/main/java/org/pdfsam/ui/components/io/PdfVersionCombo.java @@ -21,26 +21,25 @@ import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.control.ComboBox; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.builder.EqualsBuilder; -import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.pdfsam.core.context.StringPersistentProperty; import org.pdfsam.eventstudio.annotation.EventListener; import org.pdfsam.eventstudio.annotation.EventStation; import org.pdfsam.model.tool.ToolBound; import org.pdfsam.model.ui.AddPdfVersionConstraintEvent; import org.pdfsam.model.ui.ChangedSelectedPdfVersionEvent; +import org.pdfsam.model.ui.DefaultPdfVersionComboItem; +import org.pdfsam.model.ui.PdfVersionComboItem; import org.pdfsam.model.ui.RemovePdfVersionConstraintEvent; import org.pdfsam.model.ui.ResettableView; -import org.pdfsam.ui.components.io.PdfVersionCombo.PdfVersionComboItem; import org.sejda.model.pdf.PdfVersion; import java.util.Arrays; import static java.util.Objects.nonNull; import static java.util.Optional.ofNullable; +import static org.pdfsam.core.context.ApplicationContext.app; import static org.pdfsam.eventstudio.StaticStudio.eventStudio; import static org.pdfsam.i18n.I18nContext.i18n; -import static org.sejda.commons.util.RequireUtils.requireNotNullArg; /** * Combo box to let the user select the pdf version of the generated output documents @@ -49,7 +48,7 @@ */ class PdfVersionCombo extends ComboBox implements ToolBound, ResettableView { - private String toolBinding = StringUtils.EMPTY; + private final String toolBinding; private final ObservableList unfilteredItems = FXCollections.observableArrayList(); private final PdfVersionFilter versionsFilter = new PdfVersionFilter(); private final SameAsSourceComboItem sameAsSource = new SameAsSourceComboItem(); @@ -69,9 +68,9 @@ private void setFilteredItems(PdfVersion required) { if (nonNull(required)) { PdfVersionComboItem selected = getSelectionModel().getSelectedItem(); setItems(unfilteredItems.filtered(t -> t.isHigherOrEqual(required))); - int selecedIndex = getItems().indexOf(selected); - if (selecedIndex != -1) { - getSelectionModel().select(selecedIndex); + int selectedIndex = getItems().indexOf(selected); + if (selectedIndex != -1) { + getSelectionModel().select(selectedIndex); } else { getSelectionModel().selectFirst(); } @@ -104,76 +103,22 @@ public String toolBinding() { public void resetView() { versionsFilter.reset(); versionsFilter.addFilter(PdfVersion.VERSION_1_2); + app().persistentSettings().get(StringPersistentProperty.PDF_VERSION).map(PdfVersion::valueOf) + .flatMap(v -> getItems().stream().filter(i -> i.getVersion() == v).findFirst()) + .ifPresent(i -> getSelectionModel().select(i)); } public void enableSameAsSourceItem() { unfilteredItems.add(0, sameAsSource); - getSelectionModel().selectFirst(); - } - - /** - * Item for a {@link PdfVersionCombo} - * - * @author Andrea Vacondio - */ - public interface PdfVersionComboItem { - PdfVersion getVersion(); - - boolean isHigherOrEqual(PdfVersion version); - } - - /** - * Default implementations for items to be used as a model for the {@link PdfVersionCombo} - * - * @author Andrea Vacondio - * - */ - static class DefaultPdfVersionComboItem implements PdfVersionComboItem { - - private final PdfVersion version; - - public DefaultPdfVersionComboItem(PdfVersion version) { - requireNotNullArg(version, "PDF version cannot be null"); - this.version = version; - } - - @Override - public PdfVersion getVersion() { - return this.version; - } - - @Override - public boolean isHigherOrEqual(PdfVersion version) { - return this.version.getVersion() >= version.getVersion(); - } - - @Override - public String toString() { - return i18n().tr("Version {0}", version.getVersionString()); - } - - @Override - public int hashCode() { - return new HashCodeBuilder().append(version).toHashCode(); - } - - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - if (!(other instanceof DefaultPdfVersionComboItem otherItem)) { - return false; - } - return new EqualsBuilder().append(version, otherItem.version).isEquals(); + if (!app().persistentSettings().hasValueFor(StringPersistentProperty.PDF_VERSION)) { + getSelectionModel().selectFirst(); } } /** * Combo item to let the user select the same PDF version of the selected input document - * - * @author Andrea Vacondio * + * @author Andrea Vacondio */ private static class SameAsSourceComboItem implements PdfVersionComboItem { diff --git a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfDestinationPaneTest.java b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfDestinationPaneTest.java index cdffd4df..5fe9d591 100644 --- a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfDestinationPaneTest.java +++ b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfDestinationPaneTest.java @@ -25,10 +25,10 @@ import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; import org.pdfsam.core.context.BooleanPersistentProperty; +import org.pdfsam.model.ui.PdfVersionComboItem; import org.pdfsam.model.ui.SetDestinationRequest; import org.pdfsam.test.ClearEventStudioExtension; import org.pdfsam.test.JavaFxThreadInitializeExtension; -import org.pdfsam.ui.components.io.PdfVersionCombo.PdfVersionComboItem; import org.sejda.model.pdf.PdfVersion; import java.io.File; diff --git a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfVersionComboTest.java b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfVersionComboTest.java index d2cd5aed..dee13115 100644 --- a/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfVersionComboTest.java +++ b/pdfsam-ui-components/src/test/java/org/pdfsam/ui/components/io/PdfVersionComboTest.java @@ -22,17 +22,19 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.pdfsam.core.context.StringPersistentProperty; import org.pdfsam.model.ui.AddPdfVersionConstraintEvent; import org.pdfsam.model.ui.ChangedSelectedPdfVersionEvent; +import org.pdfsam.model.ui.PdfVersionComboItem; import org.pdfsam.model.ui.RemovePdfVersionConstraintEvent; import org.pdfsam.test.ClearEventStudioExtension; import org.pdfsam.test.JavaFxThreadExtension; -import org.pdfsam.ui.components.io.PdfVersionCombo.PdfVersionComboItem; import org.sejda.model.pdf.PdfVersion; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.pdfsam.core.context.ApplicationContext.app; import static org.pdfsam.eventstudio.StaticStudio.eventStudio; /** @@ -49,6 +51,7 @@ public class PdfVersionComboTest { @BeforeEach public void setUp() { + app().persistentSettings().set(StringPersistentProperty.PDF_VERSION, PdfVersion.VERSION_1_3.name()); victim = new PdfVersionCombo(MODULE); } @@ -107,7 +110,29 @@ public void reset() { assertEquals(4, victim.getItems().size()); victim.resetView(); assertEquals(6, victim.getItems().size()); + } + + @Test + public void resetWithDefaultVersion() { + assertEquals(PdfVersion.VERSION_1_3, victim.getSelectionModel().getSelectedItem().getVersion()); + app().persistentSettings().set(StringPersistentProperty.PDF_VERSION, PdfVersion.VERSION_1_7.name()); + victim.resetView(); + assertEquals(PdfVersion.VERSION_1_7, victim.getSelectionModel().getSelectedItem().getVersion()); + } + + @Test + public void sameAsSourceVersionIsSelectedIfNoDefaultPdfVersion() { + app().persistentSettings().clean(); + victim.enableSameAsSourceItem(); + assertEquals(victim.getItems().getFirst(), victim.getSelectionModel().getSelectedItem()); + } + @Test + public void sameAsSourceVersionIsNotSelectedIfDefaultPdfVersion() { + app().persistentSettings().set(StringPersistentProperty.PDF_VERSION, PdfVersion.VERSION_1_7.name()); + victim.resetView(); + victim.enableSameAsSourceItem(); + assertEquals(PdfVersion.VERSION_1_7, victim.getSelectionModel().getSelectedItem().getVersion()); } private boolean comboHasItem(PdfVersionCombo combo, PdfVersion version) {