Browse Source

Multiple Graphs supported

MW 8 years ago
parent
commit
b3ffc01ee1

+ 33 - 41
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/GraphManager.java

@@ -1,21 +1,19 @@
 package de.tu_darmstadt.informatik.tk.scopviz.main;
 
+import java.awt.Dimension;
 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.
+ * This class holds all Visualizers, provides Functions to add Graphs and get
+ * corresponding Visualizers.
  * 
  * @author Matthias Wilhelm
  * @version 1.0
@@ -23,46 +21,24 @@ import javafx.scene.layout.Pane;
  */
 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 ArrayList<Visualizer> vList = new ArrayList<Visualizer>();
+	private static int count = 0;
 	private static GUIController guiController;
-	private int currentVisualizer = 0; 
+	private static 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() {
+	public static int addGraph() {
 		String id = getGraphStringID(count);
 		Graph g = new MyGraph(id);
 		Visualizer v = new Visualizer(g);
-		gList.add(g);
 		vList.add(v);
 		return ++count;
 	}
@@ -74,12 +50,11 @@ public class GraphManager {
 	 *            path to the file on disk
 	 * @return the id to access the specific Graph
 	 */
-	public int addGraph(String fileName) {
+	public static 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++;
 	}
@@ -91,12 +66,11 @@ public class GraphManager {
 	 *            URL of the file
 	 * @return the id to access the specific Graph
 	 */
-	public int addGraph(URL fileURL) {
+	public static 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;
 	}
@@ -108,20 +82,38 @@ public class GraphManager {
 	 *            of the graph
 	 * @return visualizer for the graph
 	 */
-	public Visualizer getVisualizer(int id) {
+	public static Visualizer getVisualizer(int id) {
 		return vList.get(id);
 	}
 
-	private String getGraphStringID(int id) {
+	private static String getGraphStringID(int id) {
 		return GRAPH_STRING_ID_PREFIX + id;
 	}
 
-	public void switchActiveGraph(int id) {
+	/**
+	 * Switches the active Graph to the give id.
+	 * 
+	 * @param id
+	 *            of the graph which to switch to
+	 */
+	public static void switchActiveGraph(int id) {
 		currentVisualizer = id;
-		guiController.swingNode.setContent((JPanel) vList.get(id).getView());
+		// TODO Clean up, is copied out the ResizeListener and should be handled
+		// somewhere else
+		Pane pane = guiController.pane;
+		Main.getInstance().getVisualizer().getView()
+				.setPreferredSize(new Dimension((int) pane.getWidth() - 5, (int) pane.getHeight() - 5));
+		guiController.swingNode.setContent(Main.getInstance().getVisualizer().getView());
 	}
-	
-	public Visualizer getCurrentVisualizer() {		
+
+	/**
+	 * get the current Visualizer. To get change it call
+	 * {@link #switchActiveGraph(int)}
+	 * 
+	 * @return the current Visualizer
+	 * @see #switchActiveGraph(int)
+	 */
+	public static Visualizer getCurrentVisualizer() {
 		return vList.get(currentVisualizer);
 	}
 }

+ 25 - 37
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/Main.java

@@ -1,11 +1,8 @@
 package de.tu_darmstadt.informatik.tk.scopviz.main;
 
-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;
@@ -19,26 +16,11 @@ import javafx.animation.AnimationTimer;
  *
  */
 public final class Main {
-	
-	private GraphManager gm;
-
 	/**
 	 * Singular instance of the Class, facilitates Singleton pattern.
 	 */
 	private static Main instance;
 
-	/**
-	 * The Graph currently used by the Application. Only a reference, the actual
-	 * graph management is done by the visualizer.
-	 */
-	private Graph graph;
-
-	/**
-	 * The Visualizer that manages the Graph instance and provides the View to
-	 * access the Graph.
-	 */
-	private Visualizer visualizer;
-
 	/**
 	 * Current mode of the application for things like creating new Nodes and
 	 * Edges.
@@ -51,24 +33,29 @@ public final class Main {
 	 * manage it
 	 */
 	private Main() {
-		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");
+		int gID = GraphManager.addGraph("/Example.graphml");
+		GraphManager.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 = GraphManager.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);
+
+	private int iid = 0;
+
+	public void load2ndGraph() {
+		if (iid == 0)
+			iid = 1;
+		else
+			iid = 0;
+		GraphManager.switchActiveGraph(iid);
 		Debug.out("done");
 	}
 
@@ -98,7 +85,7 @@ public final class Main {
 	 * @return the visualizer in use
 	 */
 	public Visualizer getVisualizer() {
-		return gm.getCurrentVisualizer();
+		return GraphManager.getCurrentVisualizer();
 	}
 
 	/**
@@ -127,15 +114,16 @@ public final class Main {
 	 */
 	public String getUnusedID() {
 		int i = 0;
-		while (true){
-			String tempID = i+"";
-			if (getVisualizer().getGraph().getNode(tempID) == null && getVisualizer().getGraph().getEdge(tempID) == null){
+		while (true) {
+			String tempID = i + "";
+			if (getVisualizer().getGraph().getNode(tempID) == null
+					&& getVisualizer().getGraph().getEdge(tempID) == null) {
 				return (tempID);
-			} else{
+			} else {
 				i++;
 			}
 		}
-		//return (new Random().nextInt()+"");
+		// return (new Random().nextInt()+"");
 	}
 
 }

+ 0 - 4
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GUIController.java

@@ -5,15 +5,12 @@ 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;
 import javafx.beans.value.ObservableValue;
 import javafx.embed.swing.SwingNode;
-import javafx.event.ActionEvent;
-import javafx.event.EventHandler;
 import javafx.fxml.FXML;
 import javafx.fxml.Initializable;
 import javafx.scene.control.Button;
@@ -24,7 +21,6 @@ import javafx.scene.control.TableColumn;
 import javafx.scene.control.TableView;
 import javafx.scene.control.cell.PropertyValueFactory;
 import javafx.scene.control.cell.TextFieldTableCell;
-import javafx.scene.input.MouseEvent;
 import javafx.scene.layout.Pane;
 import javafx.util.Callback;
 import javafx.util.Pair;

+ 6 - 3
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/Visualizer.java

@@ -1,7 +1,7 @@
 package de.tu_darmstadt.informatik.tk.scopviz.ui;
 
+import java.util.HashMap;
 import java.util.LinkedList;
-import java.util.*;
 
 import org.graphstream.graph.Edge;
 import org.graphstream.graph.Graph;
@@ -10,7 +10,6 @@ import org.graphstream.ui.swingViewer.ViewPanel;
 import org.graphstream.ui.view.Viewer;
 import org.graphstream.ui.view.ViewerPipe;
 
-import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.handlers.MyViewerListener;
 
 /**
@@ -238,5 +237,9 @@ public class Visualizer {
 	public void pumpIt(){
 		fromViewer.pump();
 	}
-	
+
+	@Override
+	public String toString() {
+		return "Visualizer for Graph \"" + g.getId() + "\"";
+	}
 }

+ 2 - 3
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/handlers/MyAnimationTimer.java

@@ -7,17 +7,16 @@ 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) {
+			if (time + 2000000000 < now) {
 				Main.getInstance().load2ndGraph();
 				Debug.out("blub");
-				trigger = false;
+				time += 2000000000;
 			}
 		}
 	}