use of org.osate.ge.internal.diagram.runtime.DiagramNode in project osate2 by osate.
the class RestoreMissingDiagramElementsHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (!(activeEditor instanceof InternalDiagramEditor)) {
throw new RuntimeException("Unexpected editor: " + activeEditor);
}
// Get editor and various services
final InternalDiagramEditor diagramEditor = (InternalDiagramEditor) activeEditor;
final ProjectProvider projectProvider = (ProjectProvider) Objects.requireNonNull(diagramEditor.getAdapter(ProjectProvider.class), "Unable to retrieve project provider");
final DiagramUpdater diagramUpdater = Objects.requireNonNull(diagramEditor.getDiagramUpdater(), "Unable to retrieve diagram updater");
final ExtensionRegistryService extService = (ExtensionRegistryService) Objects.requireNonNull(diagramEditor.getAdapter(ExtensionRegistryService.class), "Unable to retrieve extension service");
final AgeDiagram diagram = diagramEditor.getDiagram();
final IEclipseContext serviceContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext());
final ReferenceService referenceService = Objects.requireNonNull(serviceContext.get(ReferenceService.class), "Unable to retrieve reference service");
final LayoutInfoProvider layoutInfoProvider = Objects.requireNonNull(Adapters.adapt(diagramEditor, LayoutInfoProvider.class), "Unable to retrieve layout info provider");
// Stores child business object contexts which are applicable for a given parent node
final Multimap<DiagramNode, BusinessObjectContext> diagramNodeToAvailableBusinessObjectContextsMap = ArrayListMultimap.create();
// Build a list of ghosts that will be presented to the user to modify
final List<DiagramUpdater.GhostedElement> ghostsToModify = new ArrayList<>();
final BusinessObjectProviderHelper bopHelper = new BusinessObjectProviderHelper(extService);
// Walk all nodes and look for ghosts
diagram.getAllDiagramNodes().forEachOrdered(parent -> {
final Collection<DiagramUpdater.GhostedElement> ghosts = diagramUpdater.getGhosts(parent);
if (!ghosts.isEmpty()) {
final BusinessObjectContext parentToUse;
if (diagram.getConfiguration().getContextBoReference() == null && parent.getParent() == null) {
parentToUse = getRootContextForProject(projectProvider);
} else {
parentToUse = parent;
}
// Create a mapping between relative reference and available child business objects
final Map<RelativeBusinessObjectReference, Object> relRefToBusinessObjectMap = bopHelper.getChildBusinessObjects(parentToUse).stream().collect(HashMap::new, (map, bo) -> {
final RelativeBusinessObjectReference relRef = referenceService.getRelativeReference(bo);
if (relRef != null) {
map.put(relRef, bo);
}
}, Map::putAll);
// Remove any entries based on existing node relative references.
parent.getChildren().forEach(de -> relRefToBusinessObjectMap.remove(de.getRelativeReference()));
// Don't show ghosts if there aren't any unused business objects
if (!relRefToBusinessObjectMap.isEmpty()) {
// Store the ghosts and the available business objects
relRefToBusinessObjectMap.values().stream().map(bo -> new BusinessObjectContext() {
@Override
public Collection<? extends BusinessObjectContext> getChildren() {
return Collections.emptyList();
}
@Override
public BusinessObjectContext getParent() {
return parent;
}
@Override
public Object getBusinessObject() {
return bo;
}
}).forEachOrdered(// see https://github.com/osate/osate2/issues/976
boc -> diagramNodeToAvailableBusinessObjectContextsMap.put(parent, (BusinessObjectContext) boc));
ghostsToModify.addAll(ghosts);
}
}
});
// Show the dialog
final RestoreMissingDiagramElementsDialog.Result<DiagramUpdater.GhostedElement, BusinessObjectContext> result = RestoreMissingDiagramElementsDialog.show(null, new RestoreMissingDiagramElementsDialog.Model<DiagramUpdater.GhostedElement, BusinessObjectContext>() {
@Override
public Collection<DiagramUpdater.GhostedElement> getElements() {
return ghostsToModify;
}
@Override
public String getParentLabel(final DiagramUpdater.GhostedElement element) {
return UiUtil.getPathLabel(element.getParent());
}
@Override
public String getMissingReferenceLabel(final DiagramUpdater.GhostedElement element) {
return referenceService.getLabel(element.getRelativeReference());
}
@Override
public Collection<BusinessObjectContext> getAvailableBusinessObjects(final DiagramUpdater.GhostedElement element) {
return diagramNodeToAvailableBusinessObjectContextsMap.get(element.getParent());
}
@Override
public String getBusinessObjectLabel(final BusinessObjectContext boc) {
return UiUtil.getDescription(boc, extService);
}
});
if (result != null) {
diagramEditor.getActionExecutor().execute("Restore Missing Diagram Elements", ExecutionMode.NORMAL, () -> {
// Update the ghosts and the diagram
diagram.modify("Restore Missing Diagram Elements", m -> {
result.getObjectToNewBoMap().forEach((ghost, newBoc) -> {
final Object newBo = newBoc.getBusinessObject();
ghost.updateBusinessObject(m, newBo, referenceService.getRelativeReference(newBo));
});
// Update the diagram
diagramUpdater.updateDiagram(diagram);
});
diagram.modify("Layout", m -> DiagramElementLayoutUtil.layoutIncrementally(diagram, m, layoutInfoProvider));
return null;
});
}
return null;
}
use of org.osate.ge.internal.diagram.runtime.DiagramNode in project osate2 by osate.
the class ShowElementHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (!(activeEditor instanceof InternalDiagramEditor)) {
throw new RuntimeException("Unexpected editor: " + activeEditor);
}
// Get diagram and selected elements
final InternalDiagramEditor diagramEditor = (InternalDiagramEditor) activeEditor;
final List<BusinessObjectContext> bocsToAdd = AgeHandlerUtil.getSelectedBusinessObjectContexts().stream().filter(boc -> !(boc instanceof DiagramNode)).collect(Collectors.toList());
final AgeDiagram diagram = diagramEditor.getDiagram();
if (diagram == null) {
throw new RuntimeException("Unable to get diagram");
}
// Get services
final DiagramUpdater diagramUpdater = diagramEditor.getDiagramUpdater();
final LayoutInfoProvider layoutInfoProvider = Objects.requireNonNull(Adapters.adapt(diagramEditor, LayoutInfoProvider.class), "Unable to retrieve layout info provider");
final ReferenceBuilderService refBuilder = Objects.requireNonNull(Adapters.adapt(diagramEditor, ReferenceBuilderService.class), "Unable to retrieve reference builder");
final BusinessObjectNode boTree = DiagramToBusinessObjectTreeConverter.createBusinessObjectNode(diagram);
// Add the selected elements to the business object tree
for (final BusinessObjectContext bocToAdd : bocsToAdd) {
// Get list of nodes that represents the path from the root to the business object context we need to add
final LinkedList<BusinessObjectContext> bocPath = new LinkedList<>();
for (BusinessObjectContext tmp = bocToAdd; !(tmp instanceof AgeDiagram); tmp = tmp.getParent()) {
bocPath.addFirst(tmp);
}
// For each element in that path. Create BusinessObjectNode if it doesn't exist
BusinessObjectNode parentNode = boTree;
for (final BusinessObjectContext tmpBoc : bocPath) {
// Get relative reference of the business object
final RelativeBusinessObjectReference ref;
if (tmpBoc instanceof DiagramElement) {
ref = ((DiagramElement) tmpBoc).getRelativeReference();
} else {
ref = refBuilder.getRelativeReference(tmpBoc.getBusinessObject());
}
// Look for a matching node
BusinessObjectNode childNode = parentNode.getChild(ref);
// If it doesn't exist, create it
if (childNode == null) {
childNode = new BusinessObjectNode(parentNode, UUID.randomUUID(), ref, tmpBoc.getBusinessObject(), Completeness.UNKNOWN, false);
}
// Continue to the next element in the path using the child node as the new parent
parentNode = childNode;
}
}
// Update the diagram
diagramEditor.getActionExecutor().execute("Show", ExecutionMode.NORMAL, () -> {
// Update the diagram
diagramUpdater.updateDiagram(diagram, boTree);
// Update layout
diagram.modify("Layout Incrementally", m -> DiagramElementLayoutUtil.layoutIncrementally(diagram, m, layoutInfoProvider));
return null;
});
return null;
}
use of org.osate.ge.internal.diagram.runtime.DiagramNode in project osate2 by osate.
the class CopyDiagramElementsHandler method setEnabled.
@Override
public void setEnabled(final Object evaluationContext) {
boolean enabled = false;
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
// Require that at least one element be selected
if (!selectedDiagramElements.isEmpty()) {
// Require that they all have the same parent.
final DiagramNode searchParent = selectedDiagramElements.get(0).getParent();
enabled = selectedDiagramElements.stream().allMatch(de -> de.getParent() == searchParent);
}
setBaseEnabled(enabled);
}
use of org.osate.ge.internal.diagram.runtime.DiagramNode in project osate2 by osate.
the class SelectContainerHandler method execute.
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
if (!(activeEditor instanceof InternalDiagramEditor)) {
throw new RuntimeException("Unexpected editor: " + activeEditor);
}
final InternalDiagramEditor editor = (InternalDiagramEditor) activeEditor;
// Get diagram and selected elements
final List<DiagramElement> selectedDiagramElements = AgeHandlerUtil.getSelectedDiagramElements();
if (selectedDiagramElements.size() == 0) {
throw new RuntimeException("No element selected");
}
final DiagramElement selectedElement = selectedDiagramElements.get(0);
final DiagramNode parent = Objects.requireNonNull(selectedElement.getParent(), "parent is null");
editor.selectDiagramNodes(Collections.singletonList(parent));
return null;
}
Aggregations