summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--src/org/unitConverter/converterGUI/UnitConverterGUI.java139
2 files changed, 138 insertions, 4 deletions
diff --git a/.gitignore b/.gitignore
index 52a523a..1d7e13f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
bin/
target/
*.class
-*~ \ No newline at end of file
+*~
+settings.txt \ No newline at end of file
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
index 82ba048..b8b8894 100644
--- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java
+++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java
@@ -22,12 +22,15 @@ import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
+import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
@@ -87,6 +90,9 @@ final class UnitConverterGUI {
}
private static class Presenter {
+ /** The default place where settings are stored. */
+ private static final String DEFAULT_SETTINGS_FILEPATH = "settings.txt";
+
/**
* Adds default units and dimensions to a database.
*
@@ -136,6 +142,12 @@ final class UnitConverterGUI {
private final Comparator<String> prefixNameComparator;
+ /** A boolean remembering whether or not one-way conversion is on */
+ private boolean oneWay = true;
+
+ /** The prefix rule */
+ private DefaultPrefixRepetitionRule prefixRule = null;
+
// conditions for existence of From and To entries
// used for one-way conversion
private final MutablePredicate<String> fromExistenceCondition = new MutablePredicate<>(
@@ -193,6 +205,9 @@ final class UnitConverterGUI {
e);
}
+ // load settings - requires database to exist
+ this.loadSettings();
+
// a comparator that can be used to compare prefix names
// any name that does not exist is less than a name that does.
// otherwise, they are compared by value
@@ -455,6 +470,72 @@ final class UnitConverterGUI {
}
/**
+ * @return The file where settings are stored;
+ * @since 2020-12-11
+ */
+ private final Path getSettingsFile() {
+ return Path.of(DEFAULT_SETTINGS_FILEPATH);
+ }
+
+ /**
+ * Loads settings from the settings file.
+ *
+ * @since 2021-02-17
+ */
+ public final void loadSettings() {
+ try (BufferedReader reader = Files
+ .newBufferedReader(this.getSettingsFile())) {
+
+ // read file line by line
+ final int lineNum = 0;
+ String line;
+ while ((line = reader.readLine()) != null) {
+ final int equalsIndex = line.indexOf('=');
+ if (equalsIndex == -1)
+ throw new IllegalStateException(
+ "Settings file is malformed at line " + lineNum);
+
+ final String param = line.substring(0, equalsIndex);
+ final String value = line.substring(equalsIndex + 1);
+
+ switch (param) {
+ // set manually to avoid the unnecessary saving of the non-manual
+ // methods
+ case "precision":
+ this.precision = Integer.valueOf(value);
+ break;
+ case "rounding_type":
+ this.roundingType = RoundingType.valueOf(value);
+ break;
+ case "prefix_rule":
+ this.prefixRule = DefaultPrefixRepetitionRule.valueOf(value);
+ this.database.setPrefixRepetitionRule(this.prefixRule);
+ break;
+ case "one_way":
+ this.oneWay = Boolean.valueOf(value);
+ if (this.oneWay) {
+ this.fromExistenceCondition.setPredicate(
+ unitName -> this.metricExceptions.contains(unitName)
+ || !this.database.getUnit(unitName)
+ .isMetric());
+ this.toExistenceCondition.setPredicate(
+ unitName -> this.metricExceptions.contains(unitName)
+ || this.database.getUnit(unitName).isMetric());
+ } else {
+ this.fromExistenceCondition.setPredicate(unitName -> true);
+ this.toExistenceCondition.setPredicate(unitName -> true);
+ }
+ break;
+ default:
+ System.err.printf("Warning: unrecognized setting \"%s\".",
+ param);
+ break;
+ }
+ }
+ } catch (final IOException e) {}
+ }
+
+ /**
* @return a set of all prefix names in the database
* @since 2019-04-14
* @since v0.2.0
@@ -485,6 +566,27 @@ final class UnitConverterGUI {
}
/**
+ * Saves the settings to the settings file.
+ *
+ * @since 2021-02-17
+ */
+ public final void saveSettings() {
+ try (BufferedWriter writer = Files
+ .newBufferedWriter(this.getSettingsFile())) {
+ writer.write(String.format("precision=%d\n", this.precision));
+ writer.write(
+ String.format("rounding_type=%s\n", this.roundingType));
+ writer.write(String.format("prefix_rule=%s\n", this.prefixRule));
+ writer.write(String.format("one_way=%s\n", this.oneWay));
+ } catch (final IOException e) {
+ e.printStackTrace();
+ this.view.showErrorDialog("I/O Error",
+ "Error occurred while saving settings: "
+ + e.getLocalizedMessage());
+ }
+ }
+
+ /**
* Enables or disables one-way conversion.
*
* @param oneWay whether one-way conversion should be on (true) or off
@@ -492,6 +594,7 @@ final class UnitConverterGUI {
* @since 2020-08-27
*/
public final void setOneWay(boolean oneWay) {
+ this.oneWay = oneWay;
if (oneWay) {
this.fromExistenceCondition.setPredicate(
unitName -> this.metricExceptions.contains(unitName)
@@ -503,6 +606,8 @@ final class UnitConverterGUI {
this.fromExistenceCondition.setPredicate(unitName -> true);
this.toExistenceCondition.setPredicate(unitName -> true);
}
+
+ this.saveSettings();
}
/**
@@ -512,6 +617,8 @@ final class UnitConverterGUI {
*/
public final void setPrecision(final int precision) {
this.precision = precision;
+
+ this.saveSettings();
}
/**
@@ -520,7 +627,14 @@ final class UnitConverterGUI {
*/
public void setPrefixRepetitionRule(
Predicate<List<UnitPrefix>> prefixRepetitionRule) {
+ if (prefixRepetitionRule instanceof DefaultPrefixRepetitionRule) {
+ this.prefixRule = (DefaultPrefixRepetitionRule) prefixRepetitionRule;
+ } else {
+ this.prefixRule = null;
+ }
this.database.setPrefixRepetitionRule(prefixRepetitionRule);
+
+ this.saveSettings();
}
/**
@@ -529,6 +643,8 @@ final class UnitConverterGUI {
*/
public final void setRoundingType(RoundingType roundingType) {
this.roundingType = roundingType;
+
+ this.saveSettings();
}
/**
@@ -1022,7 +1138,9 @@ final class UnitConverterGUI {
final JRadioButton fixedPrecision = new JRadioButton(
"Fixed Precision");
- fixedPrecision.setSelected(true);
+ if (this.presenter.roundingType == RoundingType.SIGNIFICANT_DIGITS) {
+ fixedPrecision.setSelected(true);
+ }
fixedPrecision.addActionListener(e -> this.presenter
.setRoundingType(RoundingType.SIGNIFICANT_DIGITS));
roundingRuleButtons.add(fixedPrecision);
@@ -1031,6 +1149,9 @@ final class UnitConverterGUI {
final JRadioButton fixedDecimals = new JRadioButton(
"Fixed Decimal Places");
+ if (this.presenter.roundingType == RoundingType.DECIMAL_PLACES) {
+ fixedDecimals.setSelected(true);
+ }
fixedDecimals.addActionListener(e -> this.presenter
.setRoundingType(RoundingType.DECIMAL_PLACES));
roundingRuleButtons.add(fixedDecimals);
@@ -1039,6 +1160,9 @@ final class UnitConverterGUI {
final JRadioButton relativePrecision = new JRadioButton(
"Scientific Precision");
+ if (this.presenter.roundingType == RoundingType.SCIENTIFIC) {
+ relativePrecision.setSelected(true);
+ }
relativePrecision.addActionListener(e -> this.presenter
.setRoundingType(RoundingType.SCIENTIFIC));
roundingRuleButtons.add(relativePrecision);
@@ -1058,6 +1182,7 @@ final class UnitConverterGUI {
sigDigSlider.setSnapToTicks(true);
sigDigSlider.setPaintTicks(true);
sigDigSlider.setPaintLabels(true);
+ sigDigSlider.setValue(this.presenter.precision);
sigDigSlider.addChangeListener(e -> this.presenter
.setPrecision(sigDigSlider.getValue()));
@@ -1075,6 +1200,9 @@ final class UnitConverterGUI {
final JRadioButton noRepetition = new JRadioButton(
"No Repetition");
+ if (this.presenter.prefixRule == DefaultPrefixRepetitionRule.NO_REPETITION) {
+ noRepetition.setSelected(true);
+ }
noRepetition.addActionListener(
e -> this.presenter.setPrefixRepetitionRule(
DefaultPrefixRepetitionRule.NO_REPETITION));
@@ -1086,7 +1214,9 @@ final class UnitConverterGUI {
final JRadioButton noRestriction = new JRadioButton(
"No Restriction");
- noRestriction.setSelected(true);
+ if (this.presenter.prefixRule == DefaultPrefixRepetitionRule.NO_RESTRICTION) {
+ noRestriction.setSelected(true);
+ }
noRestriction.addActionListener(
e -> this.presenter.setPrefixRepetitionRule(
DefaultPrefixRepetitionRule.NO_RESTRICTION));
@@ -1098,6 +1228,9 @@ final class UnitConverterGUI {
final JRadioButton customRepetition = new JRadioButton(
"Complex Repetition");
+ if (this.presenter.prefixRule == DefaultPrefixRepetitionRule.COMPLEX_REPETITION) {
+ customRepetition.setSelected(true);
+ }
customRepetition.addActionListener(
e -> this.presenter.setPrefixRepetitionRule(
DefaultPrefixRepetitionRule.COMPLEX_REPETITION));
@@ -1155,7 +1288,7 @@ final class UnitConverterGUI {
final JCheckBox oneWay = new JCheckBox(
"Convert One Way Only");
- oneWay.setSelected(false);
+ oneWay.setSelected(this.presenter.oneWay);
oneWay.addItemListener(
e -> this.presenter.setOneWay(e.getStateChange() == 1));
miscPanel.add(oneWay, new GridBagBuilder(0, 0)