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.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
16 import java.util.HashSet;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.variable.Variable;
23 import org.simantics.diagram.elements.DecorationSVGNode;
24 import org.simantics.diagram.elements.SVGNode;
25 import org.simantics.diagram.profile.StyleBase;
26 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
27 import org.simantics.document.DocumentResource;
28 import org.simantics.modeling.ModelingResources;
29 import org.simantics.modeling.ui.Activator;
30 import org.simantics.scenegraph.INode;
31 import org.simantics.scenegraph.g2d.nodes.Decoration;
32 import org.simantics.scenegraph.profile.EvaluationContext;
33 import org.simantics.scenegraph.profile.common.ProfileVariables;
34 import org.simantics.scenegraph.utils.NodeUtil;
35 import org.simantics.utils.datastructures.map.Tuple;
38 * @author Antti Villberg
40 public class DocumentDecorationStyle extends StyleBase<DocumentResult> {
42 private static final String DECORATION_NODE_NAME = "documentDecorations";
44 private Set<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
46 ModelingResources MOD = ModelingResources.getInstance(graph);
47 HashSet<Resource> result = new HashSet<Resource>();
49 Resource config = graph.getPossibleObject(element, MOD.ElementToComponent);
50 if (config != null) result.add(config);
51 config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection);
52 if (config != null) result.add(config);
58 public DocumentResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
60 DocumentResource DOC = DocumentResource.getInstance(graph);
61 for(Resource r : getContexts(graph, element)) {
62 if(graph.hasStatement(r, DOC.HasDocumentation)) {
63 AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
64 return new DocumentResult(transform);
73 public void applyStyleForNode(EvaluationContext observer, INode node, DocumentResult result) {
75 ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME);
79 SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer);
81 Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);
83 double tx = bounds.getX();
84 double ty = bounds.getY();
85 double h = bounds.getHeight();
87 svgNode.setZIndex( Integer.MAX_VALUE );
88 svgNode.setTransform( AffineTransform.getTranslateInstance(tx, ty+h) );
89 svgNode.setData(Activator.DOCUMENT_SVG_TEXT);
94 protected void cleanupStyleForNode(INode node) {
95 ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME);
99 public String toString() {
100 return "Document decoration";
106 * This is needed to keep the issue decoration up-to-date when its parent
109 class DocumentResult extends Tuple {
110 public DocumentResult(AffineTransform transform) {