X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.g2d%2Fsrc%2Forg%2Fsimantics%2Fg2d%2Fdiagram%2Fparticipant%2FElementPainter.java;h=6e9ff85b1f62c9fb75f4552e346c956cad80113a;hp=1eb15f61c2132ece167643ee6af6638ef4cb6e9b;hb=25f90625263911a40f785edbeb8ca44b3db23911;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07 diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java index 1eb15f61c..6e9ff85b1 100644 --- a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java +++ b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/participant/ElementPainter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * Copyright (c) 2007, 2018 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 @@ -8,6 +8,7 @@ * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - GitLab issue #66 *******************************************************************************/ package org.simantics.g2d.diagram.participant; @@ -91,6 +92,7 @@ import org.simantics.scenegraph.g2d.nodes.SingleElementNode; import org.simantics.scenegraph.g2d.nodes.UnboundedNode; import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode; import org.simantics.scenegraph.utils.ColorUtil; +import org.simantics.scenegraph.utils.GeometryUtils; import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.utils.datastructures.collections.CollectionUtils; import org.simantics.utils.datastructures.hints.HintListenerAdapter; @@ -140,6 +142,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos } private static final boolean DEBUG = false; + private static final boolean NODE_TO_ELEMENT_MAPPING = true; public static final int ELEMENT_PAINT_PRIORITY = 10; @@ -197,8 +200,9 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos return; if (oldValue != null) { + Map nodeToElementMap = oldValue.removeHint(DiagramHints.NODE_TO_ELEMENT_MAP); for (IElement e : oldValue.getElements()) { - removeElement(e); + removeElement(oldValue, e, nodeToElementMap); } oldValue.removeCompositionListener(this); @@ -216,8 +220,13 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos } if (newValue != null) { + diagram.removeHint(DiagramHints.NODE_TO_ELEMENT_MAP); + Map nodeElementMap = NODE_TO_ELEMENT_MAPPING ? new HashMap<>() : null; + if (nodeElementMap != null) + diagram.setHint(DiagramHints.NODE_TO_ELEMENT_MAP, nodeElementMap); + for (IElement e : newValue.getElements()) { - addElement(e, false); + addElement(newValue, e, false, nodeElementMap); } newValue.addCompositionListener(this); @@ -241,7 +250,8 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos public void initSG(G2DParentNode parent) { diagramParent = parent.addNode("elements_"+Node.IDCOUNTER, UnboundedNode.class); diagramParent.setZIndex(ELEMENT_PAINT_PRIORITY); - elementParent = diagramParent.addNode("spatialRoot", RTreeNode.class); + elementParent = diagramParent.addNode(SceneGraphConstants.SPATIAL_ROOT_NODE_NAME, RTreeNode.class); + elementParent.setLookupId(SceneGraphConstants.SPATIAL_ROOT_NODE_ID); elementParent.setZIndex(0); } @@ -498,10 +508,11 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos if (DEBUG) System.out.println("EP.onElementAdded(" + d + ", " + e + ")"); + Map nodeElementMap = diagram.getHint(DiagramHints.NODE_TO_ELEMENT_MAP); if (inDiagramTransaction()) { - addElement(e, false); + addElement(d, e, false, nodeElementMap); } else { - addElement(e, true); + addElement(d, e, true, nodeElementMap); } } @Override @@ -509,7 +520,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos if (DEBUG) System.out.println("EP.onElementRemoved(" + d + ", " + e + ")"); - removeElement(e); + removeElement(d, e, diagram.getHint(DiagramHints.NODE_TO_ELEMENT_MAP)); } @Override @@ -517,17 +528,19 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos if (DEBUG) System.out.println("EP.elementChildrenChanged: " + event); + Map nodeElementMap = diagram.getHint(DiagramHints.NODE_TO_ELEMENT_MAP); + for (IElement removed : event.removed) { - removeElement(removed); + removeElement(diagram, removed, nodeElementMap); } for (IElement added : event.added) { - addElement(added, false); + addElement(diagram, added, false, nodeElementMap); } } private final List childrenTemp = new ArrayList(); - public void addElement(IElement e, boolean synchronizeSceneGraphNow) { + public void addElement(IDiagram d, IElement e, boolean synchronizeSceneGraphNow, Map nodeElementMap) { if (DEBUG) System.out.println("EP.addElement(now=" + synchronizeSceneGraphNow + ", " + e + ")"); @@ -557,9 +570,13 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos ConnectionNode holder = e.getHint(sgKey); if (holder == null) { holder = parentNode.addNode(ElementUtils.generateNodeId(e), ConnectionNode.class); + holder.setKey(e.getHint(ElementHints.KEY_OBJECT)); + holder.setTypeClass(e.getHint(ElementHints.KEY_TYPE_CLASS)); holder.setTransferableProvider(new ElementTransferableProvider(getContext(), e)); e.setHint(sgKey, holder); holder.setZIndex(parentNode.getNodeCount() + 1); + if (nodeElementMap != null) + nodeElementMap.put(holder, e); } } else { @@ -567,9 +584,13 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos SingleElementNode holder = e.getHint(sgKey); if (holder == null) { holder = parentNode.addNode(ElementUtils.generateNodeId(e), SingleElementNode.class); + holder.setKey(e.getHint(ElementHints.KEY_OBJECT)); + holder.setTypeClass(e.getHint(ElementHints.KEY_TYPE_CLASS)); holder.setTransferableProvider(new ElementTransferableProvider(getContext(), e)); e.setHint(sgKey, holder); holder.setZIndex(parentNode.getNodeCount() + 1); + if (nodeElementMap != null) + nodeElementMap.put(holder, e); } } @@ -582,7 +603,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos children.getChildren(e, childrenTemp); //System.out.println("children: " + childrenTemp); for (IElement child : childrenTemp) { - addElement(child, false); + addElement(d, child, false, nodeElementMap); } childrenTemp.clear(); } @@ -593,7 +614,7 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos //setTreeDirty(); } - protected void removeElement(IElement e) { + protected void removeElement(IDiagram d, IElement e, Map nodeElementMap) { if (DEBUG) System.out.println("EP.removeElement(" + e + ")"); @@ -619,6 +640,8 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos Node n = e.removeHint(sgKey); if (n != null) { n.remove(); + if (nodeElementMap != null) + nodeElementMap.remove(n); } } @@ -1010,8 +1033,12 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos Shape shape = ElementUtils.getElementShapeOrBounds(e); Rectangle2D bounds = shape.getBounds2D(); //System.out.println("selection bounds: "+bounds); - final double margin = 1; - bounds.setFrame(bounds.getMinX() - margin, bounds.getMinY() - margin, bounds.getWidth() + 2*margin, bounds.getHeight() + 2*margin); + + Point2D scale = GeometryUtils.getScale2D(selectionTransform); + final double marginX = Math.abs(scale.getX()) > 1e-10 ? 1 / scale.getX() : 1; + final double marginY = Math.abs(scale.getY()) > 1e-10 ? 1 / scale.getY() : 1; + + bounds.setFrame(bounds.getMinX() - marginX, bounds.getMinY() - marginY, bounds.getWidth() + 2*marginX, bounds.getHeight() + 2*marginY); List ss = e.getElementClass().getItemsByClass(SelectionSpecification.class); if (!ss.isEmpty()) { @@ -1081,6 +1108,9 @@ public class ElementPainter extends AbstractDiagramParticipant implements Compos } else { SelectionNode s = selectionNode.getOrCreateNode(getNodeId("shape", e), SelectionNode.class); s.init(selectionTransform, bounds, color); + Double paddingFactor = diagram.getHint(DiagramHints.SELECTION_PADDING_SCALE_FACTOR); + if (paddingFactor != null) + s.setPaddingFactor(paddingFactor); } }