Skip to content

Commit

Permalink
Fix paths to resource files when using the jar (#44)
Browse files Browse the repository at this point in the history
* Fix paths to resource files when using the jar

* Introduced factory for SmartGraphPanel creation

* Fixed SmartGraph styling bug for package diagrams

* Fix in resource loading approach

---------

Co-authored-by: Vassilis Zafeiris <[email protected]>
  • Loading branch information
dimanyfantakis and bzafiris authored Oct 6, 2024
1 parent d9c2ee3 commit 62e23cf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 18 deletions.
31 changes: 14 additions & 17 deletions src/main/java/model/diagram/javafx/JavaFXClassVisualization.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.brunomnsilva.smartgraph.graph.DigraphEdgeList;
import com.brunomnsilva.smartgraph.graph.Graph;
import com.brunomnsilva.smartgraph.graph.Vertex;
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
import com.brunomnsilva.smartgraph.graphview.SmartGraphPanel;
import model.diagram.ClassDiagram;
import model.graph.Arc;
Expand All @@ -22,7 +21,6 @@ public class JavaFXClassVisualization implements JavaFXVisualization
private SmartGraphPanel<String, String> graphView;
private Collection<Vertex<String>> vertexCollection;


public JavaFXClassVisualization(ClassDiagram diagram)
{
this.classDiagram = diagram;
Expand All @@ -34,17 +32,12 @@ public SmartGraphPanel<String, String> createGraphView()
{
Graph<String, String> graph = createGraph();
vertexCollection = graph.vertices();
graphView = new SmartGraphPanel<>(graph, new SmartCircularSortedPlacementStrategy());
graphView = SmartGraphFactory.createGraphView(graph);
setSinkVertexCustomStyle();
return graphView;
}


@Override
public Collection<Vertex<String>> getVertexCollection()
{
return vertexCollection;
}


private Graph<String, String> createGraph()
Expand All @@ -55,10 +48,10 @@ private Graph<String, String> createGraph()
directedGraph.insertVertex(classifierVertex.getName());
}
insertSinkVertexArcs(directedGraph);

return directedGraph;
}


private void insertSinkVertexArcs(Digraph<String, String> directedGraph)
{
for (Set<Arc<ClassifierVertex>> arcs : classDiagram.getDiagram().values())
Expand Down Expand Up @@ -86,18 +79,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<Vertex<String>> getVertexCollection()
{
return vertexCollection;
}


@Override
public SmartGraphPanel<String, String> getLoadedGraph()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SmartGraphPanel<String, String> createGraphView()
{
Graph<String, String> graph = createGraph();
vertexCollection = graph.vertices();
graphView = new SmartGraphPanel<>(graph, new SmartCircularSortedPlacementStrategy());
graphView = SmartGraphFactory.createGraphView(graph);
setVertexCustomStyle();

return graphView;
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/model/diagram/javafx/SmartGraphFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package model.diagram.javafx;

import com.brunomnsilva.smartgraph.graph.Graph;
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
import com.brunomnsilva.smartgraph.graphview.SmartGraphPanel;
import com.brunomnsilva.smartgraph.graphview.SmartGraphProperties;
import util.Resources;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;

public class SmartGraphFactory {

public static final String DEFAULT_PROPERTIES_PATH = "styles/smartgraph.properties";
public static final String DEFAULT_STYLE_PATH = "styles/smartgraph.css";

/**
* Factory for SmartGraphPanel object creation
* @param graph
* @return
*/
public static SmartGraphPanel<String, String> createGraphView(Graph<String, String> graph)
{
try
{
SmartGraphProperties properties = getSmartgraphProperties();
URI url = getSmartGraphStyleURI();
URI cssFile = Objects.requireNonNull(url);
return new SmartGraphPanel<>(graph, properties, new SmartCircularSortedPlacementStrategy(), cssFile);
}
catch (URISyntaxException ignored)
{
// Fallback to default paths.
return new SmartGraphPanel<>(graph, new SmartCircularSortedPlacementStrategy());
}
}

public static SmartGraphProperties getSmartgraphProperties() {
return new SmartGraphProperties(Resources.loadResourceFile(DEFAULT_PROPERTIES_PATH));
}

public static URI getSmartGraphStyleURI() throws URISyntaxException {
return Resources.getResourceURI(DEFAULT_STYLE_PATH);
}
}
20 changes: 20 additions & 0 deletions src/main/java/util/Resources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package util;

import com.brunomnsilva.smartgraph.graphview.SmartGraphProperties;
import model.diagram.javafx.JavaFXClassVisualization;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

public class Resources {

public static InputStream loadResourceFile(String relativePath) {
return Resources.class.getClassLoader().getResourceAsStream(relativePath);
}

public static URI getResourceURI(String relativePath) throws URISyntaxException {
return Resources.class.getClassLoader().getResource(relativePath).toURI();
}

}

0 comments on commit 62e23cf

Please sign in to comment.