summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrien Hopkins <ahopk127@my.yorku.ca>2022-04-10 14:22:29 -0500
committerAdrien Hopkins <ahopk127@my.yorku.ca>2022-04-10 14:22:29 -0500
commitb1affe92460637211f560d70ee5c8770f3952822 (patch)
tree2fe58d9280794cd4c65e2f76e8068c9fd49bbc2c /src
parentc421e474a7b0d0d453e4a527907f327f2ddef320 (diff)
Created API for settings and unit/prefix viewing
Diffstat (limited to 'src')
-rw-r--r--src/main/java/sevenUnits/unit/Unit.java14
-rw-r--r--src/main/java/sevenUnits/unit/UnitType.java34
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java60
-rw-r--r--src/main/java/sevenUnitsGUI/TabbedView.java36
-rw-r--r--src/main/java/sevenUnitsGUI/UnitConversionView.java2
-rw-r--r--src/main/java/sevenUnitsGUI/View.java56
-rw-r--r--src/main/java/sevenUnitsGUI/ViewBot.java35
7 files changed, 219 insertions, 18 deletions
diff --git a/src/main/java/sevenUnits/unit/Unit.java b/src/main/java/sevenUnits/unit/Unit.java
index b80ccbd..826b59b 100644
--- a/src/main/java/sevenUnits/unit/Unit.java
+++ b/src/main/java/sevenUnits/unit/Unit.java
@@ -24,7 +24,6 @@ import java.util.function.DoubleUnaryOperator;
import sevenUnits.utils.DecimalComparison;
import sevenUnits.utils.NameSymbol;
import sevenUnits.utils.Nameable;
-import sevenUnits.utils.NamedObjectProduct;
import sevenUnits.utils.ObjectProduct;
/**
@@ -191,7 +190,7 @@ public abstract class Unit implements Nameable {
*
* @implSpec This method is used by {@link #convertTo}, and its behaviour
* affects the behaviour of {@code convertTo}.
- *
+ *
* @param value value expressed in <b>base</b> unit
* @return value expressed in <b>this</b> unit
* @since 2018-12-22
@@ -207,7 +206,7 @@ public abstract class Unit implements Nameable {
* {@code other.convertFromBase(this.convertToBase(value))}.
* Therefore, overriding either of those methods will change the
* output of this method.
- *
+ *
* @param other unit to convert to
* @param value value to convert
* @return converted value
@@ -234,7 +233,7 @@ public abstract class Unit implements Nameable {
* {@code other.convertFromBase(this.convertToBase(value))}.
* Therefore, overriding either of those methods will change the
* output of this method.
- *
+ *
* @param other unitlike form to convert to
* @param value value to convert
* @param <W> type of value to convert to
@@ -269,7 +268,7 @@ public abstract class Unit implements Nameable {
*
* @implSpec This method is used by {@link #convertTo}, and its behaviour
* affects the behaviour of {@code convertTo}.
- *
+ *
* @param value value expressed in <b>this</b> unit
* @return value expressed in <b>base</b> unit
* @since 2018-12-22
@@ -355,9 +354,8 @@ public abstract class Unit implements Nameable {
* @since 2022-03-10
*/
public String toDefinitionString() {
- if (this.unitBase instanceof NamedObjectProduct)
- return "derived from "
- + ((NamedObjectProduct<?>) this.unitBase).getName();
+ if (this.unitBase instanceof Nameable)
+ return "derived from " + ((Nameable) this.unitBase).getName();
else
return "derived from "
+ this.getBase().toString(BaseUnit::getShortName);
diff --git a/src/main/java/sevenUnits/unit/UnitType.java b/src/main/java/sevenUnits/unit/UnitType.java
new file mode 100644
index 0000000..a13051a
--- /dev/null
+++ b/src/main/java/sevenUnits/unit/UnitType.java
@@ -0,0 +1,34 @@
+/**
+ * Copyright (C) 2022 Adrien Hopkins
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package sevenUnits.unit;
+
+/**
+ * A type of unit, as chosen by the type of system it is in.
+ * <ul>
+ * <li>{@code METRIC} refers to metric/SI units that pass {@link Unit#isMetric}
+ * <li>{@code SEMI_METRIC} refers to the degree Celsius (which is an official SI
+ * unit but does not pass {@link Unit#isMetric}) and non-metric units intended
+ * for use with the SI.
+ * <li>{@code NON_METRIC} refers to units that are neither metric nor intended
+ * for use with the metric system (e.g. imperial and customary units)
+ * </ul>
+ *
+ * @since 2022-04-10
+ */
+public enum UnitType {
+ METRIC, SEMI_METRIC, NON_METRIC;
+}
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 9f8fe69..b38f90b 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -91,7 +91,7 @@ public final class Presenter {
* @return text in About file
* @since 2022-02-19
*/
- static final String getAboutText() {
+ public static final String getAboutText() {
return Presenter.getLinesFromResource("/about.txt").stream()
.map(Presenter::withoutComments).collect(Collectors.joining("\n"))
.replaceAll("\\[VERSION\\]", ProgramInfo.VERSION.toString());
@@ -155,7 +155,7 @@ public final class Presenter {
/**
* The rule used for parsing input numbers. Any number-string inputted into
- * this program will be parsed using this method.
+ * this program will be parsed using this method. <b>Not implemented yet.</b>
*/
private Function<String, UncertainDouble> numberParsingRule;
@@ -381,6 +381,25 @@ public final class Presenter {
}
/**
+ * @return the rule that is used by this presenter to convert numbers into
+ * strings
+ * @since 2022-04-10
+ */
+ public Function<UncertainDouble, String> getNumberDisplayRule() {
+ return this.numberDisplayRule;
+ }
+
+ /**
+ * @return the rule that is used by this presenter to convert strings into
+ * numbers
+ * @since 2022-04-10
+ */
+ @SuppressWarnings("unused") // not implemented yet
+ private Function<String, UncertainDouble> getNumberParsingRule() {
+ return this.numberParsingRule;
+ }
+
+ /**
* @return true iff the One-Way Conversion feature is available (views that
* show units as a list will have metric units removed from the From
* unit list and imperial/USC units removed from the To unit list)
@@ -392,11 +411,12 @@ public final class Presenter {
}
/**
- * Loads settings from the user's settings file and applies them to the view.
+ * Loads settings from the user's settings file and applies them to the
+ * presenter.
*
* @since 2021-12-15
*/
- public void loadSettings() {}
+ private void loadSettings() {}
/**
* Completes creation of the presenter. This part of the initialization
@@ -416,12 +436,32 @@ public final class Presenter {
void prefixSelected() {}
/**
- * Gets user settings from the view then saves them to the user's settings
- * file.
+ * Saves the presenter's settings to the user settings file.
*
* @since 2021-12-15
*/
- public void saveSettings() {}
+ private void saveSettings() {}
+
+ /**
+ * @param numberDisplayRule the new rule that will be used by this presenter
+ * to convert numbers into strings
+ * @since 2022-04-10
+ */
+ public void setNumberDisplayRule(
+ Function<UncertainDouble, String> numberDisplayRule) {
+ this.numberDisplayRule = numberDisplayRule;
+ }
+
+ /**
+ * @param numberParsingRule the new rule that will be used by this presenter
+ * to convert strings into numbers
+ * @since 2022-04-10
+ */
+ @SuppressWarnings("unused") // not implemented yet
+ private void setNumberParsingRule(
+ Function<String, UncertainDouble> numberParsingRule) {
+ this.numberParsingRule = numberParsingRule;
+ }
/**
* @param oneWayConversionEnabled whether not one-way conversion should be
@@ -443,6 +483,12 @@ public final class Presenter {
this.updateView();
}
+ /**
+ * Runs whenever a unit name is selected in the unit viewer. Gets the
+ * description of a unit and displays it.
+ *
+ * @since 2022-04-10
+ */
void unitNameSelected() {}
/**
diff --git a/src/main/java/sevenUnitsGUI/TabbedView.java b/src/main/java/sevenUnitsGUI/TabbedView.java
index ed45011..fd48965 100644
--- a/src/main/java/sevenUnitsGUI/TabbedView.java
+++ b/src/main/java/sevenUnitsGUI/TabbedView.java
@@ -56,6 +56,8 @@ import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import sevenUnits.ProgramInfo;
+import sevenUnits.unit.UnitType;
+import sevenUnits.utils.NameSymbol;
/**
* A View that separates its functions into multiple tabs
@@ -306,6 +308,8 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
this.prefixTextBox.setEditable(false);
this.prefixTextBox.setLineWrap(true);
+ // ============ INFO PANEL ============
+
final JPanel infoPanel = new JPanel();
this.masterPane.addTab("\uD83D\uDEC8", // info (i) character
new JScrollPane(infoPanel));
@@ -573,6 +577,16 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
}
@Override
+ public Optional<String> getViewedPrefixName() {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public Optional<String> getViewedUnitName() {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void setDimensionNames(Set<String> dimensionNames) {
this.dimensionSelector.removeAllItems();
for (final String d : dimensionNames) {
@@ -591,6 +605,16 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
}
@Override
+ public void setViewablePrefixNames(Set<String> prefixNames) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public void setViewableUnitNames(Set<String> unitNames) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void showErrorMessage(String title, String message) {
JOptionPane.showMessageDialog(this.frame, message, title,
JOptionPane.ERROR_MESSAGE);
@@ -603,8 +627,18 @@ final class TabbedView implements ExpressionConversionView, UnitConversionView {
}
@Override
+ public void showPrefix(NameSymbol name, String multiplierString) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public void showUnit(NameSymbol name, String definition,
+ String dimensionName, UnitType type) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void showUnitConversionOutput(UnitConversionRecord uc) {
this.unitOutput.setText(uc.toString());
}
-
}
diff --git a/src/main/java/sevenUnitsGUI/UnitConversionView.java b/src/main/java/sevenUnitsGUI/UnitConversionView.java
index 9d3a67b..6a95aa5 100644
--- a/src/main/java/sevenUnitsGUI/UnitConversionView.java
+++ b/src/main/java/sevenUnitsGUI/UnitConversionView.java
@@ -1,5 +1,5 @@
/**
- * Copyright (C) 2021 Adrien Hopkins
+ * Copyright (C) 2021-2022 Adrien Hopkins
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
diff --git a/src/main/java/sevenUnitsGUI/View.java b/src/main/java/sevenUnitsGUI/View.java
index e78c9cc..da3749e 100644
--- a/src/main/java/sevenUnitsGUI/View.java
+++ b/src/main/java/sevenUnitsGUI/View.java
@@ -16,6 +16,12 @@
*/
package sevenUnitsGUI;
+import java.util.Optional;
+import java.util.Set;
+
+import sevenUnits.unit.UnitType;
+import sevenUnits.utils.NameSymbol;
+
/**
* An object that controls user interaction with 7Units
*
@@ -24,6 +30,35 @@ package sevenUnitsGUI;
*/
public interface View {
/**
+ * @return name of prefix currently being viewed
+ * @since 2022-04-10
+ */
+ Optional<String> getViewedPrefixName();
+
+ /**
+ * @return name of unit currently being viewed
+ * @since 2022-04-10
+ */
+ Optional<String> getViewedUnitName();
+
+ /**
+ * Sets the list of prefixes that are available to be viewed in a prefix
+ * viewer
+ *
+ * @param prefixNames prefix names to view
+ * @since 2022-04-10
+ */
+ void setViewablePrefixNames(Set<String> prefixNames);
+
+ /**
+ * Sets the list of units that are available to be viewed in a unit viewer
+ *
+ * @param unitNames unit names to view
+ * @since 2022-04-10
+ */
+ void setViewableUnitNames(Set<String> unitNames);
+
+ /**
* Shows an error message.
*
* @param title title of error message; on any view that uses an error
@@ -32,4 +67,25 @@ public interface View {
* @since 2021-12-15
*/
void showErrorMessage(String title, String message);
+
+ /**
+ * Shows information about a prefix to the user.
+ *
+ * @param name name(s) and symbol of prefix
+ * @param multiplierString string representation of prefix multiplier
+ * @since 2022-04-10
+ */
+ void showPrefix(NameSymbol name, String multiplierString);
+
+ /**
+ * Shows information about a unit to the user.
+ *
+ * @param name name(s) and symbol of unit
+ * @param definition unit's definition string
+ * @param dimensionName name of unit's dimension
+ * @param type type of unit (metric/semi-metric/non-metric)
+ * @since 2022-04-10
+ */
+ void showUnit(NameSymbol name, String definition, String dimensionName,
+ UnitType type);
}
diff --git a/src/main/java/sevenUnitsGUI/ViewBot.java b/src/main/java/sevenUnitsGUI/ViewBot.java
index 0195dd6..9f9a524 100644
--- a/src/main/java/sevenUnitsGUI/ViewBot.java
+++ b/src/main/java/sevenUnitsGUI/ViewBot.java
@@ -23,6 +23,9 @@ import java.util.Objects;
import java.util.Optional;
import java.util.Set;
+import sevenUnits.unit.UnitType;
+import sevenUnits.utils.NameSymbol;
+
/**
* A class that simulates a View (supports both unit and expression conversion)
* for testing. Getters and setters work as expected.
@@ -146,6 +149,16 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView {
}
@Override
+ public Optional<String> getViewedPrefixName() {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public Optional<String> getViewedUnitName() {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void setDimensionNames(Set<String> dimensionNames) {
this.dimensionNames = Objects.requireNonNull(dimensionNames,
"dimensions may not be null");
@@ -237,6 +250,16 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView {
}
@Override
+ public void setViewablePrefixNames(Set<String> prefixNames) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public void setViewableUnitNames(Set<String> unitNames) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void showErrorMessage(String title, String message) {
System.err.printf("%s: %s%n", title, message);
}
@@ -248,6 +271,17 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView {
}
@Override
+ public void showPrefix(NameSymbol name, String multiplierString) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
+ public void showUnit(NameSymbol name, String definition,
+ String dimensionName, UnitType type) {
+ throw new UnsupportedOperationException("Not implemented yet");
+ }
+
+ @Override
public void showUnitConversionOutput(UnitConversionRecord uc) {
this.unitConversions.add(uc);
System.out.println("Unit Conversion: " + uc);
@@ -265,5 +299,4 @@ final class ViewBot implements UnitConversionView, ExpressionConversionView {
public List<UnitConversionRecord> unitConversionList() {
return Collections.unmodifiableList(this.unitConversions);
}
-
}