Skip to content

Commit ae08719

Browse files
author
brunomnsilva
committed
Fixed removal of individual edges from the underlying graph not
being represented in the panel. Only edges connected to removed vertices were being removed.
1 parent 27a59d0 commit ae08719

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/com/brunomnsilva/smartgraph/Main.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.brunomnsilva.smartgraph.containers.SmartGraphDemoContainer;
3939
import com.brunomnsilva.smartgraph.graph.Digraph;
4040
import com.brunomnsilva.smartgraph.graph.DigraphEdgeList;
41+
import com.brunomnsilva.smartgraph.graph.Edge;
4142
import com.brunomnsilva.smartgraph.graphview.SmartCircularSortedPlacementStrategy;
4243

4344
/**
@@ -102,6 +103,11 @@ public void start(Stage ignored) {
102103
System.out.println("Edge contains element: " + graphEdge.getUnderlyingEdge().element());
103104
//dynamically change the style when clicked
104105
graphEdge.setStyle("-fx-stroke: black; -fx-stroke-width: 2;");
106+
107+
//uncomment to see edges being removed after click
108+
//Edge<String, String> underlyingEdge = graphEdge.getUnderlyingEdge();
109+
//g.removeEdge(underlyingEdge);
110+
//graphView.update();
105111
});
106112

107113
/*

src/com/brunomnsilva/smartgraph/graphview/SmartGraphPanel.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ private void removeNodes() {
556556
Set<SmartGraphVertexNode<V>> verticesToRemove = new HashSet<>();
557557
Set<SmartGraphEdgeBase> edgesToRemove = new HashSet<>();
558558

559+
//filter vertices to remove and their adjacent edges
559560
for (Vertex<V> v : removedVertices) {
560561

561562
for (SmartGraphEdgeBase edge : values) {
@@ -581,6 +582,14 @@ private void removeNodes() {
581582
vertexNodes.remove(v.getUnderlyingVertex());
582583
removeVertice(v);
583584
}
585+
586+
//permanently remove remaining edges that were removed from the underlying graph
587+
Collection<Edge<E, V>> removedEdges = removedEdges();
588+
for (Edge<E, V> e : removedEdges) {
589+
SmartGraphEdgeBase edgeToRemove = edgeNodes.get(e);
590+
edgeNodes.remove(e);
591+
removeEdge(edgeToRemove);
592+
}
584593

585594
//remove adjacencies from remaining vertices
586595
for (SmartGraphVertexNode<V> v : vertexNodes.values()) {
@@ -739,7 +748,7 @@ private Collection<Vertex<V>> unplottedVertices() {
739748
}
740749

741750
/**
742-
* Computes the vertex collection that are currently being displayed but do
751+
* Computes the collection for vertices that are currently being displayed but do
743752
* not longer exist in the underlying graph.
744753
*
745754
* @return collection of vertices
@@ -758,6 +767,27 @@ private Collection<Vertex<V>> removedVertices() {
758767

759768
return removed;
760769
}
770+
771+
/**
772+
* Computes the collection for edges that are currently being displayed but do
773+
* not longer exist in the underlying graph.
774+
*
775+
* @return collection of edges
776+
*/
777+
private Collection<Edge<E, V>> removedEdges() {
778+
List<Edge<E, V>> removed = new LinkedList<>();
779+
780+
Collection<Edge<E, V>> graphEdges = theGraph.edges();
781+
Collection<SmartGraphEdgeBase> plotted = edgeNodes.values();
782+
783+
for (SmartGraphEdgeBase e : plotted) {
784+
if (!graphEdges.contains(e.getUnderlyingEdge())) {
785+
removed.add(e.getUnderlyingEdge());
786+
}
787+
}
788+
789+
return removed;
790+
}
761791

762792
/**
763793
* Computes the edge collection of the underlying graph that are not

0 commit comments

Comments
 (0)