Search in sources :

Example 1 with Maybe

use of org.eclipse.elk.core.util.Maybe in project elk by eclipse.

the class AlgorithmSelectionDialog method createSelectionTree.

/**
 * Create the dialog area that displays the selection tree and filter text.
 *
 * @param parent the parent composite
 * @return the control for the selection area
 */
private Control createSelectionTree(final Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    // create filter text
    final Text filterText = new Text(composite, SWT.BORDER);
    filterText.setText(Messages.getString("elk.ui.59"));
    filterText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    filterText.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    // create tree viewer
    final TreeViewer treeViewer = new TreeViewer(composite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
    final AlgorithmContentProvider contentProvider = new AlgorithmContentProvider();
    treeViewer.setContentProvider(contentProvider);
    treeViewer.setLabelProvider(new LabelProvider() {

        @Override
        public String getText(final Object element) {
            if (element instanceof LayoutAlgorithmData) {
                LayoutAlgorithmData algoData = (LayoutAlgorithmData) element;
                String bundleName = algoData.getBundleName();
                if (bundleName == null) {
                    return algoData.getName();
                } else {
                    return algoData.getName() + " (" + bundleName + ")";
                }
            } else if (element instanceof LayoutCategoryData) {
                LayoutCategoryData typeData = (LayoutCategoryData) element;
                if (typeData.getName() == null) {
                    return "Other";
                } else {
                    return typeData.getName();
                }
            }
            return super.getText(element);
        }
    });
    treeViewer.setComparator(new ViewerComparator() {

        public int category(final Object element) {
            if (element instanceof LayoutCategoryData) {
                LayoutCategoryData typeData = (LayoutCategoryData) element;
                // the "Other" layout type has empty identifier and is put to the bottom
                return typeData.getId().length() == 0 ? 1 : 0;
            }
            return super.category(element);
        }
    });
    treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    treeViewer.setInput(LayoutMetaDataService.getInstance());
    treeViewer.expandAll();
    treeViewer.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(final DoubleClickEvent event) {
            okPressed();
        }
    });
    // set up a filter on the tree viewer using the filter text
    final Maybe<Boolean> filterChanged = new Maybe<Boolean>(Boolean.FALSE);
    final Maybe<Boolean> filterLeft = new Maybe<Boolean>(Boolean.FALSE);
    filterText.addModifyListener(new ModifyListener() {

        public void modifyText(final ModifyEvent e) {
            if (!filterChanged.get()) {
                filterChanged.set(Boolean.TRUE);
                filterText.setForeground(null);
                int pos = filterText.getCaretPosition();
                String newText = filterText.getText(pos - 1, pos - 1);
                filterText.setText(newText);
                filterText.setSelection(pos);
            } else {
                contentProvider.updateFilter(filterText.getText());
                treeViewer.refresh();
                treeViewer.expandAll();
                ILayoutMetaData selected = contentProvider.getBestFilterMatch();
                if (selected != null) {
                    treeViewer.setSelection(new StructuredSelection(selected));
                }
            }
        }
    });
    filterText.addFocusListener(new FocusListener() {

        public void focusGained(final FocusEvent e) {
            if (filterLeft.get() && !filterChanged.get()) {
                filterChanged.set(Boolean.TRUE);
                filterText.setForeground(null);
                filterText.setText("");
            }
        }

        public void focusLost(final FocusEvent e) {
            filterLeft.set(Boolean.TRUE);
        }
    });
    treeViewer.addFilter(new ViewerFilter() {

        public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
            return contentProvider.applyFilter(element);
        }
    });
    // add a selection listener to the tree so that the selected element is displayed
    treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(final SelectionChangedEvent event) {
            IStructuredSelection selection = (IStructuredSelection) event.getSelection();
            Object element = selection.getFirstElement();
            if (element instanceof ILayoutMetaData) {
                updateValue((ILayoutMetaData) element);
            }
        }
    });
    composite.setLayout(new GridLayout());
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    gridData.minimumWidth = SELECTION_WIDTH;
    composite.setLayoutData(gridData);
    // register all selection listeners that have been stored in a list
    selectionProvider = treeViewer;
    for (ISelectionChangedListener listener : selectionListeners) {
        selectionProvider.addSelectionChangedListener(listener);
    }
    return composite;
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) TreeViewer(org.eclipse.jface.viewers.TreeViewer) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Viewer(org.eclipse.jface.viewers.Viewer) TreeViewer(org.eclipse.jface.viewers.TreeViewer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) GridLayout(org.eclipse.swt.layout.GridLayout) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) LayoutCategoryData(org.eclipse.elk.core.data.LayoutCategoryData) Maybe(org.eclipse.elk.core.util.Maybe) Composite(org.eclipse.swt.widgets.Composite) ViewerComparator(org.eclipse.jface.viewers.ViewerComparator) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Text(org.eclipse.swt.widgets.Text) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) LayoutAlgorithmData(org.eclipse.elk.core.data.LayoutAlgorithmData) GridData(org.eclipse.swt.layout.GridData) ILayoutMetaData(org.eclipse.elk.core.data.ILayoutMetaData) LabelProvider(org.eclipse.jface.viewers.LabelProvider) FocusListener(org.eclipse.swt.events.FocusListener)

