فهرست منبع

Highly experimental!

new GraphManager
changes to multiple Classes to incorporate the GraphManager
Matthias Wilhelm 8 سال پیش
والد
کامیت
bd45981e0b

+ 8 - 4
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/GraphMLImporter.java

@@ -20,12 +20,14 @@ public class GraphMLImporter {
 	/**
 	 * Imports a GraphML file.
 	 * 
+	 * @param id
+	 *            unique ID
 	 * @param fileName
 	 *            path to the file on disk
 	 * @return the imported Graphstream-Graph
 	 */
-	public Graph readGraph(final String fileName) {
-		Graph g = new DefaultGraph("g");
+	public Graph readGraph(String id, final String fileName) {
+		Graph g = new DefaultGraph(id);
 		FileSource fs = new FileSourceGraphML();
 		fs.addSink(g);
 		try {
@@ -41,12 +43,14 @@ public class GraphMLImporter {
 	/**
 	 * Imports a GraphML file.
 	 * 
+	 * @param id
+	 *            unique ID
 	 * @param fileURL
 	 *            URL of the file
 	 * @return the imported Graphstream-Graph
 	 */
-	public Graph readGraph(final URL fileURL) {
-		Graph g = new DefaultGraph("g");
+	public Graph readGraph(String id, final URL fileURL) {
+		Graph g = new DefaultGraph(id);
 		FileSource fs = new FileSourceGraphML();
 		fs.addSink(g);
 		try {

+ 8 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/package-info.java

@@ -0,0 +1,8 @@
+/**
+ * 
+ */
+/**
+ * @author jascha-b
+ *
+ */
+package de.tu_darmstadt.informatik.tk.scopviz.io;

+ 127 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/GraphManager.java

@@ -0,0 +1,127 @@
+package de.tu_darmstadt.informatik.tk.scopviz.main;
+
+import java.net.URL;
+import java.util.ArrayList;
+
+import javax.swing.JPanel;
+
+import org.graphstream.graph.Graph;
+
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.Visualizer;
+import javafx.embed.swing.SwingNode;
+import javafx.scene.layout.Pane;
+
+/**
+ * Class holds all Graphs and Functions to interact with them.
+ * 
+ * @author Matthias Wilhelm
+ * @version 1.0
+ *
+ */
+public class GraphManager {
+	private static final String GRAPH_STRING_ID_PREFIX = "graph";
+	ArrayList<Graph> gList;
+	ArrayList<Visualizer> vList;
+	private static GraphManager instance;
+	private int count;
+	private static GUIController guiController;
+	private int currentVisualizer = 0; 
+
+	public static void setGuiController(GUIController guiController) {
+		GraphManager.guiController = guiController;
+	}
+
+	private GraphManager() {
+		count = 0;
+		gList = new ArrayList<Graph>();
+		vList = new ArrayList<Visualizer>();
+	}
+
+	/**
+	 * Returns the singular instance of the Class, grants access to the
+	 * Singleton. Initializes the instance when called for the first time.
+	 * 
+	 * @return the singular instance of the class
+	 */
+	public static GraphManager getInstance() {
+		if (instance == null) {
+			instance = new GraphManager();
+		}
+		return instance;
+	}
+
+	/**
+	 * Adds an empty Graph to the collection.
+	 * 
+	 * @return the id to access the specific Graph
+	 */
+	public int addGraph() {
+		String id = getGraphStringID(count);
+		Graph g = new MyGraph(id);
+		Visualizer v = new Visualizer(g);
+		gList.add(g);
+		vList.add(v);
+		return ++count;
+	}
+
+	/**
+	 * Imports and adds the specified Graph to the collection.
+	 * 
+	 * @param fileName
+	 *            path to the file on disk
+	 * @return the id to access the specific Graph
+	 */
+	public int addGraph(String fileName) {
+		String id = getGraphStringID(count);
+		GraphMLImporter importer = new GraphMLImporter();
+		Graph g = importer.readGraph(id, Main.class.getResource(fileName));
+		Visualizer v = new Visualizer(g);
+		gList.add(g);
+		vList.add(v);
+		return count++;
+	}
+
+	/**
+	 * Imports and adds the specified Graph to the collection.
+	 * 
+	 * @param fileURL
+	 *            URL of the file
+	 * @return the id to access the specific Graph
+	 */
+	public int addGraph(URL fileURL) {
+		String id = getGraphStringID(count);
+		GraphMLImporter importer = new GraphMLImporter();
+		Graph g = importer.readGraph(id, fileURL);
+		Visualizer v = new Visualizer(g);
+		gList.add(g);
+		vList.add(v);
+		return ++count;
+	}
+
+	/**
+	 * Returns the Visualizer for the given graph id
+	 * 
+	 * @param id
+	 *            of the graph
+	 * @return visualizer for the graph
+	 */
+	public Visualizer getVisualizer(int id) {
+		return vList.get(id);
+	}
+
+	private String getGraphStringID(int id) {
+		return GRAPH_STRING_ID_PREFIX + id;
+	}
+
+	public void switchActiveGraph(int id) {
+		currentVisualizer = id;
+		guiController.swingNode.setContent((JPanel) vList.get(id).getView());
+	}
+	
+	public Visualizer getCurrentVisualizer() {		
+		return vList.get(currentVisualizer);
+	}
+}

+ 24 - 7
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/Main.java

@@ -1,10 +1,11 @@
 package de.tu_darmstadt.informatik.tk.scopviz.main;
 
-import java.util.Random;
-
 import org.graphstream.graph.Graph;
+import org.graphstream.graph.Node;
 
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.io.GraphMLImporter;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.GUIController;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.Visualizer;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyAnimationTimer;
 import javafx.animation.AnimationTimer;
@@ -18,6 +19,8 @@ import javafx.animation.AnimationTimer;
  *
  */
 public final class Main {
+	
+	private GraphManager gm;
 
 	/**
 	 * Singular instance of the Class, facilitates Singleton pattern.
@@ -48,12 +51,26 @@ public final class Main {
 	 * manage it
 	 */
 	private Main() {
-		GraphMLImporter importer = new GraphMLImporter();
-		graph = importer.readGraph(Main.class.getResource("/Example.graphml"));
-		visualizer = new Visualizer(graph);
+		gm = GraphManager.getInstance();
+		int gID = gm.addGraph("/Example.graphml");
+		/*GraphMLImporter importer = new GraphMLImporter();
+		graph = importer.readGraph("g", Main.class.getResource("/Example.graphml"));
+		Node n = graph.addNode("upps");
+		n.setAttribute("x", 150);
+		n.setAttribute("y", 150);
+		visualizer = new Visualizer(graph);*/
+		Node n = gm.getVisualizer(gID).getGraph().addNode("upps");
+		n.setAttribute("x", 150);
+		n.setAttribute("y", 150);
 		AnimationTimer alwaysPump = new MyAnimationTimer();
 		alwaysPump.start();
 	}
+	
+	public void load2ndGraph(){
+		int gID = gm.addGraph("/Example.graphml");
+		gm.switchActiveGraph(gID);
+		Debug.out("done");
+	}
 
 	/**
 	 * Returns the singular instance of the Class, grants access to the
@@ -81,7 +98,7 @@ public final class Main {
 	 * @return the visualizer in use
 	 */
 	public Visualizer getVisualizer() {
-		return visualizer;
+		return gm.getCurrentVisualizer();
 	}
 
 	/**
@@ -112,7 +129,7 @@ public final class Main {
 		int i = 0;
 		while (true){
 			String tempID = i+"";
-			if (visualizer.getGraph().getNode(tempID) == null && visualizer.getGraph().getEdge(tempID) == null){
+			if (getVisualizer().getGraph().getNode(tempID) == null && getVisualizer().getGraph().getEdge(tempID) == null){
 				return (tempID);
 			} else{
 				i++;

+ 30 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/MyViewerListener.java

@@ -0,0 +1,30 @@
+package de.tu_darmstadt.informatik.tk.scopviz.main;
+
+import org.graphstream.ui.view.ViewerListener;
+
+import de.tu_darmstadt.informatik.tk.scopviz.ui.Visualizer;
+
+public class MyViewerListener implements ViewerListener {
+	private Visualizer v;
+	public MyViewerListener(Visualizer viz) {
+		v=viz;
+	}
+
+	@Override
+	public void buttonPushed(String id) {
+		v.setSelectedNodeID(id);
+	}
+
+	@Override
+	public void buttonReleased(String id) {
+		// TODO Auto-generated method stub
+		
+	}
+
+	@Override
+	public void viewClosed(String viewName) {
+		// TODO Auto-generated method stub
+		
+	}
+
+}

+ 2 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GUIController.java

@@ -6,6 +6,7 @@ import java.util.ResourceBundle;
 import javax.swing.JPanel;
 
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+import de.tu_darmstadt.informatik.tk.scopviz.main.GraphManager;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.ResizeListener;
 import javafx.beans.value.ChangeListener;
@@ -126,12 +127,12 @@ public class GUIController implements Initializable {
 		ToolboxManager.initializeItems(toolbox);
 		PropertiesManager.initializeItems(properties);
 		ButtonManager.initialize(this);
+		GraphManager.setGuiController(this);
 
 		// Bind all the handlers to their corresponding UI elements
 		initializeZoomButtons();
 		initializeCreateButtons();
 		initializeDisplayPane();
-
 	}
 
 	/**

+ 12 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/handlers/MyAnimationTimer.java

@@ -1,14 +1,25 @@
 package de.tu_darmstadt.informatik.tk.scopviz.ui.handlers;
 
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import javafx.animation.AnimationTimer;
 
 public class MyAnimationTimer extends AnimationTimer{
 
+	long time = -1;
+	boolean trigger = true;
 	@Override
 	public void handle(long now) {
 		Main.getInstance().getVisualizer().pumpIt();
-		
+		if (time == -1)
+			time = now;
+		else {
+			if (trigger && time + 2000000000 < now) {
+				Main.getInstance().load2ndGraph();
+				Debug.out("blub");
+				trigger = false;
+			}
+		}
 	}
 
 }