diff options
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index e0f7c8e..aa81a74 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -25,6 +25,10 @@ import java.io.File; import java.math.BigDecimal; import java.math.MathContext; import java.text.DecimalFormat; +import java.text.FieldPosition; +import java.text.NumberFormat; +import java.text.ParseException; +import java.text.ParsePosition; import java.util.ArrayList; import java.util.Comparator; import java.util.HashSet; @@ -200,18 +204,19 @@ final class UnitConverterGUI { final Unit from = this.database.getUnit(fromSelection); final Unit to = this.database.getUnit(toSelection); - final String input = this.view.getDimensionConverterInput(); - if (input.equals("")) { - this.view.showErrorDialog("Error", "No value to convert entered."); + final double beforeValue; + try { + beforeValue = this.view.getDimensionConverterInput(); + } catch (ParseException e) { + this.view.showErrorDialog("Error", "Error in parsing: " + e.getMessage()); return; } - final double beforeValue = Double.parseDouble(input); final double value = from.convertTo(to, beforeValue); final String output = this.getRoundedString(value); this.view.setDimensionConverterOutputText( - String.format("%s %s = %s %s", input, fromSelection, output, toSelection)); + String.format("%s %s = %s %s", this.view.getDimensionConverterText(), fromSelection, output, toSelection)); } /** @@ -414,6 +419,8 @@ final class UnitConverterGUI { } private static class View { + private static final NumberFormat NUMBER_FORMATTER = new DecimalFormat(); + /** The view's frame. */ private final JFrame frame; /** The view's associated presenter. */ @@ -469,7 +476,7 @@ final class UnitConverterGUI { this.prefixTextBox = new JTextArea(); this.fromSearch = new SearchBoxList(this.presenter.unitNameSet()); this.toSearch = new SearchBoxList(this.presenter.unitNameSet()); - this.valueInput = new JFormattedTextField(new DecimalFormat("###############0.################")); + this.valueInput = new JFormattedTextField(NUMBER_FORMATTER); this.dimensionBasedOutput = new JTextArea(2, 32); this.fromEntry = new JTextField(); this.toEntry = new JTextField(); @@ -502,10 +509,24 @@ final class UnitConverterGUI { /** * @return value in dimension-based converter + * @throws ParseException + * @since 2020-07-07 + */ + public double getDimensionConverterInput() throws ParseException { + Number value = NUMBER_FORMATTER.parse(this.valueInput.getText()); + if (value instanceof Double) { + return (double) value; + } else if (value instanceof Long) { + return (double) (((Long) value).longValue()); + } else throw new AssertionError(); + } + + /** + * @return text inputted into dimension-based converter * @since 2019-04-13 * @since v0.2.0 */ - public String getDimensionConverterInput() { + public String getDimensionConverterText() { return this.valueInput.getText(); } |