From 06e9da777f047cfb96da126c38e7d6046fe33d97 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Fri, 8 Jul 2022 09:48:36 -0500 Subject: Search rules can be changed by GUI, saved and loaded --- src/main/java/sevenUnitsGUI/Presenter.java | 45 ++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/main/java/sevenUnitsGUI/Presenter.java') diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 24a6c10..a5c4d48 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -200,7 +200,7 @@ public final class Presenter { * names to prefixed versions of that unit (including the unit itself) that * should be searchable. */ - private final Function, Map> searchRule = PrefixSearchRule.COMMON_PREFIXES; + private Function, Map> searchRule = PrefixSearchRule.NO_PREFIXES; /** * The set of units that is considered neither metric nor nonmetric for the @@ -517,6 +517,23 @@ public final class Presenter { return this.prefixRepetitionRule; } + /** + * @return the rule that determines which units are prefixed + * @since 2022-07-08 + */ + public Function, Map> getSearchRule() { + return this.searchRule; + } + + /** + * @return a search rule that shows all single prefixes + * @since 2022-07-08 + */ + public Function, Map> getUniversalSearchRule() { + return PrefixSearchRule.getCoherentOnlyRule( + new HashSet<>(this.database.prefixMap(true).values())); + } + /** * @return the view associated with this presenter * @since 2022-04-19 @@ -579,6 +596,16 @@ public final class Presenter { case "include_duplicates": this.showDuplicates = Boolean.valueOf(value); break; + case "search_prefix_rule": + if (PrefixSearchRule.NO_PREFIXES.toString().equals(value)) { + this.searchRule = PrefixSearchRule.NO_PREFIXES; + } else if (PrefixSearchRule.COMMON_PREFIXES.toString() + .equals(value)) { + this.searchRule = PrefixSearchRule.COMMON_PREFIXES; + } else { + this.searchRule = this.getUniversalSearchRule(); + } + break; default: System.err.printf("Warning: unrecognized setting \"%s\".%n", param); @@ -595,7 +622,7 @@ 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 */ public boolean oneWayConversionEnabled() { @@ -656,6 +683,8 @@ public final class Presenter { String.format("one_way=%s\n", this.oneWayConversionEnabled)); writer.write( String.format("include_duplicates=%s\n", this.showDuplicates)); + writer.write( + String.format("search_prefix_rule=%s\n", this.searchRule)); } catch (final IOException e) { e.printStackTrace(); this.view.showErrorMessage("I/O Error", @@ -707,6 +736,18 @@ public final class Presenter { this.database.setPrefixRepetitionRule(prefixRepetitionRule); } + /** + * @param searchRule A rule that accepts a prefixless name-unit pair and + * returns a map mapping names to prefixed versions of that + * unit (including the unit itself) that should be + * searchable. + * @since 2022-07-08 + */ + public void setSearchRule( + Function, Map> searchRule) { + this.searchRule = searchRule; + } + /** * @param showDuplicateUnits whether or not duplicate units should be shown * @since 2022-03-30 -- cgit v1.2.3