Search in sources :

Example 1 with ShapeNodeEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart in project elk by eclipse.

the class ElkLayoutProvider method canLayoutNodes.

/**
 * {@inheritDoc}<br>
 * <br>
 * This method returns <code>true</code> only for {@link IGraphicalEditPart IGraphicalEditParts}
 * whose figures are visible and that contain at least a {@link ShapeNodeEditPart}, which is not
 * an {@link AbstractBorderItemEditPart} or an {@link AbstractImageEditPart}.<br>
 * <br>
 * In short, returns only true if the provided edit part contains children that can be layouted.
 */
@SuppressWarnings("rawtypes")
public boolean canLayoutNodes(final List layoutNodes, final boolean shouldOffsetFromBoundingBox, final IAdaptable layoutHint) {
    Object o = layoutHint.getAdapter(IGraphicalEditPart.class);
    if (!(o instanceof IGraphicalEditPart)) {
        return false;
    }
    final IGraphicalEditPart parent = (IGraphicalEditPart) o;
    // computing layout on the element wouldn't make sense otherwise
    if (layoutNodes.isEmpty()) {
        return Iterables.any((List<?>) parent.getChildren(), new Predicate<Object>() {

            public boolean apply(final Object o) {
                return o instanceof ShapeNodeEditPart && !(o instanceof AbstractBorderItemEditPart) && !(o instanceof AbstractImageEditPart);
            }
        });
    } else {
        return Iterables.any((List<?>) layoutNodes, new Predicate<Object>() {

            public boolean apply(final Object o) {
                ILayoutNode layoutNode = (ILayoutNode) o;
                IGraphicalEditPart editPart = findEditPart(parent, layoutNode.getNode());
                return editPart instanceof ShapeNodeEditPart && !(editPart instanceof AbstractBorderItemEditPart) && !(editPart instanceof AbstractImageEditPart);
            }
        });
    }
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) ILayoutNode(org.eclipse.gmf.runtime.diagram.ui.services.layout.ILayoutNode) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) AbstractImageEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.AbstractImageEditPart)

Example 2 with ShapeNodeEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method buildLayoutGraph.

@Override
public LayoutMapping buildLayoutGraph(final IWorkbenchPart workbenchPart, final Object diagramPart) {
    // get the diagram editor part
    DiagramEditor diagramEditor = getDiagramEditor(workbenchPart);
    // choose the layout root edit part
    IGraphicalEditPart layoutRootPart = null;
    List<ShapeNodeEditPart> selectedParts = null;
    if (diagramPart instanceof ShapeNodeEditPart || diagramPart instanceof DiagramEditPart) {
        layoutRootPart = (IGraphicalEditPart) diagramPart;
    } else if (diagramPart instanceof IGraphicalEditPart) {
        EditPart tgEditPart = ((IGraphicalEditPart) diagramPart).getTopGraphicEditPart();
        if (tgEditPart instanceof ShapeNodeEditPart) {
            layoutRootPart = (IGraphicalEditPart) tgEditPart;
        }
    } else if (diagramPart instanceof Collection) {
        Collection<?> selection = (Collection<?>) diagramPart;
        // determine the layout root part from the selection
        for (Object object : selection) {
            if (object instanceof IGraphicalEditPart) {
                if (layoutRootPart != null) {
                    EditPart parent = commonParent(layoutRootPart, (EditPart) object);
                    if (parent != null && !(parent instanceof RootEditPart)) {
                        layoutRootPart = (IGraphicalEditPart) parent;
                    }
                } else if (!(object instanceof ConnectionEditPart)) {
                    layoutRootPart = (IGraphicalEditPart) object;
                }
            }
        }
        // build a list of edit parts that shall be layouted completely
        if (layoutRootPart != null) {
            selectedParts = new ArrayList<ShapeNodeEditPart>(selection.size());
            for (Object object : selection) {
                if (object instanceof IGraphicalEditPart) {
                    EditPart editPart = (EditPart) object;
                    while (editPart != null && editPart.getParent() != layoutRootPart) {
                        editPart = editPart.getParent();
                    }
                    if (editPart instanceof ShapeNodeEditPart && editPartFilter.filter(editPart) && !selectedParts.contains(editPart)) {
                        selectedParts.add((ShapeNodeEditPart) editPart);
                    }
                }
            }
        }
    }
    if (layoutRootPart == null && diagramEditor != null) {
        layoutRootPart = diagramEditor.getDiagramEditPart();
    }
    if (layoutRootPart == null) {
        throw new IllegalArgumentException("Not supported by this layout connector: Workbench part " + workbenchPart + ", Edit part " + diagramPart);
    }
    // create the mapping
    LayoutMapping mapping = buildLayoutGraph(layoutRootPart, selectedParts, workbenchPart);
    return mapping;
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) DiagramEditor(org.eclipse.gmf.runtime.diagram.ui.parts.DiagramEditor) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) RootEditPart(org.eclipse.gef.RootEditPart) CompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart) EditPart(org.eclipse.gef.EditPart) IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) RootEditPart(org.eclipse.gef.RootEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart)

