]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java
Handle SelectionOutline interface implementation in SCLScenegraph
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / participant / ElementPainter.java
index 6e9ff85b1f62c9fb75f4552e346c956cad80113a..9134c1729ac161c7ec4d9bcb927880da9612b8dd 100644 (file)
@@ -137,7 +137,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
      * ElementPainter.
      */
     public static interface ISelectionProvider {
-        public void init(final IElement e, final G2DParentNode parentNode, final String nodeId,
+        public void init(int selectionId, final IElement e, final G2DParentNode parentNode, final String nodeId,
                 final AffineTransform transform, final Rectangle2D bounds, final Color color);
     }
 
@@ -158,7 +158,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
     SingleElementNode diagramParent;
     RTreeNode elementParent;
 
-    boolean paintSelectionFrames;
+    ElementPainterConfiguration cfg;
 
     /**
      * Internally reused to avert constant reallocation.
@@ -174,7 +174,11 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
     }
 
     public ElementPainter(boolean paintSelectionFrames) {
-        this.paintSelectionFrames = paintSelectionFrames;
+        this(new ElementPainterConfiguration().paintSelectionFrames(paintSelectionFrames));
+    }
+
+    public ElementPainter(ElementPainterConfiguration cfg) {
+        this.cfg = cfg;
     }
 
     @Override
@@ -828,7 +832,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
         Object task = BEGIN("EP.updateSelections");
 
         try {
-            if (!paintSelectionFrames)
+            if (!cfg.paintSelectionFrames)
                 return;
             if (selection == null)
                 return;
@@ -934,7 +938,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
         Object task = BEGIN("EP.updateSelection");
 
         try {
-            if (!paintSelectionFrames)
+            if (!cfg.paintSelectionFrames)
                 return;
 
             G2DParentNode elementNode = (G2DParentNode) el.getHint(ElementHints.KEY_SG_NODE);
@@ -953,7 +957,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
                     continue;
 
                 if (NodeUtil.needSelectionPaint(elementNode))
-                    paintSelectionFrame(elementNode, selectionNode, el, color);
+                    paintSelectionFrame(selectionId, elementNode, selectionNode, el, color);
 
                 nodesUpdated = true;
             }
@@ -980,12 +984,14 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
         Color color = getSelectionColor(selectionId);
         G2DParentNode selectionsNode = getSelectionsNode(selectionId);
 
+        Class<? extends G2DParentNode> selectionNodeClass = cfg.selectionNodeClass != null ? cfg.selectionNodeClass : G2DParentNode.class;
+
         for (IElement e : selection) {
             Node elementNode = e.getHint(ElementHints.KEY_SG_NODE);
 //            System.out.println("selectionNode: " + elementNode + " " + e);
             if (elementNode instanceof G2DParentNode) {
                 G2DParentNode en = (G2DParentNode) elementNode;
-                G2DParentNode selectionNode = en.getOrCreateNode(NodeUtil.SELECTION_NODE_NAME, G2DParentNode.class);
+                G2DParentNode selectionNode = en.getOrCreateNode(NodeUtil.SELECTION_NODE_NAME, selectionNodeClass);
                 selectionNode.setZIndex(SELECTION_PAINT_PRIORITY);
                 if (selectionNodes != null)
                     selectionNodes.add(selectionNode);
@@ -999,7 +1005,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
                 createSelectionReference(selectionsNode, elementNode);
 
                 if (NodeUtil.needSelectionPaint(elementNode))
-                    paintSelectionFrame(en, selectionNode, e, color);
+                    paintSelectionFrame(selectionId, en, selectionNode, e, color);
 
             } else {
                 if (elementNode != null) {
@@ -1027,7 +1033,18 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
         return result;
     }
 
-    public void paintSelectionFrame(G2DParentNode elementNode, G2DParentNode selectionNode, final IElement e, Color color) {
+    
+    /**
+     * We need to have separate class for SelectionNode, so that SCLSceneGraph can handle this properly.
+     * 
+     */
+    public static class SelectionShapeNode extends ShapeNode {
+
+               private static final long serialVersionUID = -5393630944240940166L;
+       
+    }
+    
+    public void paintSelectionFrame(int selectionId, G2DParentNode elementNode, G2DParentNode selectionNode, final IElement e, Color color) {
         // The element node already has the correct transform.
         AffineTransform selectionTransform = ElementUtils.getTransform(e);// no it doesnt ... new AffineTransform();
         Shape shape = ElementUtils.getElementShapeOrBounds(e);
@@ -1048,7 +1065,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
                 Outline outline = (Outline) es.getAdapter(Outline.class);
                 if (outline == null || outline.getElementShape(e) == null)
                        continue;
-                ShapeNode shapenode = shapeholder.getOrCreateNode(getNodeId("outline", e, es), ShapeNode.class);
+                ShapeNode shapenode = shapeholder.getOrCreateNode(getNodeId("outline", e, es), SelectionShapeNode.class);
 //                shapenode.setShape(es.getSelectionShape(e));
 //                shapenode.setStroke(SELECTION_STROKE);
 //                shapenode.setScaleStroke(true);
@@ -1084,7 +1101,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
             G2DParentNode shapeholder = selectionNode.getOrCreateNode(getNodeId("outlines", e), G2DParentNode.class);
 
             for (SelectionOutline es : shapeHandlers) {
-                ShapeNode shapenode = shapeholder.getOrCreateNode(getNodeId("outline", e, es), ShapeNode.class);
+                ShapeNode shapenode = shapeholder.getOrCreateNode(getNodeId("outline", e, es), SelectionShapeNode.class);
 //                shapenode.setShape(es.getSelectionShape(e));
 //                shapenode.setStroke(SELECTION_STROKE);
 //                shapenode.setScaleStroke(true);
@@ -1104,10 +1121,10 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos
 
         ISelectionProvider provider = this.getContext().getDefaultHintContext().getHint(KEY_SELECTION_PROVIDER);
         if (provider != null) {
-            provider.init(e, selectionNode, getNodeId("shape", e), selectionTransform, bounds, color);
+            provider.init(selectionId, e, selectionNode, getNodeId("shape", e), selectionTransform, bounds, color);
         } else {
             SelectionNode s = selectionNode.getOrCreateNode(getNodeId("shape", e), SelectionNode.class);
-            s.init(selectionTransform, bounds, color);
+            s.init(selectionId, selectionTransform, bounds, color);
             Double paddingFactor = diagram.getHint(DiagramHints.SELECTION_PADDING_SCALE_FACTOR);
             if (paddingFactor != null)
                 s.setPaddingFactor(paddingFactor);