]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural.ui/src/org/simantics/structural/ui/modelBrowser/contributions/ConnectionLabeler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.structural.ui / src / org / simantics / structural / ui / modelBrowser / contributions / ConnectionLabeler.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.modelBrowser.contributions;\r
13 \r
14 import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor;\r
15 import org.simantics.databoard.Bindings;\r
16 import org.simantics.db.ReadGraph;\r
17 import org.simantics.db.Resource;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.layer0.Layer0;\r
20 import org.simantics.structural.stubs.StructuralResource2;\r
21 import org.simantics.structural.ui.modelBrowser.nodes.ConnectionNode;\r
22 import org.simantics.structural2.utils.StructuralUtils;\r
23 \r
24 public class ConnectionLabeler extends LabelerContributor<ConnectionNode> {\r
25 \r
26         @Override\r
27         public String getLabel(ReadGraph graph, ConnectionNode node) throws DatabaseException {\r
28                 StructuralResource2 sr = StructuralResource2.getInstance(graph);\r
29                 StringBuilder sb = new StringBuilder();\r
30         Layer0 b = Layer0.getInstance(graph);\r
31                 \r
32                 {\r
33                         boolean first = true;\r
34                         for(Resource component : graph.getObjects(node.connection, sr.Connects)) {\r
35                                 if(first)\r
36                                         first = false;\r
37                                 else\r
38                                         sb.append(", ");\r
39                                 sb.append(graph.<String>getRelatedValue(component, b.HasName, Bindings.STRING));                                \r
40                         }\r
41                 }\r
42                 sb.append(" -> ");\r
43                 {\r
44                         boolean first = true;\r
45                         for(Resource relatedConnection : StructuralUtils.getRelatedConnections(graph, node.connection)) {\r
46                                 if(relatedConnection.equals(node.connection))\r
47                                         continue;\r
48                                 for(Resource component : graph.getObjects(relatedConnection, sr.Connects)) {\r
49                                         if(first)\r
50                                                 first = false;\r
51                                         else\r
52                                                 sb.append(", ");\r
53                                         sb.append(graph.<String>getRelatedValue(component, b.HasName, Bindings.STRING));                        \r
54                                 }\r
55                         }\r
56                 }\r
57                 \r
58                 return sb.toString();\r
59         }\r
60         \r
61         @Override\r
62         public int getCategory(ReadGraph graph, ConnectionNode node) throws DatabaseException {\r
63                 return 1;\r
64         }\r
65 \r
66 }