summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java93
-rw-r--r--src/main/java/sevenUnitsGUI/TabbedView.java18
-rw-r--r--src/main/resources/locales/en.txt1
-rw-r--r--src/main/resources/locales/fr.txt1
4 files changed, 92 insertions, 21 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
diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java
index ca9f23c..40ed0a7 100644
--- a/src/main/java/sevenUnitsGUI/TabbedView.java
+++ b/src/main/java/sevenUnitsGUI/TabbedView.java
@@ -662,10 +662,22 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
miscPanel.add(showAllVariations, new GridBagBuilder(0, 1, 2, 1)
.setAnchor(GridBagConstraints.LINE_START).build());
+ final JCheckBox useDefaultFiles = new JCheckBox();
+ this.localizedTextSetters.put("tv.settings.use_default_files",
+ useDefaultFiles::setText);
+ useDefaultFiles.setSelected(this.presenter.usingDefaultDatafiles());
+ useDefaultFiles.addItemListener(e -> {
+ this.presenter
+ .setUseDefaultDatafiles(e.getStateChange() == ItemEvent.SELECTED);
+ this.presenter.saveSettings();
+ });
+ miscPanel.add(useDefaultFiles, new GridBagBuilder(0, 2, 2, 1)
+ .setAnchor(GridBagConstraints.LINE_START).build());
+
final JLabel localeLabel = new JLabel();
this.localizedTextSetters.put("tv.settings.locale",
localeLabel::setText);
- miscPanel.add(localeLabel, new GridBagBuilder(0, 2, 1, 1)
+ miscPanel.add(localeLabel, new GridBagBuilder(0, 3, 1, 1)
.setAnchor(GridBagConstraints.LINE_START).build());
this.presenter.getAvailableLocales().stream().sorted()
@@ -675,14 +687,14 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
this.presenter.setUserLocale((String) e.getItem());
this.presenter.saveSettings();
});
- miscPanel.add(localeSelector, new GridBagBuilder(1, 2, 1, 1)
+ miscPanel.add(localeSelector, new GridBagBuilder(1, 3, 1, 1)
.setAnchor(GridBagConstraints.LINE_END).build());
final JButton unitFileButton = new JButton();
this.localizedTextSetters.put("tv.settings.unitfiles.button",
unitFileButton::setText);
unitFileButton.setEnabled(false);
- miscPanel.add(unitFileButton, new GridBagBuilder(0, 3, 2, 1)
+ miscPanel.add(unitFileButton, new GridBagBuilder(0, 4, 2, 1)
.setAnchor(GridBagConstraints.LINE_START).build());
}
diff --git a/src/main/resources/locales/en.txt b/src/main/resources/locales/en.txt
index 19ed781..5173cf3 100644
--- a/src/main/resources/locales/en.txt
+++ b/src/main/resources/locales/en.txt
@@ -25,5 +25,6 @@ tv.settings.search.common_prefixes=Include Common Prefixes
tv.settings.search.all_prefixes=Include All Single Prefixes
tv.settings.oneway=Convert One Way Only
tv.settings.show_duplicate=Show Duplicate Units & Prefixes
+tv.settings.use_default_files=Use Default Datafiles
tv.settings.locale=🌐 Locale:
tv.settings.unitfiles.button=Manage Unit Data Files
diff --git a/src/main/resources/locales/fr.txt b/src/main/resources/locales/fr.txt
index d25e2b0..e8b7138 100644
--- a/src/main/resources/locales/fr.txt
+++ b/src/main/resources/locales/fr.txt
@@ -25,5 +25,6 @@ tv.settings.search.common_prefixes=Inclure préfixes fréquents
tv.settings.search.all_prefixes=Inclure tous préfixes seuls
tv.settings.oneway=Convertir Seulement en un Direction
tv.settings.show_duplicate=Montrer unités et préfixes doubles
+tv.settings.use_default_files=Utilise donées par défaut
tv.settings.locale=🌐 Locale:
tv.settings.unitfiles.button=GĂ©rer donĂ©es d’unitĂ©s