]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.ui/src/org/simantics/structural/ui/compositeViewer/CreateCompositeGraph.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural.ui / src / org / simantics / structural / ui / compositeViewer / CreateCompositeGraph.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.structural.ui.compositeViewer;\r
13 \r
14 import java.util.HashMap;\r
15 \r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.Statement;\r
19 import org.simantics.db.common.utils.NameUtils;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.request.Read;\r
22 import org.simantics.graphviz.Edge;\r
23 import org.simantics.graphviz.Graph;\r
24 import org.simantics.graphviz.Node;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.structural.stubs.StructuralResource2;\r
27 \r
28 public class CreateCompositeGraph implements Read<Graph> {\r
29 \r
30         Resource composite;\r
31         \r
32         public CreateCompositeGraph(Resource composite) {\r
33                 this.composite = composite;\r
34         }\r
35         \r
36         static class Connection {\r
37                 Node parent;\r
38                 String label;\r
39                 public Connection(Node parent, String label) {\r
40                         this.parent = parent;\r
41                         this.label = label;\r
42                 }       \r
43         }\r
44 \r
45         private static String getName(ReadGraph g, Resource r) throws DatabaseException {\r
46                 String s = g.getPossibleRelatedValue(r, Layer0.getInstance(g).HasName);\r
47                 if (s == null)\r
48                         s = NameUtils.getSafeName(g, r);\r
49                 return s;\r
50         }\r
51         \r
52         Graph graph;\r
53         HashMap<Resource, Node> connections = new HashMap<Resource, Node>();\r
54         \r
55         private void connect(Node parent, String label, Resource connection) {\r
56                 Node cn = connections.get(connection);\r
57                 if(cn == null) {\r
58                         cn = new Node(graph);\r
59                         cn.setShape("diamond");\r
60                         cn.setFixedSize(true);\r
61                         cn.setWidth(0.2);\r
62                         cn.setHeight(0.2);\r
63                         connections.put(connection, cn);\r
64                 }\r
65                 Edge edge = new Edge(parent, cn);\r
66                 edge.setArrowhead("none");\r
67                 if(label.contains("Self"))\r
68                         edge.setColor("red");\r
69                 else\r
70                         edge.setTailLabel(label);\r
71                 edge.set("fontsize", "8");\r
72         }\r
73         \r
74         @Override\r
75         public Graph perform(ReadGraph g) throws DatabaseException {\r
76                 Layer0 L0 = Layer0.getInstance(g);\r
77                 StructuralResource2 STR = StructuralResource2.getInstance(g);\r
78                 \r
79                 graph = new Graph();\r
80                 graph.set("overlap", "false");\r
81                 graph.set("splines", "true");\r
82                 \r
83                 for(Resource child : g.getObjects(composite, L0.ConsistsOf)) {\r
84                         Node node = new Node(graph, getName(g, child));\r
85                         node.setShape("rect");\r
86                         if(g.isInstanceOf(child, STR.Composite)) {\r
87                                 for(Resource c : g.getObjects(child, STR.HasConnectionJoin))\r
88                                         connect(node, "", c);\r
89                         }\r
90                         else {\r
91                                 for(Statement stat : g.getStatements(child, STR.IsConnectedTo)) {\r
92                                         String label = getName(g, stat.getPredicate());                                 \r
93                                         connect(node, label, stat.getObject());\r
94                                 }\r
95                         }\r
96                 }\r
97                 \r
98                 Graph rgraph = graph;\r
99                 graph = null;\r
100                 connections.clear();\r
101                 \r
102                 return rgraph;\r
103         }\r
104 \r
105 }\r