diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2022-07-08 09:48:36 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2022-07-08 09:48:36 -0500 |
commit | 06e9da777f047cfb96da126c38e7d6046fe33d97 (patch) | |
tree | f2b2d21ef11dea4374117492276185a366f69e55 /src/main/java | |
parent | 60027af631f071aa12f7d2afb77179464dc69d0b (diff) |
Search rules can be changed by GUI, saved and loaded
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/sevenUnitsGUI/Presenter.java | 45 | ||||
-rw-r--r-- | src/main/java/sevenUnitsGUI/TabbedView.java | 47 |
2 files changed, 76 insertions, 16 deletions
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.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule = PrefixSearchRule.COMMON_PREFIXES; + private Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule = PrefixSearchRule.NO_PREFIXES; /** * The set of units that is considered neither metric nor nonmetric for the @@ -518,6 +518,23 @@ public final class Presenter { } /** + * @return the rule that determines which units are prefixed + * @since 2022-07-08 + */ + public Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> getSearchRule() { + return this.searchRule; + } + + /** + * @return a search rule that shows all single prefixes + * @since 2022-07-08 + */ + public Function<Map.Entry<String, LinearUnit>, Map<String, LinearUnit>> 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", @@ -708,6 +737,18 @@ public final class Presenter { } /** + * @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.Entry<String, LinearUnit>, Map<String, LinearUnit>> searchRule) { + this.searchRule = searchRule; + } + + /** * @param showDuplicateUnits whether or not duplicate units should be shown * @since 2022-03-30 */ diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java index be80ccb..e3b5610 100644 --- a/src/main/java/sevenUnitsGUI/TabbedView.java +++ b/src/main/java/sevenUnitsGUI/TabbedView.java @@ -531,33 +531,52 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView { // searching rules final ButtonGroup searchRuleButtons = new ButtonGroup(); + final var searchRule = this.presenter.getSearchRule(); + final JRadioButton noPrefixes = new JRadioButton( "Never Include Prefixed Units"); - noPrefixes.setEnabled(false); + noPrefixes.addActionListener(e -> { + this.presenter.setSearchRule(PrefixSearchRule.NO_PREFIXES); + this.presenter.updateView(); + this.presenter.saveSettings(); + }); searchRuleButtons.add(noPrefixes); searchingPanel.add(noPrefixes, new GridBagBuilder(0, 0) .setAnchor(GridBagConstraints.LINE_START).build()); - final JRadioButton fixedPrefixes = new JRadioButton( - "Include Some Prefixes"); - fixedPrefixes.setEnabled(false); - searchRuleButtons.add(fixedPrefixes); - searchingPanel.add(fixedPrefixes, new GridBagBuilder(0, 1) - .setAnchor(GridBagConstraints.LINE_START).build()); - - final JRadioButton explicitPrefixes = new JRadioButton( - "Include Explicit Prefixes"); - explicitPrefixes.setEnabled(false); - searchRuleButtons.add(explicitPrefixes); - searchingPanel.add(explicitPrefixes, new GridBagBuilder(0, 2) + final JRadioButton commonPrefixes = new JRadioButton( + "Include Common Prefixes"); + commonPrefixes.addActionListener(e -> { + this.presenter.setSearchRule(PrefixSearchRule.COMMON_PREFIXES); + this.presenter.updateView(); + this.presenter.saveSettings(); + }); + searchRuleButtons.add(commonPrefixes); + searchingPanel.add(commonPrefixes, new GridBagBuilder(0, 1) .setAnchor(GridBagConstraints.LINE_START).build()); final JRadioButton alwaysInclude = new JRadioButton( "Include All Single Prefixes"); - alwaysInclude.setEnabled(false); + alwaysInclude.addActionListener(e -> { + this.presenter + .setSearchRule(this.presenter.getUniversalSearchRule()); + this.presenter.updateView(); + this.presenter.saveSettings(); + }); searchRuleButtons.add(alwaysInclude); searchingPanel.add(alwaysInclude, new GridBagBuilder(0, 3) .setAnchor(GridBagConstraints.LINE_START).build()); + + if (PrefixSearchRule.NO_PREFIXES.equals(searchRule)) { + noPrefixes.setSelected(true); + } else if (PrefixSearchRule.COMMON_PREFIXES.equals(searchRule)) { + commonPrefixes.setSelected(true); + } else { + alwaysInclude.setSelected(true); + this.presenter + .setSearchRule(this.presenter.getUniversalSearchRule()); + this.presenter.saveSettings(); + } } // ============ OTHER SETTINGS ============ |