diff options
Diffstat (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java')
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 61 |
1 files changed, 18 insertions, 43 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index f7c3479..eff0c47 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -149,7 +149,8 @@ final class UnitConverterGUI { this.view = view; // load initial units - this.database = new UnitDatabase(); + this.database = new UnitDatabase( + DefaultPrefixRepetitionRule.NO_RESTRICTION); Presenter.addDefaults(this.database); this.database.loadUnitsFile(new File("unitsfile.txt")); @@ -308,7 +309,6 @@ final class UnitConverterGUI { this.view.setExpressionConverterOutputText((useSlash ? "1 / " : "") + String.format("%s = %s", fromUnitString, this.getRoundedString(converted, false))); - final String toString = this.getRoundedString(converted, false); return; } else { // convert to UnitValue @@ -347,45 +347,6 @@ final class UnitConverterGUI { } /** - * @param value value to round - * @return string of that value rounded to {@code significantDigits} - * significant digits. - * @since 2019-04-14 - * @since v0.2.0 - */ - private final String getRoundedString(final BigDecimal value) { - // round value based on rounding type - final BigDecimal roundedValue; - switch (this.roundingType) { - case DECIMAL_PLACES: - roundedValue = value.setScale(this.precision, - RoundingMode.HALF_EVEN); - break; - case SCIENTIFIC: - throw new UnsupportedOperationException("Not yet implemented."); - case SIGNIFICANT_DIGITS: - roundedValue = value.round(new MathContext(this.precision)); - break; - default: - throw new AssertionError("Invalid switch condition."); - } - - String output = roundedValue.toString(); - - // remove trailing zeroes - if (output.contains(".")) { - while (output.endsWith("0")) { - output = output.substring(0, output.length() - 1); - } - if (output.endsWith(".")) { - output = output.substring(0, output.length() - 1); - } - } - - return output; - } - - /** * Like {@link LinearUnitValue#toString(boolean)}, but obeys this unit * converter's rounding settings. * @@ -414,6 +375,7 @@ final class UnitConverterGUI { final BigDecimal unrounded = new BigDecimal(value.getValue()); final BigDecimal rounded; int precision = this.precision; + switch (this.roundingType) { case DECIMAL_PLACES: rounded = unrounded.setScale(precision, RoundingMode.HALF_EVEN); @@ -484,6 +446,15 @@ final class UnitConverterGUI { } /** + * @param prefixRepetitionRule the prefixRepetitionRule to set + * @since 2020-08-26 + */ + public void setPrefixRepetitionRule( + Predicate<List<UnitPrefix>> prefixRepetitionRule) { + this.database.setPrefixRepetitionRule(prefixRepetitionRule); + } + + /** * @param roundingType the roundingType to set * @since 2020-07-16 */ @@ -1031,7 +1002,9 @@ final class UnitConverterGUI { final JRadioButton noRepetition = new JRadioButton( "No Repetition"); - noRepetition.setEnabled(false); + noRepetition.addActionListener( + e -> this.presenter.setPrefixRepetitionRule( + DefaultPrefixRepetitionRule.NO_REPETITION)); prefixRuleButtons.add(noRepetition); prefixRepetitionPanel.add(noRepetition, new GridBagBuilder(0, 0) @@ -1041,7 +1014,9 @@ final class UnitConverterGUI { final JRadioButton noRestriction = new JRadioButton( "No Restriction"); noRestriction.setSelected(true); - noRestriction.setEnabled(false); + noRestriction.addActionListener( + e -> this.presenter.setPrefixRepetitionRule( + DefaultPrefixRepetitionRule.NO_RESTRICTION)); prefixRuleButtons.add(noRestriction); prefixRepetitionPanel.add(noRestriction, new GridBagBuilder(0, 1) |