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