From 168853a6513f0e3c7b57876a6a253b31ac5f66f9 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 6 Jun 2020 10:13:35 -0500 Subject: Added keyboard shortcuts to the tabs and convert buttons. --- .../converterGUI/UnitConverterGUI.java | 100 +++++++++++++-------- 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/src/org/unitConverter/converterGUI/UnitConverterGUI.java b/src/org/unitConverter/converterGUI/UnitConverterGUI.java index b84e730..0230728 100644 --- a/src/org/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/org/unitConverter/converterGUI/UnitConverterGUI.java @@ -18,6 +18,7 @@ package org.unitConverter.converterGUI; import java.awt.BorderLayout; import java.awt.GridLayout; +import java.awt.event.KeyEvent; import java.io.File; import java.math.BigDecimal; import java.math.MathContext; @@ -41,6 +42,7 @@ import javax.swing.JSlider; import javax.swing.JTabbedPane; import javax.swing.JTextArea; import javax.swing.JTextField; +import javax.swing.WindowConstants; import org.unitConverter.math.ObjectProduct; import org.unitConverter.unit.BaseDimension; @@ -57,12 +59,18 @@ import org.unitConverter.unit.UnitPrefix; * @since v0.1.0 */ final class UnitConverterGUI { + /** + * A tab in the View. + */ + private enum Pane { + UNIT_CONVERTER, EXPRESSION_CONVERTER, UNIT_VIEWER, PREFIX_VIEWER; + } + private static class Presenter { /** * Adds default units and dimensions to a database. * - * @param database - * database to add to + * @param database database to add to * @since 2019-04-14 * @since v0.2.0 */ @@ -110,8 +118,7 @@ final class UnitConverterGUI { /** * Creates the presenter. * - * @param view - * presenter's associated view + * @param view presenter's associated view * @since 2018-12-27 * @since v0.1.0 */ @@ -203,8 +210,8 @@ final class UnitConverterGUI { * Runs whenever the convert button is pressed. * *

- * Reads and parses a unit expression from the from and to boxes, then converts {@code from} to {@code to}. Any - * errors are shown in JOptionPanes. + * Reads and parses a unit expression from the from and to boxes, then converts + * {@code from} to {@code to}. Any errors are shown in JOptionPanes. *

* * @since 2019-01-26 @@ -269,7 +276,6 @@ final class UnitConverterGUI { this.view.showErrorDialog("Conversion Error", String.format("Cannot convert between %s and %s", fromUnitString, toUnitString)); } - } /** @@ -291,9 +297,9 @@ final class UnitConverterGUI { } /** - * @param value - * value to round - * @return string of that value rounded to {@code significantDigits} significant digits. + * @param value value to round + * @return string of that value rounded to {@code significantDigits} significant + * digits. * @since 2019-04-14 * @since v0.2.0 */ @@ -345,8 +351,7 @@ final class UnitConverterGUI { } /** - * @param significantFigures - * new value of significantFigures + * @param significantFigures new value of significantFigures * @since 2019-01-15 * @since v0.1.0 */ @@ -355,13 +360,11 @@ final class UnitConverterGUI { } /** - * Returns true if and only if the unit represented by {@code unitName} has the dimension represented by - * {@code dimensionName}. + * Returns true if and only if the unit represented by {@code unitName} has the + * dimension represented by {@code dimensionName}. * - * @param unitName - * name of unit to test - * @param dimensionName - * name of dimension to test + * @param unitName name of unit to test + * @param dimensionName name of dimension to test * @return whether unit has dimenision * @since 2019-04-13 * @since v0.2.0 @@ -407,6 +410,8 @@ final class UnitConverterGUI { private final JFrame frame; /** The view's associated presenter. */ private final Presenter presenter; + /** The master pane containing all of the tabs. */ + private final JTabbedPane masterPane; // DIMENSION-BASED CONVERTER /** The panel for inputting values in the dimension-based converter */ @@ -445,7 +450,8 @@ final class UnitConverterGUI { public View() { this.presenter = new Presenter(this); this.frame = new JFrame("Unit Converter"); - this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + this.masterPane = new JTabbedPane(); // create the components this.unitNameList = new SearchBoxList(this.presenter.unitNameSet()); @@ -467,6 +473,25 @@ final class UnitConverterGUI { this.frame.pack(); } + /** + * @return the currently selected pane. + * @throws AssertionError if no pane (or an invalid pane) is selected + */ + public Pane getActivePane() { + switch (this.masterPane.getSelectedIndex()) { + case 0: + return Pane.UNIT_CONVERTER; + case 1: + return Pane.EXPRESSION_CONVERTER; + case 2: + return Pane.UNIT_VIEWER; + case 3: + return Pane.PREFIX_VIEWER; + default: + throw new AssertionError("No selected pane, or invalid pane."); + } + } + /** * @return value in dimension-based converter * @since 2019-04-13 @@ -553,12 +578,12 @@ final class UnitConverterGUI { masterPanel.setLayout(new BorderLayout()); { // pane with all of the tabs - final JTabbedPane masterPane = new JTabbedPane(); - masterPanel.add(masterPane, BorderLayout.CENTER); + masterPanel.add(this.masterPane, BorderLayout.CENTER); { // a panel for unit conversion using a selector final JPanel convertUnitPanel = new JPanel(); - masterPane.addTab("Convert Units", convertUnitPanel); + this.masterPane.addTab("Convert Units", convertUnitPanel); + this.masterPane.setMnemonicAt(0, KeyEvent.VK_U); convertUnitPanel.setLayout(new BorderLayout()); @@ -644,6 +669,7 @@ final class UnitConverterGUI { outputPanel.add(convertButton); convertButton.addActionListener(e -> this.presenter.convertDimensionBased()); + convertButton.setMnemonic(KeyEvent.VK_ENTER); } { // output of conversion @@ -655,7 +681,8 @@ final class UnitConverterGUI { { // panel for unit conversion using expressions final JPanel convertExpressionPanel = new JPanel(); - masterPane.addTab("Convert Unit Expressions", convertExpressionPanel); + this.masterPane.addTab("Convert Unit Expressions", convertExpressionPanel); + this.masterPane.setMnemonicAt(1, KeyEvent.VK_E); convertExpressionPanel.setLayout(new GridLayout(5, 1)); @@ -684,10 +711,11 @@ final class UnitConverterGUI { } { // button to convert - final JButton convertButton = new JButton("Convert!"); + final JButton convertButton = new JButton("Convert"); convertExpressionPanel.add(convertButton); convertButton.addActionListener(e -> this.presenter.convertExpressions()); + convertButton.setMnemonic(KeyEvent.VK_ENTER); } { // output of conversion @@ -727,7 +755,8 @@ final class UnitConverterGUI { { // panel to look up units final JPanel unitLookupPanel = new JPanel(); - masterPane.addTab("Unit Viewer", unitLookupPanel); + this.masterPane.addTab("Unit Viewer", unitLookupPanel); + this.masterPane.setMnemonicAt(2, KeyEvent.VK_V); unitLookupPanel.setLayout(new GridLayout()); @@ -747,7 +776,8 @@ final class UnitConverterGUI { { // panel to look up prefixes final JPanel prefixLookupPanel = new JPanel(); - masterPane.addTab("Prefix Viewer", prefixLookupPanel); + this.masterPane.addTab("Prefix Viewer", prefixLookupPanel); + this.masterPane.setMnemonicAt(3, KeyEvent.VK_P); prefixLookupPanel.setLayout(new GridLayout(1, 2)); @@ -770,8 +800,7 @@ final class UnitConverterGUI { /** * Sets the text in the output of the dimension-based converter. * - * @param text - * text to set + * @param text text to set * @since 2019-04-13 * @since v0.2.0 */ @@ -782,8 +811,7 @@ final class UnitConverterGUI { /** * Sets the text in the output of the conversion panel. * - * @param text - * text to set + * @param text text to set * @since 2019-01-15 * @since v0.1.0 */ @@ -794,8 +822,7 @@ final class UnitConverterGUI { /** * Sets the text of the prefix text box in the prefix viewer. * - * @param text - * text to set + * @param text text to set * @since 2019-01-15 * @since v0.1.0 */ @@ -806,8 +833,7 @@ final class UnitConverterGUI { /** * Sets the text of the unit text box in the unit viewer. * - * @param text - * text to set + * @param text text to set * @since 2019-01-15 * @since v0.1.0 */ @@ -818,10 +844,8 @@ final class UnitConverterGUI { /** * Shows an error dialog. * - * @param title - * title of dialog - * @param message - * message in dialog + * @param title title of dialog + * @param message message in dialog * @since 2019-01-14 * @since v0.1.0 */ -- cgit v1.2.3