2017-07-31 16:48:04 -07:00
|
|
|
package org.nwapw.abacus.fx;
|
|
|
|
|
2017-07-31 17:49:57 -07:00
|
|
|
import javafx.collections.FXCollections;
|
|
|
|
import javafx.collections.ObservableList;
|
2017-07-31 16:52:34 -07:00
|
|
|
import javafx.fxml.FXML;
|
2017-07-31 22:53:42 -07:00
|
|
|
import javafx.scene.control.*;
|
2017-08-02 18:48:42 -07:00
|
|
|
import javafx.scene.control.cell.CheckBoxListCell;
|
2017-07-31 17:17:56 -07:00
|
|
|
import javafx.scene.text.Text;
|
2017-07-31 22:53:42 -07:00
|
|
|
import javafx.util.Callback;
|
2017-08-02 18:48:42 -07:00
|
|
|
import javafx.util.StringConverter;
|
2017-07-31 16:52:34 -07:00
|
|
|
import org.nwapw.abacus.Abacus;
|
2017-08-02 19:01:01 -07:00
|
|
|
import org.nwapw.abacus.config.Configuration;
|
2017-07-31 17:17:56 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
2017-08-02 19:18:33 -07:00
|
|
|
import org.nwapw.abacus.plugin.PluginListener;
|
|
|
|
import org.nwapw.abacus.plugin.PluginManager;
|
2017-07-31 17:17:56 -07:00
|
|
|
import org.nwapw.abacus.tree.TreeNode;
|
2017-07-31 16:52:34 -07:00
|
|
|
|
2017-08-02 19:01:01 -07:00
|
|
|
import java.util.Set;
|
|
|
|
|
2017-07-31 22:53:42 -07:00
|
|
|
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* The controller for the abacus FX UI, responsible
|
|
|
|
* for all the user interaction.
|
|
|
|
*/
|
2017-08-02 19:18:33 -07:00
|
|
|
public class AbacusController implements PluginListener {
|
2017-07-31 16:48:04 -07:00
|
|
|
|
2017-08-03 13:55:39 -07:00
|
|
|
/**
|
|
|
|
* The title for the apply alert dialog.
|
|
|
|
*/
|
|
|
|
private static final String APPLY_MSG_TITLE = "\"Apply\" Needed";
|
|
|
|
/**
|
|
|
|
* The text for the header of the apply alert dialog.
|
|
|
|
*/
|
|
|
|
private static final String APPLY_MSG_HEADER = "The settings have not been applied.";
|
|
|
|
/**
|
|
|
|
* The text for the dialog that is shown if settings haven't been applied.
|
|
|
|
*/
|
|
|
|
private static final String APPLY_MSG_TEXT = "You have made changes to the configuration, however, you haven't pressed \"Apply\". " +
|
2017-08-04 13:20:57 -07:00
|
|
|
"The changes to the configuration will not be present in the calculator until \"Apply\" is pressed.";
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* Constant string that is displayed if the text could not be lexed or parsed.
|
|
|
|
*/
|
2017-07-31 17:17:56 -07:00
|
|
|
private static final String ERR_SYNTAX = "Syntax Error";
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* Constant string that is displayed if the tree could not be reduced.
|
|
|
|
*/
|
2017-07-31 17:17:56 -07:00
|
|
|
private static final String ERR_EVAL = "Evaluation Error";
|
|
|
|
|
2017-08-03 18:16:48 -07:00
|
|
|
@FXML
|
|
|
|
private TabPane coreTabPane;
|
|
|
|
@FXML
|
|
|
|
private Tab calculateTab;
|
|
|
|
@FXML
|
|
|
|
private Tab settingsTab;
|
2017-07-31 22:29:44 -07:00
|
|
|
@FXML
|
|
|
|
private TableView<HistoryModel> historyTable;
|
|
|
|
@FXML
|
|
|
|
private TableColumn<HistoryModel, String> inputColumn;
|
|
|
|
@FXML
|
|
|
|
private TableColumn<HistoryModel, String> parsedColumn;
|
|
|
|
@FXML
|
|
|
|
private TableColumn<HistoryModel, String> outputColumn;
|
2017-07-31 17:17:56 -07:00
|
|
|
@FXML
|
|
|
|
private Text outputText;
|
|
|
|
@FXML
|
|
|
|
private TextField inputField;
|
|
|
|
@FXML
|
|
|
|
private Button inputButton;
|
2017-08-01 11:52:48 -07:00
|
|
|
@FXML
|
|
|
|
private ComboBox<String> numberImplementationBox;
|
2017-08-02 18:24:20 -07:00
|
|
|
@FXML
|
2017-08-02 18:48:42 -07:00
|
|
|
private ListView<ToggleablePlugin> enabledPluginView;
|
2017-07-31 17:17:56 -07:00
|
|
|
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* The list of history entries, created by the users.
|
|
|
|
*/
|
2017-07-31 17:49:57 -07:00
|
|
|
private ObservableList<HistoryModel> historyData;
|
|
|
|
|
2017-08-01 10:47:31 -07:00
|
|
|
/**
|
|
|
|
* The abacus instance used for calculations and all
|
|
|
|
* other main processing code.
|
|
|
|
*/
|
2017-08-01 11:52:48 -07:00
|
|
|
private ObservableList<String> numberImplementationOptions;
|
|
|
|
|
2017-08-02 18:48:42 -07:00
|
|
|
/**
|
|
|
|
* The list of plugin objects that can be toggled on and off,
|
|
|
|
* and, when reloaded, get added to the plugin manager's black list.
|
|
|
|
*/
|
|
|
|
private ObservableList<ToggleablePlugin> enabledPlugins;
|
|
|
|
|
2017-08-03 13:55:39 -07:00
|
|
|
/**
|
|
|
|
* The abacus instance used for changing the plugin configuration.
|
|
|
|
*/
|
2017-07-31 16:52:34 -07:00
|
|
|
private Abacus abacus;
|
|
|
|
|
2017-08-03 13:55:39 -07:00
|
|
|
/**
|
|
|
|
* Boolean which represents whether changes were made to the configuration.
|
|
|
|
*/
|
|
|
|
private boolean changesMade;
|
2017-08-03 18:12:40 -07:00
|
|
|
/**
|
|
|
|
* Whether an alert about changes to the configuration was already shown.
|
|
|
|
*/
|
|
|
|
private boolean reloadAlertShown;
|
2017-08-03 13:55:39 -07:00
|
|
|
/**
|
|
|
|
* The alert shown when a press to "apply" is needed.
|
|
|
|
*/
|
|
|
|
private Alert reloadAlert;
|
|
|
|
|
2017-08-03 18:12:40 -07:00
|
|
|
/**
|
|
|
|
* Alerts the user if the changes they made
|
|
|
|
* have not yet been applied.
|
|
|
|
*/
|
2017-08-04 13:20:57 -07:00
|
|
|
private void alertIfApplyNeeded(boolean ignorePrevious) {
|
|
|
|
if (changesMade && (!reloadAlertShown || ignorePrevious)) {
|
2017-08-03 18:12:40 -07:00
|
|
|
reloadAlertShown = true;
|
|
|
|
reloadAlert.showAndWait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 16:52:34 -07:00
|
|
|
@FXML
|
2017-08-04 13:20:57 -07:00
|
|
|
public void initialize() {
|
2017-07-31 22:53:42 -07:00
|
|
|
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
|
|
|
param -> new CopyableCell<>();
|
2017-08-02 18:48:42 -07:00
|
|
|
Callback<ListView<ToggleablePlugin>, ListCell<ToggleablePlugin>> pluginCellFactory =
|
|
|
|
param -> new CheckBoxListCell<>(ToggleablePlugin::enabledProperty, new StringConverter<ToggleablePlugin>() {
|
|
|
|
@Override
|
|
|
|
public String toString(ToggleablePlugin object) {
|
|
|
|
return object.getClassName().substring(object.getClassName().lastIndexOf('.') + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ToggleablePlugin fromString(String string) {
|
|
|
|
return new ToggleablePlugin(true, string);
|
|
|
|
}
|
|
|
|
});
|
2017-07-31 22:53:42 -07:00
|
|
|
|
2017-07-31 17:49:57 -07:00
|
|
|
historyData = FXCollections.observableArrayList();
|
2017-07-31 22:29:44 -07:00
|
|
|
historyTable.setItems(historyData);
|
2017-08-01 11:52:48 -07:00
|
|
|
numberImplementationOptions = FXCollections.observableArrayList();
|
|
|
|
numberImplementationBox.setItems(numberImplementationOptions);
|
2017-08-03 18:12:40 -07:00
|
|
|
numberImplementationBox.getSelectionModel().selectedIndexProperty().addListener(e -> changesMade = true);
|
2017-07-31 22:34:49 -07:00
|
|
|
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
2017-08-02 18:48:42 -07:00
|
|
|
enabledPlugins = FXCollections.observableArrayList();
|
|
|
|
enabledPluginView.setItems(enabledPlugins);
|
|
|
|
enabledPluginView.setCellFactory(pluginCellFactory);
|
2017-07-31 22:53:42 -07:00
|
|
|
inputColumn.setCellFactory(cellFactory);
|
2017-07-31 22:29:44 -07:00
|
|
|
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
2017-07-31 22:53:42 -07:00
|
|
|
parsedColumn.setCellFactory(cellFactory);
|
2017-07-31 22:29:44 -07:00
|
|
|
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
2017-07-31 22:53:42 -07:00
|
|
|
outputColumn.setCellFactory(cellFactory);
|
2017-07-31 22:29:44 -07:00
|
|
|
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
2017-08-03 18:16:48 -07:00
|
|
|
coreTabPane.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
|
2017-08-04 13:20:57 -07:00
|
|
|
if (oldValue.equals(settingsTab)) alertIfApplyNeeded(true);
|
2017-08-03 18:16:48 -07:00
|
|
|
});
|
2017-08-01 11:52:48 -07:00
|
|
|
|
|
|
|
abacus = new Abacus();
|
2017-08-02 19:18:33 -07:00
|
|
|
abacus.getPluginManager().addListener(this);
|
|
|
|
abacus.getPluginManager().reload();
|
2017-08-03 13:55:39 -07:00
|
|
|
|
|
|
|
changesMade = false;
|
2017-08-03 18:12:40 -07:00
|
|
|
reloadAlertShown = false;
|
2017-08-03 13:55:39 -07:00
|
|
|
|
|
|
|
reloadAlert = new Alert(Alert.AlertType.WARNING);
|
|
|
|
reloadAlert.setTitle(APPLY_MSG_TITLE);
|
2017-08-03 18:12:40 -07:00
|
|
|
reloadAlert.setHeaderText(APPLY_MSG_HEADER);
|
2017-08-03 13:55:39 -07:00
|
|
|
reloadAlert.setContentText(APPLY_MSG_TEXT);
|
2017-07-31 16:52:34 -07:00
|
|
|
}
|
|
|
|
|
2017-07-31 17:17:56 -07:00
|
|
|
@FXML
|
2017-08-04 13:20:57 -07:00
|
|
|
private void performCalculation() {
|
2017-07-31 17:17:56 -07:00
|
|
|
inputButton.setDisable(true);
|
|
|
|
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
2017-08-04 13:20:57 -07:00
|
|
|
if (constructedTree == null) {
|
2017-07-31 17:17:56 -07:00
|
|
|
outputText.setText(ERR_SYNTAX);
|
|
|
|
inputButton.setDisable(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
2017-08-04 13:20:57 -07:00
|
|
|
if (evaluatedNumber == null) {
|
2017-07-31 17:17:56 -07:00
|
|
|
outputText.setText(ERR_EVAL);
|
|
|
|
inputButton.setDisable(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
outputText.setText(evaluatedNumber.toString());
|
2017-07-31 22:29:44 -07:00
|
|
|
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
|
|
|
|
|
|
|
inputButton.setDisable(false);
|
|
|
|
inputField.setText("");
|
2017-07-31 17:17:56 -07:00
|
|
|
}
|
|
|
|
|
2017-08-03 19:00:13 -07:00
|
|
|
@FXML
|
2017-08-04 13:20:57 -07:00
|
|
|
private void performSaveAndReload() {
|
2017-08-03 19:00:13 -07:00
|
|
|
performSave();
|
|
|
|
performReload();
|
|
|
|
changesMade = false;
|
|
|
|
reloadAlertShown = false;
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:01:01 -07:00
|
|
|
@FXML
|
2017-08-04 13:20:57 -07:00
|
|
|
private void performReload() {
|
2017-08-03 18:12:40 -07:00
|
|
|
alertIfApplyNeeded(true);
|
2017-08-02 19:01:01 -07:00
|
|
|
abacus.getPluginManager().reload();
|
2017-08-02 19:37:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@FXML
|
2017-08-04 13:20:57 -07:00
|
|
|
private void performSave() {
|
2017-08-02 19:37:35 -07:00
|
|
|
Configuration configuration = abacus.getConfiguration();
|
|
|
|
configuration.setNumberImplementation(numberImplementationBox.getSelectionModel().getSelectedItem());
|
2017-08-03 13:55:39 -07:00
|
|
|
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
|
|
|
disabledPlugins.clear();
|
2017-08-04 13:20:57 -07:00
|
|
|
for (ToggleablePlugin pluginEntry : enabledPlugins) {
|
|
|
|
if (!pluginEntry.isEnabled()) disabledPlugins.add(pluginEntry.getClassName());
|
2017-08-03 13:55:39 -07:00
|
|
|
}
|
2017-08-02 19:37:35 -07:00
|
|
|
configuration.saveTo(Abacus.CONFIG_FILE);
|
2017-08-03 13:55:39 -07:00
|
|
|
changesMade = false;
|
2017-08-03 18:12:40 -07:00
|
|
|
reloadAlertShown = false;
|
2017-08-02 19:01:01 -07:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:18:33 -07:00
|
|
|
@Override
|
|
|
|
public void onLoad(PluginManager manager) {
|
|
|
|
Configuration configuration = abacus.getConfiguration();
|
|
|
|
Set<String> disabledPlugins = configuration.getDisabledPlugins();
|
2017-08-04 12:04:37 -07:00
|
|
|
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumberImplementations());
|
2017-08-02 19:18:33 -07:00
|
|
|
String actualImplementation = configuration.getNumberImplementation();
|
2017-08-02 19:26:14 -07:00
|
|
|
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "<default>";
|
2017-08-02 19:18:33 -07:00
|
|
|
numberImplementationBox.getSelectionModel().select(toSelect);
|
2017-08-04 13:20:57 -07:00
|
|
|
for (Class<?> pluginClass : abacus.getPluginManager().getLoadedPluginClasses()) {
|
2017-08-02 19:18:33 -07:00
|
|
|
String fullName = pluginClass.getName();
|
2017-08-03 18:12:40 -07:00
|
|
|
ToggleablePlugin plugin = new ToggleablePlugin(!disabledPlugins.contains(fullName), fullName);
|
|
|
|
plugin.enabledProperty().addListener(e -> changesMade = true);
|
|
|
|
enabledPlugins.add(plugin);
|
2017-08-02 19:18:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUnload(PluginManager manager) {
|
|
|
|
enabledPlugins.clear();
|
|
|
|
numberImplementationOptions.clear();
|
|
|
|
}
|
2017-07-31 16:48:04 -07:00
|
|
|
}
|