|
1 | 1 | package org.cirdles.topsoil.app.util.dialog;
|
2 | 2 |
|
| 3 | +import javafx.application.Platform; |
3 | 4 | import javafx.collections.MapChangeListener;
|
4 |
| -import javafx.fxml.FXMLLoader; |
5 |
| -import javafx.scene.Node; |
| 5 | +import javafx.geometry.Insets; |
| 6 | +import javafx.geometry.Pos; |
6 | 7 | import javafx.scene.control.ButtonType;
|
7 | 8 | import javafx.scene.control.Dialog;
|
| 9 | +import javafx.scene.control.Label; |
| 10 | +import javafx.scene.layout.VBox; |
8 | 11 | import javafx.stage.Stage;
|
9 |
| -import org.cirdles.commons.util.ResourceExtractor; |
10 | 12 | import org.cirdles.topsoil.app.MainWindow;
|
11 | 13 | import org.cirdles.topsoil.app.plot.variable.Variable;
|
| 14 | +import org.cirdles.topsoil.app.plot.variable.Variables; |
12 | 15 | import org.cirdles.topsoil.app.table.TopsoilDataColumn;
|
13 | 16 | import org.cirdles.topsoil.app.table.TopsoilTableController;
|
14 |
| -import org.cirdles.topsoil.app.util.dialog.controller.VariableChooserController; |
| 17 | +import org.cirdles.topsoil.app.util.dialog.controller.VariableColumnChooser; |
15 | 18 |
|
16 |
| -import java.io.IOException; |
17 | 19 | import java.util.List;
|
18 | 20 | import java.util.Map;
|
19 | 21 |
|
|
22 | 24 | */
|
23 | 25 | public class VariableChooserDialog extends Dialog<Map<Variable<Number>, TopsoilDataColumn>> {
|
24 | 26 |
|
25 |
| - private final ResourceExtractor RESOURCE_EXTRACTOR = new ResourceExtractor(VariableChooserDialog.class); |
26 |
| - private final String VARIABLE_CHOOSER_FXML = "controller/variable-chooser.fxml"; |
27 |
| - |
28 |
| - private VariableChooserController controller; |
29 |
| - |
30 | 27 | private VariableChooserDialog(List<TopsoilDataColumn> columns,
|
| 28 | + List<Variable<Number>> variables, |
31 | 29 | Map<Variable<Number>, TopsoilDataColumn> selections,
|
32 |
| - List<Variable<Number>> requiredVariables) { |
| 30 | + List<Variable<Number>> required) { |
33 | 31 | super();
|
34 | 32 |
|
35 | 33 | Stage stage = (Stage) this.getDialogPane().getScene().getWindow();
|
36 | 34 | stage.getIcons().add(MainWindow.getWindowIcon());
|
37 | 35 | stage.initOwner(MainWindow.getPrimaryStage());
|
38 | 36 | stage.setTitle("Variable Chooser");
|
39 |
| - |
| 37 | + stage.setResizable(true); |
40 | 38 | this.getDialogPane().getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);
|
41 | 39 |
|
42 |
| - Node content; |
43 |
| - controller = null; |
44 |
| - try { |
45 |
| - FXMLLoader loader = new FXMLLoader( |
46 |
| - RESOURCE_EXTRACTOR.extractResourceAsPath(VARIABLE_CHOOSER_FXML).toUri().toURL()); |
47 |
| - content = loader.load(); |
48 |
| - controller = loader.getController(); |
49 |
| - |
50 |
| - // Disable OK button if not all required variables are assigned. |
51 |
| - this.getDialogPane().lookupButton(ButtonType.OK).setDisable(true); |
52 |
| - controller.selectionsProperty().addListener((MapChangeListener<? super Variable<Number>, ? super TopsoilDataColumn>) c -> { |
53 |
| - Boolean shouldDisableOK = false; |
54 |
| - if (!(requiredVariables == null)) { |
55 |
| - for (Variable<Number> v : requiredVariables) { |
56 |
| - if (!controller.selectionsProperty().containsKey(v)) { |
57 |
| - shouldDisableOK = true; |
58 |
| - break; |
59 |
| - } |
| 40 | + Label messageLabel = new Label("Choose which variables to associate with each column."); |
| 41 | + messageLabel.setPadding(new Insets(10.0, 10.0, 10.0, 10.0)); |
| 42 | + VariableColumnChooser chooser = new VariableColumnChooser(columns, variables, selections, required); |
| 43 | + |
| 44 | + VBox container = new VBox(messageLabel, chooser); |
| 45 | + container.setAlignment(Pos.TOP_CENTER); |
| 46 | + |
| 47 | + // Disable OK button if not all required variables are assigned. |
| 48 | + this.getDialogPane().lookupButton(ButtonType.OK).setDisable(true); |
| 49 | + chooser.selectionsProperty().addListener((MapChangeListener<? super Variable<Number>, ? super TopsoilDataColumn>) c -> { |
| 50 | + Boolean shouldDisableOK = false; |
| 51 | + if (!(required == null)) { |
| 52 | + for (Variable<Number> v : required) { |
| 53 | + if (!chooser.selectionsProperty().containsKey(v)) { |
| 54 | + shouldDisableOK = true; |
| 55 | + break; |
60 | 56 | }
|
61 | 57 | }
|
62 |
| - this.getDialogPane().lookupButton(ButtonType.OK).setDisable(shouldDisableOK); |
63 |
| - }); |
64 |
| - |
65 |
| - controller.setup(columns, selections, requiredVariables); |
66 |
| - |
67 |
| - this.getDialogPane().setContent(content); |
68 |
| - |
69 |
| - this.setResultConverter(result -> { |
70 |
| - if (result == ButtonType.OK) { |
71 |
| - return controller.getSelections(); |
72 |
| - } else { |
73 |
| - return null; |
74 |
| - } |
75 |
| - }); |
76 |
| - |
77 |
| - } catch (IOException e) { |
78 |
| - e.printStackTrace(); |
79 |
| - } |
| 58 | + } |
| 59 | + this.getDialogPane().lookupButton(ButtonType.OK).setDisable(shouldDisableOK); |
| 60 | + }); |
| 61 | + this.getDialogPane().setContent(container); |
| 62 | + |
| 63 | + // The Scene doesn't seem to be completely done laying out its Nodes by the time this event is fired. Since |
| 64 | + // Platform.runLater() isn't being used extensively elsewhere, this works fine. If that changes, this may |
| 65 | + // have to be changed, as well. |
| 66 | + this.setOnShown(event -> Platform.runLater(() -> { |
| 67 | + chooser.callAfterVisible(); |
| 68 | + if (stage.getWidth() > 800.0) { |
| 69 | + stage.setWidth(800.0); |
| 70 | + } |
| 71 | + if (stage.getHeight() > 600.0) { |
| 72 | + stage.setHeight(600.0); |
| 73 | + } |
| 74 | + })); |
| 75 | + |
| 76 | + this.setResultConverter(result -> { |
| 77 | + if (result == ButtonType.OK) { |
| 78 | + return chooser.getSelections(); |
| 79 | + } else { |
| 80 | + return null; |
| 81 | + } |
| 82 | + }); |
80 | 83 | }
|
81 | 84 |
|
82 | 85 | public static Map<Variable<Number>, TopsoilDataColumn> showDialog(TopsoilTableController tableController,
|
83 | 86 | List<Variable<Number>> requiredVariables) {
|
84 | 87 |
|
85 | 88 | List<TopsoilDataColumn> columns = tableController.getTable().getDataColumns();
|
| 89 | + List<Variable<Number>> variables = Variables.VARIABLE_LIST; |
86 | 90 | Map<Variable<Number>, TopsoilDataColumn> currentSelections = tableController.getTable().getVariableAssignments();
|
87 |
| - return new VariableChooserDialog(columns, currentSelections, requiredVariables).showAndWait().orElse(null); |
88 |
| - } |
89 | 91 |
|
90 |
| - public static Map<Variable<Number>, TopsoilDataColumn> showDialog(List<TopsoilDataColumn> columns, |
91 |
| - Map<Variable<Number>, TopsoilDataColumn> |
92 |
| - currentSelections, |
93 |
| - List<Variable<Number>> requiredVariables) { |
94 |
| - return new VariableChooserDialog(columns, currentSelections, requiredVariables).showAndWait().orElse(null); |
| 92 | + return new VariableChooserDialog(columns, variables, currentSelections, requiredVariables).showAndWait().orElse(null); |
95 | 93 | }
|
96 |
| - |
97 | 94 | }
|
0 commit comments