Browse Source

Node and Edge Creation now use GraphManager instead of Graph directly,
prep for operator graph dropdown

Jan Enders 7 years ago
parent
commit
4e2de447c1

+ 61 - 24
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/GraphManager.java

@@ -37,6 +37,12 @@ public class GraphManager {
 	/** The Graph this instance of GraphManager manages. */
 	protected MyGraph g;
 
+	/**
+	 * The Subgraph to add new Nodes and Edges to if the current Graph is a
+	 * composite.
+	 */
+	protected MyGraph graphToAddTo;
+
 	/**
 	 * The Stylesheet for this Graph, excluding parts that can be set by
 	 * NodeGraphics.
@@ -80,10 +86,11 @@ public class GraphManager {
 	 */
 	public GraphManager(MyGraph graph) {
 		g = graph;
-		/* Viewer */ viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
+		graphToAddTo = graph;
+		viewer = new Viewer(g, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
 		view = viewer.addDefaultView(false);
 		viewer.setCloseFramePolicy(Viewer.CloseFramePolicy.EXIT);
-		/* ViewerPipe */fromViewer = viewer.newViewerPipe();
+		fromViewer = viewer.newViewerPipe();
 		view.setMouseManager(new MyMouseManager(this));
 		fromViewer.addSink(graph);
 		fromViewer.removeElementSink(graph);
@@ -105,6 +112,9 @@ public class GraphManager {
 		// and need the Node to still be in the Graph
 		deleteEdgesOfNode(id);
 		deletedNode = g.removeNode(id);
+		if (graphToAddTo != g) {
+			graphToAddTo.removeNode(id);
+		}
 	}
 
 	/**
@@ -120,6 +130,9 @@ public class GraphManager {
 		deletedEdges.removeAll(deletedEdges);
 		deletedNode = null;
 		deletedEdges.add(g.removeEdge(id));
+		if (graphToAddTo != g) {
+			graphToAddTo.removeEdge(id);
+		}
 	}
 
 	/**
@@ -143,6 +156,9 @@ public class GraphManager {
 				// adds the Edge to the list of deleted Edges and remove sit
 				// from the Graph
 				deletedEdges.add(g.removeEdge(e));
+				if (graphToAddTo != g) {
+					graphToAddTo.removeEdge(e);
+				}
 			}
 		}
 	}
@@ -155,7 +171,6 @@ public class GraphManager {
 	 */
 	public void undelete() {
 		String newId = "";
-		// System.out.println("test-undel");
 		HashMap<String, Object> attributes = new HashMap<String, Object>();
 		if (deletedNode != null) {
 			for (String s : deletedNode.getAttributeKeySet()) {
@@ -164,6 +179,10 @@ public class GraphManager {
 			newId = Main.getInstance().getUnusedID();
 			g.addNode(newId);
 			g.getNode(newId).addAttributes(attributes);
+			if (graphToAddTo != g) {
+				graphToAddTo.addNode(newId);
+				graphToAddTo.getNode(newId).addAttributes(attributes);
+			}
 		}
 
 		for (Edge e : deletedEdges) {
@@ -184,6 +203,10 @@ public class GraphManager {
 			}
 			g.addEdge(id, sourceId, targetId);
 			g.getEdge(id).addAttributes(attributes);
+			if (graphToAddTo != g) {
+				graphToAddTo.addEdge(id, sourceId, targetId);
+				graphToAddTo.getEdge(id).addAttributes(attributes);
+			}
 		}
 		deletedEdges = new LinkedList<>();
 		deletedNode = null;
@@ -359,8 +382,20 @@ public class GraphManager {
 		for (String s : e.getAttributeKeySet()) {
 			attributes.put(s, e.getAttribute(s));
 		}
-		g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode());
-		g.getEdge(e.getId()).addAttributes(attributes);
+
+		Node sourceNode = e.getSourceNode();
+		Node targetNode = e.getTargetNode();
+
+		if (graphToAddTo.getNode(sourceNode.getId()) != null && graphToAddTo.getNode(targetNode.getId()) != null) {
+			g.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode());
+			g.getEdge(e.getId()).addAttributes(attributes);
+			if (graphToAddTo != g) {
+				graphToAddTo.addEdge(e.getId(), (Node) e.getSourceNode(), (Node) e.getTargetNode());
+				graphToAddTo.getEdge(e.getId()).addAttributes(attributes);
+			}
+		} else {
+			Debug.out("Trying to create an Edge where both nodes do not belong to the same subgraph!", 2);
+		}
 	}
 
 	/**
@@ -374,19 +409,20 @@ public class GraphManager {
 		HashMap<String, Object> attributes = new HashMap<>();
 
 		for (String s : n.getAttributeKeySet()) {
-			attributes.put(s, deletedNode.getAttribute(s));
+			attributes.put(s, n.getAttribute(s));
 		}
 		g.addNode(n.getId());
 		g.getNode(n.getId()).addAttributes(attributes);
+		if (graphToAddTo != g) {
+			graphToAddTo.addNode(n.getId());
+			graphToAddTo.getNode(n.getId()).addAttributes(attributes);
+		}
 	}
 
 	/**
 	 * Returns the smallest X Coordinate of any Node in the Graph.
 	 * 
 	 * @return the smallest X Coordinate in the Graph
-	 * @deprecated Use
-	 *             {@link de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph#getMinX()}
-	 *             instead
 	 */
 	public double getMinX() {
 		return g.getMinX();
@@ -396,9 +432,6 @@ public class GraphManager {
 	 * Returns the biggest X Coordinate of any Node in the Graph.
 	 * 
 	 * @return the biggest X Coordinate in the Graph
-	 * @deprecated Use
-	 *             {@link de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph#getMaxX()}
-	 *             instead
 	 */
 	public double getMaxX() {
 		return g.getMaxX();
@@ -408,9 +441,6 @@ public class GraphManager {
 	 * Returns the smallest Y Coordinate of any Node in the Graph.
 	 * 
 	 * @return the smallest Y Coordinate in the Graph
-	 * @deprecated Use
-	 *             {@link de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph#getMinY()}
-	 *             instead
 	 */
 	public double getMinY() {
 		return g.getMinY();
@@ -420,9 +450,6 @@ public class GraphManager {
 	 * Returns the biggest Y Coordinate of any Node in the Graph.
 	 * 
 	 * @return the biggest Y Coordinate in the Graph
-	 * @deprecated Use
-	 *             {@link de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph#getMaxY()}
-	 *             instead
 	 */
 	public double getMaxY() {
 		return g.getMaxY();
@@ -535,15 +562,25 @@ public class GraphManager {
 		if (getGraph().getNode(from).hasEdgeBetween(to))
 			return false;
 		String newID = Main.getInstance().getUnusedID();
+		boolean isDirected = (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE);
 
-		if (Main.getInstance().getCreationMode() == CreationMode.CREATE_DIRECTED_EDGE) {
-			getGraph().addEdge(newID, from, to, true);
-			Debug.out("Created an directed edge with Id " + newID + " between " + from + " and " + to);
+		if (graphToAddTo.getNode(from) != null && graphToAddTo.getNode(to) != null) {
+			g.addEdge(newID, from, to, isDirected);
+			if (graphToAddTo != g) {
+				graphToAddTo.addEdge(newID, from, to, isDirected);
+			}
 		} else {
-			getGraph().addEdge(newID, from, to);
-			Debug.out("Created an undirected edge with Id " + newID + " between " + from + " and " + to);
+			Debug.out("Trying to create an Edge where both nodes do not belong to the same subgraph!", 2);
 		}
-
+		/*
+		 * if (Main.getInstance().getCreationMode() ==
+		 * CreationMode.CREATE_DIRECTED_EDGE) { getGraph().addEdge(newID, from,
+		 * to, true); Debug.out("Created an directed edge with Id " + newID +
+		 * " between " + from + " and " + to); } else {
+		 * getGraph().addEdge(newID, from, to);
+		 * Debug.out("Created an undirected edge with Id " + newID + " between "
+		 * + from + " and " + to); }
+		 */
 		selectEdge(newID);
 
 		return true;

+ 6 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/MyGraph.java

@@ -127,9 +127,14 @@ public class MyGraph extends SingleGraph {
 	 * Notifies all added NodeCreatedListener. also sets defaults
 	 * 
 	 * @param n
-	 *            the Edge that was just created
+	 *            the Node that was just created
 	 */
 	private void nodeCreatedNotify(Node n) {
+		if (n.getAttribute("typeofNode") != null && n.getAttribute("typeofNode").equals("procEn")) {
+			ToolboxManager.createProcMaxDialog(n);
+		} else if (n.getAttribute("typeOfNode") != null && n.getAttribute("typeofNode").equals("operator")) {
+			ToolboxManager.createProcNeedDialog(n);
+		}
 		GraphHelper.setAllDefaults(this);
 		for (NodeCreatedListener list : allNodeListeners) {
 			list.nodeCreated(n, id);

+ 8 - 2
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ToolboxManager.java

@@ -357,19 +357,23 @@ public final class ToolboxManager {
 	private static org.graphstream.graph.Node lastCreatedNode = null;
 
 	public static void createProcMaxDialog(org.graphstream.graph.Node n) {
+
 		if (n.equals(lastCreatedNode)) {
 			return;
 		}
 		lastCreatedNode = n;
 		Platform.runLater(() -> {
+
 			TextInputDialog weightDialog = new TextInputDialog(Double.toString(OptionsManager.getDefaultWeight()));
 			weightDialog.setTitle("Maximum Processing Power");
 			weightDialog.setHeaderText("Please enter the maximum processing power of the Node");
 			weightDialog.setContentText("processing power");
 			Optional<String> result = weightDialog.showAndWait();
+			org.graphstream.graph.Node actualNode = Main.getInstance().getGraphManager().getGraph().getNode(n.getId());
 			if (result.isPresent()) {
-				n.addAttribute("process-max", Double.parseDouble(result.get()));
+				actualNode.addAttribute("process-max", Double.parseDouble(result.get()));
 			}
+			PropertiesManager.setItemsProperties();
 		});
 	}
 
@@ -384,9 +388,11 @@ public final class ToolboxManager {
 			weightDialog.setHeaderText("Please enter the amount of processing power the node needs");
 			weightDialog.setContentText("neede Power");
 			Optional<String> result = weightDialog.showAndWait();
+			org.graphstream.graph.Node actualNode = Main.getInstance().getGraphManager().getGraph().getNode(n.getId());
 			if (result.isPresent()) {
-				n.addAttribute("process-need", Double.parseDouble(result.get()));
+				actualNode.addAttribute("process-need", Double.parseDouble(result.get()));
 			}
+			PropertiesManager.setItemsProperties();
 		});
 	}
 }

+ 12 - 9
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/handlers/MyMouseManager.java

@@ -3,7 +3,6 @@ package de.tu_darmstadt.informatik.tk.scopviz.ui.handlers;
 import java.awt.event.MouseEvent;
 
 import org.graphstream.graph.Edge;
-import org.graphstream.graph.Graph;
 import org.graphstream.graph.Node;
 import org.graphstream.ui.geom.Point3;
 import org.graphstream.ui.graphicGraph.GraphicElement;
@@ -12,6 +11,7 @@ import org.graphstream.ui.view.util.DefaultMouseManager;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.graphs.GraphManager;
+import de.tu_darmstadt.informatik.tk.scopviz.graphs.MyGraph;
 import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
 import de.tu_darmstadt.informatik.tk.scopviz.main.EdgeSelectionHelper;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
@@ -69,9 +69,9 @@ public class MyMouseManager extends DefaultMouseManager {
 	protected void mouseButtonPress(MouseEvent event) {
 		view.requestFocus();
 
-		Graph graph = graphManager.getGraph();
 		Point3 cursorPos = graphManager.getView().getCamera().transformPxToGu(event.getX(), event.getY());
 		Node n;
+		MyGraph nodeProducer = new MyGraph("temp");
 		Edge selectedEdge = EdgeSelectionHelper.getClosestEdge(cursorPos);
 
 		switch (Main.getInstance().getCreationMode()) {
@@ -86,10 +86,11 @@ public class MyMouseManager extends DefaultMouseManager {
 
 		// Otherwise, create node based on creation Mode
 		case CREATE_STANDARD_NODE:
-			n = graph.addNode(Main.getInstance().getUnusedID());
+			n = nodeProducer.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "standard");
 			n.setAttribute("typeofNode", "standard");
+			graphManager.addNode(n);
 			graphManager.selectNode(n.getId());
 			Debug.out("INFORMATION: Added Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
 					+ cursorPos.y + ")", 1);
@@ -97,10 +98,11 @@ public class MyMouseManager extends DefaultMouseManager {
 			break;
 
 		case CREATE_SOURCE_NODE:
-			n = graph.addNode(Main.getInstance().getUnusedID());
+			n = nodeProducer.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "source");
 			n.setAttribute("typeofNode", "source");
+			graphManager.addNode(n);
 			graphManager.selectNode(n.getId());
 			Debug.out("INFORMATION: Added Source Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
 					+ cursorPos.y + ")", 1);
@@ -108,10 +110,11 @@ public class MyMouseManager extends DefaultMouseManager {
 			break;
 
 		case CREATE_SINK_NODE:
-			n = graph.addNode(Main.getInstance().getUnusedID());
+			n = nodeProducer.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "sink");
 			n.setAttribute("typeofNode", "sink");
+			graphManager.addNode(n);
 			graphManager.selectNode(n.getId());
 			Debug.out("INFORMATION: Added Sink Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
 					+ cursorPos.y + ")", 1);
@@ -119,12 +122,12 @@ public class MyMouseManager extends DefaultMouseManager {
 			break;
 
 		case CREATE_PROC_NODE:
-			n = graph.addNode(Main.getInstance().getUnusedID());
+			n = nodeProducer.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "procEn");
 			n.setAttribute("typeofNode", "procEn");
 			ToolboxManager.createProcMaxDialog(n);
-
+			graphManager.addNode(n);
 			graphManager.selectNode(n.getId());
 			Debug.out("INFORMATION: Added ProcEn Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
 					+ cursorPos.y + ")", 1);
@@ -132,12 +135,12 @@ public class MyMouseManager extends DefaultMouseManager {
 			break;
 
 		case CREATE_OPERATOR_NODE:
-			n = graph.addNode(Main.getInstance().getUnusedID());
+			n = nodeProducer.addNode(Main.getInstance().getUnusedID());
 			n.setAttribute("xyz", cursorPos);
 			n.setAttribute("ui.class", "operator");
 			n.setAttribute("typeofNode", "operator");
 			ToolboxManager.createProcNeedDialog(n);
-
+			graphManager.addNode(n);
 			graphManager.selectNode(n.getId());
 			Debug.out("INFORMATION: Added Operator Node with ID " + n.getId() + " at Position (" + cursorPos.x + "/"
 					+ cursorPos.y + ")", 1);