-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Persist GUI app settings between executions (#1169)
Currently, the GUI app does not remember settings (input and output paths, advanced settings) between different executions of the app. This PR adds support for persisting settings between runs of the validator, using the Java Preferences API for storage. Closes #1165.
- Loading branch information
1 parent
3ee0051
commit 75c64a9
Showing
6 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/gui/src/main/java/org/mobilitydata/gtfsvalidator/app/gui/GtfsValidatorPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.mobilitydata.gtfsvalidator.app.gui; | ||
|
||
import java.nio.file.Path; | ||
import java.util.function.Consumer; | ||
import java.util.function.Supplier; | ||
import java.util.prefs.Preferences; | ||
|
||
/** | ||
* Supports loading and saving application preferences from a persistent {@link Preferences} backend | ||
* across application runs. | ||
*/ | ||
public class GtfsValidatorPreferences { | ||
|
||
private static final String KEY_GTFS_SOURCE = "gtfs_source"; | ||
private static final String KEY_OUTPUT_DIRECTORY = "output_directory"; | ||
private static final String KEY_NUM_THREADS = "num_threads"; | ||
private static final String KEY_COUNTRY_CODE = "country_code"; | ||
|
||
private final Preferences prefs; | ||
|
||
public GtfsValidatorPreferences() { | ||
this.prefs = Preferences.userNodeForPackage(GtfsValidatorPreferences.class); | ||
} | ||
|
||
public void loadPreferences(GtfsValidatorApp app) { | ||
loadStringSetting(KEY_GTFS_SOURCE, app::setGtfsSource); | ||
loadPathSetting(KEY_OUTPUT_DIRECTORY, app::setOutputDirectory); | ||
loadIntSetting(KEY_NUM_THREADS, app::setNumThreads); | ||
loadStringSetting(KEY_COUNTRY_CODE, app::setCountryCode); | ||
} | ||
|
||
public void savePreferences(GtfsValidatorApp app) { | ||
saveStringSetting(app::getGtfsSource, KEY_GTFS_SOURCE); | ||
saveStringSetting(app::getOutputDirectory, KEY_OUTPUT_DIRECTORY); | ||
saveIntSetting(app::getNumThreads, KEY_NUM_THREADS); | ||
saveStringSetting(app::getCountryCode, KEY_COUNTRY_CODE); | ||
} | ||
|
||
private void loadStringSetting(String key, Consumer<String> setter) { | ||
String value = prefs.get(key, ""); | ||
if (!value.isBlank()) { | ||
setter.accept(value); | ||
} | ||
} | ||
|
||
private void loadIntSetting(String key, Consumer<Integer> setter) { | ||
loadStringSetting(key, (value) -> setter.accept(Integer.parseInt(value))); | ||
} | ||
|
||
private void loadPathSetting(String key, Consumer<Path> setter) { | ||
loadStringSetting(key, (value) -> setter.accept(Path.of(value))); | ||
} | ||
|
||
private void saveStringSetting(Supplier<String> getter, String key) { | ||
String value = getter.get(); | ||
if (!value.isBlank()) { | ||
prefs.put(key, value); | ||
} | ||
} | ||
|
||
private void saveIntSetting(Supplier<Integer> getter, String key) { | ||
saveStringSetting( | ||
() -> { | ||
Integer value = getter.get(); | ||
if (value == null || value == 0) { | ||
return ""; | ||
} | ||
return value.toString(); | ||
}, | ||
key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ui/src/test/java/org/mobilitydata/gtfsvalidator/app/gui/GtfsValidatorPreferencesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.mobilitydata.gtfsvalidator.app.gui; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.nio.file.Path; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnit; | ||
import org.mockito.junit.MockitoRule; | ||
import org.mockito.quality.Strictness; | ||
|
||
@RunWith(JUnit4.class) | ||
public class GtfsValidatorPreferencesTest { | ||
|
||
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); | ||
|
||
@Mock private MonitoredValidationRunner runner; | ||
@Mock private ValidationDisplay display; | ||
|
||
@Test | ||
public void testEndToEnd() { | ||
{ | ||
GtfsValidatorApp source = new GtfsValidatorApp(runner, display); | ||
source.setGtfsSource("http://gtfs.org/gtfs.zip"); | ||
source.setOutputDirectory(Path.of("/tmp/gtfs")); | ||
source.setNumThreads(3); | ||
source.setCountryCode("CA"); | ||
|
||
GtfsValidatorPreferences prefs = new GtfsValidatorPreferences(); | ||
prefs.savePreferences(source); | ||
} | ||
|
||
{ | ||
GtfsValidatorPreferences prefs = new GtfsValidatorPreferences(); | ||
GtfsValidatorApp dest = new GtfsValidatorApp(runner, display); | ||
prefs.loadPreferences(dest); | ||
|
||
assertThat(dest.getGtfsSource()).isEqualTo("http://gtfs.org/gtfs.zip"); | ||
assertThat(dest.getOutputDirectory()).isEqualTo("/tmp/gtfs"); | ||
assertThat(dest.getNumThreads()).isEqualTo(3); | ||
assertThat(dest.getCountryCode()).isEqualTo("CA"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters