From 9a2625e5020f1b9bb3d00731778549b3704b1e3e Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Thu, 29 Nov 2018 15:52:59 -0800 Subject: [PATCH] corefx - fixed issue with home directory creation due to log file, added configuration for node labels in preferences --- corefx/src/main/java/com/core/Controller.java | 3 +++ corefx/src/main/java/com/core/Main.java | 2 +- .../core/graph/CoreVertexLabelRenderer.java | 15 ++++++++--- .../java/com/core/graph/NetworkGraph.java | 16 ++++++++++- .../core/ui/dialogs/GuiPreferencesDialog.java | 9 +++++++ .../main/java/com/core/utils/ConfigUtils.java | 27 ++++++++++++------- .../java/com/core/utils/Configuration.java | 2 ++ .../main/resources/fxml/gui_preferences.fxml | 13 +++++++++ 8 files changed, 72 insertions(+), 15 deletions(-) diff --git a/corefx/src/main/java/com/core/Controller.java b/corefx/src/main/java/com/core/Controller.java index d6334c12..71ad5f4c 100644 --- a/corefx/src/main/java/com/core/Controller.java +++ b/corefx/src/main/java/com/core/Controller.java @@ -432,6 +432,9 @@ public class Controller implements Initializable { logger.info("controller initialize"); swingNode.setContent(networkGraph.getGraphViewer()); + // update graph preferences + networkGraph.updatePreferences(configuration); + // set node types / default services graphToolbar.setupNodeTypes(); defaultServices = configuration.getNodeTypeConfigs().stream() diff --git a/corefx/src/main/java/com/core/Main.java b/corefx/src/main/java/com/core/Main.java index 41b84e40..4834278f 100644 --- a/corefx/src/main/java/com/core/Main.java +++ b/corefx/src/main/java/com/core/Main.java @@ -25,7 +25,7 @@ public class Main extends Application { System.setProperty("core_log", LOG_FILE.toString()); // check for and create gui home directory - ConfigUtils.checkForHomeDirectory(); + ConfigUtils.checkHomeDirectory(); // load svg icons SVGGlyphLoader.loadGlyphsFont(getClass().getResourceAsStream("/icons/icomoon_material.svg"), diff --git a/corefx/src/main/java/com/core/graph/CoreVertexLabelRenderer.java b/corefx/src/main/java/com/core/graph/CoreVertexLabelRenderer.java index 88d03b67..25c49fa3 100644 --- a/corefx/src/main/java/com/core/graph/CoreVertexLabelRenderer.java +++ b/corefx/src/main/java/com/core/graph/CoreVertexLabelRenderer.java @@ -7,19 +7,26 @@ import javax.swing.border.EmptyBorder; import java.awt.*; public class CoreVertexLabelRenderer extends DefaultVertexLabelRenderer { + private Color foregroundColor = Color.WHITE; + private Color backgroundColor = Color.BLACK; - CoreVertexLabelRenderer(Color pickedVertexLabelColor) { - super(pickedVertexLabelColor); + CoreVertexLabelRenderer() { + super(Color.YELLOW); + } + + public void setColors(Color foregroundColor, Color backgroundColor) { + this.foregroundColor = foregroundColor; + this.backgroundColor = backgroundColor; } @Override public Component getVertexLabelRendererComponent(JComponent vv, Object value, Font font, boolean isSelected, V vertex) { - super.setForeground(Color.WHITE); + super.setForeground(foregroundColor); if (isSelected) { this.setForeground(this.pickedVertexLabelColor); } - super.setBackground(Color.BLACK); + super.setBackground(backgroundColor); if (font != null) { this.setFont(font); } else { diff --git a/corefx/src/main/java/com/core/graph/NetworkGraph.java b/corefx/src/main/java/com/core/graph/NetworkGraph.java index 383a6108..c8357d7a 100644 --- a/corefx/src/main/java/com/core/graph/NetworkGraph.java +++ b/corefx/src/main/java/com/core/graph/NetworkGraph.java @@ -3,6 +3,7 @@ package com.core.graph; import com.core.Controller; import com.core.data.*; import com.core.ui.Toast; +import com.core.utils.Configuration; import com.core.utils.IconUtils; import com.google.common.base.Supplier; import edu.uci.ics.jung.algorithms.layout.StaticLayout; @@ -56,6 +57,7 @@ public class NetworkGraph { private CorePopupGraphMousePlugin customPopupPlugin; private CoreAnnotatingGraphMousePlugin customAnnotatingPlugin; private BackgroundPaintable backgroundPaintable; + private CoreVertexLabelRenderer nodeLabelRenderer = new CoreVertexLabelRenderer(); public NetworkGraph(Controller controller) { this.controller = controller; @@ -70,7 +72,7 @@ public class NetworkGraph { // node render properties renderContext.setVertexLabelTransformer(CoreNode::getName); - renderContext.setVertexLabelRenderer(new CoreVertexLabelRenderer(Color.YELLOW)); + renderContext.setVertexLabelRenderer(nodeLabelRenderer); renderContext.setVertexShapeTransformer(node -> { double offset = -(IconUtils.ICON_SIZE / 2.0); return new Ellipse2D.Double(offset, offset, IconUtils.ICON_SIZE, IconUtils.ICON_SIZE); @@ -171,6 +173,18 @@ public class NetworkGraph { }); } + private Color convertJfxColor(String hexValue) { + javafx.scene.paint.Color color = javafx.scene.paint.Color.web(hexValue); + return new Color((float) color.getRed(), (float) color.getGreen(), (float) color.getBlue()); + } + + public void updatePreferences(Configuration configuration) { + Color nodeLabelColor = convertJfxColor(configuration.getNodeLabelColor()); + Color nodeLabelBackgroundColor = convertJfxColor(configuration.getNodeLabelBackgroundColor()); + nodeLabelRenderer.setColors(nodeLabelColor, nodeLabelBackgroundColor); + graphViewer.repaint(); + } + public void setBackground(String imagePath) { try { backgroundPaintable = new BackgroundPaintable<>(imagePath, graphViewer); diff --git a/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java b/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java index cbd7fd51..68c4132a 100644 --- a/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java +++ b/corefx/src/main/java/com/core/ui/dialogs/GuiPreferencesDialog.java @@ -5,10 +5,12 @@ import com.core.ui.Toast; import com.core.utils.ConfigUtils; import com.core.utils.Configuration; import com.jfoenix.controls.JFXButton; +import com.jfoenix.controls.JFXColorPicker; import com.jfoenix.controls.JFXTextField; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.fxml.FXML; +import javafx.scene.paint.Color; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -20,6 +22,8 @@ public class GuiPreferencesDialog extends StageDialog { @FXML private JFXTextField mobilityFilePathTextField; @FXML private JFXTextField shellCommandTextField; @FXML private JFXTextField iconPathTextField; + @FXML private JFXColorPicker nodeLabelColorPicker; + @FXML private JFXColorPicker nodeLabelBackgroundColorPicker; @FXML private JFXButton saveButton; public GuiPreferencesDialog(Controller controller) { @@ -36,6 +40,9 @@ public class GuiPreferencesDialog extends StageDialog { configuration.setMobilityPath(mobilityFilePathTextField.getText()); configuration.setShellCommand(shellCommandTextField.getText()); configuration.setIconPath(iconPathTextField.getText()); + configuration.setNodeLabelColor(nodeLabelColorPicker.getValue().toString()); + configuration.setNodeLabelBackgroundColor(nodeLabelBackgroundColorPicker.getValue().toString()); + getController().getNetworkGraph().updatePreferences(configuration); try { ConfigUtils.save(configuration); Toast.success("Updated preferences"); @@ -51,6 +58,8 @@ public class GuiPreferencesDialog extends StageDialog { mobilityFilePathTextField.setText(configuration.getMobilityPath()); shellCommandTextField.setText(configuration.getShellCommand()); iconPathTextField.setText(configuration.getIconPath()); + nodeLabelColorPicker.setValue(Color.web(configuration.getNodeLabelColor())); + nodeLabelBackgroundColorPicker.setValue(Color.web(configuration.getNodeLabelBackgroundColor())); show(); } } diff --git a/corefx/src/main/java/com/core/utils/ConfigUtils.java b/corefx/src/main/java/com/core/utils/ConfigUtils.java index a48a1b0d..0859c4c3 100644 --- a/corefx/src/main/java/com/core/utils/ConfigUtils.java +++ b/corefx/src/main/java/com/core/utils/ConfigUtils.java @@ -1,6 +1,7 @@ package com.core.utils; import com.core.data.NodeType; +import javafx.scene.paint.Color; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -59,13 +60,19 @@ public final class ConfigUtils { return new NodeTypeConfig(model, display, iconPath.toUri().toString(), services); } - public static void checkForHomeDirectory() throws IOException { - if (!HOME.toFile().exists()) { - logger.info("creating core home directory"); - Files.createDirectory(HOME); - Files.createDirectory(XML_DIR); - Files.createDirectory(MOBILITY_DIR); - Files.createDirectory(ICON_DIR); + private static void checkDirectory(Path path) throws IOException { + if (!path.toFile().exists()) { + Files.createDirectory(path); + } + } + + public static void checkHomeDirectory() throws IOException { + logger.info("checking core home directory"); + checkDirectory(HOME); + checkDirectory(XML_DIR); + checkDirectory(MOBILITY_DIR); + checkDirectory(ICON_DIR); + if (!CONFIG_FILE.toFile().exists()) { createDefaultConfigFile(); } } @@ -78,6 +85,8 @@ public final class ConfigUtils { configuration.setMobilityPath(MOBILITY_DIR.toString()); configuration.setIconPath(ICON_DIR.toString()); configuration.setNodeTypeConfigs(createDefaults()); + configuration.setNodeLabelColor(Color.WHITE.toString()); + configuration.setNodeLabelBackgroundColor(Color.BLACK.toString()); save(configuration); } @@ -103,8 +112,8 @@ public final class ConfigUtils { return configuration; } catch (IOException ex) { - logger.error("error reading config file"); - throw new RuntimeException("configuration file did not exist"); + logger.error("error reading config file", ex); + throw new RuntimeException("configuration file did not exist", ex); } } } diff --git a/corefx/src/main/java/com/core/utils/Configuration.java b/corefx/src/main/java/com/core/utils/Configuration.java index e7815166..d5faec05 100644 --- a/corefx/src/main/java/com/core/utils/Configuration.java +++ b/corefx/src/main/java/com/core/utils/Configuration.java @@ -15,4 +15,6 @@ public class Configuration { private String iconPath; private String shellCommand; private List nodeTypeConfigs = new ArrayList<>(); + private String nodeLabelColor; + private String nodeLabelBackgroundColor; } diff --git a/corefx/src/main/resources/fxml/gui_preferences.fxml b/corefx/src/main/resources/fxml/gui_preferences.fxml index cbe6a44f..64057360 100644 --- a/corefx/src/main/resources/fxml/gui_preferences.fxml +++ b/corefx/src/main/resources/fxml/gui_preferences.fxml @@ -1,5 +1,6 @@ + @@ -21,6 +22,18 @@ + + + + + + + +