]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/style/IssueDecorationStyle.java
Improved the positioning of issue decoration for connection elements
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / style / IssueDecorationStyle.java
index f0384d115f659b097b4e94cc02b134da706f8f4f..68210f9e263f5c48e51eb10bbbc30f0324b85244 100644 (file)
-/*******************************************************************************\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.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+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.connection.RouteGraph;
+import org.simantics.diagram.elements.DecorationSVGNode;
+import org.simantics.diagram.elements.SVGNode;
+import org.simantics.diagram.handler.Paster;
+import org.simantics.diagram.handler.Paster.RouteLine;
+import org.simantics.diagram.profile.StyleBase;
+import org.simantics.diagram.stubs.DiagramResource;
+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.ConnectionNode;
+import org.simantics.scenegraph.g2d.nodes.Decoration;
+import org.simantics.scenegraph.g2d.nodes.connection.RouteGraphNode;
+import org.simantics.scenegraph.profile.EvaluationContext;
+import org.simantics.scenegraph.profile.common.ProfileVariables;
+import org.simantics.scenegraph.utils.NodeUtil;
+import org.simantics.structural.stubs.StructuralResource2;
+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);
+        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, getIdentifier(graph, runtimeDiagram, element));
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private static Object getIdentifier(ReadGraph graph, Resource runtimeDiagram, Resource element) throws DatabaseException {
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        if (graph.isInstanceOf(element, DIA.RouteGraphConnection)) {
+            Collection<Resource> connectors = graph.getObjects(element, DIA.HasConnector);
+            Collection<Resource> routeNodes = graph.getObjects(element, DIA.HasInteriorRouteNode);
+
+            // This is needed to make this query result change every time the underlying element changes visually.
+            Set<Object> identifier = new HashSet<Object>(connectors.size() + routeNodes.size());
+
+            for (Resource connector : connectors) {
+                for (Resource connectedTo : graph.getObjects(connector, STR.Connects)) {
+                    if (!connectedTo.equals(element)) {
+                        AffineTransform at = DiagramGraphUtil.getDynamicAffineTransform(graph, runtimeDiagram, connectedTo, DIA.HasDynamicTransform, false);
+                        identifier.add(at);
+                    }
+                }
+            }
+            for (Resource routeLine : routeNodes) {
+                RouteLine rl = Paster.readRouteLine(graph, routeLine);
+                identifier.add(rl);
+            }
+            return identifier;
+        } else {
+            return DiagramGraphUtil.getAffineTransform(graph, element);
+        }
+    }
+
+    @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);
+
+        if (node instanceof ConnectionNode) {
+            for (INode child : ((ConnectionNode)node).getSortedNodes()) {
+                if (child instanceof RouteGraphNode) {
+                    RouteGraphNode rgn = (RouteGraphNode) child;
+                    RouteGraph rg = rgn.getRouteGraph();
+                    Point2D nearest = rg.findNearestPoint(bounds.getCenterX(), bounds.getCenterY());
+                    if (nearest != null) {
+                        return AffineTransform.getTranslateInstance(nearest.getX(), nearest.getY());
+                    }
+                }
+            }
+        }
+        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, Object identifier) {
+            super(severity, identifier);
+        }
+        public Severity getSeverity() {
+            return (Severity) getField(0);
+        }
+    }
+
+}
+
+