From d2b459e5033ae470e9f53a0d32d7481928331c07 Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Fri, 21 Sep 2018 14:01:25 -0700 Subject: [PATCH] gui - some refactoring for creating dynamic config ui elements --- .../src/main/java/com/core/ui/ConfigItem.java | 127 ------------------ .../main/java/com/core/ui/GraphToolbar.java | 12 +- .../com/core/ui/dialogs/ConfigDialog.java | 14 +- .../java/com/core/ui/dialogs/HookDialog.java | 7 +- 4 files changed, 9 insertions(+), 151 deletions(-) delete mode 100644 corefx/src/main/java/com/core/ui/ConfigItem.java diff --git a/corefx/src/main/java/com/core/ui/ConfigItem.java b/corefx/src/main/java/com/core/ui/ConfigItem.java deleted file mode 100644 index fd35583f..00000000 --- a/corefx/src/main/java/com/core/ui/ConfigItem.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.core.ui; - -import com.core.client.rest.ConfigOption; -import com.core.data.ConfigDataType; -import com.jfoenix.controls.JFXButton; -import com.jfoenix.controls.JFXComboBox; -import com.jfoenix.controls.JFXTextField; -import com.jfoenix.controls.JFXToggleButton; -import javafx.scene.Node; -import javafx.scene.control.Label; -import javafx.scene.layout.ColumnConstraints; -import javafx.scene.layout.GridPane; -import javafx.stage.FileChooser; -import javafx.stage.Stage; -import javafx.stage.Window; -import lombok.Data; - -import java.io.File; - -@Data -public class ConfigItem { - private final Window window; - private ConfigOption option; - private Label label; - private Node node; - - public ConfigItem(Stage window, ConfigOption option) { - this.window = window; - this.option = option; - label = new Label(option.getLabel()); - createNode(); - } - - private void createNode() { - ConfigDataType dataType = ConfigDataType.get(option.getType()); - switch (dataType) { - case BOOL: - node = booleanConfig(); - break; - default: - if (!option.getSelect().isEmpty()) { - node = optionsConfig(); - } else if (option.getLabel().contains("file")) { - node = fileConfigItem(); - } else { - node = defaultConfigItem(); - } - break; - } - } - - public ConfigOption getOption() { - String value; - ConfigDataType dataType = ConfigDataType.get(option.getType()); - switch (dataType) { - case BOOL: - JFXToggleButton button = (JFXToggleButton) node; - if (button.isSelected()) { - value = "1"; - } else { - value = "0"; - } - break; - default: - if (!option.getSelect().isEmpty()) { - JFXComboBox comboBox = (JFXComboBox) node; - value = comboBox.getSelectionModel().getSelectedItem(); - } else { - JFXTextField textField = (JFXTextField) node; - value = textField.getText(); - } - break; - } - option.setValue(value); - return option; - } - - private GridPane fileConfigItem() { - GridPane gridPane = new GridPane(); - gridPane.setHgap(10); - JFXTextField textField = new JFXTextField(option.getValue()); - textField.setMaxWidth(Double.MAX_VALUE); - gridPane.addColumn(0, textField); - JFXButton button = new JFXButton("File"); - button.setMaxWidth(Double.MAX_VALUE); - button.getStyleClass().add("core-button"); - gridPane.addColumn(1, button); - ColumnConstraints firstColumn = new ColumnConstraints(10); - firstColumn.setPercentWidth(80); - ColumnConstraints secondColumn = new ColumnConstraints(10); - secondColumn.setPercentWidth(20); - gridPane.getColumnConstraints().addAll(firstColumn, secondColumn); - button.setOnAction(event -> { - FileChooser fileChooser = new FileChooser(); - fileChooser.setTitle("Select File"); - fileChooser.setInitialDirectory(new File(System.getProperty("user.home"))); - File file = fileChooser.showOpenDialog(window); - if (file != null) { - textField.setText(file.getPath()); - } - }); - return gridPane; - } - - private JFXTextField defaultConfigItem() { - JFXTextField textField = new JFXTextField(option.getValue()); - textField.setMaxWidth(Double.MAX_VALUE); - return textField; - } - - private JFXToggleButton booleanConfig() { - JFXToggleButton button = new JFXToggleButton(); - button.setMaxWidth(Double.MAX_VALUE); - if ("1".equals(option.getValue())) { - button.setSelected(true); - } - return button; - } - - private JFXComboBox optionsConfig() { - JFXComboBox comboBox = new JFXComboBox<>(); - comboBox.setMaxWidth(Double.MAX_VALUE); - comboBox.getItems().addAll(option.getSelect()); - comboBox.getSelectionModel().select(option.getValue()); - return comboBox; - } -} diff --git a/corefx/src/main/java/com/core/ui/GraphToolbar.java b/corefx/src/main/java/com/core/ui/GraphToolbar.java index 2db66037..0952a663 100644 --- a/corefx/src/main/java/com/core/ui/GraphToolbar.java +++ b/corefx/src/main/java/com/core/ui/GraphToolbar.java @@ -2,6 +2,7 @@ package com.core.ui; import com.core.Controller; import com.core.data.NodeType; +import com.core.utils.FxmlUtils; import com.core.utils.IconUtils; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXListView; @@ -12,7 +13,6 @@ import javafx.application.Platform; import javafx.css.PseudoClass; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.scene.control.ComboBox; import javafx.scene.control.Label; import javafx.scene.control.ProgressBar; @@ -54,15 +54,7 @@ public class GraphToolbar extends VBox { public GraphToolbar(Controller controller) { this.controller = controller; - FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/graph_toolbar.fxml")); - loader.setRoot(this); - loader.setController(this); - - try { - loader.load(); - } catch (IOException ex) { - throw new RuntimeException(ex); - } + FxmlUtils.loadRootController(this, "/fxml/graph_toolbar.fxml"); startIcon = IconUtils.get("play_circle_filled"); startIcon.setSize(ICON_SIZE); diff --git a/corefx/src/main/java/com/core/ui/dialogs/ConfigDialog.java b/corefx/src/main/java/com/core/ui/dialogs/ConfigDialog.java index 69358145..10f04b27 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/ConfigDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/ConfigDialog.java @@ -4,8 +4,8 @@ import com.core.Controller; import com.core.client.rest.ConfigGroup; import com.core.client.rest.ConfigOption; import com.core.client.rest.GetConfig; -import com.core.data.CoreNode; -import com.core.ui.ConfigItem; +import com.core.ui.config.ConfigItemUtils; +import com.core.ui.config.IConfigItem; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXScrollPane; import com.jfoenix.controls.JFXTabPane; @@ -15,7 +15,6 @@ import javafx.scene.control.ScrollPane; import javafx.scene.control.Tab; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; -import javafx.scene.layout.HBox; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,11 +24,9 @@ import java.util.stream.Collectors; public class ConfigDialog extends StageDialog { private static final Logger logger = LogManager.getLogger(); - private CoreNode coreNode; - private List configItems = new ArrayList<>(); + private List configItems = new ArrayList<>(); private JFXButton saveButton; @FXML private JFXTabPane tabPane; - @FXML private HBox buttonBar; public ConfigDialog(Controller controller) { super(controller, "/fxml/config_dialog.fxml"); @@ -38,7 +35,7 @@ public class ConfigDialog extends StageDialog { } public List getOptions() { - return configItems.stream().map(ConfigItem::getOption).collect(Collectors.toList()); + return configItems.stream().map(IConfigItem::getOption).collect(Collectors.toList()); } public void showDialog(String title, GetConfig getConfig, Runnable runnable) { @@ -64,11 +61,10 @@ public class ConfigDialog extends StageDialog { gridPane.setHgap(10); gridPane.setVgap(10); int index = 0; - logger.info("tabs: {}", tabPane.getTabs()); tabPane.getTabs().add(tab); for (ConfigOption option : group.getOptions()) { - ConfigItem configItem = new ConfigItem(getStage(), option); + IConfigItem configItem = ConfigItemUtils.get(getStage(), option); gridPane.addRow(index, configItem.getLabel(), configItem.getNode()); configItems.add(configItem); index += 1; diff --git a/corefx/src/main/java/com/core/ui/dialogs/HookDialog.java b/corefx/src/main/java/com/core/ui/dialogs/HookDialog.java index 6aeb1315..6e6267ac 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/HookDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/HookDialog.java @@ -26,15 +26,12 @@ public class HookDialog extends StageDialog { public HookDialog(Controller controller) { super(controller, "/fxml/hook_dialog.fxml"); - setTitle("Hook"); - saveButton = createButton("Save"); addCancelButton(); - stateCombo.getItems().addAll( - Arrays.stream(SessionState.values()).map(Enum::name).sorted().collect(Collectors.toList()) - ); + stateCombo.getItems() + .addAll(Arrays.stream(SessionState.values()).map(Enum::name).sorted().collect(Collectors.toList())); stateCombo.getSelectionModel().select(SessionState.RUNTIME.name()); }