X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FdiagramEditor%2FDefaultTerminalNamingStrategy.java;h=b24970ffd67a1b7cb5f1c26db62b09ad0399dee4;hb=560d8aa2e37cb6b0249aec6d7e96e67d5a64c59f;hp=78a4714ebb9b65ed820b89b2d0283f63ed6a0db5;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DefaultTerminalNamingStrategy.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DefaultTerminalNamingStrategy.java index 78a4714eb..b24970ffd 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DefaultTerminalNamingStrategy.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/DefaultTerminalNamingStrategy.java @@ -1,110 +1,110 @@ -/******************************************************************************* - * Copyright (c) 2012 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Semantum Oy - initial API and implementation - *******************************************************************************/ -package org.simantics.modeling.ui.diagramEditor; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.request.UnaryRead; -import org.simantics.db.common.utils.NameUtils; -import org.simantics.db.exception.DatabaseException; -import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; -import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo; -import org.simantics.g2d.element.ElementUtils; -import org.simantics.layer0.Layer0; -import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.ui.diagramEditor.TerminalInformer.TerminalNamingStrategy; -import org.simantics.structural.stubs.StructuralResource2; -import org.simantics.structural2.queries.Terminal; - -/** - * @author Tuukka Lehtonen - */ -public final class DefaultTerminalNamingStrategy implements TerminalNamingStrategy { - - @Override - public String getName(ReadGraph graph, TerminalInfo info) throws DatabaseException { - Terminal t = graph.syncRequest(new ResolveTerminal(info)); - if (t == null) - return "Could not resolve component terminal"; - return graph.syncRequest(new TerminalMessage(t)); - } - - public static class ResolveTerminal extends UnaryRead { - - public ResolveTerminal(TerminalInfo parameter) { - super(parameter); - } - - @Override - public Terminal perform(ReadGraph graph) throws DatabaseException { - ModelingResources mr = ModelingResources.getInstance(graph); - - Object elementObject = ElementUtils.getObject(parameter.e); - if (!(elementObject instanceof Resource)) - return null; - Resource element = (Resource) elementObject; - - Resource diagramBindingRelation = DiagramGraphUtil.getConnectionPointOfTerminal(graph, parameter.t); - if (diagramBindingRelation == null) - return null; - - Resource component = graph.getPossibleObject(element, mr.ElementToComponent); - Resource connectionRelation = graph.getPossibleObject(diagramBindingRelation, mr.DiagramConnectionRelationToConnectionRelation); - if (component == null || connectionRelation == null) - return null; - - return new Terminal(component, connectionRelation); - } - - } - - static class TerminalMessage extends UnaryRead { - - public TerminalMessage(Terminal terminal) { - super(terminal); - } - - @Override - public String perform(ReadGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - StructuralResource2 STR = StructuralResource2.getInstance(graph); - - String name = getStringPropertyValue(graph, parameter.getRelation(), L0.HasName); - String label = getStringPropertyValue(graph, parameter.getRelation(), L0.HasLabel); - String componentName = NameUtils.getSafeName(graph, parameter.getComponent()); - Resource componentType = graph.getPossibleType(parameter.getComponent(), STR.Component); - String componentTypeName = componentType != null - ? getStringPropertyValue(graph, componentType, L0.HasLabel) - : null; - - StringBuilder sb = new StringBuilder(); - if (name.equals(label)) { - sb.append(name); - } else { - sb.append("(").append(name).append(") ").append(label); - } - sb.append(" [").append(componentName); - if (componentTypeName != null) - sb.append(" : ").append(componentTypeName); - sb.append("]"); - - return sb.toString(); - } - - private String getStringPropertyValue(ReadGraph graph, Resource entity, Resource property) throws DatabaseException { - String name = graph.getPossibleRelatedValue2(entity, property); - return name != null ? name : NameUtils.getSafeName(graph, entity); - } - - } - -} +/******************************************************************************* + * Copyright (c) 2012 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.diagramEditor; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.g2d.diagram.participant.pointertool.TerminalUtil.TerminalInfo; +import org.simantics.g2d.element.ElementUtils; +import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.diagramEditor.TerminalInformer.TerminalNamingStrategy; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.structural2.queries.Terminal; + +/** + * @author Tuukka Lehtonen + */ +public final class DefaultTerminalNamingStrategy implements TerminalNamingStrategy { + + @Override + public String getName(ReadGraph graph, TerminalInfo info) throws DatabaseException { + Terminal t = graph.syncRequest(new ResolveTerminal(info)); + if (t == null) + return "Could not resolve component terminal"; + return graph.syncRequest(new TerminalMessage(t)); + } + + public static class ResolveTerminal extends UnaryRead { + + public ResolveTerminal(TerminalInfo parameter) { + super(parameter); + } + + @Override + public Terminal perform(ReadGraph graph) throws DatabaseException { + ModelingResources mr = ModelingResources.getInstance(graph); + + Object elementObject = ElementUtils.getObject(parameter.e); + if (!(elementObject instanceof Resource)) + return null; + Resource element = (Resource) elementObject; + + Resource diagramBindingRelation = DiagramGraphUtil.getConnectionPointOfTerminal(graph, parameter.t); + if (diagramBindingRelation == null) + return null; + + Resource component = graph.getPossibleObject(element, mr.ElementToComponent); + Resource connectionRelation = graph.getPossibleObject(diagramBindingRelation, mr.DiagramConnectionRelationToConnectionRelation); + if (component == null || connectionRelation == null) + return null; + + return new Terminal(component, connectionRelation); + } + + } + + static class TerminalMessage extends UnaryRead { + + public TerminalMessage(Terminal terminal) { + super(terminal); + } + + @Override + public String perform(ReadGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + StructuralResource2 STR = StructuralResource2.getInstance(graph); + + String name = getStringPropertyValue(graph, parameter.getRelation(), L0.HasName); + String label = getStringPropertyValue(graph, parameter.getRelation(), L0.HasLabel); + String componentName = NameUtils.getSafeName(graph, parameter.getComponent()); + Resource componentType = graph.getPossibleType(parameter.getComponent(), STR.Component); + String componentTypeName = componentType != null + ? getStringPropertyValue(graph, componentType, L0.HasLabel) + : null; + + StringBuilder sb = new StringBuilder(); + if (name.equals(label)) { + sb.append(name); + } else { + sb.append("(").append(name).append(") ").append(label); + } + sb.append(" [").append(componentName); + if (componentTypeName != null) + sb.append(" : ").append(componentTypeName); + sb.append("]"); + + return sb.toString(); + } + + private String getStringPropertyValue(ReadGraph graph, Resource entity, Resource property) throws DatabaseException { + String name = graph.getPossibleRelatedValue2(entity, property); + return name != null ? name : NameUtils.getSafeName(graph, entity); + } + + } + +}