diff options
Diffstat (limited to 'src/unitConverter/converterGUI')
4 files changed, 164 insertions, 7 deletions
diff --git a/src/unitConverter/converterGUI/DelegateListModel.java b/src/unitConverter/converterGUI/DelegateListModel.java index 42bc0dc..0e9b342 100755 --- a/src/unitConverter/converterGUI/DelegateListModel.java +++ b/src/unitConverter/converterGUI/DelegateListModel.java @@ -32,10 +32,12 @@ import javax.swing.AbstractListModel; * * @author Adrien Hopkins * @since 2019-01-14 + * @since v0.1.0 */ final class DelegateListModel<E> extends AbstractListModel<E> implements List<E> { /** * @since 2019-01-14 + * @since v0.1.0 */ private static final long serialVersionUID = 8985494428224810045L; @@ -43,6 +45,7 @@ final class DelegateListModel<E> extends AbstractListModel<E> implements List<E> * The list that this model is a delegate to. * * @since 2019-01-14 + * @since v0.1.0 */ private final List<E> delegate; @@ -52,6 +55,7 @@ final class DelegateListModel<E> extends AbstractListModel<E> implements List<E> * @param delegate * list to delegate * @since 2019-01-14 + * @since v0.1.0 */ public DelegateListModel(final List<E> delegate) { this.delegate = delegate; diff --git a/src/unitConverter/converterGUI/FilterComparator.java b/src/unitConverter/converterGUI/FilterComparator.java index ab25f2f..27ec3ab 100755 --- a/src/unitConverter/converterGUI/FilterComparator.java +++ b/src/unitConverter/converterGUI/FilterComparator.java @@ -17,14 +17,29 @@ package unitConverter.converterGUI;
import java.util.Comparator;
+import java.util.Objects;
/**
+ * A comparator that compares strings using a filter.
+ *
* @author Adrien Hopkins
* @since 2019-01-15
+ * @since v0.1.0
*/
public final class FilterComparator implements Comparator<String> {
-
+ /**
+ * The filter that the comparator is filtered by.
+ *
+ * @since 2019-01-15
+ * @since v0.1.0
+ */
private final String filter;
+ /**
+ * The comparator to use if the arguments are otherwise equal.
+ *
+ * @since 2019-01-15
+ * @since v0.1.0
+ */
private final Comparator<String> comparator;
/**
@@ -32,6 +47,7 @@ public final class FilterComparator implements Comparator<String> { *
* @param filter
* @since 2019-01-15
+ * @since v0.1.0
*/
public FilterComparator(final String filter) {
this(filter, null);
@@ -41,11 +57,16 @@ public final class FilterComparator implements Comparator<String> { * Creates the {@code FilterComparator}.
*
* @param filter
+ * string to filter by
* @param comparator
+ * comparator to fall back to if all else fails, null is compareTo.
* @since 2019-01-15
+ * @since v0.1.0
+ * @throws NullPointerException
+ * if filter is null
*/
public FilterComparator(final String filter, final Comparator<String> comparator) {
- this.filter = filter;
+ this.filter = Objects.requireNonNull(filter, "filter must not be null.");
this.comparator = comparator;
}
diff --git a/src/unitConverter/converterGUI/GridBagBuilder.java b/src/unitConverter/converterGUI/GridBagBuilder.java index 7a0615a..e036677 100755 --- a/src/unitConverter/converterGUI/GridBagBuilder.java +++ b/src/unitConverter/converterGUI/GridBagBuilder.java @@ -20,11 +20,16 @@ import java.awt.GridBagConstraints; import java.awt.Insets; /** + * A builder for Java's {@link java.awt.GridBagConstraints} class. + * * @author Adrien Hopkins * @since 2018-11-30 + * @since v0.1.0 */ final class GridBagBuilder { /** + * The built {@code GridBagConstraints}'s {@code gridx} property. + * <p> * Specifies the cell containing the leading edge of the component's display area, where the first cell in a row has * <code>gridx=0</code>. The leading edge of a component's display area is its left edge for a horizontal, * left-to-right container and its right edge for a horizontal, right-to-left container. The value @@ -41,6 +46,8 @@ final class GridBagBuilder { private final int gridx; /** + * The built {@code GridBagConstraints}'s {@code gridy} property. + * <p> * Specifies the cell at the top of the component's display area, where the topmost cell has <code>gridy=0</code>. * The value <code>RELATIVE</code> specifies that the component be placed just below the component that was added to * the container just before this component was added. @@ -54,6 +61,8 @@ final class GridBagBuilder { private final int gridy; /** + * The built {@code GridBagConstraints}'s {@code gridwidth} property. + * <p> * Specifies the number of cells in a row for the component's display area. * <p> * Use <code>REMAINDER</code> to specify that the component's display area will be from <code>gridx</code> to the @@ -69,6 +78,8 @@ final class GridBagBuilder { private final int gridwidth; /** + * The built {@code GridBagConstraints}'s {@code gridheight} property. + * <p> * Specifies the number of cells in a column for the component's display area. * <p> * Use <code>REMAINDER</code> to specify that the component's display area will be from <code>gridy</code> to the @@ -84,6 +95,8 @@ final class GridBagBuilder { private final int gridheight; /** + * The built {@code GridBagConstraints}'s {@code weightx} property. + * <p> * Specifies how to distribute extra horizontal space. * <p> * The grid bag layout manager calculates the weight of a column to be the maximum <code>weightx</code> of all the @@ -103,6 +116,8 @@ final class GridBagBuilder { private double weightx; /** + * The built {@code GridBagConstraints}'s {@code weighty} property. + * <p> * Specifies how to distribute extra vertical space. * <p> * The grid bag layout manager calculates the weight of a row to be the maximum <code>weighty</code> of all the @@ -122,6 +137,8 @@ final class GridBagBuilder { private double weighty; /** + * The built {@code GridBagConstraints}'s {@code anchor} property. + * <p> * This field is used when the component is smaller than its display area. It determines where, within the display * area, to place the component. * <p> @@ -145,6 +162,8 @@ final class GridBagBuilder { private int anchor; /** + * The built {@code GridBagConstraints}'s {@code fill} property. + * <p> * This field is used when the component's display area is larger than the component's requested size. It determines * whether to resize the component, and if so, how. * <p> @@ -167,6 +186,8 @@ final class GridBagBuilder { private int fill; /** + * The built {@code GridBagConstraints}'s {@code insets} property. + * <p> * This field specifies the external padding of the component, the minimum amount of space between the component and * the edges of its display area. * <p> @@ -178,6 +199,8 @@ final class GridBagBuilder { private Insets insets; /** + * The built {@code GridBagConstraints}'s {@code ipadx} property. + * <p> * This field specifies the internal padding of the component, how much space to add to the minimum width of the * component. The width of the component is at least its minimum width plus <code>ipadx</code> pixels. * <p> @@ -190,6 +213,8 @@ final class GridBagBuilder { private int ipadx; /** + * The built {@code GridBagConstraints}'s {@code ipady} property. + * <p> * This field specifies the internal padding, that is, how much space to add to the minimum height of the component. * The height of the component is at least its minimum height plus <code>ipady</code> pixels. * <p> @@ -207,6 +232,7 @@ final class GridBagBuilder { * @param gridy * y position * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder(final int gridx, final int gridy) { this(gridx, gridy, 1, 1); @@ -222,6 +248,7 @@ final class GridBagBuilder { * @param gridheight * number of cells occupied vertically * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder(final int gridx, final int gridy, final int gridwidth, final int gridheight) { this(gridx, gridy, gridwidth, gridheight, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, @@ -245,6 +272,7 @@ final class GridBagBuilder { * @param ipadx * @param ipady * @since 2018-11-30 + * @since v0.1.0 */ private GridBagBuilder(final int gridx, final int gridy, final int gridwidth, final int gridheight, final double weightx, final double weighty, final int anchor, final int fill, final Insets insets, @@ -266,6 +294,7 @@ final class GridBagBuilder { /** * @return {@code GridBagConstraints} created by this builder * @since 2018-11-30 + * @since v0.1.0 */ public GridBagConstraints build() { return new GridBagConstraints(this.gridx, this.gridy, this.gridwidth, this.gridheight, this.weightx, @@ -275,6 +304,7 @@ final class GridBagBuilder { /** * @return anchor * @since 2018-11-30 + * @since v0.1.0 */ public int getAnchor() { return this.anchor; @@ -283,6 +313,7 @@ final class GridBagBuilder { /** * @return fill * @since 2018-11-30 + * @since v0.1.0 */ public int getFill() { return this.fill; @@ -291,6 +322,7 @@ final class GridBagBuilder { /** * @return gridheight * @since 2018-11-30 + * @since v0.1.0 */ public int getGridheight() { return this.gridheight; @@ -299,6 +331,7 @@ final class GridBagBuilder { /** * @return gridwidth * @since 2018-11-30 + * @since v0.1.0 */ public int getGridwidth() { return this.gridwidth; @@ -307,6 +340,7 @@ final class GridBagBuilder { /** * @return gridx * @since 2018-11-30 + * @since v0.1.0 */ public int getGridx() { return this.gridx; @@ -315,6 +349,7 @@ final class GridBagBuilder { /** * @return gridy * @since 2018-11-30 + * @since v0.1.0 */ public int getGridy() { return this.gridy; @@ -323,6 +358,7 @@ final class GridBagBuilder { /** * @return insets * @since 2018-11-30 + * @since v0.1.0 */ public Insets getInsets() { return this.insets; @@ -331,6 +367,7 @@ final class GridBagBuilder { /** * @return ipadx * @since 2018-11-30 + * @since v0.1.0 */ public int getIpadx() { return this.ipadx; @@ -339,6 +376,7 @@ final class GridBagBuilder { /** * @return ipady * @since 2018-11-30 + * @since v0.1.0 */ public int getIpady() { return this.ipady; @@ -347,6 +385,7 @@ final class GridBagBuilder { /** * @return weightx * @since 2018-11-30 + * @since v0.1.0 */ public double getWeightx() { return this.weightx; @@ -355,6 +394,7 @@ final class GridBagBuilder { /** * @return weighty * @since 2018-11-30 + * @since v0.1.0 */ public double getWeighty() { return this.weighty; @@ -364,6 +404,7 @@ final class GridBagBuilder { * @param anchor * anchor to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setAnchor(final int anchor) { this.anchor = anchor; @@ -374,6 +415,7 @@ final class GridBagBuilder { * @param fill * fill to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setFill(final int fill) { this.fill = fill; @@ -384,6 +426,7 @@ final class GridBagBuilder { * @param insets * insets to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setInsets(final Insets insets) { this.insets = insets; @@ -394,6 +437,7 @@ final class GridBagBuilder { * @param ipadx * ipadx to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setIpadx(final int ipadx) { this.ipadx = ipadx; @@ -404,6 +448,7 @@ final class GridBagBuilder { * @param ipady * ipady to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setIpady(final int ipady) { this.ipady = ipady; @@ -414,6 +459,7 @@ final class GridBagBuilder { * @param weightx * weightx to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setWeightx(final double weightx) { this.weightx = weightx; @@ -424,6 +470,7 @@ final class GridBagBuilder { * @param weighty * weighty to set * @since 2018-11-30 + * @since v0.1.0 */ public GridBagBuilder setWeighty(final double weighty) { this.weighty = weighty; diff --git a/src/unitConverter/converterGUI/UnitConverterGUI.java b/src/unitConverter/converterGUI/UnitConverterGUI.java index 0068312..b54e0da 100755 --- a/src/unitConverter/converterGUI/UnitConverterGUI.java +++ b/src/unitConverter/converterGUI/UnitConverterGUI.java @@ -52,6 +52,7 @@ import unitConverter.unit.UnitPrefix; /** * @author Adrien Hopkins * @since 2018-12-27 + * @since v0.1.0 */ final class UnitConverterGUI { private static class Presenter { @@ -83,6 +84,7 @@ final class UnitConverterGUI { * @param view * presenter's associated view * @since 2018-12-27 + * @since v0.1.0 */ Presenter(final View view) { this.view = view; @@ -141,6 +143,17 @@ final class UnitConverterGUI { AbstractUnit.getBaseUnitCount()); } + /** + * Runs whenever the convert button is pressed. + * + * <p> + * 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. + * </p> + * + * @since 2019-01-26 + * @since v0.1.0 + */ public final void convert() { final String fromUnitString = this.view.getFromText(); final String toUnitString = this.view.getToText(); @@ -196,6 +209,7 @@ final class UnitConverterGUI { * @param filter * filter to use * @since 2019-01-15 + * @since v0.1.0 */ private final void filterFilteredPrefixModel(final Predicate<String> filter) { this.prefixNamesFiltered.clear(); @@ -212,6 +226,7 @@ final class UnitConverterGUI { * @param filter * filter to use * @since 2019-01-15 + * @since v0.1.0 */ private final void filterFilteredUnitModel(final Predicate<String> filter) { this.unitNamesFiltered.clear(); @@ -225,11 +240,21 @@ final class UnitConverterGUI { /** * @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("")) { @@ -241,13 +266,22 @@ final class UnitConverterGUI { } /** - * @return a list model of all fo the prefix names + * @return a list model of all of the prefix names * @since 2019-01-15 */ public final ListModel<String> prefixNameListModel() { return this.prefixNamesFiltered; } + /** + * Runs whenever a prefix is selected in the viewer. + * <p> + * Shows its information in the text box to the right. + * </p> + * + * @since 2019-01-15 + * @since v0.1.0 + */ public final void prefixSelected() { final int index = this.view.getPrefixListSelection(); if (index == -1) @@ -269,6 +303,15 @@ final class UnitConverterGUI { this.significantFigures = significantFigures; } + /** + * 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("")) { @@ -280,8 +323,13 @@ final class UnitConverterGUI { } /** + * Runs whenever a unit is selected in the viewer. + * <p> + * Shows its information in the text box to the right. + * </p> * * @since 2019-01-15 + * @since v0.1.0 */ public void unitNameSelected() { final int index = this.view.getUnitListSelection(); @@ -302,26 +350,37 @@ 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; + /** The "From" entry in the conversion panel */ private final JTextField fromEntry; + /** The "To" entry in the conversion panel */ private final JTextField toEntry; + /** The output area in the conversion panel */ private final JTextArea output; /** * Creates the {@code View}. * * @since 2019-01-14 + * @since v0.1.0 */ public View() { this.presenter = new Presenter(this); this.frame = new JFrame("Unit Converter"); 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(); @@ -332,11 +391,17 @@ final class UnitConverterGUI { this.toEntry = new JTextField(); this.output = new JTextArea(2, 32); + // create more components this.initComponents(); this.frame.pack(); } + /** + * @return text in "From" box in converter panel + * @since 2019-01-15 + * @since v0.1.0 + */ public String getFromText() { return this.fromEntry.getText(); } @@ -344,6 +409,7 @@ final class UnitConverterGUI { /** * @return text in prefix filter * @since 2019-01-15 + * @since v0.1.0 */ public String getPrefixFilterText() { return this.prefixFilterEntry.getText(); @@ -352,11 +418,17 @@ final class UnitConverterGUI { /** * @return index of selected prefix * @since 2019-01-15 + * @since v0.1.0 */ public int getPrefixListSelection() { return this.prefixNameList.getSelectedIndex(); } + /** + * @return text in "To" box in converter panel + * @since 2019-01-26 + * @since v0.1.0 + */ public String getToText() { return this.toEntry.getText(); } @@ -372,6 +444,7 @@ final class UnitConverterGUI { /** * @return index of selected unit * @since 2019-01-15 + * @since v0.1.0 */ public int getUnitListSelection() { return this.unitNameList.getSelectedIndex(); @@ -381,6 +454,7 @@ final class UnitConverterGUI { * Starts up the application. * * @since 2018-12-27 + * @since v0.1.0 */ public final void init() { this.frame.setVisible(true); @@ -390,6 +464,7 @@ final class UnitConverterGUI { * Initializes the view's components. * * @since 2018-12-27 + * @since v0.1.0 */ private final void initComponents() { final JPanel masterPanel = new JPanel(); @@ -451,7 +526,7 @@ final class UnitConverterGUI { } } - { + { // panel for specifying precision final JPanel sigDigPanel = new JPanel(); convertPanel.add(sigDigPanel); @@ -485,7 +560,7 @@ final class UnitConverterGUI { listPanel.setLayout(new BorderLayout()); - { + { // unit search box listPanel.add(this.unitFilterEntry, BorderLayout.PAGE_START); this.unitFilterEntry.addCaretListener(e -> this.presenter.unitFilterUpdated()); } @@ -512,13 +587,13 @@ 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()); } @@ -540,6 +615,14 @@ final class UnitConverterGUI { } } + /** + * Sets the text in the output of the conversion panel. + * + * @param text + * text to set + * @since 2019-01-15 + * @since v0.1.0 + */ public void setOutputText(final String text) { this.output.setText(text); } @@ -550,6 +633,7 @@ final class UnitConverterGUI { * @param text * text to set * @since 2019-01-15 + * @since v0.1.0 */ public void setPrefixTextBoxText(final String text) { this.prefixTextBox.setText(text); @@ -574,6 +658,7 @@ final class UnitConverterGUI { * @param message * message in dialog * @since 2019-01-14 + * @since v0.1.0 */ public void showErrorDialog(final String title, final String message) { JOptionPane.showMessageDialog(this.frame, message, title, JOptionPane.ERROR_MESSAGE); |