Skip to content

Commit

Permalink
Replaces Variable Chooser Dialog (#399)
Browse files Browse the repository at this point in the history
* Replaces Variable Chooser Dialog

* Sets default variable assignments for example data tables
  • Loading branch information
jakemarotta committed Oct 23, 2017
1 parent b2e7d79 commit 3e48b58
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 383 deletions.
11 changes: 2 additions & 9 deletions app/src/main/java/org/cirdles/topsoil/app/menu/MainMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,16 +383,9 @@ private Menu getTableMenu(TopsoilTabPane tabs) {
uPbExampleTableItem,
uThExampleTableItem);

uPbExampleTableItem.setOnAction(event -> {
TopsoilDataTable table = MenuItemEventHandler.handleOpenExampleTable(tabs, IsotopeType.UPb);
tabs.add(table);
});
uPbExampleTableItem.setOnAction(event -> MenuItemEventHandler.handleOpenExampleTable(tabs, IsotopeType.UPb) );

uThExampleTableItem.setOnAction(event -> {
TopsoilDataTable table = MenuItemEventHandler.handleOpenExampleTable(tabs, IsotopeType.UTh);
tabs.add(table);
});

uThExampleTableItem.setOnAction(event -> MenuItemEventHandler.handleOpenExampleTable(tabs, IsotopeType.UTh) );

// Import Table from File
tableFromFileItem.setOnAction(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.cirdles.topsoil.app.metadata.TopsoilMetadata;
import org.cirdles.topsoil.app.isotope.IsotopeType;
import org.cirdles.topsoil.app.plot.variable.Variable;
import org.cirdles.topsoil.app.plot.variable.Variables;
import org.cirdles.topsoil.app.table.TopsoilDataColumn;
import org.cirdles.topsoil.app.table.TopsoilTableController;
import org.cirdles.topsoil.app.table.uncertainty.UncertaintyFormat;
import org.cirdles.topsoil.app.tab.TopsoilTabPane;
import org.cirdles.topsoil.app.dataset.entry.TopsoilDataEntry;
Expand Down Expand Up @@ -301,16 +303,15 @@ public static TopsoilDataTable handleNewTable() {
*
* @param tabs the TopsoilTabPane to which to add tables
* @param isotopeType the isotope type of the example table to be opened
* @return the resulting TopsoilDataTable
*/
public static TopsoilDataTable handleOpenExampleTable(TopsoilTabPane tabs, IsotopeType isotopeType) {
TopsoilDataTable table = null;
public static void handleOpenExampleTable(TopsoilTabPane tabs, IsotopeType isotopeType) {
TopsoilDataTable table;
UncertaintyFormat format;

if (isotopeType != null) {

List<TopsoilDataEntry> entries = null;
String[] headers = null;
List<TopsoilDataEntry> entries;
String[] headers;
String exampleContent = new ExampleDataTable().getSampleData(isotopeType);
String exampleContentDelimiter = ",";

Expand All @@ -335,12 +336,26 @@ public static TopsoilDataTable handleOpenExampleTable(TopsoilTabPane tabs, Isoto
ObservableList<TopsoilDataEntry> data = FXCollections.observableList(entries);

table = new TopsoilDataTable(headers, isotopeType, format, data.toArray(new TopsoilDataEntry[data.size()]));
table.setTitle(isotopeType.getName() + " Example Data");
table.setTitle(isotopeType.getAbbreviation() + " Example Data");

// Set default variable associations.
List<TopsoilDataColumn> columns = table.getDataColumns();
Map<Variable<Number>, TopsoilDataColumn> assignments = new HashMap<>();

assignments.put(Variables.X, columns.get(0));
assignments.put(Variables.SIGMA_X, columns.get(1));
assignments.put(Variables.Y, columns.get(2));
assignments.put(Variables.SIGMA_Y, columns.get(3));
assignments.put(Variables.RHO, columns.get(4));

tabs.add(table);
TopsoilTableController tableController = tabs.getSelectedTab().getTableController();
tableController.setVariableAssignments(assignments);

} catch (TopsoilException e) {
e.printStackTrace();
}
}
return table;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ public Map<Variable<Number>, TopsoilDataColumn> getVariableAssignments() {

public void setVariableAssignments(Map<Variable<Number>, TopsoilDataColumn> assignments) {
variableColumnMap = assignments;
// TODO Check if TopsoilDataColumns have had their variables set correctly.
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,16 @@ public List<Map<String, Object>> getPlotData() {

public void showVariableChooserDialog(@Nullable List<Variable<Number>> required) {
Map<Variable<Number>, TopsoilDataColumn> selections = VariableChooserDialog.showDialog(this, required);
setVariableAssignments(selections);
}

public void setVariableAssignments(Map<Variable<Number>, TopsoilDataColumn> assignments) {
List<TopsoilDataColumn> columns = table.getDataColumns();
Double uncertaintyFormatValue = table.getUncertaintyFormat().getValue();
if (selections != null) {
if (assignments != null) {

// Apply selections to columns
for (Map.Entry<Variable<Number>, TopsoilDataColumn> entry : selections.entrySet()) {
for (Map.Entry<Variable<Number>, TopsoilDataColumn> entry : assignments.entrySet()) {
if (entry.getValue().getVariable() != entry.getKey()) {
if (Variables.UNCERTAINTY_VARIABLES.contains(entry.getKey())) {

Expand All @@ -280,7 +284,7 @@ public void showVariableChooserDialog(@Nullable List<Variable<Number>> required)

// Set other columns' variable properties to null
for (TopsoilDataColumn column : columns) {
if (!selections.containsValue(column)) {
if (!assignments.containsValue(column)) {
// If the column was an uncertainty variable, but isn't anymore
if (column.hasVariable()) {
if (Variables.UNCERTAINTY_VARIABLES.contains(column.getVariable())) {
Expand All @@ -293,19 +297,19 @@ public void showVariableChooserDialog(@Nullable List<Variable<Number>> required)
}
}

table.setVariableAssignments(selections);
table.setVariableAssignments(assignments);
for (PlotInformation plotInfo : table.getOpenPlots()) {
plotInfo.getPlot().setData(getPlotData());
}

updateColumnListeners();

// Re-name x and y axis titles
if (selections.containsKey(Variables.X)) {
tabContent.getPlotPropertiesPanelController().setxAxisTitle(selections.get(Variables.X).getName());
if (assignments.containsKey(Variables.X)) {
tabContent.getPlotPropertiesPanelController().setxAxisTitle(assignments.get(Variables.X).getName());
}
if (selections.containsKey(Variables.Y)) {
tabContent.getPlotPropertiesPanelController().setyAxisTitle(selections.get(Variables.Y).getName());
if (assignments.containsKey(Variables.Y)) {
tabContent.getPlotPropertiesPanelController().setyAxisTitle(assignments.get(Variables.Y).getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.cirdles.topsoil.app.util.dialog;

import javafx.application.Platform;
import javafx.collections.MapChangeListener;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.cirdles.commons.util.ResourceExtractor;
import org.cirdles.topsoil.app.MainWindow;
import org.cirdles.topsoil.app.plot.variable.Variable;
import org.cirdles.topsoil.app.plot.variable.Variables;
import org.cirdles.topsoil.app.table.TopsoilDataColumn;
import org.cirdles.topsoil.app.table.TopsoilTableController;
import org.cirdles.topsoil.app.util.dialog.controller.VariableChooserController;
import org.cirdles.topsoil.app.util.dialog.controller.VariableColumnChooser;

import java.io.IOException;
import java.util.List;
import java.util.Map;

Expand All @@ -22,76 +24,71 @@
*/
public class VariableChooserDialog extends Dialog<Map<Variable<Number>, TopsoilDataColumn>> {

private final ResourceExtractor RESOURCE_EXTRACTOR = new ResourceExtractor(VariableChooserDialog.class);
private final String VARIABLE_CHOOSER_FXML = "controller/variable-chooser.fxml";

private VariableChooserController controller;

private VariableChooserDialog(List<TopsoilDataColumn> columns,
List<Variable<Number>> variables,
Map<Variable<Number>, TopsoilDataColumn> selections,
List<Variable<Number>> requiredVariables) {
List<Variable<Number>> required) {
super();

Stage stage = (Stage) this.getDialogPane().getScene().getWindow();
stage.getIcons().add(MainWindow.getWindowIcon());
stage.initOwner(MainWindow.getPrimaryStage());
stage.setTitle("Variable Chooser");

stage.setResizable(true);
this.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);

Node content;
controller = null;
try {
FXMLLoader loader = new FXMLLoader(
RESOURCE_EXTRACTOR.extractResourceAsPath(VARIABLE_CHOOSER_FXML).toUri().toURL());
content = loader.load();
controller = loader.getController();

// Disable OK button if not all required variables are assigned.
this.getDialogPane().lookupButton(ButtonType.OK).setDisable(true);
controller.selectionsProperty().addListener((MapChangeListener<? super Variable<Number>, ? super TopsoilDataColumn>) c -> {
Boolean shouldDisableOK = false;
if (!(requiredVariables == null)) {
for (Variable<Number> v : requiredVariables) {
if (!controller.selectionsProperty().containsKey(v)) {
shouldDisableOK = true;
break;
}
Label messageLabel = new Label("Choose which variables to associate with each column.");
messageLabel.setPadding(new Insets(10.0, 10.0, 10.0, 10.0));
VariableColumnChooser chooser = new VariableColumnChooser(columns, variables, selections, required);

VBox container = new VBox(messageLabel, chooser);
container.setAlignment(Pos.TOP_CENTER);

// Disable OK button if not all required variables are assigned.
this.getDialogPane().lookupButton(ButtonType.OK).setDisable(true);
chooser.selectionsProperty().addListener((MapChangeListener<? super Variable<Number>, ? super TopsoilDataColumn>) c -> {
Boolean shouldDisableOK = false;
if (!(required == null)) {
for (Variable<Number> v : required) {
if (!chooser.selectionsProperty().containsKey(v)) {
shouldDisableOK = true;
break;
}
}
this.getDialogPane().lookupButton(ButtonType.OK).setDisable(shouldDisableOK);
});

controller.setup(columns, selections, requiredVariables);

this.getDialogPane().setContent(content);

this.setResultConverter(result -> {
if (result == ButtonType.OK) {
return controller.getSelections();
} else {
return null;
}
});

} catch (IOException e) {
e.printStackTrace();
}
}
this.getDialogPane().lookupButton(ButtonType.OK).setDisable(shouldDisableOK);
});
this.getDialogPane().setContent(container);

// The Scene doesn't seem to be completely done laying out its Nodes by the time this event is fired. Since
// Platform.runLater() isn't being used extensively elsewhere, this works fine. If that changes, this may
// have to be changed, as well.
this.setOnShown(event -> Platform.runLater(() -> {
chooser.callAfterVisible();
if (stage.getWidth() > 800.0) {
stage.setWidth(800.0);
}
if (stage.getHeight() > 600.0) {
stage.setHeight(600.0);
}
}));

this.setResultConverter(result -> {
if (result == ButtonType.OK) {
return chooser.getSelections();
} else {
return null;
}
});
}

public static Map<Variable<Number>, TopsoilDataColumn> showDialog(TopsoilTableController tableController,
List<Variable<Number>> requiredVariables) {

List<TopsoilDataColumn> columns = tableController.getTable().getDataColumns();
List<Variable<Number>> variables = Variables.VARIABLE_LIST;
Map<Variable<Number>, TopsoilDataColumn> currentSelections = tableController.getTable().getVariableAssignments();
return new VariableChooserDialog(columns, currentSelections, requiredVariables).showAndWait().orElse(null);
}

public static Map<Variable<Number>, TopsoilDataColumn> showDialog(List<TopsoilDataColumn> columns,
Map<Variable<Number>, TopsoilDataColumn>
currentSelections,
List<Variable<Number>> requiredVariables) {
return new VariableChooserDialog(columns, currentSelections, requiredVariables).showAndWait().orElse(null);
return new VariableChooserDialog(columns, variables, currentSelections, requiredVariables).showAndWait().orElse(null);
}

}

0 comments on commit 3e48b58

Please sign in to comment.