Ver Fonte

Merge remote-tracking branch 'origin/Jascha'

Jan Enders há 8 anos atrás
pai
commit
63a6c1b2ae

+ 65 - 6
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/GraphHelper.java

@@ -9,6 +9,8 @@ import org.graphstream.graph.Edge;
 import org.graphstream.graph.Node;
 import org.graphstream.ui.geom.Point3;
 
+import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
+import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
 
 public class GraphHelper {
@@ -40,13 +42,8 @@ public class GraphHelper {
 		double xOffset = targetMaxX - sourceMinX + 10;
 
 		double targetMinY = target.getMinY();
-		// double targetMaxY = target.getMaxY();
 		double sourceMinY = source.getMinY();
-		// double sourceMaxY = source.getMaxY();
-		// experimental
-		double scalingFactorY = scalingFactorX;// ((targetMaxY - targetMinY + 1)
-												// /target.getNodeCount())
-		// / ((sourceMaxY - sourceMinY + 1) /source.getNodeCount());
+		double scalingFactorY = scalingFactorX;
 
 		// adjust Cordinates and Attributes
 		adjustSourceXCoordinates(source, scalingFactorX, xOffset, sourceMinX);
@@ -175,4 +172,66 @@ public class GraphHelper {
 			}
 		}
 	}
+	
+	/**
+	 * adds default to all Nodes and
+	 * converts yEd attributes to regular ones.
+	 * 
+	 * @param g
+	 *            the graph that the attributes will be added onto
+	 */
+	public static void setAllDefaults(MyGraph g){
+		for (Node n : g.getNodeSet()) {
+			//yed conversion
+			if (!n.hasAttribute("ui.label") && n.hasAttribute("yEd.label")) {
+				n.addAttribute("ui.label", n.getAttribute("yEd.label").toString());
+				n.removeAttribute("yEd.label");
+			} else if(n.hasAttribute("ui.label")) {
+				n.removeAttribute("yEd.label");
+			}
+			if (n.hasAttribute("yEd.x") && !n.getAttribute("yEd.x").equals("")) {
+				n.addAttribute("x", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.x"), new Double(0.0)));
+				n.removeAttribute("yEd.x");
+			} else{
+				n.removeAttribute("yEd.x");
+			}
+			if (n.hasAttribute("yEd.y") && !n.getAttribute("yEd.y").equals("")) {
+				n.addAttribute("y", Main.getInstance().convertAttributeTypes(n.getAttribute("yEd.y"), new Double(0.0)));
+				n.removeAttribute("yEd.y");
+			} else {
+				n.removeAttribute("yEd.y");
+			}
+
+			//general defaults
+			if (!n.hasAttribute("ui.label")) {
+				n.addAttribute("ui.label", "");
+			}
+			if (!n.hasAttribute("typeofNode") || n.getAttribute("typeofNode").equals("")) {
+				n.addAttribute("typeofNode", "standard");
+			}
+
+			//underlay defaults
+			if(Layer.UNDERLAY.equals(g.getAttribute("layer"))){
+				if (!n.hasAttribute("typeofDevice") || n.getAttribute("typeofDevice").equals("")) {
+					n.addAttribute("typeofDevice", "unknown");
+				}
+				if (!n.hasAttribute("lat") || n.getAttribute("long").equals("")) {
+					n.addAttribute("lat", OptionsManager.getDefaultLat());
+				}
+				if (!n.hasAttribute("long") || n.getAttribute("long").equals("")) {
+					n.addAttribute("long", OptionsManager.getDefaultLong());
+				}
+				if (!n.hasAttribute("process-max") || n.getAttribute("process-max").equals("")) {
+					n.addAttribute("process-max", 0.0);
+				}
+			}
+
+			//operator defaults
+			if(Layer.OPERATOR.equals(g.getAttribute("layer"))){
+				if (!n.hasAttribute("process-need") || n.getAttribute("process-need").equals("")) {
+					n.addAttribute("process-need", 0.0);
+				}
+			}
+		}
+	}
 }

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

