use of org.osate.ge.gef.BaseConnectionNode in project osate2 by osate.
the class GefAgeDiagram method ensureSceneNodeExists.
/**
* Ensures that a scene node exists for the specified GEF diagram element.
* Creates or recreates scene nodes and adds to the scene graph as necessary. Updates the specified GEF diagram element.
* @param gefDiagramElement the GEF diagram element for which to ensure that the scene node exists.
* @param parentDiagramElementSceneNode the scene node for the parent of the GEF diagram element. This is specified instead of using
* the value contained in the GEF diagram element because it may not be up to date.
* @return the scene node for the diagram element. This specified GEF diagram element will be updated to hold this value.
*/
private Node ensureSceneNodeExists(GefDiagramElement gefDiagramElement, final Node parentDiagramElementSceneNode) {
Objects.requireNonNull(parentDiagramElementSceneNode, "parentDiagramElementScenenNode must not be null");
final Graphic graphic = Objects.requireNonNull(gefDiagramElement.diagramElement.getGraphic(), "graphic must not be null");
final DiagramElement childDiagramElement = gefDiagramElement.diagramElement;
//
// The following final variables determine the operations that needs to be performed by the remainder of the function.
// They are set by comparing the previous state with the current state of the diagram element.
//
final boolean docked = childDiagramElement.getDockArea() != null;
final boolean parentIsConnection = parentDiagramElementSceneNode instanceof BaseConnectionNode;
final boolean create = !Objects.equals(graphic, gefDiagramElement.sourceGraphic) || docked != gefDiagramElement.sceneNode instanceof DockedShape || parentIsConnection != gefDiagramElement.parentDiagramNodeSceneNode instanceof BaseConnectionNode;
final boolean addToScene = create || gefDiagramElement.parentDiagramNodeSceneNode != parentDiagramElementSceneNode;
final boolean removeFromScene = addToScene && gefDiagramElement.sceneNode != null;
// Update other fields
gefDiagramElement.sourceGraphic = graphic;
// Remove the node for the scene graph
if (removeFromScene) {
removeNode(gefDiagramElement.sceneNode);
}
//
if (create) {
// Remove mapping to old scene node
if (gefDiagramElement.sceneNode != null) {
sceneNodeToGefDiagramElementMap.remove(gefDiagramElement.sceneNode);
}
// Create the new node. Create a graphic and then a wrapper as appropriate
final Node graphicNode = GraphicToFx.createNode(graphic);
if (graphicNode instanceof BaseConnectionNode) {
final BaseConnectionNode newConnectionNode = (BaseConnectionNode) graphicNode;
gefDiagramElement.sceneNode = graphicNode;
// Create the primary label node
final LabelNode primaryLabel = new LabelNode();
newConnectionNode.getPrimaryLabels().add(primaryLabel);
gefDiagramElement.primaryLabel = primaryLabel;
} else if (graphicNode instanceof LabelNode) {
gefDiagramElement.sceneNode = graphicNode;
} else if (parentIsConnection) {
// NOTE: This should only occur for fixed sized graphics
// Rotate midpoint decorations 180.0 degrees because our connection not expects midpoint decorations to be oriented as if
// the connection was left to right and that is not how graphics are specified in the graphical editor.
final Group rotationWrapper = new Group();
rotationWrapper.getChildren().add(graphicNode);
rotationWrapper.setRotate(180.0);
gefDiagramElement.sceneNode = rotationWrapper;
} else {
if (docked) {
final DockedShape newDockedShape = new DockedShape();
newDockedShape.setGraphic(graphicNode);
gefDiagramElement.sceneNode = newDockedShape;
// Create the primary label node
final LabelNode primaryLabel = new LabelNode();
newDockedShape.getPrimaryLabels().add(primaryLabel);
gefDiagramElement.primaryLabel = primaryLabel;
// Create annotation node
final LabelNode annotationLabel = new LabelNode();
newDockedShape.getSecondaryLabels().add(annotationLabel);
gefDiagramElement.annotationLabel = annotationLabel;
} else {
final ContainerShape newContainerShape = new ContainerShape();
newContainerShape.setGraphic(graphicNode);
gefDiagramElement.sceneNode = newContainerShape;
// Create the primary label node
final LabelNode primaryLabel = new LabelNode();
newContainerShape.getPrimaryLabels().add(primaryLabel);
gefDiagramElement.primaryLabel = primaryLabel;
}
}
// Add mapping to scene node
if (gefDiagramElement.sceneNode != null) {
sceneNodeToGefDiagramElementMap.put(gefDiagramElement.sceneNode, gefDiagramElement);
}
StyleRoot.set(gefDiagramElement.sceneNode, true);
}
//
if (addToScene) {
if (gefDiagramElement.sceneNode instanceof BaseConnectionNode) {
diagramNode.getChildren().add(gefDiagramElement.sceneNode);
// Flow indicators are positioned relative to the scene node of the parent diagram element
if (gefDiagramElement.sceneNode instanceof FlowIndicatorNode) {
if (parentDiagramElementSceneNode instanceof ContainerShape) {
((FlowIndicatorNode) gefDiagramElement.sceneNode).setPositioningReference(parentDiagramElementSceneNode);
} else {
throw new AgeGefRuntimeException("Unexpected parent diagram element scene node for flow indicator: " + parentDiagramElementSceneNode);
}
}
} else if (gefDiagramElement.sceneNode instanceof LabelNode) {
// Add label to parent
if (parentDiagramElementSceneNode instanceof ContainerShape) {
((ContainerShape) parentDiagramElementSceneNode).getSecondaryLabels().add(gefDiagramElement.sceneNode);
} else if (parentDiagramElementSceneNode instanceof DockedShape) {
((DockedShape) parentDiagramElementSceneNode).getSecondaryLabels().add(gefDiagramElement.sceneNode);
} else if (parentDiagramElementSceneNode instanceof BaseConnectionNode) {
((BaseConnectionNode) parentDiagramElementSceneNode).getSecondaryLabels().add(gefDiagramElement.sceneNode);
} else {
throw new AgeGefRuntimeException("Unexpected parent node for label: " + parentDiagramElementSceneNode);
}
} else if (parentIsConnection) {
((BaseConnectionNode) parentDiagramElementSceneNode).getMidpointDecorations().add(gefDiagramElement.sceneNode);
} else {
final DockArea dockArea = childDiagramElement.getDockArea();
if (gefDiagramElement.sceneNode instanceof DockedShape) {
final DockedShape dockedShape = (DockedShape) gefDiagramElement.sceneNode;
// Add the docked shape to the appropriate list
if (parentDiagramElementSceneNode instanceof ContainerShape) {
final ContainerShape containerShapeParent = (ContainerShape) parentDiagramElementSceneNode;
containerShapeParent.addOrUpdateDockedChild(dockedShape, GefAgeDiagramUtil.toDockSide(dockArea));
} else if (parentDiagramElementSceneNode instanceof DockedShape) {
final DockedShape dockedShapeParent = (DockedShape) parentDiagramElementSceneNode;
dockedShapeParent.getNestedChildren().add(dockedShape);
} else {
throw new AgeGefRuntimeException("Unexpected parent for docked shape: " + parentDiagramElementSceneNode);
}
} else {
if (parentDiagramElementSceneNode instanceof ContainerShape) {
final ContainerShape containerShapeParent = (ContainerShape) parentDiagramElementSceneNode;
containerShapeParent.getFreeChildren().add(gefDiagramElement.sceneNode);
} else if (parentDiagramElementSceneNode instanceof Group) {
((Group) parentDiagramElementSceneNode).getChildren().add(gefDiagramElement.sceneNode);
} else {
throw new AgeGefRuntimeException("Unexpected parent node for container shape: " + parentDiagramElementSceneNode);
}
}
}
gefDiagramElement.parentDiagramNodeSceneNode = parentDiagramElementSceneNode;
}
return gefDiagramElement.sceneNode;
}
use of org.osate.ge.gef.BaseConnectionNode in project osate2 by osate.
the class GefAgeDiagram method updateDiagramFromSceneGraph.
/**
* Triggers a layout of the scene graph nodes and then updates the diagram based on the layout of the scene graph nodes.
* Updates position and size. Optionally updates bendpoints.
* Should only be called after the root node has been added to a scene.
* @param updateBendpoints whether to update bendpoints in addition to the position and size of elements.
*/
public void updateDiagramFromSceneGraph(final boolean updateBendpoints) {
updatingDiagramFromSceneGraph = true;
forceSceneGraphLayout();
diagram.modify("Update Diagram from Scene Graph", m -> {
for (final Entry<DiagramElement, GefDiagramElement> e : this.diagramElementToGefDiagramElementMap.entrySet()) {
final DiagramElement de = e.getKey();
final GefDiagramElement ge = e.getValue();
final Node sceneNode = ge.sceneNode;
final DiagramNode parent = de.getParent();
if (DiagramElementPredicates.isMoveable(de)) {
if (parent instanceof DiagramElement && DiagramElementPredicates.isConnection((DiagramElement) parent)) {
// Store the preferred position.
final Point2D p = PreferredPosition.get(sceneNode);
m.setPosition(de, GefAgeDiagramUtil.toAgePoint(p));
} else {
final double newX = sceneNode.getLayoutX();
final double newY = sceneNode.getLayoutY();
if (de.hasPosition() || (newX != 0.0 || newY != 0)) {
m.setPosition(de, new Point(newX, newY));
}
}
if (sceneNode instanceof DockedShape && ge.parentDiagramNodeSceneNode instanceof ContainerShape) {
final DockedShape ds = (DockedShape) sceneNode;
final DockSide side = ds.getSide();
if (side != null) {
m.setDockArea(de, GefAgeDiagramUtil.toDockArea(side));
}
}
}
// Set the size for all elements. Even for non-resizable elements, the layout engine uses the sizes in the diagram.
// This is important for secondary labels of connections.
final Bounds layoutBounds = sceneNode.getLayoutBounds();
if (de.hasSize() || (layoutBounds.getWidth() != 0.0 || layoutBounds.getHeight() != 0)) {
m.setSize(de, new Dimension(layoutBounds.getWidth(), layoutBounds.getHeight()));
}
if (DiagramElementPredicates.isConnection(de) && sceneNode instanceof BaseConnectionNode) {
final BaseConnectionNode cn = (BaseConnectionNode) sceneNode;
// Primary label position
if (!cn.getPrimaryLabels().isEmpty()) {
// Store the preferred position of the connection label
final Node primaryLabel = cn.getPrimaryLabels().get(0);
final Point2D p = PreferredPosition.get(primaryLabel);
m.setConnectionPrimaryLabelPosition(de, GefAgeDiagramUtil.toAgePoint(p));
}
// Bendpoints
if (updateBendpoints) {
final List<org.eclipse.gef.geometry.planar.Point> controlPoints = cn.getInnerConnection().getControlPoints();
if (!controlPoints.isEmpty() || !de.getBendpoints().isEmpty()) {
final Point controlPointOrigin = getControlPointOriginFromSceneGraph(sceneNode);
m.setBendpoints(de, cn.getInnerConnection().getControlPoints().stream().map(p -> new Point(p.x + controlPointOrigin.x, p.y + controlPointOrigin.y)).collect(Collectors.toList()));
}
}
}
}
});
updatingDiagramFromSceneGraph = false;
}
use of org.osate.ge.gef.BaseConnectionNode in project osate2 by osate.
the class GefAgeDiagram method updateSceneNode.
/**
* Updates the scene nodes related to the specified GEF diagram element based on the diagram element.
* Only updates properties which do not effect the structure of the scene graph. Not-recursive
* @param gefDiagramElement is the GEF diagram element for which scene nodes will be updated.
*/
private void updateSceneNode(final GefDiagramElement gefDiagramElement) {
final DiagramElement diagramElement = gefDiagramElement.diagramElement;
final Node sceneNode = gefDiagramElement.sceneNode;
// Update connections
if (sceneNode instanceof BaseConnectionNode) {
final BaseConnectionNode connectionNode = (BaseConnectionNode) sceneNode;
final Point controlPointOrigin = getControlPointOriginFromDiagram(diagramElement, sceneNode);
if (sceneNode instanceof FlowIndicatorNode) {
PreferredPosition.set(sceneNode, convertPoint(diagramElement.getPosition()));
}
// Update the connection anchor
updateConnectionAnchors(diagramElement, (BaseConnectionNode) sceneNode);
// Set control points. Coordinates are specified in the diagram model relative to the diagram. The need to be specified relative to the
// connection position. For regular connection this is the same because the node's parent is the diagram node.
// However, flow indicators have a position and have parent nodes other than the diagram.
connectionNode.getInnerConnection().setControlPoints(diagramElement.getBendpoints().stream().map(p -> new org.eclipse.gef.geometry.planar.Point(p.x - controlPointOrigin.x, p.y - controlPointOrigin.y)).collect(Collectors.toList()));
PreferredPosition.set(gefDiagramElement.primaryLabel, convertPoint(diagramElement.getConnectionPrimaryLabelPosition()));
} else if (sceneNode instanceof LabelNode) {
// Such a label represents a secondary label
final LabelNode label = (LabelNode) sceneNode;
label.setText(Strings.nullToEmpty(diagramElement.getLabelName()));
setLabelVisibility(label);
// Update element position
if (gefDiagramElement.parentDiagramNodeSceneNode instanceof BaseConnectionNode) {
PreferredPosition.set(label, convertPoint(diagramElement.getPosition()));
}
} else if (sceneNode instanceof ContainerShape) {
final ContainerShape containerShape = (ContainerShape) sceneNode;
PreferredPosition.set(sceneNode, convertPoint(diagramElement.getPosition()));
// Set configured size
final Dimension size = diagramElement.getSize();
if (size == null) {
containerShape.setConfiguredWidth(ContainerShape.NOT_SPECIFIED);
containerShape.setConfiguredHeight(ContainerShape.NOT_SPECIFIED);
} else {
containerShape.setConfiguredWidth(size.width);
containerShape.setConfiguredHeight(size.height);
}
} else if (sceneNode instanceof DockedShape) {
final DockedShape n = (DockedShape) sceneNode;
PreferredPosition.set(sceneNode, convertPoint(diagramElement.getPosition()));
// Set configured size
final Dimension size = diagramElement.getSize();
if (size == null) {
n.setConfiguredWidth(ContainerShape.NOT_SPECIFIED);
n.setConfiguredHeight(ContainerShape.NOT_SPECIFIED);
} else {
n.setConfiguredWidth(size.width);
n.setConfiguredHeight(size.height);
}
final DockArea dockArea = diagramElement.getDockArea();
if (dockArea != null && dockArea != DockArea.GROUP && gefDiagramElement.parentDiagramNodeSceneNode instanceof ContainerShape) {
final DockSide side = GefAgeDiagramUtil.toDockSide(dockArea);
final ContainerShape cs = (ContainerShape) gefDiagramElement.parentDiagramNodeSceneNode;
cs.addOrUpdateDockedChild(n, side);
}
}
// Update the primary label
if (gefDiagramElement.primaryLabel != null) {
gefDiagramElement.primaryLabel.setText(getPrimaryLabelText(diagramElement));
setLabelVisibility(gefDiagramElement.primaryLabel);
gefDiagramElement.primaryLabel.setWrapText(diagramElement.getGraphicalConfiguration().isPrimaryLabelIsMultiline());
}
// Update the secondary label
if (gefDiagramElement.annotationLabel != null) {
final String annotation = diagramElement.getGraphicalConfiguration().getAnnotation();
gefDiagramElement.annotationLabel.setText(Strings.nullToEmpty(annotation));
setLabelVisibility(gefDiagramElement.annotationLabel);
}
}
use of org.osate.ge.gef.BaseConnectionNode in project osate2 by osate.
the class JavaFXBot method click.
/**
* Clicks a scene graph node.
* If the node is contained in a {@link ScrollPane}, this function will scroll the pane so that it is visible.
* @param node the node to click.
*/
public void click(final Node node) {
ensureRobotCreated();
UiTestUtil.waitUntil(() -> UIThreadRunnable.syncExec(() -> isVisible(node)), "Node " + node + " is not visible");
Display.getDefault().syncExec(() -> {
ensureVisible(node);
final Point2D p;
if (node instanceof BaseConnectionNode) {
final BaseConnectionNode cn = (BaseConnectionNode) node;
final Connection ic = cn.getInnerConnection();
final Point startPoint = ic.getStartPoint();
p = ic.localToScreen(startPoint.x, startPoint.y);
} else {
p = node.localToScreen(4, 4);
}
robot.mouseMove(p.getX(), p.getY());
robot.mousePress(MouseButton.PRIMARY);
robot.mouseRelease(MouseButton.PRIMARY);
});
}
use of org.osate.ge.gef.BaseConnectionNode in project osate2 by osate.
the class SelectedElementsMover method handleEvent.
@Override
public HandledEvent handleEvent(final InputEvent e) {
if (!editor.getPaletteModel().isSelectToolActive()) {
return null;
}
if (e instanceof MouseEvent) {
final MouseEvent mouseEvent = (MouseEvent) e;
if ((mouseEvent.getEventType() != MouseEvent.MOUSE_PRESSED && mouseEvent.getEventType() != MouseEvent.MOUSE_DRAGGED) || mouseEvent.getButton() != MouseButton.PRIMARY) {
return null;
}
final DiagramElement clickedDiagramElement = InputEventHandlerUtil.getTargetDiagramElement(editor.getGefDiagram(), e.getTarget());
if (clickedDiagramElement == null) {
return null;
}
if (e.getEventType() == MouseEvent.MOUSE_PRESSED) {
// Store the starting position.
mousePressLocationDiagram = editor.getGefDiagram().getSceneNode().getSceneToLocalTransform().transform(mouseEvent.getSceneX(), mouseEvent.getSceneY());
// Determine if the primary label is the node being moved
mousePressPrimaryConnectionLabel = null;
if (DiagramElementPredicates.isConnection(clickedDiagramElement)) {
for (Node tmp = (Node) e.getTarget(); tmp != null; tmp = tmp.getParent()) {
if (tmp instanceof BaseConnectionNode) {
break;
}
if (tmp instanceof LabelNode) {
mousePressPrimaryConnectionLabel = tmp;
break;
}
}
}
} else if (e.getEventType() == MouseEvent.MOUSE_DRAGGED && mousePressLocationDiagram != null) {
final Point2D newPositionDiagram = editor.getGefDiagram().getSceneNode().getSceneToLocalTransform().transform(mouseEvent.getSceneX(), mouseEvent.getSceneY());
final double d = mousePressLocationDiagram.distance(newPositionDiagram);
if (d > MIN_MOUSE_DRAGGED_DISTANCE) {
try {
if (mousePressPrimaryConnectionLabel == null) {
final MouseMoveSelectedElementsInteraction newInteraction = new MouseMoveSelectedElementsInteraction(editor, mousePressLocationDiagram);
return HandledEvent.newInteraction(newInteraction);
} else {
final BaseConnectionNode cn = InputEventHandlerUtil.getClosestConnection(mousePressPrimaryConnectionLabel);
if (cn == null) {
return null;
}
final MovePrimaryConnectionLabelInteraction newInteraction = new MovePrimaryConnectionLabelInteraction(editor, mousePressLocationDiagram, cn, mousePressPrimaryConnectionLabel);
return HandledEvent.newInteraction(newInteraction);
}
} finally {
mousePressLocationDiagram = null;
}
}
}
return HandledEvent.consumed();
} else if (e instanceof KeyEvent) {
if (e.getEventType() != KeyEvent.KEY_PRESSED) {
return null;
}
final KeyEvent keyEvent = (KeyEvent) e;
if (keyEvent.getCode() != KeyCode.PERIOD) {
return null;
}
final KeyboardMoveSelectedElementsInteraction newInteraction = new KeyboardMoveSelectedElementsInteraction(editor);
return HandledEvent.newInteraction(newInteraction);
} else {
return null;
}
}
Aggregations