Example 2 with Maybe

use of org.eclipse.elk.core.util.Maybe in project elk by eclipse.

the class MonitoredOperation method runUnmonitored.

/**
 * Run the operation from the current thread without a progress monitor.
 *
 * @param display the display that runs the UI thread
 * @param isUiThread if true, the current thread is the UI thread
 */
private void runUnmonitored(final Display display, final boolean isUiThread) {
    final Maybe<IStatus> status = new Maybe<IStatus>();
    if (isUiThread) {
        try {
            // execute UI code prior to the actual operation
            preUIexec();
            // execute the actual operation without progress monitor
            synchronized (executorService) {
                executorService.execute(new Runnable() {

                    public void run() {
                        status.set(execute(createMonitor()));
                        assert status.get() != null;
                        display.wake();
                    }
                });
            }
            while (status.get() == null && !isCanceled()) {
                boolean hasMoreToDispatch;
                do {
                    hasMoreToDispatch = display.readAndDispatch();
                } while (hasMoreToDispatch && status.get() == null && !isCanceled());
                if (status.get() == null && !isCanceled()) {
                    display.sleep();
                }
            }
            if (status.get() != null && status.get().getSeverity() == IStatus.OK && !isCanceled()) {
                // execute UI code after the actual operation
                postUIexec();
            }
        } catch (Throwable throwable) {
            status.set(new Status(IStatus.ERROR, ElkServicePlugin.PLUGIN_ID, "Error in monitored operation", throwable));
        }
    } else {
        // execute UI code prior to the actual operation
        display.syncExec(new Runnable() {

            public void run() {
                try {
                    preUIexec();
                } catch (Throwable throwable) {
                    status.set(new Status(IStatus.ERROR, ElkServicePlugin.PLUGIN_ID, "Error in monitored operation", throwable));
                }
            }
        });
        if (status.get() == null && !isCanceled()) {
            // execute the actual operation without progress monitor
            status.set(execute(createMonitor()));
            if (status.get().getSeverity() == IStatus.OK && !isCanceled()) {
                // execute UI code after the actual operation
                display.syncExec(new Runnable() {

                    public void run() {
                        try {
                            postUIexec();
                        } catch (Throwable throwable) {
                            status.set(new Status(IStatus.ERROR, ElkServicePlugin.PLUGIN_ID, "Error in monitored operation", throwable));
                        }
                    }
                });
            }
        }
    }
    handleStatus(status);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IStatus(org.eclipse.core.runtime.IStatus) Maybe(org.eclipse.elk.core.util.Maybe)

Example 3 with Maybe

use of org.eclipse.elk.core.util.Maybe 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 Maybe

use of org.eclipse.elk.core.util.Maybe 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)

Example 5 with Maybe

use of org.eclipse.elk.core.util.Maybe 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)5 ElkPadding (org.eclipse.elk.core.math.ElkPadding)2 ElkNode (org.eclipse.elk.graph.ElkNode)2 DiagramEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart)2 ShapeNodeEditPart (org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeNodeEditPart)2 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 IFigure (org.eclipse.draw2d.IFigure)1 Rectangle (org.eclipse.draw2d.geometry.Rectangle)1 ILayoutMetaData (org.eclipse.elk.core.data.ILayoutMetaData)1 LayoutAlgorithmData (org.eclipse.elk.core.data.LayoutAlgorithmData)1 LayoutCategoryData (org.eclipse.elk.core.data.LayoutCategoryData)1 KVector (org.eclipse.elk.core.math.KVector)1 LayoutMapping (org.eclipse.elk.core.service.LayoutMapping)1 ElkLabel (org.eclipse.elk.graph.ElkLabel)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)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