-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.diagram.style;\r
-\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Rectangle2D;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.diagram.profile.StyleBase;\r
-import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;\r
-import org.simantics.issues.Severity;\r
-import org.simantics.issues.common.IssueResourcesContexts;\r
-import org.simantics.issues.common.ListModelIssuesBySeverity;\r
-import org.simantics.modeling.ModelingResources;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.modeling.ui.diagram.style.IssueDecorationStyle.IssueResult;\r
-import org.simantics.scenegraph.INode;\r
-import org.simantics.scenegraph.g2d.nodes.Decoration;\r
-import org.simantics.scenegraph.g2d.nodes.DecorationSVGNode;\r
-import org.simantics.scenegraph.g2d.nodes.SVGNode;\r
-import org.simantics.scenegraph.profile.EvaluationContext;\r
-import org.simantics.scenegraph.profile.common.ProfileVariables;\r
-import org.simantics.scenegraph.utils.NodeUtil;\r
-import org.simantics.utils.datastructures.map.Tuple;\r
-\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class IssueDecorationStyle extends StyleBase<IssueResult> {\r
-\r
- private static final String DECORATION_NODE_NAME = "issueDecorations";\r
-\r
- private List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {\r
-\r
- ModelingResources MOD = ModelingResources.getInstance(graph);\r
- List<Resource> result = new ArrayList<Resource>(3);\r
- result.add(element);\r
- Resource config = graph.getPossibleObject(element, MOD.ElementToComponent);\r
- if (config != null && result.indexOf(config) == -1) result.add(config);\r
- config = graph.getPossibleObject(element, MOD.DiagramConnectionToConnection);\r
- if (config != null && result.indexOf(config) == -1) result.add(config);\r
- // For diagram reference element support\r
- config = graph.getPossibleObject(element, MOD.HasParentComponent);\r
- if (config != null && result.indexOf(config) == -1) result.add(config);\r
- return result;\r
-\r
- }\r
-\r
- @Override\r
- public IssueResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException {\r
-\r
- Resource model = Variables.getModel(graph, configuration);\r
- if (model == null)\r
- return null;\r
-\r
- List<Resource> contexts = getContexts(graph, element);\r
- AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);\r
- Map<Severity, List<Resource>> issuesBySeverity = graph.syncRequest(new ListModelIssuesBySeverity(model, true, true, Severity.NOTE),\r
- TransientCacheListener.<Map<Severity, List<Resource>>>instance());\r
-\r
- for (Severity severity : Severity.values()) {\r
- List<Resource> issues = issuesBySeverity.get(severity);\r
- if (issues != null) {\r
- Set<Resource> issueContexts = graph.syncRequest(new IssueResourcesContexts(issues));\r
- if (!Collections.disjoint(issueContexts, contexts))\r
- return new IssueResult(severity, transform);\r
- }\r
- }\r
-\r
- return null;\r
- }\r
-\r
- @Override\r
- public void applyStyleForNode(EvaluationContext observer, INode node, IssueResult result) {\r
- if (result == null) {\r
- ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME);\r
- return;\r
- }\r
-\r
- SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer);\r
-\r
- svgNode.setZIndex( Integer.MAX_VALUE );\r
- svgNode.setTransform(getDecorationPosition(node)); \r
-\r
- String svgData = svgDataForSeverity(result.getSeverity());\r
- if (svgData != null)\r
- svgNode.setData(svgData);\r
- }\r
-\r
- /**\r
- * Returns position of the decoration.\r
- * By default decoration is placed to the top left corner. Override this method to change the position.\r
- * \r
- * @param node\r
- * @return\r
- */\r
- protected AffineTransform getDecorationPosition(INode node) {\r
- Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);\r
-\r
- double tx = bounds.getX();\r
- double ty = bounds.getY();\r
- return AffineTransform.getTranslateInstance(tx, ty);\r
- }\r
-\r
- protected String svgDataForSeverity(Severity s) {\r
- switch (s) {\r
- case FATAL: return Activator.FATAL_SVG_TEXT;\r
- case ERROR: return Activator.ERROR_SVG_TEXT;\r
- case WARNING: return Activator.WARNING_SVG_TEXT;\r
- case INFO: return Activator.INFO_SVG_TEXT;\r
- case NOTE: return Activator.NOTE_SVG_TEXT;\r
- default: return null;\r
- }\r
- }\r
-\r
- @Override\r
- protected void cleanupStyleForNode(INode node) {\r
- ProfileVariables.denyChild(node, "", DECORATION_NODE_NAME);\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return "Issue decoration";\r
- }\r
- \r
- /**\r
- * This is needed to keep the issue decoration up-to-date when its parent\r
- * element moves.\r
- */\r
- public static class IssueResult extends Tuple {\r
- public IssueResult(Severity severity, AffineTransform transform) {\r
- super(severity, transform);\r
- }\r
- public Severity getSeverity() {\r
- return (Severity) getField(0);\r
- }\r
- }\r
-\r
-}\r
-\r
-\r
+/*******************************************************************************
+ * 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.elements.DecorationSVGNode;
+import org.simantics.diagram.elements.SVGNode;
+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.modeling.ui.diagram.style.IssueDecorationStyle.IssueResult;
+import org.simantics.scenegraph.INode;
+import org.simantics.scenegraph.g2d.nodes.Decoration;
+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<IssueResult> {
+
+ private static final String DECORATION_NODE_NAME = "issueDecorations"; //$NON-NLS-1$
+
+ private List<Resource> getContexts(ReadGraph graph, Resource element) throws DatabaseException {
+
+ ModelingResources MOD = ModelingResources.getInstance(graph);
+ List<Resource> result = new ArrayList<Resource>(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<Resource> contexts = getContexts(graph, element);
+ AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element);
+ Map<Severity, List<Resource>> issuesBySeverity = graph.syncRequest(new ListModelIssuesBySeverity(model, true, true, Severity.NOTE),
+ TransientCacheListener.<Map<Severity, List<Resource>>>instance());
+
+ for (Severity severity : Severity.values()) {
+ List<Resource> issues = issuesBySeverity.get(severity);
+ if (issues != null) {
+ Set<Resource> 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); //$NON-NLS-1$
+ return;
+ }
+
+ SVGNode svgNode = ProfileVariables.claimChild(node, "", DECORATION_NODE_NAME, DecorationSVGNode.class, observer); //$NON-NLS-1$
+
+ svgNode.setZIndex( Integer.MAX_VALUE );
+ svgNode.setTransform(getDecorationPosition(node));
+
+ String svgData = svgDataForSeverity(result.getSeverity());
+ if (svgData != null)
+ svgNode.setData(svgData);
+ }
+
+ /**
+ * Returns position of the decoration.
+ * By default decoration is placed to the top left corner. Override this method to change the position.
+ *
+ * @param node
+ * @return
+ */
+ protected AffineTransform getDecorationPosition(INode node) {
+ Rectangle2D bounds = NodeUtil.getLocalBounds(node, Decoration.class);
+
+ double tx = bounds.getX();
+ double ty = bounds.getY();
+ return AffineTransform.getTranslateInstance(tx, ty);
+ }
+
+ protected 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); //$NON-NLS-1$
+ }
+
+ @Override
+ public String toString() {
+ return "Issue decoration"; //$NON-NLS-1$
+ }
+
+ /**
+ * This is needed to keep the issue decoration up-to-date when its parent
+ * element moves.
+ */
+ public static class IssueResult extends Tuple {
+ public IssueResult(Severity severity, AffineTransform transform) {
+ super(severity, transform);
+ }
+ public Severity getSeverity() {
+ return (Severity) getField(0);
+ }
+ }
+
+}
+
+