diff options
Diffstat (limited to 'src/main/java/sevenUnitsGUI/Presenter.java')
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 98 |
1 files changed, 89 insertions, 9 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 5c8ce53..981af21 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -33,13 +33,17 @@ import java.util.stream.Collectors; import sevenUnits.ProgramInfo; import sevenUnits.unit.BaseDimension; +import sevenUnits.unit.BaseUnit; import sevenUnits.unit.BritishImperial; +import sevenUnits.unit.LinearUnit; import sevenUnits.unit.LinearUnitValue; import sevenUnits.unit.Metric; import sevenUnits.unit.Unit; import sevenUnits.unit.UnitDatabase; import sevenUnits.unit.UnitPrefix; +import sevenUnits.unit.UnitType; import sevenUnits.unit.UnitValue; +import sevenUnits.utils.Nameable; import sevenUnits.utils.ObjectProduct; import sevenUnits.utils.UncertainDouble; @@ -383,6 +387,21 @@ public final class Presenter { } /** + * Gets a name for this dimension using the database + * + * @param dimension dimension to name + * @return name of dimension + * @since 2022-04-16 + */ + final String getDimensionName(ObjectProduct<BaseDimension> dimension) { + // find this dimension in the database and get its name + // if it isn't there, use the dimension's toString instead + return this.database.dimensionMap().values().stream() + .filter(d -> d.equals(dimension)).findAny().map(Nameable::getName) + .orElse(dimension.toString(Nameable::getName)); + } + + /** * @return the rule that is used by this presenter to convert numbers into * strings * @since 2022-04-10 @@ -402,14 +421,25 @@ public final class Presenter { } /** - * @return true iff the One-Way Conversion feature is available (views that - * show units as a list will have metric units removed from the From - * unit list and imperial/USC units removed from the To unit list) - * - * @since 2022-03-30 + * @return type of unit {@code u} + * @since 2022-04-16 */ - public boolean oneWayConversionEnabled() { - return this.oneWayConversionEnabled; + private final UnitType getUnitType(Unit u) { + // determine if u is an exception + final var primaryName = u.getPrimaryName(); + final var symbol = u.getSymbol(); + final boolean isException = primaryName.isPresent() + && this.metricExceptions.contains(primaryName.orElseThrow()) + || symbol.isPresent() + && this.metricExceptions.contains(symbol.orElseThrow()); + + // determine unit type + if (isException) + return UnitType.SEMI_METRIC; + else if (u.isMetric()) + return UnitType.METRIC; + else + return UnitType.NON_METRIC; } /** @@ -422,6 +452,17 @@ public final class Presenter { void loadSettings(Path settingsFile) {} /** + * @return true iff the One-Way Conversion feature is available (views that + * show units as a list will have metric units removed from the From + * unit list and imperial/USC units removed from the To unit list) + * + * @since 2022-03-30 + */ + public boolean oneWayConversionEnabled() { + return this.oneWayConversionEnabled; + } + + /** * Completes creation of the presenter. This part of the initialization * depends on the view's functions, so it cannot be run if the components * they depend on are not created yet. @@ -434,9 +475,24 @@ public final class Presenter { final UnitConversionView ucview = (UnitConversionView) this.view; ucview.setDimensionNames(this.database.dimensionMap().keySet()); } + + // load units & prefixes into viewers + this.view.setViewableUnitNames( + this.database.unitMapPrefixless(this.showDuplicateUnits).keySet()); + this.view.setViewablePrefixNames(this.database.prefixMap().keySet()); } - void prefixSelected() {} + void prefixSelected() { + final Optional<String> selectedPrefixName = this.view + .getViewedPrefixName(); + final Optional<UnitPrefix> selectedPrefix = selectedPrefixName + .map(name -> this.database.containsPrefixName(name) + ? this.database.getPrefix(name) + : null); + selectedPrefix + .ifPresent(prefix -> this.view.showPrefix(prefix.getNameSymbol(), + String.valueOf(prefix.getMultiplier()))); + } /** * Saves the presenter's settings to the user settings file. @@ -488,12 +544,36 @@ public final class Presenter { } /** + * Shows a unit in the unit viewer + * + * @param u unit to show + * @since 2022-04-16 + */ + private final void showUnit(Unit u) { + final var nameSymbol = u.getNameSymbol(); + final boolean isBase = u instanceof BaseUnit + || u instanceof LinearUnit && ((LinearUnit) u).isBase(); + final var definition = isBase ? "(Base unit)" : u.toDefinitionString(); + final var dimensionString = this.getDimensionName(u.getDimension()); + final var unitType = this.getUnitType(u); + this.view.showUnit(nameSymbol, definition, dimensionString, unitType); + } + + /** * Runs whenever a unit name is selected in the unit viewer. Gets the * description of a unit and displays it. * * @since 2022-04-10 */ - void unitNameSelected() {} + void unitNameSelected() { + // get selected unit, if it's there and valid + final Optional<String> selectedUnitName = this.view.getViewedUnitName(); + final Optional<Unit> selectedUnit = selectedUnitName + .map(unitName -> this.database.containsUnitName(unitName) + ? this.database.getUnit(unitName) + : null); + selectedUnit.ifPresent(this::showUnit); + } /** * Updates the view's From and To units, if it has some |