X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2Fstyle%2FIssueDecorationStyle.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2Fstyle%2FIssueDecorationStyle.java;h=6a2202ee0877eff40ae9d6b1294ce1a57b7b844a;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/IssueDecorationStyle.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/IssueDecorationStyle.java new file mode 100644 index 000000000..6a2202ee0 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/IssueDecorationStyle.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.ui.diagram.style; + +import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.procedure.adapter.TransientCacheListener; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.diagram.profile.StyleBase; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.issues.Severity; +import org.simantics.issues.common.IssueResourcesContexts; +import org.simantics.issues.common.ListModelIssuesBySeverity; +import org.simantics.modeling.ModelingResources; +import org.simantics.modeling.ui.Activator; +import org.simantics.scenegraph.INode; +import org.simantics.scenegraph.g2d.nodes.Decoration; +import org.simantics.scenegraph.g2d.nodes.DecorationSVGNode; +import org.simantics.scenegraph.g2d.nodes.SVGNode; +import org.simantics.scenegraph.profile.EvaluationContext; +import org.simantics.scenegraph.profile.common.ProfileVariables; +import org.simantics.scenegraph.utils.NodeUtil; +import org.simantics.utils.datastructures.map.Tuple; + + +/** + * @author Tuukka Lehtonen + */ +public class IssueDecorationStyle extends StyleBase { + + private static final String DECORATION_NODE_NAME = "issueDecorations"; + + private List getContexts(ReadGraph graph, Resource element) throws DatabaseException { + + ModelingResources MOD = ModelingResources.getInstance(graph); + List result = new ArrayList(3); + result.add(element); + Resource config = graph.getPossibleObject(element, MOD.ElementToComponent); + if (config != null && result.indexOf(config) == -1) result.add(config); + config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection); + if (config != null && result.indexOf(config) == -1) result.add(config); + // For diagram reference element support + config = graph.getPossibleObject(element, MOD.HasParentComponent); + if (config != null && result.indexOf(config) == -1) result.add(config); + return result; + + } + + @Override + public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException { + + Resource model = Variables.getModel(graph, configuration); + if (model == null) + return null; + + List contexts = getContexts(graph, element); + AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element); + Map> issuesBySeverity = graph.syncRequest(new ListModelIssuesBySeverity(model, true, true, Severity.NOTE), + TransientCacheListener.>>instance()); + + for (Severity severity : Severity.values()) { + List issues = issuesBySeverity.get(severity); + if (issues != null) { + Set issueContexts = graph.syncRequest(new IssueResourcesContexts(issues)); + if (!Collections.disjoint(issueContexts, contexts)) + return new IssueResult(severity, transform); + } + } + + return null; + } + + @Override + public void applyStyleForNode(EvaluationContext observer, INode node, IssueResult result) { + if (result == null) { + ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); + return; + } + + SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer); + + Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class); + + double tx = bounds.getX(); + double ty = bounds.getY(); + + //new Exception("tx=" + tx + " ty=" + ty).printStackTrace(); + + svgNode.setZIndex( Integer.MAX_VALUE ); + svgNode.setTransform( AffineTransform.getTranslateInstance(tx, ty) ); + + String svgData = svgDataForSeverity(result.getSeverity()); + if (svgData != null) + svgNode.setData(svgData); + } + + private String svgDataForSeverity(Severity s) { + switch (s) { + case FATAL: return Activator.FATAL_SVG_TEXT; + case ERROR: return Activator.ERROR_SVG_TEXT; + case WARNING: return Activator.WARNING_SVG_TEXT; + case INFO: return Activator.INFO_SVG_TEXT; + case NOTE: return Activator.NOTE_SVG_TEXT; + default: return null; + } + } + + @Override + protected void cleanupStyleForNode(INode node) { + ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME); + } + + @Override + public String toString() { + return "Issue decoration"; + } + +} + +/** + * This is needed to keep the issue decoration up-to-date when its parent + * element moves. + */ +class IssueResult extends Tuple { + public IssueResult(Severity severity, AffineTransform transform) { + super(severity, transform); + } + public Severity getSeverity() { + return (Severity) getField(0); + } +}