Programmatically rearrange diagram

631 views Asked by At

I am trying to rearrange (auto-layout) a UML2 diagram programmatically.
However, I am unable to do so based on a Diagram instance.

My code so far looks like:

Diagram diagram = /*...*/;
EditPart editPart = new DiagramEditPart(diagram);

List<EditPart> editParts = CollectionUtil.createNewSequence();
editParts.add(editPart);

ArrangeRequest request = new ArrangeRequest("");
request.setPartsToArrange(editParts);
editPart.getCommand(request).execute();

At the moment it has absolutely no effect. I guess that I shouldn't manually create the DiagramEditPart and rather get it from somewhere else - but I don't know where to start looking.

1

There are 1 answers

0
Matthias On BEST ANSWER

Using OffscreenEditPartFactory.createDiagramEditPart to create the EditPart seems to work:

EditPart editPart = OffscreenEditPartFactory.getInstance().createDiagramEditPart(diagram, getShell());

List<EditPart> editParts = CollectionUtil.createNewSequence();
editParts.add(editPart);

ArrangeRequest request = new ArrangeRequest(ActionIds.ACTION_ARRANGE_ALL, LayoutType.DEFAULT);
request.setPartsToArrange(editParts);
Command cmd = editPart.getCommand(request);
if (cmd != null)
    cmd.execute();