]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/IssueDecorationStyle.java
d473ff46df044a4db03b1bb694fb23e5bbf3ca3c
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / style / IssueDecorationStyle.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.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.variable.Variable;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.diagram.elements.DecorationSVGNode;
29 import org.simantics.diagram.elements.SVGNode;
30 import org.simantics.diagram.profile.StyleBase;
31 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
32 import org.simantics.issues.Severity;
33 import org.simantics.issues.common.IssueResourcesContexts;
34 import org.simantics.issues.common.ListModelIssuesBySeverity;
35 import org.simantics.modeling.ModelingResources;
36 import org.simantics.modeling.ui.Activator;
37 import org.simantics.modeling.ui.diagram.style.IssueDecorationStyle.IssueResult;
38 import org.simantics.scenegraph.INode;
39 import org.simantics.scenegraph.g2d.nodes.Decoration;
40 import org.simantics.scenegraph.profile.EvaluationContext;
41 import org.simantics.scenegraph.profile.common.ProfileVariables;
42 import org.simantics.scenegraph.utils.NodeUtil;
43 import org.simantics.utils.datastructures.map.Tuple;
44
45
46 /**
47  * @author Tuukka Lehtonen
48  */
49 public class IssueDecorationStyle extends StyleBase<IssueResult> {
50
51     private static final String DECORATION_NODE_NAME = "issueDecorations"; //$NON-NLS-1$
52
53     private List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
54
55         ModelingResources MOD = ModelingResources.getInstance(graph);
56         List<Resource> result = new ArrayList<Resource>(3);
57         result.add(element);
58         Resource config = graph.getPossibleObject(element, MOD.ElementToComponent);
59         if (config != null && result.indexOf(config) == -1) result.add(config);
60         config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection);
61         if (config != null && result.indexOf(config) == -1) result.add(config);
62         // For diagram reference element support
63         config = graph.getPossibleObject(element, MOD.HasParentComponent);
64         if (config != null && result.indexOf(config) == -1) result.add(config);
65         return result;
66
67     }
68
69     @Override
70     public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
71
72         Resource model = Variables.getModel(graph, configuration);
73         if (model == null)
74             return null;
75
76         List<Resource> contexts = getContexts(graph, element);
77         AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
78         Map<Severity, List<Resource>> issuesBySeverity = graph.syncRequest(new ListModelIssuesBySeverity(model, true, true, Severity.NOTE),
79                 TransientCacheListener.<Map<Severity, List<Resource>>>instance());
80
81         for (Severity severity : Severity.values()) {
82             List<Resource> issues = issuesBySeverity.get(severity);
83             if (issues != null) {
84                 Set<Resource> issueContexts = graph.syncRequest(new IssueResourcesContexts(issues));
85                 if (!Collections.disjoint(issueContexts, contexts))
86                     return new IssueResult(severity, transform);
87             }
88         }
89
90         return null;
91     }
92
93     @Override
94     public void applyStyleForNode(EvaluationContext observer, INode node, IssueResult result) {
95         if (result == null) {
96             ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
97             return;
98         }
99
100         SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer); //$NON-NLS-1$
101
102         svgNode.setZIndex( Integer.MAX_VALUE );
103         svgNode.setTransform(getDecorationPosition(node)); 
104
105         String svgData = svgDataForSeverity(result.getSeverity());
106         if (svgData != null)
107             svgNode.setData(svgData);
108     }
109
110     /**
111      * Returns position of the decoration.
112      * By default decoration is placed to the top left corner.  Override this method to change the position.
113      *  
114      * @param node
115      * @return
116      */
117     protected AffineTransform getDecorationPosition(INode node) {
118         Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);
119
120         double tx = bounds.getX();
121         double ty = bounds.getY();
122         return AffineTransform.getTranslateInstance(tx, ty);
123     }
124
125     protected String svgDataForSeverity(Severity s) {
126         switch (s) {
127         case FATAL: return Activator.FATAL_SVG_TEXT;
128         case ERROR: return Activator.ERROR_SVG_TEXT;
129         case WARNING: return Activator.WARNING_SVG_TEXT;
130         case INFO: return Activator.INFO_SVG_TEXT;
131         case NOTE: return Activator.NOTE_SVG_TEXT;
132         default: return null;
133         }
134     }
135
136     @Override
137     protected void cleanupStyleForNode(INode node) {
138         ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
139     }
140
141     @Override
142     public String toString() {
143         return "Issue decoration";  //$NON-NLS-1$
144     }
145     
146     /**
147      * This is needed to keep the issue decoration up-to-date when its parent
148      * element moves.
149      */
150     public static class IssueResult extends Tuple {
151         public IssueResult(Severity severity, AffineTransform transform) {
152             super(severity, transform);
153         }
154         public Severity getSeverity() {
155             return (Severity) getField(0);
156         }
157     }
158
159 }
160
161