--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.structural.ui.compositeViewer;\r
+\r
+import java.util.HashMap;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.graphviz.Edge;\r
+import org.simantics.graphviz.Graph;\r
+import org.simantics.graphviz.Node;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+public class CreateCompositeGraph implements Read<Graph> {\r
+\r
+ Resource composite;\r
+ \r
+ public CreateCompositeGraph(Resource composite) {\r
+ this.composite = composite;\r
+ }\r
+ \r
+ static class Connection {\r
+ Node parent;\r
+ String label;\r
+ public Connection(Node parent, String label) {\r
+ this.parent = parent;\r
+ this.label = label;\r
+ } \r
+ }\r
+\r
+ private static String getName(ReadGraph g, Resource r) throws DatabaseException {\r
+ String s = g.getPossibleRelatedValue(r, Layer0.getInstance(g).HasName);\r
+ if (s == null)\r
+ s = NameUtils.getSafeName(g, r);\r
+ return s;\r
+ }\r
+ \r
+ Graph graph;\r
+ HashMap<Resource, Node> connections = new HashMap<Resource, Node>();\r
+ \r
+ private void connect(Node parent, String label, Resource connection) {\r
+ Node cn = connections.get(connection);\r
+ if(cn == null) {\r
+ cn = new Node(graph);\r
+ cn.setShape("diamond");\r
+ cn.setFixedSize(true);\r
+ cn.setWidth(0.2);\r
+ cn.setHeight(0.2);\r
+ connections.put(connection, cn);\r
+ }\r
+ Edge edge = new Edge(parent, cn);\r
+ edge.setArrowhead("none");\r
+ if(label.contains("Self"))\r
+ edge.setColor("red");\r
+ else\r
+ edge.setTailLabel(label);\r
+ edge.set("fontsize", "8");\r
+ }\r
+ \r
+ @Override\r
+ public Graph perform(ReadGraph g) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(g);\r
+ StructuralResource2 STR = StructuralResource2.getInstance(g);\r
+ \r
+ graph = new Graph();\r
+ graph.set("overlap", "false");\r
+ graph.set("splines", "true");\r
+ \r
+ for(Resource child : g.getObjects(composite, L0.ConsistsOf)) {\r
+ Node node = new Node(graph, getName(g, child));\r
+ node.setShape("rect");\r
+ if(g.isInstanceOf(child, STR.Composite)) {\r
+ for(Resource c : g.getObjects(child, STR.HasConnectionJoin))\r
+ connect(node, "", c);\r
+ }\r
+ else {\r
+ for(Statement stat : g.getStatements(child, STR.IsConnectedTo)) {\r
+ String label = getName(g, stat.getPredicate()); \r
+ connect(node, label, stat.getObject());\r
+ }\r
+ }\r
+ }\r
+ \r
+ Graph rgraph = graph;\r
+ graph = null;\r
+ connections.clear();\r
+ \r
+ return rgraph;\r
+ }\r
+\r
+}\r