1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagram.style;
14 import java.awt.Color;
16 import java.awt.geom.AffineTransform;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.datatypes.literal.Vec2d;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.diagram.elements.TextNode;
25 import org.simantics.diagram.profile.StyleBase;
26 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
27 import org.simantics.g2d.utils.Alignment;
28 import org.simantics.layer0.Layer0;
29 import org.simantics.modeling.ModelingResources;
30 import org.simantics.scenegraph.INode;
31 import org.simantics.scenegraph.g2d.IG2DNode;
32 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
33 import org.simantics.scenegraph.profile.EvaluationContext;
34 import org.simantics.scenegraph.profile.common.ProfileVariables;
35 import org.simantics.scenegraph.utils.NodeUtil;
36 import org.simantics.utils.datastructures.map.Tuple;
40 * @author Tuukka Lehtonen
42 public class SymbolTerminalNameStyle extends StyleBase<SymbolNameResult> {
44 static final Font FONT = Font.decode("Arial 6");
46 private static final String NODE_NAME = "terminalName";
49 * Must override the StyleBase implementation because terminals do not
50 * depend on runtime variable. They need to be shown regardless of variable
54 public SymbolNameResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource groupItem) throws DatabaseException {
55 return calculateStyle(graph, runtimeDiagram, entry, groupItem, null);
59 public SymbolNameResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
61 AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
63 Layer0 L0 = Layer0.getInstance(graph);
64 ModelingResources MOD = ModelingResources.getInstance(graph);
66 Resource binds = DiagramGraphUtil.getPossibleConnectionPointOfTerminal(graph, element);
70 Resource conn = graph.getPossibleObject(binds, MOD.DiagramConnectionRelationToConnectionRelation);
71 String name = graph.getPossibleRelatedValue(conn, L0.HasName, Bindings.STRING);
72 if (conn == null || name == null)
75 return new SymbolNameResult(name, transform);
79 private AffineTransform translateAndScaleIfNeeded(AffineTransform tr, Vec2d offset, double scale) {
80 if(!offset.isZero() || scale != 1.0) {
81 tr = new AffineTransform(tr);
82 tr.translate(offset.x, offset.y);
83 tr.scale(scale, scale);
88 private AffineTransform getSymbolTransform(INode node, Vec2d offset, double size) {
89 if(node instanceof SingleElementNode) {
90 SingleElementNode s = (SingleElementNode)node;
91 INode symbol = NodeUtil.findChildByPrefix(s, "composite_image");
92 return translateAndScaleIfNeeded(symbol != null ? ((IG2DNode)symbol).getTransform() : new AffineTransform(), offset, size);
98 public void applyStyleForNode(EvaluationContext observer, INode _node, SymbolNameResult result) {
100 if (result == null) {
101 ProfileVariables.denyChild(_node, "", NODE_NAME);
105 TextNode node = ProfileVariables.claimChild(_node, "", NODE_NAME, TextNode.class, observer);
107 node.setZIndex( Integer.MAX_VALUE );
108 node.setTransform( getSymbolTransform(_node, new Vec2d(0, -1), 0.08) );
109 node.setText(result.getName());
110 node.setHorizontalAlignment((byte)Alignment.CENTER.ordinal());
111 node.setBackgroundColor(Color.WHITE);
112 node.setPadding(0.2, 0);
117 protected void cleanupStyleForNode(INode node) {
118 ProfileVariables.denyChild(node, "", NODE_NAME);
124 * This is needed to keep the issue decoration up-to-date when its parent
127 class SymbolNameResult extends Tuple {
128 public SymbolNameResult(String name, AffineTransform transform) {
129 super(name, transform);
131 public String getName() {
132 return (String) getField(0);