Example 3 with ShapeNodeEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method buildLayoutGraphRecursively.

/**
 * Recursively builds a layout graph by analyzing the children of the given edit part.
 *
 * @param mapping
 *            the layout mapping
 * @param parentEditPart
 *            the parent edit part of the current elements
 * @param parentLayoutNode
 *            the corresponding KNode
 * @param currentEditPart
 *            the currently analyzed edit part
 */
protected void buildLayoutGraphRecursively(final LayoutMapping mapping, final IGraphicalEditPart parentEditPart, final ElkNode parentLayoutNode, final IGraphicalEditPart currentEditPart) {
    Maybe<ElkPadding> kinsets = new Maybe<ElkPadding>();
    // iterate through the children of the element
    for (Object obj : currentEditPart.getChildren()) {
        // check visibility of the child
        if (obj instanceof IGraphicalEditPart) {
            IFigure figure = ((IGraphicalEditPart) obj).getFigure();
            if (!figure.isVisible()) {
                continue;
            }
        }
        // process a port (border item)
        if (obj instanceof AbstractBorderItemEditPart) {
            AbstractBorderItemEditPart borderItem = (AbstractBorderItemEditPart) obj;
            if (editPartFilter.filter(borderItem)) {
                createPort(mapping, borderItem, parentEditPart, parentLayoutNode);
            }
        // process a compartment, which may contain other elements
        } else if (obj instanceof ResizableCompartmentEditPart && ((CompartmentEditPart) obj).getChildren().size() > 0) {
            CompartmentEditPart compartment = (CompartmentEditPart) obj;
            if (editPartFilter.filter(compartment)) {
                boolean compExp = true;
                IFigure compartmentFigure = compartment.getFigure();
                if (compartmentFigure instanceof ResizableCompartmentFigure) {
                    ResizableCompartmentFigure resizCompFigure = (ResizableCompartmentFigure) compartmentFigure;
                    // check whether the compartment is collapsed
                    compExp = resizCompFigure.isExpanded();
                }
                if (compExp) {
                    buildLayoutGraphRecursively(mapping, parentEditPart, parentLayoutNode, compartment);
                }
            }
        // process a node, which may be a parent of ports, compartments, or other nodes
        } else if (obj instanceof ShapeNodeEditPart) {
            ShapeNodeEditPart childNodeEditPart = (ShapeNodeEditPart) obj;
            if (editPartFilter.filter(childNodeEditPart)) {
                ElkNode node = createNode(mapping, childNodeEditPart, parentEditPart, parentLayoutNode, kinsets);
                // process the child as new current edit part
                buildLayoutGraphRecursively(mapping, childNodeEditPart, node, childNodeEditPart);
            }
        // process a label of the current node
        } else if (obj instanceof IGraphicalEditPart) {
            createNodeLabel(mapping, (IGraphicalEditPart) obj, parentEditPart, parentLayoutNode);
        }
    }
}
Also used : IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) Maybe(org.eclipse.elk.core.util.Maybe) ResizableCompartmentFigure(org.eclipse.gmf.runtime.diagram.ui.figures.ResizableCompartmentFigure) ElkNode(org.eclipse.elk.graph.ElkNode) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) CompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) EObject(org.eclipse.emf.ecore.EObject) ElkPadding(org.eclipse.elk.core.math.ElkPadding) IFigure(org.eclipse.draw2d.IFigure)

Example 4 with ShapeNodeEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method refreshDiagram.

/**
 * Refreshes all ports in the diagram. This is necessary in order correctly move ports, which
 * does not work due to GMF bugs. See Eclipse bug #291484.
 *
 * @param editor
 *            the diagram editor
 * @param rootPart
 *            the root edit part
 * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=291484
 */
private static void refreshDiagram(final DiagramEditor editor, final IGraphicalEditPart rootPart) {
    EditPart editPart = rootPart;
    if (editPart == null) {
        editPart = editor.getDiagramEditPart();
    }
    for (Object obj : editPart.getViewer().getEditPartRegistry().values()) {
        if (obj instanceof ShapeNodeEditPart) {
            IFigure figure = ((ShapeNodeEditPart) obj).getFigure();
            if (figure instanceof BorderedNodeFigure) {
                IFigure portContainer = ((BorderedNodeFigure) figure).getBorderItemContainer();
                portContainer.invalidate();
                portContainer.validate();
            }
        }
    }
}
Also used : ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) BorderedNodeFigure(org.eclipse.gmf.runtime.diagram.ui.figures.BorderedNodeFigure) ResizableCompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart) ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) RootEditPart(org.eclipse.gef.RootEditPart) CompartmentEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart) RenderedDiagramRootEditPart(org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart) EditPart(org.eclipse.gef.EditPart) IGraphicalEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart) AbstractBorderItemEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart) LabelEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) EObject(org.eclipse.emf.ecore.EObject) IFigure(org.eclipse.draw2d.IFigure)

