diff options
Diffstat (limited to 'src/org/unitConverter/converterGUI/UnitConverterGUI.java')
-rw-r--r-- | src/org/unitConverter/converterGUI/UnitConverterGUI.java | 89 |
1 files changed, 75 insertions, 14 deletions
diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index 5fe4ee5..75ab16d 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -56,6 +56,7 @@ import javax.swing.JTextField; import javax.swing.WindowConstants; import javax.swing.border.TitledBorder; +import org.unitConverter.math.ConditionalExistenceCollections; import org.unitConverter.math.ObjectProduct; import org.unitConverter.unit.BaseDimension; import org.unitConverter.unit.BritishImperial; @@ -129,6 +130,13 @@ final class UnitConverterGUI { private final Comparator<String> prefixNameComparator; + // conditions for existence of From and To entries + // used for one-way conversion + private final MutablePredicate<String> fromExistenceCondition = new MutablePredicate<>( + s -> true); + private final MutablePredicate<String> toExistenceCondition = new MutablePredicate<>( + s -> true); + /* * Rounding-related settings. I am using my own system, and not * MathContext, because MathContext does not support decimal place based @@ -338,6 +346,16 @@ final class UnitConverterGUI { } /** + * @return a list of all the entries in the dimension-based converter's + * From box + * @since 2020-08-27 + */ + public final Set<String> fromEntries() { + return ConditionalExistenceCollections.conditionalExistenceSet( + this.unitNameSet(), this.fromExistenceCondition); + } + + /** * @return a comparator to compare prefix names * @since 2019-04-14 * @since v0.2.0 @@ -437,6 +455,25 @@ final class UnitConverterGUI { } /** + * Enables or disables one-way conversion. + * + * @param oneWay whether one-way conversion should be on (true) or off + * (false) + * @since 2020-08-27 + */ + public final void setOneWay(boolean oneWay) { + if (oneWay) { + this.fromExistenceCondition.setPredicate( + unitName -> !this.database.getUnit(unitName).isMetric()); + this.toExistenceCondition.setPredicate( + unitName -> this.database.getUnit(unitName).isMetric()); + } else { + this.fromExistenceCondition.setPredicate(unitName -> true); + this.toExistenceCondition.setPredicate(unitName -> true); + } + } + + /** * @param precision new value of precision * @since 2019-01-15 * @since v0.1.0 @@ -463,6 +500,16 @@ final class UnitConverterGUI { } /** + * @return a list of all the entries in the dimension-based converter's To + * box + * @since 2020-08-27 + */ + public final Set<String> toEntries() { + return ConditionalExistenceCollections.conditionalExistenceSet( + this.unitNameSet(), this.toExistenceCondition); + } + + /** * Returns true if and only if the unit represented by {@code unitName} * has the dimension represented by {@code dimensionName}. * @@ -505,7 +552,7 @@ final class UnitConverterGUI { * @since 2019-04-14 * @since v0.2.0 */ - public final Set<String> unitNameSet() { + private final Set<String> unitNameSet() { return this.database.unitMapPrefixless().keySet(); } } @@ -579,8 +626,8 @@ final class UnitConverterGUI { this.presenter.getPrefixNameComparator(), true); this.unitTextBox = new JTextArea(); this.prefixTextBox = new JTextArea(); - this.fromSearch = new SearchBoxList(this.presenter.unitNameSet()); - this.toSearch = new SearchBoxList(this.presenter.unitNameSet()); + this.fromSearch = new SearchBoxList(this.presenter.fromEntries()); + this.toSearch = new SearchBoxList(this.presenter.toEntries()); this.valueInput = new JFormattedTextField(NUMBER_FORMATTER); this.dimensionBasedOutput = new JTextArea(2, 32); this.fromEntry = new JTextField(); @@ -630,15 +677,6 @@ final class UnitConverterGUI { } /** - * @return text inputted into dimension-based converter - * @since 2019-04-13 - * @since v0.2.0 - */ - public String getDimensionConverterText() { - return this.valueInput.getText(); - } - - /** * @return selection in "From" selector in dimension-based converter * @since 2019-04-13 * @since v0.2.0 @@ -717,6 +755,9 @@ final class UnitConverterGUI { { // pane with all of the tabs masterPanel.add(this.masterPane, BorderLayout.CENTER); + // update stuff + this.masterPane.addChangeListener(e -> this.update()); + { // a panel for unit conversion using a selector final JPanel convertUnitPanel = new JPanel(); this.masterPane.addTab("Convert Units", convertUnitPanel); @@ -1080,17 +1121,25 @@ final class UnitConverterGUI { .setBorder(new TitledBorder("Miscellaneous Settings")); miscPanel.setLayout(new GridBagLayout()); + final JCheckBox oneWay = new JCheckBox( + "Convert One Way Only"); + oneWay.setSelected(false); + oneWay.addItemListener( + e -> this.presenter.setOneWay(e.getStateChange() == 1)); + miscPanel.add(oneWay, new GridBagBuilder(0, 0) + .setAnchor(GridBagConstraints.LINE_START).build()); + final JCheckBox showAllVariations = new JCheckBox( "Show Symbols in \"Convert Units\""); showAllVariations.setSelected(true); showAllVariations.setEnabled(false); - miscPanel.add(showAllVariations, new GridBagBuilder(0, 0) + miscPanel.add(showAllVariations, new GridBagBuilder(0, 1) .setAnchor(GridBagConstraints.LINE_START).build()); final JButton unitFileButton = new JButton( "Manage Unit Data Files"); unitFileButton.setEnabled(false); - miscPanel.add(unitFileButton, new GridBagBuilder(0, 1) + miscPanel.add(unitFileButton, new GridBagBuilder(0, 2) .setAnchor(GridBagConstraints.LINE_START).build()); } } @@ -1153,6 +1202,18 @@ final class UnitConverterGUI { JOptionPane.showMessageDialog(this.frame, message, title, JOptionPane.ERROR_MESSAGE); } + + public void update() { + switch (this.getActivePane()) { + case UNIT_CONVERTER: + this.fromSearch.updateList(); + this.toSearch.updateList(); + break; + default: + // do nothing, for now + break; + } + } } public static void main(final String[] args) { |