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