Example 5 with ShapeNodeEditPart

use of org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart in project elk by eclipse.

the class GmfDiagramLayoutConnector method buildLayoutGraph.

/**
 * Creates the actual mapping given an edit part which functions as the root for the layout.
 *
 * @param layoutRootPart the layout root edit part
 * @param selection a selection of contained edit parts to process, or {@code null} if the whole
 *          content shall be processed
 * @param workbenchPart the workbench part, or {@code null}
 * @return a layout graph mapping
 */
protected LayoutMapping buildLayoutGraph(final IGraphicalEditPart layoutRootPart, final List<ShapeNodeEditPart> selection, final IWorkbenchPart workbenchPart) {
    LayoutMapping mapping = new LayoutMapping(workbenchPart);
    mapping.setProperty(CONNECTIONS, new LinkedList<ConnectionEditPart>());
    mapping.setParentElement(layoutRootPart);
    // find the diagram edit part
    mapping.setProperty(DIAGRAM_EDIT_PART, getDiagramEditPart(layoutRootPart));
    ElkNode topNode;
    if (layoutRootPart instanceof ShapeNodeEditPart) {
        // start with a specific node as root for layout
        topNode = createNode(mapping, (ShapeNodeEditPart) layoutRootPart, null, null, null);
    } else {
        // start with the whole diagram as root for layout
        topNode = ElkGraphUtil.createGraph();
        Rectangle rootBounds = layoutRootPart.getFigure().getBounds();
        if (layoutRootPart instanceof DiagramEditPart) {
            String labelText = ((DiagramEditPart) layoutRootPart).getDiagramView().getName();
            if (labelText.length() > 0) {
                ElkLabel label = ElkGraphUtil.createLabel(topNode);
                label.setText(labelText);
            }
        } else {
            topNode.setLocation(rootBounds.x, rootBounds.y);
        }
        topNode.setDimensions(rootBounds.width, rootBounds.height);
        mapping.getGraphMap().put(topNode, layoutRootPart);
    }
    mapping.setLayoutGraph(topNode);
    if (selection != null && !selection.isEmpty()) {
        // layout only the selected elements
        double minx = Integer.MAX_VALUE;
        double miny = Integer.MAX_VALUE;
        Maybe<ElkPadding> kinsets = new Maybe<>();
        for (ShapeNodeEditPart editPart : selection) {
            ElkNode node = createNode(mapping, editPart, layoutRootPart, topNode, kinsets);
            minx = Math.min(minx, node.getX());
            miny = Math.min(miny, node.getY());
            buildLayoutGraphRecursively(mapping, editPart, node, editPart);
        }
        mapping.setProperty(COORDINATE_OFFSET, new KVector(minx, miny));
    } else {
        // traverse all children of the layout root part
        buildLayoutGraphRecursively(mapping, layoutRootPart, topNode, layoutRootPart);
    }
    // transform all connections in the selected area
    processConnections(mapping);
    return mapping;
}
Also used : ShapeNodeEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart) Maybe(org.eclipse.elk.core.util.Maybe) ConnectionEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart) ElkNode(org.eclipse.elk.graph.ElkNode) Rectangle(org.eclipse.draw2d.geometry.Rectangle) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) LayoutMapping(org.eclipse.elk.core.service.LayoutMapping) ElkPadding(org.eclipse.elk.core.math.ElkPadding)

Aggregations

ShapeNodeEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart)5 AbstractBorderItemEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.AbstractBorderItemEditPart)4 IGraphicalEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart)4 EObject (org.eclipse.emf.ecore.EObject)3 CompartmentEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.CompartmentEditPart)3 ConnectionEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart)3 DiagramEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart)3 ResizableCompartmentEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ResizableCompartmentEditPart)3 IFigure (org.eclipse.draw2d.IFigure)2 ElkPadding (org.eclipse.elk.core.math.ElkPadding)2 LayoutMapping (org.eclipse.elk.core.service.LayoutMapping)2 Maybe (org.eclipse.elk.core.util.Maybe)2 ElkNode (org.eclipse.elk.graph.ElkNode)2 EditPart (org.eclipse.gef.EditPart)2 RootEditPart (org.eclipse.gef.RootEditPart)2 LabelEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.LabelEditPart)2 RenderedDiagramRootEditPart (org.eclipse.gmf.runtime.diagram.ui.render.editparts.RenderedDiagramRootEditPart)2 Collection (java.util.Collection)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 KVector (org.eclipse.elk.core.math.KVector)1