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.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
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;
47 * @author Tuukka Lehtonen
49 public class IssueDecorationStyle extends StyleBase<IssueResult> {
51 private static final String DECORATION_NODE_NAME = "issueDecorations"; //$NON-NLS-1$
53 private List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
55 ModelingResources MOD = ModelingResources.getInstance(graph);
56 List<Resource> result = new ArrayList<Resource>(3);
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);
70 public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {
72 Resource model = Variables.getModel(graph, configuration);
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());
81 for (Severity severity : Severity.values()) {
82 List<Resource> issues = issuesBySeverity.get(severity);
84 Set<Resource> issueContexts = graph.syncRequest(new IssueResourcesContexts(issues));
85 if (!Collections.disjoint(issueContexts, contexts))
86 return new IssueResult(severity, transform);
94 public void applyStyleForNode(EvaluationContext observer, INode node, IssueResult result) {
96 ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
100 SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer); //$NON-NLS-1$
102 svgNode.setZIndex( Integer.MAX_VALUE );
103 svgNode.setTransform(getDecorationPosition(node));
105 String svgData = svgDataForSeverity(result.getSeverity());
107 svgNode.setData(svgData);
111 * Returns position of the decoration.
112 * By default decoration is placed to the top left corner. Override this method to change the position.
117 protected AffineTransform getDecorationPosition(INode node) {
118 Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);
120 double tx = bounds.getX();
121 double ty = bounds.getY();
122 return AffineTransform.getTranslateInstance(tx, ty);
125 protected String svgDataForSeverity(Severity 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;
137 protected void cleanupStyleForNode(INode node) {
138 ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); //$NON-NLS-1$
142 public String toString() {
143 return "Issue decoration"; //$NON-NLS-1$
147 * This is needed to keep the issue decoration up-to-date when its parent
150 public static class IssueResult extends Tuple {
151 public IssueResult(Severity severity, AffineTransform transform) {
152 super(severity, transform);
154 public Severity getSeverity() {
155 return (Severity) getField(0);