From 8626f6bd60226dcecc651b452099578d63d86da1 Mon Sep 17 00:00:00 2001 From: Dimitris Anyfadakis Date: Sat, 21 Sep 2024 16:51:21 +0200 Subject: [PATCH] Fix paths to resource files when using the jar --- .../javafx/JavaFXClassVisualization.java | 52 ++++++++++++++----- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/src/main/java/model/diagram/javafx/JavaFXClassVisualization.java b/src/main/java/model/diagram/javafx/JavaFXClassVisualization.java index d8ba544..aadb1c4 100644 --- a/src/main/java/model/diagram/javafx/JavaFXClassVisualization.java +++ b/src/main/java/model/diagram/javafx/JavaFXClassVisualization.java @@ -6,18 +6,28 @@ import com.brunomnsilva.smartgraph.graph.Vertex; import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy; import com.brunomnsilva.smartgraph.graphview.SmartGraphPanel; +import com.brunomnsilva.smartgraph.graphview.SmartGraphProperties; import model.diagram.ClassDiagram; import model.graph.Arc; import model.graph.ArcType; import model.graph.ClassifierVertex; import model.graph.VertexType; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Collection; +import java.util.Objects; import java.util.Set; public class JavaFXClassVisualization implements JavaFXVisualization { + public static final String DEFAULT_PROPERTIES_PATH = "styles/smartgraph.properties"; + public static final String DEFAULT_STYLE_PATH = "styles/smartgraph.css"; + private final ClassDiagram classDiagram; private SmartGraphPanel graphView; private Collection> vertexCollection; @@ -34,16 +44,28 @@ public SmartGraphPanel createGraphView() { Graph graph = createGraph(); vertexCollection = graph.vertices(); - graphView = new SmartGraphPanel<>(graph, new SmartCircularSortedPlacementStrategy()); + graphView = createGraphView(graph); setSinkVertexCustomStyle(); return graphView; } - @Override - public Collection> getVertexCollection() + private static SmartGraphPanel createGraphView(Graph graph) { - return vertexCollection; + try (InputStream resource = JavaFXClassVisualization.class.getClassLoader().getResourceAsStream(DEFAULT_PROPERTIES_PATH)) + { + SmartGraphProperties properties = new SmartGraphProperties(resource); + + URL url = JavaFXClassVisualization.class.getClassLoader().getResource(DEFAULT_STYLE_PATH); + URI cssFile = Objects.requireNonNull(url).toURI(); + + return new SmartGraphPanel<>(graph, properties, new SmartCircularSortedPlacementStrategy(), cssFile); + } + catch (IOException | URISyntaxException ignored) + { + // Fallback to default paths. + return new SmartGraphPanel<>(graph, new SmartCircularSortedPlacementStrategy()); + } } @@ -55,10 +77,10 @@ private Graph createGraph() directedGraph.insertVertex(classifierVertex.getName()); } insertSinkVertexArcs(directedGraph); + return directedGraph; } - private void insertSinkVertexArcs(Digraph directedGraph) { for (Set> arcs : classDiagram.getDiagram().values()) @@ -86,18 +108,22 @@ private void setSinkVertexCustomStyle() { for (ClassifierVertex classifierVertex : classDiagram.getDiagram().keySet()) { - if (classifierVertex.getVertexType().equals(VertexType.INTERFACE)) - { - graphView.getStylableVertex(classifierVertex.getName()).setStyleClass("vertexInterface"); - } - else - { - graphView.getStylableVertex(classifierVertex.getName()).setStyleClass("vertexPackage"); - } + String styleClass = classifierVertex.getVertexType().equals(VertexType.INTERFACE) ? + "vertexInterface" : + "vertexPackage"; + + graphView.getStylableVertex(classifierVertex.getName()).setStyleClass(styleClass); } } + @Override + public Collection> getVertexCollection() + { + return vertexCollection; + } + + @Override public SmartGraphPanel getLoadedGraph() {