1 /*******************************************************************************
2 * Copyright (c) 2017 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 - #7119 initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.adapter;
14 import java.awt.Shape;
15 import java.awt.geom.AffineTransform;
16 import java.util.Collection;
18 import org.simantics.diagram.content.ResourceTerminal;
19 import org.simantics.g2d.diagram.handler.Topology.Terminal;
20 import org.simantics.g2d.element.IElement;
21 import org.simantics.g2d.element.handler.TerminalLayout;
22 import org.simantics.g2d.element.handler.TerminalTopology;
23 import org.simantics.g2d.element.handler.impl.ObjectTerminal;
24 import org.simantics.g2d.element.handler.impl.Terminals;
25 import org.simantics.scenegraph.g2d.IG2DNode;
26 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
27 import org.simantics.scenegraph.utils.NodeUtil;
30 * A {@link TerminalTopology} and more specifically a {@link TerminalLayout}
31 * implementation that relies primarily on the scene graph and only secondarily
32 * on its internal datastructures to resolve terminal locations. This implementation
33 * is used to support dynamic terminals.
35 * @author Antti Villberg
38 public class DefinedElementTerminals extends Terminals {
40 private static final long serialVersionUID = -726490868093887444L;
42 public DefinedElementTerminals(Collection<ObjectTerminal> ts) {
46 private IG2DNode findResourceTerminalNode(IG2DNode node, ResourceTerminal rt) {
47 return (IG2DNode) NodeUtil.forChildrenDeep(node, SingleElementNode.class, n -> {
48 Object key = n.getKey();
49 if (rt.getResource().equals(key)) {
50 IG2DNode[] children = n.getSortedNodes();
51 if (children.length > 0)
60 public AffineTransform getTerminalPosition(IElement e, Terminal t) {
61 if (t instanceof ResourceTerminal) {
62 ResourceTerminal rt = (ResourceTerminal) t;
63 IG2DNode node = e.getHint(DefinedElementHandler.KEY_SG_NODE);
65 IG2DNode n = findResourceTerminalNode(node, rt);
67 return n.getTransform();
72 ObjectTerminal ti = terminalMap.get(t);
75 return ti.getTransform();
79 public Shape getTerminalShape(IElement e, Terminal t) {
80 if (t instanceof ResourceTerminal) {
81 ResourceTerminal rt = (ResourceTerminal) t;
82 IG2DNode node = e.getHint(DefinedElementHandler.KEY_SG_NODE);
84 IG2DNode n = findResourceTerminalNode(node, rt);
86 return n.getBoundsInLocal();
90 return super.getTerminalShape(e, t);