Search in sources :

Example 1 with OffscreenEditPartFactory

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();
}
Also used : Maybe(org.eclipse.elk.core.util.Maybe) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) Diagram(org.eclipse.gmf.runtime.notation.Diagram) Shell(org.eclipse.swt.widgets.Shell) DiagramEditPart(org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart) OffscreenEditPartFactory(org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory)

Aggregations

Maybe (org.eclipse.elk.core.util.Maybe)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 OffscreenEditPartFactory (org.eclipse.gmf.runtime.diagram.ui.OffscreenEditPartFactory)1 DiagramEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart)1 Diagram (org.eclipse.gmf.runtime.notation.Diagram)1 Shell (org.eclipse.swt.widgets.Shell)1