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);
}
});
}
}
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;
}
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);
}
}
}
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();
}
}
}
}
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;
}
Aggregations