Search in sources :

Example 1 with Dimension

use of org.osate.ge.graphics.Dimension in project osate2 by osate.

the class SaveImageHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (!(activeEditor instanceof InternalDiagramEditor)) {
        throw new RuntimeException("Unexpected editor: " + activeEditor);
    }
    final InternalDiagramEditor editor = (InternalDiagramEditor) activeEditor;
    final InternalDiagramExportService diagramExportService = getDiagramExportService().orElseThrow();
    final DiagramNode selectionOnlyExportNode = editor.getSelectedDiagramElements().stream().findFirst().filter(DiagramElementPredicates::isShape).orElse(null);
    // Get the dimensions of the diagram and the current selection
    final Dimension diagramDimension = diagramExportService.getDimensions(editor, editor.getDiagram());
    final Dimension selectionDimension = selectionOnlyExportNode == null ? null : diagramExportService.getDimensions(editor, selectionOnlyExportNode);
    final Shell parentShell = Display.getCurrent().getActiveShell();
    // Display the export diagram dialog
    ExportDiagramDialog.open(parentShell, diagramDimension, selectionDimension).ifPresent(r -> {
        final FileDialog saveDialog = new FileDialog(parentShell, SWT.SAVE);
        saveDialog.setOverwrite(true);
        // TOOD; Based on format
        saveDialog.setFilterExtensions(new String[] { "*" + r.getFormat().getDotExtension() });
        final String filepath = saveDialog.open();
        if (filepath != null) {
            // Export the image
            try (FileOutputStream s = new FileOutputStream(filepath, false)) {
                final DiagramNode exportNode = r.getAll() ? editor.getDiagram() : selectionOnlyExportNode;
                diagramExportService.export(editor, s, r.getFormat().getExporterFormat(), exportNode, r.getScaling());
            } catch (final Exception ex) {
                final Status status = new Status(IStatus.ERROR, FrameworkUtil.getBundle(getClass()).getSymbolicName(), "Error Exporting Diagram", ex);
                StatusManager.getManager().handle(status, StatusManager.LOG | StatusManager.SHOW);
            }
        }
    });
    return null;
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) Shell(org.eclipse.swt.widgets.Shell) InternalDiagramExportService(org.osate.ge.internal.services.InternalDiagramExportService) FileOutputStream(java.io.FileOutputStream) IEditorPart(org.eclipse.ui.IEditorPart) Dimension(org.osate.ge.graphics.Dimension) FileDialog(org.eclipse.swt.widgets.FileDialog) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 2 with Dimension

use of org.osate.ge.graphics.Dimension in project osate2 by osate.

the class MatchSizeHandler method execute.

@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
    final AgeDiagram diagram = UiUtil.getDiagram(selectedDiagramElements);
    if (diagram == null) {
        throw new RuntimeException("Unable to get diagram");
    }
    diagram.modify("Match Size", m -> {
        final Dimension newSize = AgeHandlerUtil.getPrimaryDiagramElement(selectedDiagramElements).getSize();
        for (final DiagramElement tmpElement : selectedDiagramElements) {
            m.setSize(tmpElement, newSize);
        }
    });
    return null;
}
Also used : DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) Dimension(org.osate.ge.graphics.Dimension)

Example 3 with Dimension

use of org.osate.ge.graphics.Dimension 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;
}
Also used : DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode) DiagramRootNode(org.osate.ge.gef.DiagramRootNode) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) FeatureGroupNode(org.osate.ge.gef.FeatureGroupNode) LabelNode(org.osate.ge.gef.LabelNode) Node(javafx.scene.Node) ConnectionNode(org.osate.ge.gef.ConnectionNode) FlowIndicatorNode(org.osate.ge.gef.FlowIndicatorNode) DockedShape(org.osate.ge.gef.DockedShape) Bounds(javafx.geometry.Bounds) Point(org.osate.ge.graphics.Point) Dimension(org.osate.ge.graphics.Dimension) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) DockSide(org.osate.ge.gef.DockSide) Point2D(javafx.geometry.Point2D) ContainerShape(org.osate.ge.gef.ContainerShape) BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode)

Example 4 with Dimension

use of org.osate.ge.graphics.Dimension 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);
    }
}
Also used : LabelNode(org.osate.ge.gef.LabelNode) BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode) DiagramRootNode(org.osate.ge.gef.DiagramRootNode) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) FeatureGroupNode(org.osate.ge.gef.FeatureGroupNode) LabelNode(org.osate.ge.gef.LabelNode) Node(javafx.scene.Node) ConnectionNode(org.osate.ge.gef.ConnectionNode) FlowIndicatorNode(org.osate.ge.gef.FlowIndicatorNode) DockedShape(org.osate.ge.gef.DockedShape) Point(org.osate.ge.graphics.Point) Dimension(org.osate.ge.graphics.Dimension) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) FlowIndicatorNode(org.osate.ge.gef.FlowIndicatorNode) DockSide(org.osate.ge.gef.DockSide) DockArea(org.osate.ge.internal.diagram.runtime.DockArea) ContainerShape(org.osate.ge.gef.ContainerShape) BaseConnectionNode(org.osate.ge.gef.BaseConnectionNode)

Example 5 with Dimension

use of org.osate.ge.graphics.Dimension in project osate2 by osate.

the class GefDiagramExportService method getDimensions.

@Override
public Dimension getDimensions(final GraphicalEditor editor, final DiagramNode exportNode) {
    Objects.requireNonNull(exportNode, "exportNode must not be null");
    final GefAgeDiagram diagram = checkEditor(editor).getGefDiagram();
    final Bounds bounds = getBounds(getExportNodes(diagram, exportNode), diagram.getSceneNode().getSceneToLocalTransform());
    return new Dimension(bounds.getWidth(), bounds.getHeight());
}
Also used : GefAgeDiagram(org.osate.ge.gef.ui.diagram.GefAgeDiagram) Bounds(javafx.geometry.Bounds) Dimension(org.osate.ge.graphics.Dimension)

Aggregations

Dimension (org.osate.ge.graphics.Dimension)14 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)9 Point (org.osate.ge.graphics.Point)5 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)5 DiagramNode (org.osate.ge.internal.diagram.runtime.DiagramNode)5 ElkLabel (org.eclipse.elk.graph.ElkLabel)4 ElkPort (org.eclipse.elk.graph.ElkPort)4 DockArea (org.osate.ge.internal.diagram.runtime.DockArea)4 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 PortSide (org.eclipse.elk.core.options.PortSide)3 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)3 ElkEdge (org.eclipse.elk.graph.ElkEdge)3 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)3 ElkNode (org.eclipse.elk.graph.ElkNode)3 IEditorPart (org.eclipse.ui.IEditorPart)3 AgeConnection (org.osate.ge.graphics.internal.AgeConnection)3 AgeShape (org.osate.ge.graphics.internal.AgeShape)3 Label (org.osate.ge.graphics.internal.Label)3 InternalDiagramEditor (org.osate.ge.internal.ui.editor.InternalDiagramEditor)3