use of org.osate.ge.internal.services.ReferenceService in project osate2 by osate.
the class GefDiagramExportService method loadDiagram.
private GefAgeDiagram loadDiagram(final IFile diagramFile) {
final URI uri = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
final IProject project = ProjectUtil.getProjectOrNull(uri);
final org.osate.ge.diagram.Diagram mmDiagram = DiagramSerialization.readMetaModelDiagram(uri);
final IEclipseContext eclipseContext = EclipseContextFactory.getServiceContext(FrameworkUtil.getBundle(GefDiagramExportService.class).getBundleContext());
final ExtensionRegistryService extensionRegistry = Objects.requireNonNull(eclipseContext.get(ExtensionRegistryService.class), "Unable to retrieve extension registry");
final ReferenceService referenceService = Objects.requireNonNull(eclipseContext.get(ReferenceService.class), "unable to retrieve reference service");
final ActionService actionService = Objects.requireNonNull(eclipseContext.get(ActionService.class), "unable to retrieve action service");
final AgeDiagram diagram = DiagramSerialization.createAgeDiagram(project, mmDiagram, extensionRegistry);
// Update the diagram
final QueryService queryService = new DefaultQueryService(referenceService);
final ProjectProvider projectProvider = diagramFile::getProject;
final ProjectReferenceService projectReferenceService = new ProjectReferenceServiceProxy(referenceService, projectProvider);
final BusinessObjectNodeFactory nodeFactory = new BusinessObjectNodeFactory(projectReferenceService);
final DefaultBusinessObjectTreeUpdater boTreeUpdater = new DefaultBusinessObjectTreeUpdater(projectProvider, extensionRegistry, projectReferenceService, queryService, nodeFactory);
final DefaultDiagramElementGraphicalConfigurationProvider deInfoProvider = new DefaultDiagramElementGraphicalConfigurationProvider(queryService, () -> diagram, extensionRegistry);
final DiagramUpdater diagramUpdater = new DiagramUpdater(boTreeUpdater, deInfoProvider, actionService, projectReferenceService, projectReferenceService);
diagramUpdater.updateDiagram(diagram);
// Create the GEF Diagram
final GefAgeDiagram gefDiagram = new GefAgeDiagram(diagram, new DefaultColoringService(new org.osate.ge.internal.services.impl.DefaultColoringService.StyleRefresher() {
@Override
public void refreshDiagramColoring() {
// No-op. Handling coloring service refresh requests is not required.
}
@Override
public void refreshColoring(final Collection<DiagramElement> diagramElements) {
// No-op. Handling coloring service refresh requests is not required.
}
}));
// Add to scene. This is required for text rendering
new Scene(gefDiagram.getSceneNode());
// Update the diagram to reflect the scene graph and perform incremental layout
gefDiagram.updateDiagramFromSceneGraph(false);
diagram.modify("Incremental Layout", m -> DiagramElementLayoutUtil.layoutIncrementally(diagram, m, gefDiagram));
return gefDiagram;
}
use of org.osate.ge.internal.services.ReferenceService 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.services.ReferenceService in project osate2 by osate.
the class SelectionUtil method findDiagramContextForSelectedObject.
private static Element findDiagramContextForSelectedObject(final Object selectedObject) {
if (selectedObject instanceof IFile) {
final String ext = ((IFile) selectedObject).getFileExtension();
if (FileNameConstants.SOURCE_FILE_EXT.equalsIgnoreCase(ext) || FileNameConstants.INSTANCE_FILE_EXT.equalsIgnoreCase(ext)) {
URI uri = OsateResourceUtil.toResourceURI((IFile) selectedObject);
final EList<EObject> contents = new ResourceSetImpl().getResource(uri, true).getContents();
if (contents != null && !contents.isEmpty()) {
final EObject root = contents.get(0);
if (root instanceof AadlPackage) {
return (AadlPackage) root;
} else if (root instanceof ComponentInstance) {
return (ComponentInstance) root;
}
}
}
} else if (selectedObject instanceof DiagramGroup) {
final DiagramGroup dg = (DiagramGroup) selectedObject;
if (dg.isContextReferenceValid()) {
final ReferenceService referenceService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(ReferenceService.class);
if (referenceService == null) {
return null;
}
final Object bo = referenceService.getProjectReferenceService(dg.getProject()).resolve(dg.getContextReference());
if (bo instanceof AadlPackage) {
return (AadlPackage) bo;
} else if (bo instanceof Classifier) {
return (Classifier) bo;
} else if (bo instanceof SystemInstance) {
return (SystemInstance) bo;
}
}
} else if (selectedObject instanceof Element) {
final Classifier classifier = findClassifier((Element) selectedObject);
if (classifier != null) {
return classifier;
}
final AadlPackage pkg = findPackage((Element) selectedObject);
if (pkg != null) {
return pkg;
}
} else if (selectedObject instanceof EObjectURIWrapper) {
final EObjectURIWrapper wrapper = (EObjectURIWrapper) selectedObject;
final EObject eObject = new ResourceSetImpl().getEObject(wrapper.getUri(), true);
final Classifier classifier = findClassifier(eObject);
if (classifier != null) {
return classifier;
}
final AadlPackage pkg = findPackage(eObject);
if (pkg != null) {
return pkg;
}
}
return null;
}
Aggregations