From 33deb34925e2d854aab3dad66dbf90f123b5b516 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 1 Aug 2017 10:24:32 -0700 Subject: [PATCH 1/2] Merge the two configuration classes into one. --- src/main/java/org/nwapw/abacus/Abacus.java | 10 +- .../nwapw/abacus/config/Configuration.java | 69 ++++++++++- .../abacus/config/ConfigurationObject.java | 111 ------------------ 3 files changed, 70 insertions(+), 120 deletions(-) delete mode 100644 src/main/java/org/nwapw/abacus/config/ConfigurationObject.java diff --git a/src/main/java/org/nwapw/abacus/Abacus.java b/src/main/java/org/nwapw/abacus/Abacus.java index 16738b2..bf8328f 100644 --- a/src/main/java/org/nwapw/abacus/Abacus.java +++ b/src/main/java/org/nwapw/abacus/Abacus.java @@ -1,6 +1,6 @@ package org.nwapw.abacus; -import org.nwapw.abacus.config.ConfigurationObject; +import org.nwapw.abacus.config.Configuration; import org.nwapw.abacus.fx.AbacusApplication; import org.nwapw.abacus.number.NaiveNumber; import org.nwapw.abacus.number.NumberInterface; @@ -46,7 +46,7 @@ public class Abacus { /** * The configuration loaded from a file. */ - private ConfigurationObject configuration; + private Configuration configuration; /** * The tree builder used to construct a tree * from a string. @@ -59,8 +59,8 @@ public class Abacus { public Abacus() { pluginManager = new PluginManager(); numberReducer = new NumberReducer(this); - configuration = new ConfigurationObject(CONFIG_FILE); - configuration.save(CONFIG_FILE); + configuration = new Configuration(CONFIG_FILE); + configuration.saveTo(CONFIG_FILE); LexerTokenizer lexerTokenizer = new LexerTokenizer(); ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this); treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser); @@ -114,7 +114,7 @@ public class Abacus { * * @return the configuration object. */ - public ConfigurationObject getConfiguration() { + public Configuration getConfiguration() { return configuration; } diff --git a/src/main/java/org/nwapw/abacus/config/Configuration.java b/src/main/java/org/nwapw/abacus/config/Configuration.java index 2039d60..3b3ef16 100644 --- a/src/main/java/org/nwapw/abacus/config/Configuration.java +++ b/src/main/java/org/nwapw/abacus/config/Configuration.java @@ -1,14 +1,75 @@ package org.nwapw.abacus.config; +import com.moandjiezana.toml.Toml; +import com.moandjiezana.toml.TomlWriter; + +import java.io.File; +import java.io.IOException; + /** - * Serializable class that will be used to load TOML - * configurations. + * The configuration object that stores + * options that the user can change. */ public class Configuration { /** - * The type of number this calculator should use. + * The TOML writer used to write this configuration to a file. */ - public String numberType; + private static final TomlWriter TOML_WRITER = new TomlWriter(); + /** + * The TOML reader used to load this config from a file. + */ + private static final Toml TOML_READER = new Toml(); + /** + * The implementation of the number that should be used. + */ + private String numberImplementation = "naive"; + + /** + * Creates a new configuration with the given values. + * @param numberImplementation the number implementation, like "naive" or "precise" + */ + public Configuration(String numberImplementation){ + this.numberImplementation = numberImplementation; + } + + /** + * Loads a configuration from a given file, keeping non-specified fields default. + * @param fromFile the file to load from. + */ + public Configuration(File fromFile){ + if(!fromFile.exists()) return; + copyFrom(TOML_READER.read(fromFile).to(Configuration.class)); + } + + /** + * Copies the values from the given configuration into this one. + * @param otherConfiguration the configuration to copy from. + */ + public void copyFrom(Configuration otherConfiguration){ + this.numberImplementation = otherConfiguration.numberImplementation; + } + + /** + * Saves this configuration to the given file, creating + * any directories that do not exist. + * @param file the file to save to. + */ + public void saveTo(File file){ + if(file.getParentFile() != null) file.getParentFile().mkdirs(); + try { + TOML_WRITER.write(this, file); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Gets the number implementation from this configuration. + * @return the number implementation. + */ + public String getNumberImplementation() { + return numberImplementation; + } } diff --git a/src/main/java/org/nwapw/abacus/config/ConfigurationObject.java b/src/main/java/org/nwapw/abacus/config/ConfigurationObject.java deleted file mode 100644 index a181038..0000000 --- a/src/main/java/org/nwapw/abacus/config/ConfigurationObject.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.nwapw.abacus.config; - -import com.moandjiezana.toml.Toml; -import com.moandjiezana.toml.TomlWriter; - -import java.io.File; -import java.io.IOException; - -/** - * A configuration object, which essentially - * manages saving, loading, and getting values - * from the configuration. While Configuration is - * the data model, this is the interface with it. - */ -public class ConfigurationObject { - - /** - * The writer used to store the configuration. - */ - private static final TomlWriter TOML_WRITER = new TomlWriter(); - /** - * The configuration instance being modeled. - */ - private Configuration configuration; - - /** - * Creates a new configuration object with the given config. - * - * @param config the config to use. - */ - public ConfigurationObject(Configuration config) { - setup(config); - } - - /** - * Create a configuration object by attempting to - * load a config from the given path, using the - * default configuration otherwise. - * - * @param path the path to attempt to load. - */ - public ConfigurationObject(File path) { - Configuration config; - if (!path.exists()) { - config = getDefaultConfig(); - } else { - Toml parse = new Toml(); - parse.read(path); - config = parse.to(Configuration.class); - } - setup(config); - } - - /** - * Creates a new configuration object with the - * default configuration. - */ - public ConfigurationObject() { - setup(getDefaultConfig()); - } - - /** - * Sets up the ConfigurationObject. - * different constructors do different things, - * but they all lead here. - * - * @param configuration the configuration to set up with. - */ - private void setup(Configuration configuration) { - this.configuration = configuration; - } - - /** - * Creates a default configuration. - * - * @return the newly created default configuration. - */ - private Configuration getDefaultConfig() { - configuration = new Configuration(); - configuration.numberType = "naive"; - return configuration; - } - - /** - * Returns the implementation the user has requested to - * represent their numbers. - * - * @return the implementation name. - */ - public String getNumberImplementation() { - return configuration.numberType; - } - - /** - * Saves the ConfigurationObject to the given file. - * - * @param toFile the file to save ot. - * @return true if the save succeed, false if otherwise. - */ - public boolean save(File toFile) { - if (toFile.getParentFile() != null) toFile.getParentFile().mkdirs(); - try { - TOML_WRITER.write(configuration, toFile); - return true; - } catch (IOException e) { - e.printStackTrace(); - } - return false; - } - -} From 1344090a58555bedfd8c2d2f9128224b7f33f524 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 1 Aug 2017 11:52:48 -0700 Subject: [PATCH 2/2] Add a number implementation selector box. --- .../org/nwapw/abacus/config/Configuration.java | 8 ++++++++ .../org/nwapw/abacus/fx/AbacusController.java | 18 +++++++++++++++++- src/main/resources/abacus.fxml | 10 +++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/config/Configuration.java b/src/main/java/org/nwapw/abacus/config/Configuration.java index 3b3ef16..6253632 100644 --- a/src/main/java/org/nwapw/abacus/config/Configuration.java +++ b/src/main/java/org/nwapw/abacus/config/Configuration.java @@ -72,4 +72,12 @@ public class Configuration { public String getNumberImplementation() { return numberImplementation; } + + /** + * Sets the number implementation for the configuration + * @param numberImplementation the number implementation. + */ + public void setNumberImplementation(String numberImplementation) { + this.numberImplementation = numberImplementation; + } } diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 7c9d539..10ac77b 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -30,9 +30,13 @@ public class AbacusController { private TextField inputField; @FXML private Button inputButton; + @FXML + private ComboBox numberImplementationBox; private ObservableList historyData; + private ObservableList numberImplementationOptions; + private Abacus abacus; @FXML @@ -40,9 +44,15 @@ public class AbacusController { Callback, TableCell> cellFactory = param -> new CopyableCell<>(); - abacus = new Abacus(); historyData = FXCollections.observableArrayList(); historyTable.setItems(historyData); + numberImplementationOptions = FXCollections.observableArrayList(); + numberImplementationBox.setItems(numberImplementationOptions); + numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue) + -> { + abacus.getConfiguration().setNumberImplementation(newValue); + abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE); + }); historyTable.getSelectionModel().setCellSelectionEnabled(true); inputColumn.setCellFactory(cellFactory); inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty()); @@ -50,6 +60,12 @@ public class AbacusController { parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty()); outputColumn.setCellFactory(cellFactory); outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty()); + + abacus = new Abacus(); + numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers()); + String actualImplementation = abacus.getConfiguration().getNumberImplementation(); + String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive"; + numberImplementationBox.getSelectionModel().select(toSelect); } @FXML diff --git a/src/main/resources/abacus.fxml b/src/main/resources/abacus.fxml index 9a68947..183e247 100644 --- a/src/main/resources/abacus.fxml +++ b/src/main/resources/abacus.fxml @@ -5,6 +5,8 @@ + + @@ -39,7 +41,13 @@ - + + + + +