Skip to content

Commit

Permalink
close #555: Added a default output PDF version setting
Browse files Browse the repository at this point in the history
  • Loading branch information
torakiki committed Oct 18, 2023
1 parent 941eb54 commit 27c55d4
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -153,7 +166,10 @@ public Observable<Optional<Boolean>> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,7 +36,8 @@ public enum StringPersistentProperty implements PersistentProperty<String> {
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<String> defaultSupplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @param <T> the type of the elements in the combo
* @author Andrea Vacondio
*/
public class PreferenceComboBox<T extends ComboItem<String>> extends ComboBox<T> {
public class PreferenceComboBox<T extends ComboItem<?>> extends ComboBox<T> {
private static final Logger LOG = LoggerFactory.getLogger(PreferenceComboBox.class);

PreferenceComboBox(StringPersistentProperty property) {
Expand All @@ -44,7 +44,7 @@ public class PreferenceComboBox<T extends ComboItem<String>> extends ComboBox<T>
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());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -111,6 +115,21 @@ public PreferenceComboBox<ComboItem<String>> fontSizeCombo() {
return fontSizeCombo;
}

@Provides
@Named("pdfVersionCombo")
public PreferenceComboBox<ComboItem<PdfVersion>> pdfVersionCombo() {
PreferenceComboBox<ComboItem<PdfVersion>> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,59 @@

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;
import static org.pdfsam.ui.components.support.Views.helpIcon;

/**
* 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<ComboItem<PdfVersion>> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComboItem<String>>(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<ComboItem<String>>(StringPersistentProperty.STARTUP_MODULE,
appContext);
var themeCombo = new PreferenceComboBox<ComboItem<String>>(StringPersistentProperty.THEME, appContext);
var fontSizeCombo = new PreferenceComboBox<ComboItem<String>>(StringPersistentProperty.FONT_SIZE, appContext);
PreferenceAppearencePane victim = new PreferenceAppearencePane(localeCombo, startupModuleCombo, themeCombo,
fontSizeCombo);
victim.setId("victim");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -51,21 +54,32 @@ public class PreferenceComboBoxTest {
@Start
public void start(Stage stage) {
when(appContext.persistentSettings()).thenReturn(persistentSettings);
PreferenceComboBox<ComboItem<String>> 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()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ComboItem<PdfVersion>>(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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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. ([email protected]).
*
* 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 <http://www.gnu.org/licenses/>.
*/

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<PdfVersion> 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();
}
}
Loading

0 comments on commit 27c55d4

Please sign in to comment.