@@ -584,6 +584,7 @@ public class GraphManager {
 	public void deselectEdgeCreationNodes() {
 		if (lastClickedID != null)
 			deselectNodesAfterEdgeCreation(lastClickedID);
+		lastClickedID = null;
 	}
 
 	protected boolean addClass(String id, String className) {

+ 11 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/graphs/MyGraph.java

@@ -2,11 +2,17 @@ package de.tu_darmstadt.informatik.tk.scopviz.graphs;
 
 import java.util.Iterator;
 import java.util.LinkedList;
+import java.util.Optional;
 
 import org.graphstream.graph.Edge;
 import org.graphstream.graph.Node;
 import org.graphstream.graph.implementations.SingleGraph;
 
+import de.tu_darmstadt.informatik.tk.scopviz.main.Layer;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.OptionsManager;
+import de.tu_darmstadt.informatik.tk.scopviz.ui.ToolboxManager;
+import javafx.scene.control.TextInputDialog;
+
 /**
  * Our own Class to extend GraphStreams Graph with our own Functionality.
  * 
@@ -96,6 +102,9 @@ public class MyGraph extends SingleGraph {
 	 *            the Edge that was just created
 	 */
 	private void edgeCreatedNotify(Edge e) {
+		if(Layer.UNDERLAY.equals(this.getAttribute("layer"))){
+			ToolboxManager.createWeighDialog(e);
+		}
 		for (EdgeCreatedListener list : allEdgeListeners) {
 			list.edgeCreated(e, id);
 		}
@@ -114,11 +123,13 @@ public class MyGraph extends SingleGraph {
 
 	/**
 	 * Notifies all added NodeCreatedListener.
+	 * also sets defaults
 	 * 
 	 * @param n
 	 *            the Edge that was just created
 	 */
 	private void nodeCreatedNotify(Node n) {
+		GraphHelper.setAllDefaults(this);
 		for (NodeCreatedListener list : allNodeListeners) {
 			list.nodeCreated(n, id);
 		}

+ 1 - 1
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/GraphMLExporter.java

@@ -32,7 +32,7 @@ public class GraphMLExporter {
 	 *            The Location on disk the File will be saved on
 	 */
 	public void writeGraph(final Graph g, final String fileName) {
-		FileSinkGraphML writer = new FileSinkGraphML();
+		FileSinkGraphML writer = new MyFileSinkGraphML();
 		clearAttributes(g);
 		try {
 			writer.writeAll(g, new FileOutputStream(fileName));

+ 7 - 48
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/GraphMLImporter.java

@@ -47,56 +47,13 @@ public class GraphMLImporter {
 			e.printStackTrace();
 		}
 		fs.removeSink(g);
-		handleAttributes(g);
-		return g;
-	}
-
-	/**
-	 * adds default values for typeofNode and typeofDevice to all Nodes and
-	 * converts yEd attributes to regular ones.
-	 * 
-	 * @param g
-	 *            the graph that the attributes will be added onto
-	 */
-	private void handleAttributes(MyGraph g) {
-		for (Node n : g.getNodeSet()) {
-			if (!n.hasAttribute("ui.label")) {
-				n.addAttribute("ui.label", "");
-			}
-			if (!n.hasAttribute("typeofNode") || n.getAttribute("typeofNode").equals("")) {
-				n.addAttribute("typeofNode", "standard");
-			}
-			if (!n.hasAttribute("typeofDevice") || n.getAttribute("typeofDevice").equals("")) {
-				n.addAttribute("typeofDevice", "unknown");
-			}
-			if (!n.hasAttribute("lat") || n.getAttribute("long").equals("")) {
-				n.addAttribute("lat", OptionsManager.getDefaultLat());
-			}
-			if (!n.hasAttribute("long") || n.getAttribute("long").equals("")) {
-				n.addAttribute("long", OptionsManager.getDefaultLong());
-			}
-
-			if (!n.hasAttribute("ui.label") && n.hasAttribute("yEd.label")) {
-				n.addAttribute("ui.label", n.getAttribute("yEd.label").toString());
-				n.removeAttribute("yEd.label");
-			}
-			if (n.hasAttribute("yEd.x") && !n.getAttribute("yEd.x").equals("")) {
-				n.addAttribute("x", Double.parseDouble(n.getAttribute("yEd.x").toString()));
-				n.removeAttribute("yEd.x");
-			}
-			if (n.hasAttribute("yEd.y") && !n.getAttribute("yEd.y").equals("")) {
-				n.addAttribute("y", Double.parseDouble(n.getAttribute("yEd.y").toString()));
-				n.removeAttribute("yEd.y");
-			}
-			if (!n.hasAttribute("process-need") || n.getAttribute("process-need").equals("")) {
-				n.addAttribute("process-need", 0.0);
-			}
-			if (!n.hasAttribute("process-max") || n.getAttribute("process-max").equals("")) {
-				n.addAttribute("process-max", 0.0);
-			}
+		for(Node n : g.getNodeSet()){
+			n.removeAttribute("ui.class");
 		}
+		return g;
 	}
 
+	
 	/**
 	 * Imports a GraphML file. Opens a open dialog. Returns null if the process
 	 * is aborted.
@@ -139,7 +96,9 @@ public class GraphMLImporter {
 			e.printStackTrace();
 		}
 		fs.removeSink(g);
-		handleAttributes(g);
+		for(Node n : g.getNodeSet()){
+			n.removeAttribute("ui.class");
+		}
 		return g;
 	}
 

+ 161 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/io/MyFileSinkGraphML.java

@@ -0,0 +1,161 @@
+package de.tu_darmstadt.informatik.tk.scopviz.io;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+import org.graphstream.graph.Edge;
+import org.graphstream.graph.Graph;
+import org.graphstream.graph.Node;
+import org.graphstream.stream.file.FileSinkGraphML;
+
+import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
+
+
+public class MyFileSinkGraphML extends FileSinkGraphML{
+
+	private void print(String format, Object... args) throws IOException {
+		output.write(String.format(format, args));
+	}
+
+	@Override
+	protected void exportGraph(Graph g){
+		try {
+			int attribute = 0;
+			HashMap<String, String> nodeAttributes = new HashMap<String, String>();
+			HashMap<String, String> edgeAttributes = new HashMap<String, String>();
+			HashMap<String, String> graphAttributes = new HashMap<String, String>();
+			Debug.out(g.getAttributeCount());
+			for (String j : g.getAttributeKeySet()) {
+				if (!graphAttributes.containsKey(j)) {
+					Object gValue = g.getAttribute(j);
+					String gType;
+
+					if (gValue == null)
+						continue;
+
+					String gId = String.format("attr%04X", attribute++);
+
+					if (gValue instanceof Boolean)
+						gType = "boolean";
+					else if (gValue instanceof Long)
+						gType = "long";
+					else if (gValue instanceof Integer)
+						gType = "int";
+					else if (gValue instanceof Double)
+						gType = "double";
+					else if (gValue instanceof Float)
+						gType = "float";
+					else
+						gType = "string";
+
+					graphAttributes.put(j, gId);
+
+					print("\t<key id=\"%s\" for=\"graph\" attr.name=\"%s\" attr.type=\"%s\"/>\n",
+							gId, escapeXmlString(j), gType);
+				}
+			}
+
+			for (Node n : g.getEachNode()) {
+				for (String k : n.getAttributeKeySet()) {
+					if (!nodeAttributes.containsKey(k)) {
+						Object value = n.getAttribute(k);
+						String type;
+
+						if (value == null)
+							continue;
+
+						String id = String.format("attr%04X", attribute++);
+
+						if (value instanceof Boolean)
+							type = "boolean";
+						else if (value instanceof Long)
+							type = "long";
+						else if (value instanceof Integer)
+							type = "int";
+						else if (value instanceof Double)
+							type = "double";
+						else if (value instanceof Float)
+							type = "float";
+						else
+							type = "string";
+
+						nodeAttributes.put(k, id);
+
+						print("\t<key id=\"%s\" for=\"node\" attr.name=\"%s\" attr.type=\"%s\"/>\n",
+								id, escapeXmlString(k), type);
+					}
+				}
+			}
+
+			for (Edge n : g.getEachEdge()) {
+				for (String k : n.getAttributeKeySet()) {
+					if (!edgeAttributes.containsKey(k)) {
+						Object value = n.getAttribute(k);
+						String type;
+
+						if (value == null)
+							continue;
+
+						String id = String.format("attr%04X", attribute++);
+
+						if (value instanceof Boolean)
+							type = "boolean";
+						else if (value instanceof Long)
+							type = "long";
+						else if (value instanceof Integer)
+							type = "int";
+						else if (value instanceof Double)
+							type = "double";
+						else if (value instanceof Float)
+							type = "float";
+						else
+							type = "string";
+
+						edgeAttributes.put(k, id);
+						print("\t<key id=\"%s\" for=\"edge\" attr.name=\"%s\" attr.type=\"%s\"/>\n",
+								id, escapeXmlString(k), type);
+					}
+				}
+			}
+
+			print("\t<graph id=\"%s\" edgedefault=\"undirected\">\n", escapeXmlString(g.getId()));
+			for (String k : g.getAttributeKeySet()) {
+				print("\t\t\t<data key=\"%s\">%s</data>\n", graphAttributes
+						.get(k), escapeXmlString(g.getAttribute(k).toString()));
+			}
+			
+			for (Node n : g.getEachNode()) {
+				print("\t\t<node id=\"%s\">\n", n.getId());
+				for (String k : n.getAttributeKeySet()) {
+					print("\t\t\t<data key=\"%s\">%s</data>\n", nodeAttributes
+							.get(k), escapeXmlString(n.getAttribute(k).toString()));
+				}
+				print("\t\t</node>\n");
+			}
+			for (Edge e : g.getEachEdge()) {
+				print(
+						"\t\t<edge id=\"%s\" source=\"%s\" target=\"%s\" directed=\"%s\">\n",
+						e.getId(), e.getSourceNode().getId(), e.getTargetNode()
+						.getId(), e.isDirected());
+				for (String k : e.getAttributeKeySet()) {
+					print("\t\t\t<data key=\"%s\">%s</data>\n", edgeAttributes
+							.get(k), escapeXmlString(e.getAttribute(k).toString()));
+				}
+				print("\t\t</edge>\n");
+			}
+			print("\t</graph>\n");
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	private static String escapeXmlString(String s){
+		//why do you make me do this graphstream???
+		return s
+				.replace("&", "&amp;")
+				.replace("<", "&lt;")
+				.replace(">", "&gt;")
+				.replace("\"", "&quot;")
+				.replace("'", "&apos;");
+	}
+}

+ 3 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/main/Main.java

@@ -145,6 +145,9 @@ public final class Main {
 	// don't worry I checked all the conversions
 	@SuppressWarnings("unchecked")
 	public <T extends Number> T convertAttributeTypes(Object attribute, T result) {
+		if(attribute == null){
+			return null;
+		}
 		String currentType = attribute.getClass().getSimpleName().toLowerCase();
 		String targetType = result.getClass().getSimpleName().toLowerCase();
 		switch (targetType) {

+ 8 - 4
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ButtonManager.java

@@ -130,7 +130,8 @@ public final class ButtonManager {
 	 * Handler for the Underlay Layer switch Button.
 	 */
 	public static void underlayAction(ActionEvent arg0) {
-
+		Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
+		
 		switchfromSymbolLayer();
 
 		GraphDisplayManager.setCurrentLayer(Layer.UNDERLAY);
@@ -159,7 +160,8 @@ public final class ButtonManager {
 	 * Handler for the Operator Layer switch Button.
 	 */
 	public static void operatorAction(ActionEvent arg0) {
-
+		Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
+		
 		switchfromSymbolLayer();
 
 		GraphDisplayManager.setCurrentLayer(Layer.OPERATOR);
@@ -188,7 +190,8 @@ public final class ButtonManager {
 	 * Handler for the Mapping Layer switch Button.
 	 */
 	public static void mappingAction(ActionEvent arg0) {
-
+		Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
+		
 		// show metricbox/update button
 		if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.MAPPING))) {
 			controller.rightSide.getChildren().add(controller.updateButtonAPane);
@@ -220,7 +223,8 @@ public final class ButtonManager {
 	 * Handler for the Symbol Representation Layer switch Button.
 	 */
 	public static void symbolRepAction(ActionEvent arg0) {
-
+		Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
+		
 		if (!(GraphDisplayManager.getCurrentLayer().equals(Layer.SYMBOL))) {
 
 			// add a copy of the underlay graph to the the symbol layer

+ 3 - 0
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/GraphDisplayManager.java

@@ -194,12 +194,15 @@ public final class GraphDisplayManager {
 		if (g == null) {
 			throw new NullArgumentException();
 		}
+				
 		GraphManager v;
 		int ret = 0;
 		// replacing the current graph or merging
 		if (replaceCurrent) {
 			v = new GraphManager(g);
 			v.getGraph().addAttribute("layer", currentLayer);
+			//set default values
+			GraphHelper.setAllDefaults(g);
 			v.getGraph().addAttribute("ui.antialias");
 			removeAllCurrentGraphs();
 			vList.add(v);

+ 38 - 3
scopviz/src/main/java/de/tu_darmstadt/informatik/tk/scopviz/ui/ToolboxManager.java

@@ -1,9 +1,14 @@
 package de.tu_darmstadt.informatik.tk.scopviz.ui;
 
+import java.util.Optional;
+
+import org.graphstream.graph.Edge;
+
 import de.tu_darmstadt.informatik.tk.scopviz.debug.Debug;
 import de.tu_darmstadt.informatik.tk.scopviz.main.CreationMode;
 import de.tu_darmstadt.informatik.tk.scopviz.main.Main;
 import de.tu_darmstadt.informatik.tk.scopviz.main.MainApp;
+import javafx.application.Platform;
 import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.FXCollections;
@@ -14,6 +19,7 @@ import javafx.scene.control.CheckBox;
 import javafx.scene.control.TableCell;
 import javafx.scene.control.TableColumn;
 import javafx.scene.control.TableRow;
+import javafx.scene.control.TextInputDialog;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.input.MouseEvent;
@@ -176,9 +182,10 @@ public final class ToolboxManager {
 					changeCreationMode(CreationMode.CREATE_DIRECTED_EDGE);
 				}
 
-				// Unselecet Rows if Creation Mode is None
+				// Deselect Rows if Creation Mode is None
 				if (Main.getInstance().getCreationMode().equals(CreationMode.CREATE_NONE)) {
 					controller.toolbox.getSelectionModel().clearSelection();
+					Main.getInstance().getGraphManager().deselectEdgeCreationNodes();
 				}
 			}
 		}
@@ -209,13 +216,41 @@ public final class ToolboxManager {
 	private static Pair<Object, String> pair(Object picture, String name) {
 		return new Pair<>(picture, name);
 	}
+	
+	/**
+	 * the last edge that was created
+	 */
+	private static Edge lastCreatedEdge = null;
+	
+	/**
+	 * opens a dialog that asks for a weight for a newly created Edge. 
+	 * The default value is Optionsmanager.getDefaultWeight()
+	 *   
+	 * @param e the new Edge that needs a weight
+	 */
+	public static void createWeighDialog(Edge e){
+		if(e.equals(lastCreatedEdge)){
+			return;
+		}
+		lastCreatedEdge = e;
+		Platform.runLater(() -> {
+			TextInputDialog weightDialog = new TextInputDialog(Integer.toString(OptionsManager.getDefaultWeight()));
+			weightDialog.setTitle("Edge Weight");
+			weightDialog.setHeaderText("Please enter the weight of the Edge");
+			weightDialog.setContentText("Edge Weight");
+			Optional<String> result = weightDialog.showAndWait();
+			if(result.isPresent()){
+				e.addAttribute("weight", result.get());
+			}
+		});
+	}
 
 	/**
 	 * Class for getting the string out of the pair elements in each row
 	 *
 	 */
 	public static class PairKeyFactory
-			implements Callback<TableColumn.CellDataFeatures<Pair<Object, String>, String>, ObservableValue<String>> {
+	implements Callback<TableColumn.CellDataFeatures<Pair<Object, String>, String>, ObservableValue<String>> {
 		@Override
 		public ObservableValue<String> call(TableColumn.CellDataFeatures<Pair<Object, String>, String> data) {
 			return new ReadOnlyObjectWrapper<>(data.getValue().getValue());
@@ -227,7 +262,7 @@ public final class ToolboxManager {
 	 *
 	 */
 	public static class PairValueFactory
-			implements Callback<TableColumn.CellDataFeatures<Pair<Object, String>, Object>, ObservableValue<Object>> {
+	implements Callback<TableColumn.CellDataFeatures<Pair<Object, String>, Object>, ObservableValue<Object>> {
 		@SuppressWarnings("unchecked")
 		@Override
 		public ObservableValue<Object> call(TableColumn.CellDataFeatures<Pair<Object, String>, Object> data) {