1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
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;
29 * @author Tuukka Lehtonen
31 public final class DefaultTerminalNamingStrategy implements TerminalNamingStrategy {
34 public String getName(ReadGraph graph, TerminalInfo info) throws DatabaseException {
35 Terminal t = graph.syncRequest(new ResolveTerminal(info));
37 return "Could not resolve component terminal";
38 return graph.syncRequest(new TerminalMessage(t));
41 public static class ResolveTerminal extends UnaryRead<TerminalInfo, Terminal> {
43 public ResolveTerminal(TerminalInfo parameter) {
48 public Terminal perform(ReadGraph graph) throws DatabaseException {
49 ModelingResources mr = ModelingResources.getInstance(graph);
51 Object elementObject = ElementUtils.getObject(parameter.e);
52 if (!(elementObject instanceof Resource))
54 Resource element = (Resource) elementObject;
56 Resource diagramBindingRelation = DiagramGraphUtil.getConnectionPointOfTerminal(graph, parameter.t);
57 if (diagramBindingRelation == null)
60 Resource component = graph.getPossibleObject(element, mr.ElementToComponent);
61 Resource connectionRelation = graph.getPossibleObject(diagramBindingRelation, mr.DiagramConnectionRelationToConnectionRelation);
62 if (component == null || connectionRelation == null)
65 return new Terminal(component, connectionRelation);
70 static class TerminalMessage extends UnaryRead<Terminal, String> {
72 public TerminalMessage(Terminal terminal) {
77 public String perform(ReadGraph graph) throws DatabaseException {
78 Layer0 L0 = Layer0.getInstance(graph);
79 StructuralResource2 STR = StructuralResource2.getInstance(graph);
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)
89 StringBuilder sb = new StringBuilder();
90 if (name.equals(label)) {
93 sb.append("(").append(name).append(") ").append(label);
95 sb.append(" [").append(componentName);
96 if (componentTypeName != null)
97 sb.append(" : ").append(componentTypeName);
100 return sb.toString();
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);