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 /src/org/unitConverter/converterGUI/UnitConverterGUI.java | |
parent | fc1083454e4e9215140802602a17aafeef4515fa (diff) |
The unit and prefix viewers now use SearchBoxList.
Diffstat (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java')
-rwxr-xr-x | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 401 |
1 files changed, 137 insertions, 264 deletions
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); } /** |