use of org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory in project elk by eclipse.
the class LayoutDiagramFileHandler method layoutDiagram.
/**
* Perform layout on the given diagram file.
*
* @param file a diagram file
* @param monitor a progress monitor
* @throws IOException if loading or saving the file fails
*/
private static void layoutDiagram(final IFile file, final IElkProgressMonitor monitor) throws IOException {
monitor.begin("Layout diagram file " + file.toString(), 2);
// load the notation diagram element
URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet resourceSet = new ResourceSetImpl();
final Resource resource = resourceSet.createResource(uri);
resource.load(Collections.emptyMap());
if (resource.getContents().isEmpty() || !(resource.getContents().get(0) instanceof Diagram)) {
throw new IllegalArgumentException("The selected file does not contain a diagram.");
}
// create a diagram edit part
TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain(resourceSet);
final Maybe<DiagramEditPart> editPart = new Maybe<DiagramEditPart>();
final Maybe<RuntimeException> wrappedException = new Maybe<RuntimeException>();
Display.getDefault().syncExec(new Runnable() {
public void run() {
try {
Diagram diagram = (Diagram) resource.getContents().get(0);
OffscreenEditPartFactory offscreenFactory = OffscreenEditPartFactory.getInstance();
editPart.set(offscreenFactory.createDiagramEditPart(diagram, new Shell()));
} catch (RuntimeException re) {
wrappedException.set(re);
}
}
});
if (wrappedException.get() != null) {
throw wrappedException.get();
}
monitor.worked(1);
// perform layout on the diagram
DiagramLayoutEngine.invokeLayout(null, editPart.get(), monitor.subTask(1), null);
// save the modified diagram
resource.save(Collections.emptyMap());
monitor.done();
}
Aggregations