]> gerrit.simantics Code Review - simantics/platform.git/blob
b24970ffd67a1b7cb5f1c26db62b09ad0399dee4
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2012 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.request.UnaryRead;
17 import org.simantics.db.common.utils.NameUtils;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
20 import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.layer0.Layer0;
23 import org.simantics.modeling.ModelingResources;
24 import org.simantics.modeling.ui.diagramEditor.TerminalInformer.TerminalNamingStrategy;
25 import org.simantics.structural.stubs.StructuralResource2;
26 import org.simantics.structural2.queries.Terminal;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public final class DefaultTerminalNamingStrategy implements TerminalNamingStrategy {
32
33     @Override
34     public String getName(ReadGraph graph, TerminalInfo info) throws DatabaseException {
35         Terminal t = graph.syncRequest(new ResolveTerminal(info));
36         if (t == null)
37             return "Could not resolve component terminal";
38         return graph.syncRequest(new TerminalMessage(t));
39     }
40
41     public static class ResolveTerminal extends UnaryRead<TerminalInfo, Terminal> {
42
43         public ResolveTerminal(TerminalInfo parameter) {
44             super(parameter);
45         }
46
47         @Override
48         public Terminal perform(ReadGraph graph) throws DatabaseException {
49             ModelingResources mr = ModelingResources.getInstance(graph);
50
51             Object elementObject = ElementUtils.getObject(parameter.e);
52             if (!(elementObject instanceof Resource))
53                 return null;
54             Resource element = (Resource) elementObject;
55
56             Resource diagramBindingRelation = DiagramGraphUtil.getConnectionPointOfTerminal(graph, parameter.t);
57             if (diagramBindingRelation == null)
58                 return null;
59
60             Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
61             Resource connectionRelation = graph.getPossibleObject(diagramBindingRelation, mr.DiagramConnectionRelationToConnectionRelation);
62             if (component == null || connectionRelation == null)
63                 return null;
64
65             return new Terminal(component, connectionRelation);
66         }
67
68     }
69
70     static class TerminalMessage extends UnaryRead<Terminal, String> {
71
72         public TerminalMessage(Terminal terminal) {
73             super(terminal);
74         }
75
76         @Override
77         public String perform(ReadGraph graph) throws DatabaseException {
78             Layer0 L0 = Layer0.getInstance(graph);
79             StructuralResource2 STR = StructuralResource2.getInstance(graph);
80
81             String name = getStringPropertyValue(graph, parameter.getRelation(), L0.HasName);
82             String label = getStringPropertyValue(graph, parameter.getRelation(), L0.HasLabel);
83             String componentName = NameUtils.getSafeName(graph, parameter.getComponent());
84             Resource componentType = graph.getPossibleType(parameter.getComponent(), STR.Component);
85             String componentTypeName = componentType != null
86                     ? getStringPropertyValue(graph, componentType, L0.HasLabel)
87                     : null;
88
89             StringBuilder sb = new StringBuilder();
90             if (name.equals(label)) {
91                 sb.append(name);
92             } else {
93                 sb.append("(").append(name).append(") ").append(label);
94             }
95             sb.append(" [").append(componentName);
96             if (componentTypeName != null)
97                 sb.append(" : ").append(componentTypeName);
98             sb.append("]");
99
100             return sb.toString();
101         }
102
103         private String getStringPropertyValue(ReadGraph graph, Resource entity, Resource property) throws DatabaseException {
104             String name = graph.getPossibleRelatedValue2(entity, property);
105             return name != null ? name : NameUtils.getSafeName(graph, entity);
106         }
107
108     }
109
110 }