diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-04-14 15:39:38 -0400 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-04-14 15:39:38 -0400 |
commit | 54b9f00faba367e1ef325c9d0bc75a848fadb906 (patch) | |
tree | b3036a32e053846625aad2c467cb6dd8ce004265 | |
parent | fc1083454e4e9215140802602a17aafeef4515fa (diff) |
The unit and prefix viewers now use SearchBoxList.
-rwxr-xr-x | src/org/unitConverter/UnitsDatabase.java | 2 | ||||
-rwxr-xr-x | src/org/unitConverter/converterGUI/FilterComparator.java | 50 | ||||
-rw-r--r-- | src/org/unitConverter/converterGUI/SearchBoxList.java | 77 | ||||
-rwxr-xr-x | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 401 | ||||
-rwxr-xr-x | unitsfile.txt | 2 |
5 files changed, 247 insertions, 285 deletions
diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index 959c151..abe6546 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -134,6 +134,7 @@ public final class UnitsDatabase { // the indices of the prefixes attached to the current unit private final List<Integer> prefixCoordinates = new ArrayList<>(); + // values from the unit entry set private final Map<String, Unit> map; private final List<String> unitNames; private final List<String> prefixNames; @@ -366,6 +367,7 @@ public final class UnitsDatabase { // the indices of the prefixes attached to the current unit private final List<Integer> prefixCoordinates = new ArrayList<>(); + // values from the unit name set private final Map<String, Unit> map; private final List<String> unitNames; private final List<String> prefixNames; diff --git a/src/org/unitConverter/converterGUI/FilterComparator.java b/src/org/unitConverter/converterGUI/FilterComparator.java index ad8d0b0..ef94602 100755 --- a/src/org/unitConverter/converterGUI/FilterComparator.java +++ b/src/org/unitConverter/converterGUI/FilterComparator.java @@ -41,6 +41,12 @@ public final class FilterComparator implements Comparator<String> { * @since v0.1.0
*/
private final Comparator<String> comparator;
+ /**
+ * Whether or not the comparison is case-sensitive.
+ *
+ * @since 2019-04-14
+ */
+ private final boolean caseSensitive;
/**
* Creates the {@code FilterComparator}.
@@ -60,38 +66,62 @@ public final class FilterComparator implements Comparator<String> { * string to filter by
* @param comparator
* comparator to fall back to if all else fails, null is compareTo.
+ * @throws NullPointerException
+ * if filter is null
* @since 2019-01-15
* @since v0.1.0
+ */
+ public FilterComparator(final String filter, final Comparator<String> comparator) {
+ this(filter, comparator, false);
+ }
+
+ /**
+ * Creates the {@code FilterComparator}.
+ *
+ * @param filter
+ * string to filter by
+ * @param comparator
+ * comparator to fall back to if all else fails, null is compareTo.
+ * @param caseSensitive
+ * whether or not the comparator is case-sensitive
* @throws NullPointerException
* if filter is null
+ * @since 2019-04-14
*/
- public FilterComparator(final String filter, final Comparator<String> comparator) {
+ public FilterComparator(final String filter, final Comparator<String> comparator, final boolean caseSensitive) {
this.filter = Objects.requireNonNull(filter, "filter must not be null.");
this.comparator = comparator;
+ this.caseSensitive = caseSensitive;
}
@Override
public int compare(final String arg0, final String arg1) {
- // this is case insensitive, so make them lowercase
- final String arg0lower = arg0.toLowerCase();
- final String arg1lower = arg1.toLowerCase();
+ // if this is case insensitive, make them lowercase
+ final String str0, str1;
+ if (this.caseSensitive) {
+ str0 = arg0;
+ str1 = arg1;
+ } else {
+ str0 = arg0.toLowerCase();
+ str1 = arg1.toLowerCase();
+ }
// elements that start with the filter always go first
- if (arg0lower.startsWith(this.filter) && !arg1lower.startsWith(this.filter))
+ if (str0.startsWith(this.filter) && !str1.startsWith(this.filter))
return -1;
- else if (!arg0lower.startsWith(this.filter) && arg1lower.startsWith(this.filter))
+ else if (!str0.startsWith(this.filter) && str1.startsWith(this.filter))
return 1;
// elements that contain the filter but don't start with them go next
- if (arg0lower.contains(this.filter) && !arg1lower.contains(this.filter))
+ if (str0.contains(this.filter) && !str1.contains(this.filter))
return -1;
- else if (!arg0lower.contains(this.filter) && !arg1lower.contains(this.filter))
+ else if (!str0.contains(this.filter) && !str1.contains(this.filter))
return 1;
// other elements go last
if (this.comparator == null)
- return arg0lower.compareTo(arg1lower);
+ return str0.compareTo(str1);
else
- return this.comparator.compare(arg0lower, arg1lower);
+ return this.comparator.compare(str0, str1);
}
}
diff --git a/src/org/unitConverter/converterGUI/SearchBoxList.java b/src/org/unitConverter/converterGUI/SearchBoxList.java index 7d3b748..35cc347 100644 --- a/src/org/unitConverter/converterGUI/SearchBoxList.java +++ b/src/org/unitConverter/converterGUI/SearchBoxList.java @@ -22,6 +22,7 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.function.Predicate; import javax.swing.JList; @@ -61,16 +62,25 @@ final class SearchBoxList extends JPanel { // event. private boolean searchBoxFocused = false; - private Predicate<String> searchFilter = o -> true; + private Predicate<String> customSearchFilter = o -> true; + private final Comparator<String> defaultOrdering; + private final boolean caseSensitive; + + public SearchBoxList(final Collection<String> itemsToFilter) { + this(itemsToFilter, null, false); + } /** * Creates the {@code SearchBoxList}. * * @since 2019-04-13 */ - public SearchBoxList(final Collection<String> itemsToFilter) { + public SearchBoxList(final Collection<String> itemsToFilter, final Comparator<String> defaultOrdering, + final boolean caseSensitive) { super(new BorderLayout(), true); this.itemsToFilter = itemsToFilter; + this.defaultOrdering = defaultOrdering; + this.caseSensitive = caseSensitive; // create the components this.listModel = new DelegateListModel<>(new ArrayList<>(itemsToFilter)); @@ -108,7 +118,7 @@ final class SearchBoxList extends JPanel { * @since 2019-04-13 */ public void addSearchFilter(final Predicate<String> filter) { - this.searchFilter = this.searchFilter.and(filter); + this.customSearchFilter = this.customSearchFilter.and(filter); } /** @@ -117,7 +127,44 @@ final class SearchBoxList extends JPanel { * @since 2019-04-13 */ public void clearSearchFilters() { - this.searchFilter = o -> true; + this.customSearchFilter = o -> true; + } + + /** + * @return this component's search box component + * @since 2019-04-14 + */ + public final JTextField getSearchBox() { + return this.searchBox; + } + + /** + * @param searchText + * text to search for + * @return a filter that filters out that text, based on this list's case sensitive setting + * @since 2019-04-14 + */ + private Predicate<String> getSearchFilter(final String searchText) { + if (this.caseSensitive) + return string -> string.contains(searchText); + else + return string -> string.toLowerCase().contains(searchText.toLowerCase()); + } + + /** + * @return this component's list component + * @since 2019-04-14 + */ + public final JList<String> getSearchList() { + return this.searchItems; + } + + /** + * @return index selected in item list + * @since 2019-04-14 + */ + public int getSelectedIndex() { + return this.searchItems.getSelectedIndex(); } /** @@ -135,17 +182,18 @@ final class SearchBoxList extends JPanel { */ public void reapplyFilter() { final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); - final FilterComparator comparator = new FilterComparator(searchText); + final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive); + final Predicate<String> searchFilter = this.getSearchFilter(searchText); this.listModel.clear(); this.itemsToFilter.forEach(string -> { - if (string.toLowerCase().contains(searchText.toLowerCase())) { + if (searchFilter.test(string)) { this.listModel.add(string); } }); // applies the custom filters - this.listModel.removeIf(this.searchFilter.negate()); + this.listModel.removeIf(this.customSearchFilter.negate()); // sorts the remaining items this.listModel.sort(comparator); @@ -181,23 +229,32 @@ final class SearchBoxList extends JPanel { } } + /** + * Runs whenever the text in the search box is changed. + * <p> + * Reapplies the search filter, and custom filters. + * </p> + * + * @since 2019-04-14 + */ private void searchBoxTextChanged() { if (this.searchBoxFocused) { this.searchBoxEmpty = this.searchBox.getText().equals(""); } final String searchText = this.searchBoxEmpty ? "" : this.searchBox.getText(); - final FilterComparator comparator = new FilterComparator(searchText); + final FilterComparator comparator = new FilterComparator(searchText, this.defaultOrdering, this.caseSensitive); + final Predicate<String> searchFilter = this.getSearchFilter(searchText); // initialize list with items that match the filter then sort this.listModel.clear(); this.itemsToFilter.forEach(string -> { - if (string.toLowerCase().contains(searchText.toLowerCase())) { + if (searchFilter.test(string)) { this.listModel.add(string); } }); // applies the custom filters - this.listModel.removeIf(this.searchFilter.negate()); + this.listModel.removeIf(this.customSearchFilter.negate()); // sorts the remaining items this.listModel.sort(comparator); diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 34cbef9..cacc3b7 100755 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -35,16 +35,12 @@ import javax.swing.JComboBox; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; -import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; -import javax.swing.ListModel; -import javax.swing.ListSelectionModel; import org.unitConverter.UnitsDatabase; import org.unitConverter.dimension.StandardDimensions; @@ -95,20 +91,14 @@ final class UnitConverterGUI { private final View view; /** The units known by the program. */ - private final UnitsDatabase units; + private final UnitsDatabase database; /** The names of all of the units */ private final List<String> unitNames; - /** The names of all of the units, but filtered */ - private final DelegateListModel<String> unitNamesFiltered; - /** The names of all of the prefixes */ private final List<String> prefixNames; - /** The names of all of the prefixes */ - private final DelegateListModel<String> prefixNamesFiltered; - /** The names of all of the dimensions */ private final List<String> dimensionNames; @@ -128,23 +118,23 @@ final class UnitConverterGUI { this.view = view; // load initial units - this.units = new UnitsDatabase(); - Presenter.addDefaults(this.units); + this.database = new UnitsDatabase(); + Presenter.addDefaults(this.database); - this.units.loadUnitsFile(new File("unitsfile.txt")); - this.units.loadDimensionFile(new File("dimensionfile.txt")); + this.database.loadUnitsFile(new File("unitsfile.txt")); + this.database.loadDimensionFile(new File("dimensionfile.txt")); // a comparator that can be used to compare prefix names // any name that does not exist is less than a name that does. // otherwise, they are compared by value this.prefixNameComparator = (o1, o2) -> { - if (!Presenter.this.units.containsPrefixName(o1)) + if (!Presenter.this.database.containsPrefixName(o1)) return -1; - else if (!Presenter.this.units.containsPrefixName(o2)) + else if (!Presenter.this.database.containsPrefixName(o2)) return 1; - final UnitPrefix p1 = Presenter.this.units.getPrefix(o1); - final UnitPrefix p2 = Presenter.this.units.getPrefix(o2); + final UnitPrefix p1 = Presenter.this.database.getPrefix(o1); + final UnitPrefix p2 = Presenter.this.database.getPrefix(o2); if (p1.getMultiplier() < p2.getMultiplier()) return -1; @@ -154,19 +144,13 @@ final class UnitConverterGUI { return o1.compareTo(o2); }; - this.unitNames = new ArrayList<>(this.units.unitMapPrefixless().keySet()); + this.unitNames = new ArrayList<>(this.database.unitMapPrefixless().keySet()); this.unitNames.sort(null); // sorts it using Comparable - this.unitNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.unitMapPrefixless().keySet())); - this.unitNamesFiltered.sort(null); // sorts it using Comparable - - this.prefixNames = new ArrayList<>(this.units.prefixMap().keySet()); + this.prefixNames = new ArrayList<>(this.database.prefixMap().keySet()); this.prefixNames.sort(this.prefixNameComparator); // sorts it using my comparator - this.prefixNamesFiltered = new DelegateListModel<>(new ArrayList<>(this.units.prefixMap().keySet())); - this.prefixNamesFiltered.sort(this.prefixNameComparator); // sorts it using my comparator - - this.dimensionNames = new DelegateListModel<>(new ArrayList<>(this.units.dimensionMap().keySet())); + this.dimensionNames = new DelegateListModel<>(new ArrayList<>(this.database.dimensionMap().keySet())); this.dimensionNames.sort(null); // sorts it using Comparable // a Predicate that returns true iff the argument is a full base unit @@ -174,9 +158,43 @@ final class UnitConverterGUI { // print out unit counts System.out.printf("Successfully loaded %d units with %d unit names (%d base units).%n", - new HashSet<>(this.units.unitMapPrefixless().values()).size(), - this.units.unitMapPrefixless().size(), - new HashSet<>(this.units.unitMapPrefixless().values()).stream().filter(isFullBase).count()); + new HashSet<>(this.database.unitMapPrefixless().values()).size(), + this.database.unitMapPrefixless().size(), + new HashSet<>(this.database.unitMapPrefixless().values()).stream().filter(isFullBase).count()); + } + + /** + * Converts in the dimension-based converter + * + * @since 2019-04-13 + */ + public final void convertDimensionBased() { + final String fromSelection = this.view.getFromSelection(); + if (fromSelection == null) { + this.view.showErrorDialog("Error", "No unit selected in From field"); + return; + } + final String toSelection = this.view.getToSelection(); + if (toSelection == null) { + this.view.showErrorDialog("Error", "No unit selected in To field"); + return; + } + + 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."); + return; + } + final double beforeValue = Double.parseDouble(input); + final double value = to.convertFromBase(from.convertToBase(beforeValue)); + + final String output = this.getRoundedString(value); + + this.view.setDimensionConverterOutputText( + String.format("%s %s = %s %s", input, fromSelection, output, toSelection)); } /** @@ -190,7 +208,7 @@ final class UnitConverterGUI { * @since 2019-01-26 * @since v0.1.0 */ - public final void convert() { + public final void convertExpressions() { final String fromUnitString = this.view.getFromText(); final String toUnitString = this.view.getToText(); @@ -202,7 +220,7 @@ final class UnitConverterGUI { // try to parse from final Unit from; try { - from = this.units.getUnitFromExpression(fromUnitString); + from = this.database.getUnitFromExpression(fromUnitString); } catch (final IllegalArgumentException e) { this.view.showErrorDialog("Parse Error", "Could not recognize text in From entry: " + e.getMessage()); return; @@ -212,11 +230,11 @@ final class UnitConverterGUI { // try to parse to final Unit to; try { - if (this.units.containsUnitName(toUnitString)) { + if (this.database.containsUnitName(toUnitString)) { // if it's a unit, convert to that - to = this.units.getUnit(toUnitString); + to = this.database.getUnit(toUnitString); } else { - to = this.units.getUnitFromExpression(toUnitString); + to = this.database.getUnitFromExpression(toUnitString); } } catch (final IllegalArgumentException e) { this.view.showErrorDialog("Parse Error", "Could not recognize text in To entry: " + e.getMessage()); @@ -233,50 +251,35 @@ final class UnitConverterGUI { value = to.convertFromBase(from.convertToBase(1)); // round value - final BigDecimal bigValue = new BigDecimal(value).round(new MathContext(this.significantFigures)); - String output = bigValue.toString(); + final String output = this.getRoundedString(value); - // remove trailing zeroes - if (output.contains(".")) { - while (output.endsWith("0")) { - output = output.substring(0, output.length() - 1); - } - if (output.endsWith(".")) { - output = output.substring(0, output.length() - 1); - } - } - - this.view.setOutputText(String.format("%s = %s %s", fromUnitString, output, toUnitString)); + this.view.setExpressionConverterOutputText( + String.format("%s = %s %s", fromUnitString, output, toUnitString)); } /** - * Converts in the dimension-based converter - * + * @return a list of all of the unit dimensions * @since 2019-04-13 */ - public final void convertDimensionBased() { - final String fromSelection = this.view.getFromSelection(); - if (fromSelection == null) { - this.view.showErrorDialog("Error", "No unit selected in From field"); - return; - } - final String toSelection = this.view.getToSelection(); - if (toSelection == null) { - this.view.showErrorDialog("Error", "No unit selected in To field"); - return; - } - - final Unit from = this.units.getUnit(fromSelection); - final Unit to = this.units.getUnit(toSelection); + public final List<String> dimensionNameList() { + return this.dimensionNames; + } - final String input = this.view.getDimensionBasedInput(); - if (input.equals("")) { - this.view.showErrorDialog("Error", "No value to convert entered."); - return; - } - final double beforeValue = Double.parseDouble(input); - final double value = to.convertFromBase(from.convertToBase(beforeValue)); + /** + * @return a comparator to compare prefix names + * @since 2019-04-14 + */ + public final Comparator<String> getPrefixNameComparator() { + return this.prefixNameComparator; + } + /** + * @param value + * value to round + * @return string of that value rounded to {@code significantDigits} significant digits. + * @since 2019-04-14 + */ + private final String getRoundedString(final double value) { // round value final BigDecimal bigValue = new BigDecimal(value).round(new MathContext(this.significantFigures)); String output = bigValue.toString(); @@ -291,86 +294,15 @@ final class UnitConverterGUI { } } - this.view.setDimensionBasedOutputText( - String.format("%s %s = %s %s", input, fromSelection, output, toSelection)); - } - - /** - * @return a list of all of the unit dimensions - * @since 2019-04-13 - */ - public final List<String> dimensionNameList() { - return this.dimensionNames; - } - - /** - * Filters the filtered model for units - * - * @param filter - * filter to use - * @since 2019-01-15 - * @since v0.1.0 - */ - private final void filterFilteredPrefixModel(final Predicate<String> filter) { - this.prefixNamesFiltered.clear(); - for (final String prefixName : this.prefixNames) { - if (filter.test(prefixName)) { - this.prefixNamesFiltered.add(prefixName); - } - } - } - - /** - * Filters the filtered model for units - * - * @param filter - * filter to use - * @since 2019-01-15 - * @since v0.1.0 - */ - private final void filterFilteredUnitModel(final Predicate<String> filter) { - this.unitNamesFiltered.clear(); - for (final String unitName : this.unitNames) { - if (filter.test(unitName)) { - this.unitNamesFiltered.add(unitName); - } - } + return output; } /** - * @return a list model of all of the unit keys - * @since 2019-01-14 - * @since v0.1.0 - */ - public final ListModel<String> keyListModel() { - return this.unitNamesFiltered; - } - - /** - * Runs whenever the prefix filter is changed. - * <p> - * Filters the prefix list then sorts it using a {@code FilterComparator}. - * </p> - * - * @since 2019-01-15 - * @since v0.1.0 - */ - public final void prefixFilterUpdated() { - final String filter = this.view.getPrefixFilterText(); - if (filter.equals("")) { - this.filterFilteredPrefixModel(t -> true); - } else { - this.filterFilteredPrefixModel(t -> t.contains(filter)); - } - this.prefixNamesFiltered.sort(new FilterComparator(filter)); - } - - /** - * @return a list model of all of the prefix names - * @since 2019-01-15 + * @return a set of all prefix names in the database + * @since 2019-04-14 */ - public final ListModel<String> prefixNameListModel() { - return this.prefixNamesFiltered; + public final Set<String> prefixNameSet() { + return this.database.prefixMap().keySet(); } /** @@ -383,12 +315,11 @@ final class UnitConverterGUI { * @since v0.1.0 */ public final void prefixSelected() { - final int index = this.view.getPrefixListSelection(); - if (index == -1) + final String prefixName = this.view.getPrefixViewerSelection(); + if (prefixName == null) return; else { - final String prefixName = this.prefixNamesFiltered.get(index); - final UnitPrefix prefix = this.units.getPrefix(prefixName); + final UnitPrefix prefix = this.database.getPrefix(prefixName); this.view.setPrefixTextBoxText(String.format("%s%nMultiplier: %s", prefixName, prefix.getMultiplier())); } @@ -404,25 +335,6 @@ final class UnitConverterGUI { } /** - * Runs whenever the unit filter is changed. - * <p> - * Filters the unit list then sorts it using a {@code FilterComparator}. - * </p> - * - * @since 2019-01-15 - * @since v0.1.0 - */ - public final void unitFilterUpdated() { - final String filter = this.view.getUnitFilterText(); - if (filter.equals("")) { - this.filterFilteredUnitModel(t -> true); - } else { - this.filterFilteredUnitModel(t -> t.contains(filter)); - } - this.unitNamesFiltered.sort(new FilterComparator(filter)); - } - - /** * Returns true if and only if the unit represented by {@code unitName} has the dimension represented by * {@code dimensionName}. * @@ -433,9 +345,9 @@ final class UnitConverterGUI { * @return whether unit has dimenision * @since 2019-04-13 */ - public boolean unitMatchesDimension(final String unitName, final String dimensionName) { - final Unit unit = this.units.getUnit(unitName); - final UnitDimension dimension = this.units.getDimension(dimensionName); + public final boolean unitMatchesDimension(final String unitName, final String dimensionName) { + final Unit unit = this.database.getUnit(unitName); + final UnitDimension dimension = this.database.getDimension(dimensionName); return unit.getDimension().equals(dimension); } @@ -448,20 +360,23 @@ final class UnitConverterGUI { * @since 2019-01-15 * @since v0.1.0 */ - public void unitNameSelected() { - final int index = this.view.getUnitListSelection(); - if (index == -1) + public final void unitNameSelected() { + final String unitName = this.view.getUnitViewerSelection(); + if (unitName == null) return; else { - final String unitName = this.unitNamesFiltered.get(index); - final Unit unit = this.units.getUnit(unitName); + final Unit unit = this.database.getUnit(unitName); this.view.setUnitTextBoxText(unit.toString()); } } + /** + * @return a set of all of the unit names + * @since 2019-04-14 + */ public final Set<String> unitNameSet() { - return this.units.unitMapPrefixless().keySet(); + return this.database.unitMapPrefixless().keySet(); } } @@ -471,26 +386,17 @@ final class UnitConverterGUI { /** The view's associated presenter. */ private final Presenter presenter; - /** The list of unit names in the unit viewer */ - private final JList<String> unitNameList; - /** The list of prefix names in the prefix viewer */ - private final JList<String> prefixNameList; - /** The unit search box in the unit viewer */ - private final JTextField unitFilterEntry; - /** The text box for unit data in the unit viewer */ - private final JTextArea unitTextBox; - /** The prefix search box in the prefix viewer */ - private final JTextField prefixFilterEntry; - /** The text box for prefix data in the prefix viewer */ - private final JTextArea prefixTextBox; + // DIMENSION-BASED CONVERTER + /** The panel for inputting values in the dimension-based converter */ + private final JTextField valueInput; /** The panel for "From" in the dimension-based converter */ private final SearchBoxList fromSearch; /** The panel for "To" in the dimension-based converter */ private final SearchBoxList toSearch; - /** The panel for inputting values in the dimension-based converter */ - private final JTextField valueInput; /** The output area in the dimension-based converter */ private final JTextArea dimensionBasedOutput; + + // EXPRESSION-BASED CONVERTER /** The "From" entry in the conversion panel */ private final JTextField fromEntry; /** The "To" entry in the conversion panel */ @@ -498,6 +404,16 @@ final class UnitConverterGUI { /** The output area in the conversion panel */ private final JTextArea output; + // UNIT AND PREFIX VIEWERS + /** The searchable list of unit names in the unit viewer */ + private final SearchBoxList unitNameList; + /** The searchable list of prefix names in the prefix viewer */ + private final SearchBoxList prefixNameList; + /** The text box for unit data in the unit viewer */ + private final JTextArea unitTextBox; + /** The text box for prefix data in the prefix viewer */ + private final JTextArea prefixTextBox; + /** * Creates the {@code View}. * @@ -510,11 +426,10 @@ final class UnitConverterGUI { this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // create the components - this.unitNameList = new JList<>(this.presenter.keyListModel()); - this.prefixNameList = new JList<>(this.presenter.prefixNameListModel()); - this.unitFilterEntry = new JTextField(); + this.unitNameList = new SearchBoxList(this.presenter.unitNameSet()); + this.prefixNameList = new SearchBoxList(this.presenter.prefixNameSet(), + this.presenter.getPrefixNameComparator(), true); this.unitTextBox = new JTextArea(); - this.prefixFilterEntry = new JTextField(); this.prefixTextBox = new JTextArea(); this.fromSearch = new SearchBoxList(this.presenter.unitNameSet()); this.toSearch = new SearchBoxList(this.presenter.unitNameSet()); @@ -534,7 +449,7 @@ final class UnitConverterGUI { * @return value in dimension-based converter * @since 2019-04-13 */ - public String getDimensionBasedInput() { + public String getDimensionConverterInput() { return this.valueInput.getText(); } @@ -556,21 +471,12 @@ final class UnitConverterGUI { } /** - * @return text in prefix filter + * @return index of selected prefix in prefix viewer * @since 2019-01-15 * @since v0.1.0 */ - public String getPrefixFilterText() { - return this.prefixFilterEntry.getText(); - } - - /** - * @return index of selected prefix - * @since 2019-01-15 - * @since v0.1.0 - */ - public int getPrefixListSelection() { - return this.prefixNameList.getSelectedIndex(); + public String getPrefixViewerSelection() { + return this.prefixNameList.getSelectedValue(); } /** @@ -591,20 +497,12 @@ final class UnitConverterGUI { } /** - * @return text in unit filter - * @see javax.swing.text.JTextComponent#getText() - */ - public String getUnitFilterText() { - return this.unitFilterEntry.getText(); - } - - /** - * @return index of selected unit + * @return index of selected unit in unit viewer * @since 2019-01-15 * @since v0.1.0 */ - public int getUnitListSelection() { - return this.unitNameList.getSelectedIndex(); + public String getUnitViewerSelection() { + return this.unitNameList.getSelectedValue(); } /** @@ -764,7 +662,7 @@ final class UnitConverterGUI { final JButton convertButton = new JButton("Convert!"); convertExpressionPanel.add(convertButton); - convertButton.addActionListener(e -> this.presenter.convert()); + convertButton.addActionListener(e -> this.presenter.convertExpressions()); } { // output of conversion @@ -808,24 +706,11 @@ final class UnitConverterGUI { unitLookupPanel.setLayout(new GridLayout()); - { // panel for listing and searching - final JPanel listPanel = new JPanel(); - unitLookupPanel.add(listPanel); - - listPanel.setLayout(new BorderLayout()); + { // search panel + unitLookupPanel.add(this.unitNameList); - { // unit search box - listPanel.add(this.unitFilterEntry, BorderLayout.PAGE_START); - this.unitFilterEntry.addCaretListener(e -> this.presenter.unitFilterUpdated()); - } - - { // a list of units - listPanel.add(new JScrollPane(this.unitNameList), BorderLayout.CENTER); - this.unitNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // temp - this.unitNameList.addListSelectionListener(e -> { - this.presenter.unitNameSelected(); - }); - } + this.unitNameList.getSearchList() + .addListSelectionListener(e -> this.presenter.unitNameSelected()); } { // the text box for unit's toString @@ -842,23 +727,10 @@ final class UnitConverterGUI { prefixLookupPanel.setLayout(new GridLayout(1, 2)); { // panel for listing and seaching - final JPanel prefixListPanel = new JPanel(); - prefixLookupPanel.add(prefixListPanel); - - prefixListPanel.setLayout(new BorderLayout()); - - { // prefix search box - prefixListPanel.add(this.prefixFilterEntry, BorderLayout.PAGE_START); - this.prefixFilterEntry.addCaretListener(e -> this.presenter.prefixFilterUpdated()); - } + prefixLookupPanel.add(this.prefixNameList); - { // a list of prefixes - prefixListPanel.add(new JScrollPane(this.prefixNameList), BorderLayout.CENTER); - this.prefixNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // temp - this.prefixNameList.addListSelectionListener(e -> { - this.presenter.prefixSelected(); - }); - } + this.prefixNameList.getSearchList() + .addListSelectionListener(e -> this.presenter.prefixSelected()); } { // the text box for prefix's toString @@ -876,7 +748,7 @@ final class UnitConverterGUI { * text to set * @since 2019-04-13 */ - public void setDimensionBasedOutputText(final String text) { + public void setDimensionConverterOutputText(final String text) { this.dimensionBasedOutput.setText(text); } @@ -888,12 +760,12 @@ final class UnitConverterGUI { * @since 2019-01-15 * @since v0.1.0 */ - public void setOutputText(final String text) { + public void setExpressionConverterOutputText(final String text) { this.output.setText(text); } /** - * Sets the text of the prefix text box. + * Sets the text of the prefix text box in the prefix viewer. * * @param text * text to set @@ -905,14 +777,15 @@ final class UnitConverterGUI { } /** - * Sets the text of the unit text box. + * Sets the text of the unit text box in the unit viewer. * - * @param t + * @param text * text to set - * @see javax.swing.text.JTextComponent#setText(java.lang.String) + * @since 2019-01-15 + * @since v0.1.0 */ - public void setUnitTextBoxText(final String t) { - this.unitTextBox.setText(t); + public void setUnitTextBoxText(final String text) { + this.unitTextBox.setText(text); } /** diff --git a/unitsfile.txt b/unitsfile.txt index 553fd5e..bda9b81 100755 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -138,7 +138,7 @@ radian m / m rad radian steradian m^2 / m^2 sr steradian -degree 360 / tau * radian +degree tau / 360 radian deg degree ° degree |