Skip to content

Commit

Permalink
Mark GraphCanvas as stable and improve GraphCanvas error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Nov 25, 2023
1 parent 20c1d11 commit 9a6cbe6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ public static <T> void drawTree(T root, Function<T, String> label, Function<T, L
}

/**
* <b>EXPERIMENTAL API</b>
* <p>
* Draws a graph based on the provided graph canvas.
*
* @param graphCanvas canvas containing the graph to draw
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ee/ut/dendroloj/GenericGraphLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,26 @@ public static Graph assembleGraph(GraphCanvas<?> graphCanvas) {
if (vertex.color != null) node.setAttribute("ui.color", vertex.color);
}
for (var edge : graphCanvas.edges) {
Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), IdHelper.getNodeId(edge.v1), IdHelper.getNodeId(edge.v2), edge.directed);
Node v1 = graph.getNode(IdHelper.getNodeId(edge.v1));
Node v2 = graph.getNode(IdHelper.getNodeId(edge.v2));
if (v1 == null && v2 == null) {
throw new IllegalArgumentException("Attempt to render edge " + formatEdge(edge) + ", but vertices " + edge.v1 + " and " + edge.v2 + " were not found");
}
if (v1 == null) {
throw new IllegalArgumentException("Attempt to render edge " + formatEdge(edge) + ", but vertex " + edge.v1 + " was not found");
}
if (v2 == null) {
throw new IllegalArgumentException("Attempt to render edge " + formatEdge(edge) + ", but vertex " + edge.v2 + " was not found");
}
Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), v1, v2, edge.directed);
if (edge.label != null) graphEdge.setAttribute("label", edge.label);
if (edge.color != null) graphEdge.setAttribute("ui.color", edge.color);
}
return graph;
}

private static String formatEdge(GraphCanvas.Edge<?> edge) {
return edge.v1 + (edge.directed ? " -> " : " -- ") + edge.v2;
}

}
14 changes: 12 additions & 2 deletions src/main/java/ee/ut/dendroloj/GraphCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.Set;

/**
* <b>EXPERIMENTAL API</b>
* <p>
* A helper class for drawing a graph.
* Can be passed to {@link Dendrologist#drawGraph(GraphCanvas)} to render the drawn graph on screen.
*
Expand All @@ -30,11 +28,17 @@ public final class GraphCanvas<V> {
// drawVertex(vertex, vertex.toString(), null);
// }

/**
* Draws a vertex with the given label.
*/
public void drawVertex(V vertex, String label) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, null));
}

/**
* Draws a vertex with the given label.
*/
public void drawVertex(V vertex, String label, Color color) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, color));
Expand All @@ -45,11 +49,17 @@ public void drawVertex(V vertex, String label, Color color) {
// drawVertex(vertex, vertex.toString(), null);
// }

/**
* Draws a vertex with the given label at the given coordinates.
*/
public void drawFixedVertex(V vertex, String label, double x, double y) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, null, x, y));
}

/**
* Draws a vertex with the given label at the given coordinates.
*/
public void drawFixedVertex(V vertex, String label, double x, double y, Color color) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
addVertex(new Vertex<>(vertex, label, color, x, y));
Expand Down

0 comments on commit 9a6cbe6

Please sign in to comment.