]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/DocumentDecorationStyle.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / style / DocumentDecorationStyle.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagram.style;
13
14 import java.awt.geom.AffineTransform;
15 import java.awt.geom.Rectangle2D;
16 import java.util.HashSet;
17 import java.util.Set;
18
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;
36
37 /**
38  * @author Antti Villberg
39  */
40 public class DocumentDecorationStyle extends StyleBase<DocumentResult> {
41
42     private static final String DECORATION_NODE_NAME = "documentDecorations"; //$NON-NLS-1$
43
44     private Set<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
45
46         ModelingResources MOD = ModelingResources.getInstance(graph);
47         HashSet<Resource> result = new HashSet<Resource>();
48         result.add(element);
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);
53         return result;
54         
55     }
56     
57     @Override
58     public DocumentResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
59         
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);
65                 }
66         }
67         
68         return null;
69         
70     }
71
72     @Override
73     public void applyStyleForNode(EvaluationContext observer, INode node, DocumentResult result) {
74         if (result == null) {
75             ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
76             return;
77         }
78
79         SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer); //$NON-NLS-1$
80
81         Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);
82
83         double tx = bounds.getX();
84         double ty = bounds.getY();
85         double h = bounds.getHeight();
86         
87         svgNode.setZIndex( Integer.MAX_VALUE );
88         svgNode.setTransform( AffineTransform.getTranslateInstance(tx, ty+h) ); 
89         svgNode.setData(Activator.DOCUMENT_SVG_TEXT);
90
91     }
92
93     @Override
94     protected void cleanupStyleForNode(INode node) {
95         ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
96     }
97
98     @Override
99     public String toString() {
100         return "Document decoration"; //$NON-NLS-1$
101     }
102
103 }
104
105 /**
106  * This is needed to keep the issue decoration up-to-date when its parent
107  * element moves.
108  */
109 class DocumentResult extends Tuple {
110     public DocumentResult(AffineTransform transform) {
111         super(transform);
112     }
113 }