diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-08-22 12:19:30 -0500 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2024-08-22 12:19:30 -0500 |
commit | b0242b898653f2dc23f2187deec9db0bb652751d (patch) | |
tree | 6fc4b19b0f514e34235dbc9963ad78ddbb987b6b | |
parent | 6feabb35336fa3ca1faaff203f50086dc018b5ce (diff) |
Add loading counts to About tab
-rw-r--r-- | CHANGELOG.org | 2 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 88 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/TabbedView.java | 2 | ||||
-rw-r--r-- | src/main/resources/about.txt | 4 |
4 files changed, 73 insertions, 23 deletions
diff --git a/CHANGELOG.org b/CHANGELOG.org index 344b4a0..78bb9a1 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -5,7 +5,7 @@ All notable changes in this project will be shown in this file. - *Allowed conversion to a sum of units (e.g. 4/3 ft \rightarrow 1 ft + 4 in).* - *Allowed exponents on units to be non-integer numbers.* The resulting exponents are rounded to the nearest integer, and the user is warned if this rounding changes the value by more than normal floating-point error. -- Add more information to the loading-success message. +- Added more information to the loading-success message, and added it to the about tab. *** Changed - *Errors in unit/dimension files are shown in popups, rather than crashing the program.* *** Fixed diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 4ff2d65..c46ee53 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -74,6 +74,9 @@ public final class Presenter { private static final String DEFAULT_DIMENSIONS_FILEPATH = "/dimensionfile.txt"; /** The default place where exceptions are stored. */ private static final String DEFAULT_EXCEPTIONS_FILEPATH = "/metric_exceptions.txt"; + /** A Predicate that returns true iff the argument is a full base unit */ + private static final Predicate<Unit> IS_FULL_BASE = unit -> unit instanceof LinearUnit + && ((LinearUnit) unit).isBase(); /** * Adds default units and dimensions to a database. @@ -119,13 +122,17 @@ public final class Presenter { } /** - * @return text in About file - * @since 2022-02-19 + * Determines where to wrap {@code toWrap} with a max line length of + * {@code maxLineLength}. If no good spot is found, returns -1. + * + * @since 2024-08-22 */ - public static final String getAboutText() { - return Presenter.getLinesFromResource("/about.txt").stream() - .map(Presenter::withoutComments).collect(Collectors.joining("\n")) - .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString()); + private static final int findLineSplit(String toWrap, int maxLineLength) { + for (int i = maxLineLength - 1; i >= 0; i--) { + if (Character.isWhitespace(toWrap.charAt(i))) + return i; + } + return -1; } /** @@ -242,6 +249,30 @@ public final class Presenter { } /** + * Wraps a string, ensuring no line is longer than {@code maxLineLength}. + * + * @since 2024-08-22 + */ + private static final String wrapString(String toWrap, int maxLineLength) { + final StringBuilder wrapped = new StringBuilder(toWrap.length()); + String remaining = toWrap; + while (remaining.length() > maxLineLength) { + final int spot = findLineSplit(toWrap, maxLineLength); + if (spot == -1) { + wrapped.append(remaining.substring(0, maxLineLength)); + wrapped.append("-\n"); + remaining = remaining.substring(maxLineLength).stripLeading(); + } else { + wrapped.append(remaining.substring(0, spot)); + wrapped.append("\n"); + remaining = remaining.substring(spot + 1).stripLeading(); + } + } + wrapped.append(remaining); + return wrapped.toString(); + } + + /** * The view that this presenter communicates with */ private final View view; @@ -349,21 +380,8 @@ public final class Presenter { this.loadSettings(CONFIG_FILE); } - // a Predicate that returns true iff the argument is a full base unit - final Predicate<Unit> isFullBase = unit -> unit instanceof LinearUnit - && ((LinearUnit) unit).isBase(); - // print out unit counts - System.out.printf( - "Successfully loaded %d unique units with %d names (%d base units), %d unique prefixes with %d names, %d unit sets, and %d named dimensions.%n", - this.database.unitMapPrefixless(false).size(), - this.database.unitMapPrefixless(true).size(), - this.database.unitMapPrefixless(false).values().stream() - .filter(isFullBase).count(), - this.database.prefixMap(false).size(), - this.database.prefixMap(true).size(), - this.database.unitSetMap().size(), - this.database.dimensionMap().size()); + System.out.println(this.loadStatMsg()); } /** @@ -707,6 +725,17 @@ public final class Presenter { } /** + * @return text in About file + * @since 2022-02-19 + */ + public final String getAboutText() { + return Presenter.getLinesFromResource("/about.txt").stream() + .map(Presenter::withoutComments).collect(Collectors.joining("\n")) + .replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString()) + .replaceAll("\\[LOADSTATS\\]", wrapString(this.loadStatMsg(), 72)); + } + + /** * Gets a name for this dimension using the database * * @param dimension dimension to name @@ -892,6 +921,23 @@ public final class Presenter { } /** + * @return a message showing how much stuff has been loaded + * @since 2024-08-22 + */ + private String loadStatMsg() { + return String.format( + "Successfully loaded %d unique units with %d names (%d base units), %d unique prefixes with %d names, %d unit sets, and %d named dimensions.", + this.database.unitMapPrefixless(false).size(), + this.database.unitMapPrefixless(true).size(), + this.database.unitMapPrefixless(false).values().stream() + .filter(IS_FULL_BASE).count(), + this.database.prefixMap(false).size(), + this.database.prefixMap(true).size(), + this.database.unitSetMap().size(), + this.database.dimensionMap().size()); + } + + /** * @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) @@ -1072,7 +1118,7 @@ public final class Presenter { * @param u unit to show * @since 2022-04-16 */ - private final void showUnit(Unit u) { + private void showUnit(Unit u) { final var nameSymbol = u.getNameSymbol(); final boolean isBase = u instanceof BaseUnit || u instanceof LinearUnit && ((LinearUnit) u).isBase(); diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index a71de44..6542541 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -357,7 +357,7 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { infoTextArea.setEditable(false); infoTextArea.setOpaque(false); infoPanel.add(infoTextArea); - infoTextArea.setText(Presenter.getAboutText()); + infoTextArea.setText(this.presenter.getAboutText()); // ============ SETTINGS PANEL ============ this.masterPane.addTab("\u2699", diff --git a/src/main/resources/about.txt b/src/main/resources/about.txt index 782b422..4c33f8c 100644 --- a/src/main/resources/about.txt +++ b/src/main/resources/about.txt @@ -9,6 +9,10 @@ like "10 m/s + (25^2 - 5^2) mi/hr". This software was written by Adrien Hopkins <adrien.p.hopkins@gmail.com>. +Unit/Prefix/Dimension Statistics: + +[LOADSTATS] + Copyright Notice: Unit Converter Copyright (C) 2018-2024 Adrien Hopkins |