summaryrefslogtreecommitdiff
path: root/src/main/java/sevenUnitsGUI/Presenter.java
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-02-23 19:20:30 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2025-02-23 20:36:03 -0500
commit4436b29053a0b757562ecc1d0a78e22902e6e5ae (patch)
tree7d2435198bc3108f00b6f820bc2e51c1597f51b2 /src/main/java/sevenUnitsGUI/Presenter.java
parent1007169658004c78c408f8bd1f4efbbeb6448323 (diff)
Allow default datafile to be disabled
If this option is deselected, the default unit, prefix, dimension and metric exception data will not be loaded, and only custom data and the few units that are not provided by files will be available. The main rationale for this change is so that the data can be localized by custom unit files.
Diffstat (limited to 'src/main/java/sevenUnitsGUI/Presenter.java')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java93
1 files changed, 75 insertions, 18 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 6467f03..ba600e3 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -347,6 +347,19 @@ public final class Presenter {
* unit view in views that show units as a list to choose from.
*/
private boolean showDuplicates = false;
+
+ /**
+ * The default unit, prefix, dimension and exception data will only be loaded
+ * if this variable is true.
+ */
+ private boolean useDefaultDatafiles = true;
+
+ /** Custom unit datafiles that will be loaded by {@link #reloadData} */
+ private final Set<Path> customUnitFiles = new HashSet<>();
+ /** Custom dimension datafiles that will be loaded by {@link #reloadData} */
+ private final Set<Path> customDimensionFiles = new HashSet<>();
+ /** Custom exception datafiles that will be loaded by {@link #reloadData} */
+ private final Set<Path> customExceptionFiles = new HashSet<>();
/**
* Creates a Presenter
@@ -357,8 +370,45 @@ public final class Presenter {
public Presenter(View view) {
this.view = view;
this.database = new UnitDatabase();
- addDefaults(this.database);
+ this.metricExceptions = new HashSet<>();
+
+ this.locales = this.loadLocales();
+ this.userLocale = DEFAULT_LOCALE;
+
+ // set default settings temporarily
+ if (Files.exists(CONFIG_FILE)) {
+ this.loadSettings(CONFIG_FILE);
+ }
+ this.reloadData();
+
+ // print out unit counts
+ System.out.println(this.loadStatMsg());
+ }
+
+ /**
+ * Clears then reloads all unit, prefix, dimension and exception data.
+ */
+ public void reloadData() {
+ this.database.clear();
+ this.metricExceptions.clear();
+ addDefaults(this.database);
+
+ if (this.useDefaultDatafiles) {
+ this.loadDefaultData();
+ }
+
+ this.customUnitFiles.forEach(
+ path -> this.handleLoadErrors(this.database.loadUnitsFile(path)));
+ this.customDimensionFiles.forEach(
+ path -> this.handleLoadErrors(this.database.loadDimensionFile(path)));
+ this.customExceptionFiles.forEach(this::loadExceptionFile);
+ }
+
+ /**
+ * Load units, prefixes and dimensions from the default files.
+ */
+ private void loadDefaultData() {
// load units and prefixes
try (final var units = inputStream(DEFAULT_UNITS_FILEPATH)) {
this.handleLoadErrors(this.database.loadUnitsFromStream(units));
@@ -377,7 +427,6 @@ public final class Presenter {
// load metric exceptions
try {
- this.metricExceptions = new HashSet<>();
try (var exceptions = inputStream(DEFAULT_EXCEPTIONS_FILEPATH);
var scanner = new Scanner(exceptions)) {
while (scanner.hasNextLine()) {
@@ -392,17 +441,6 @@ public final class Presenter {
throw new AssertionError("Loading of metric_exceptions.txt failed.",
e);
}
-
- this.locales = this.loadLocales();
- this.userLocale = DEFAULT_LOCALE;
-
- // set default settings temporarily
- if (Files.exists(CONFIG_FILE)) {
- this.loadSettings(CONFIG_FILE);
- }
-
- // print out unit counts
- System.out.println(this.loadStatMsg());
}
/**
@@ -962,6 +1000,10 @@ public final class Presenter {
* @since 2021-12-15
*/
void loadSettings(Path settingsFile) {
+ this.customDimensionFiles.clear();
+ this.customExceptionFiles.clear();
+ this.customUnitFiles.clear();
+
for (final Map.Entry<String, String> setting : this
.settingsFromFile(settingsFile)) {
final var value = setting.getValue();
@@ -970,15 +1012,13 @@ public final class Presenter {
// set manually to avoid the unnecessary saving of the non-manual
// methods
case "custom_dimension_file":
- this.handleLoadErrors(
- this.database.loadDimensionFile(pathFromConfig(value)));
+ this.customDimensionFiles.add(pathFromConfig(value));
break;
case "custom_exception_file":
- this.loadExceptionFile(pathFromConfig(value));
+ this.customExceptionFiles.add(pathFromConfig(value));
break;
case "custom_unit_file":
- this.handleLoadErrors(
- this.database.loadUnitsFile(pathFromConfig(value)));
+ this.customUnitFiles.add(pathFromConfig(value));
break;
case "number_display_rule":
this.setDisplayRuleFromString(value);
@@ -1204,6 +1244,16 @@ public final class Presenter {
return null;
}
}
+
+ /**
+ * Sets whether or not the default datafiles will be loaded.
+ * This method automatically updates the view's units.
+ */
+ public void setUseDefaultDatafiles(boolean useDefaultDatafiles) {
+ this.useDefaultDatafiles = useDefaultDatafiles;
+ this.reloadData();
+ this.updateView();
+ }
/**
* Sets the user's locale, updating the view.
@@ -1304,6 +1354,13 @@ public final class Presenter {
ucview.setToUnitNames(toNames);
}
}
+
+ /**
+ * @return true iff the default datafiles are being used
+ */
+ public boolean usingDefaultDatafiles() {
+ return this.useDefaultDatafiles;
+ }
/**
* @param message message to add