use of org.osate.ge.CanonicalBusinessObjectReference in project osate2 by osate.
the class DefaultDiagramService method findDiagramsByContextBusinessObject.
@Override
public List<InternalDiagramReference> findDiagramsByContextBusinessObject(final Object bo) {
final CanonicalBusinessObjectReference boReference = referenceService.getCanonicalReference(bo);
if (boReference == null) {
throw new GraphicalEditorException("Unable to get canonical reference for business object : " + bo);
}
final IProject project = ProjectUtil.getProjectForBoOrThrow(bo);
// Build a set containing the project containing the business object and all projects which reference that project.
final HashSet<IProject> relevantProjects = new HashSet<>();
relevantProjects.add(project);
for (final IProject referencingProject : project.getReferencingProjects()) {
if (referencingProject.isAccessible()) {
relevantProjects.add(referencingProject);
}
}
final Map<IFile, InternalDiagramEditor> fileToEditorMap = getOpenEditorsMap(relevantProjects);
// Add saved diagram files if they are not open
return savedDiagramIndex.getDiagramsByContext(relevantProjects.stream(), boReference).stream().map(e -> new InternalDiagramReference(e, fileToEditorMap.get(e.getDiagramFile()))).collect(Collectors.toList());
}
use of org.osate.ge.CanonicalBusinessObjectReference in project osate2 by osate.
the class DefaultDiagramService method getReferences.
@Override
public ReferenceCollection getReferences(final Set<IProject> relevantProjects, final Set<CanonicalBusinessObjectReference> originalCanonicalReferences) {
final InternalReferencesToUpdate references = new InternalReferencesToUpdate();
Display.getDefault().syncExec(() -> {
// Create updateable reference for open diagrams
for (final InternalDiagramEditor editor : getOpenEditorsMap(relevantProjects).values()) {
final AgeDiagramProvider diagramProvider = editor.getAdapter(AgeDiagramProvider.class);
if (diagramProvider == null) {
continue;
}
final AgeDiagram diagram = diagramProvider.getAgeDiagram();
if (diagram == null) {
continue;
}
// Update the diagram immediately. This is intended to ensure the diagram doesn't have any proxies
editor.updateNowIfModelHasChanged();
final CanonicalBusinessObjectReference diagramContextRef = diagram.getConfiguration().getContextBoReference();
if (diagramContextRef != null && originalCanonicalReferences.contains(diagramContextRef)) {
references.addReference(editor, diagramContextRef, new OpenDiagramContextReference(diagram));
}
// Get references from the diagram elements
getRuntimeReferencesFromChildren(editor, diagram, originalCanonicalReferences, references);
}
// Create updateable references for saved diagrams
savedDiagramIndex.getDiagramsByContexts(relevantProjects.stream(), originalCanonicalReferences).forEach(e -> references.addReference(e.getDiagramFile(), e.getContext(), new SavedDiagramContextReference()));
savedDiagramIndex.getDiagramElementUrisByReferences(relevantProjects.stream(), originalCanonicalReferences).forEach(e -> references.addReference(e.diagramFile, e.reference, new SavedDiagramElementReference(e.diagramElementUri)));
});
return references;
}
use of org.osate.ge.CanonicalBusinessObjectReference in project osate2 by osate.
the class DiagramContextChecker method promptToRelink.
/**
* Returns whether or not the diagram context was adjusted.
* @param diagram
* @param missingContextRef
* @return
*/
private boolean promptToRelink(final AgeDiagram diagram) {
final CanonicalBusinessObjectReference missingContextRef = diagram.getConfiguration().getContextBoReference();
final List<String> refSegs = missingContextRef.getSegments();
if (refSegs.size() < 2) {
return false;
}
final boolean isPackageRef = DeclarativeReferenceType.PACKAGE.getId().equals(refSegs.get(0));
final boolean isClassifierRef = DeclarativeReferenceType.CLASSIFIER.getId().equals(refSegs.get(0));
final boolean isSystemInstance = AadlReferenceUtil.isSystemInstanceReference(missingContextRef);
if (!isPackageRef && !isClassifierRef && !isSystemInstance) {
return false;
}
// Determine the options to present to the user
final Collection<?> options;
String searchPrefix = "";
if (isPackageRef || isClassifierRef) {
// Find all packages
final Collection<IEObjectDescription> packageDescriptions = AadlModelAccessUtil.getAllEObjectsByType(project, Aadl2Package.eINSTANCE.getAadlPackage());
if (isPackageRef) {
options = packageDescriptions;
} else {
// isClassifierRef
options = AadlModelAccessUtil.getAllEObjectsByType(project, Aadl2Package.eINSTANCE.getClassifier());
// Check if the package portion of the qualified name is a valid package.
// If so, use it as the initial filter
final String referencedClassifierQualifiedName = refSegs.get(1);
final String[] qualifiedNameParts = referencedClassifierQualifiedName.split("::");
if (qualifiedNameParts.length == 2) {
final String pkgName = qualifiedNameParts[0];
for (final IEObjectDescription desc : packageDescriptions) {
if (desc.getName().toString("::").equalsIgnoreCase(pkgName)) {
searchPrefix = pkgName.toLowerCase() + "::";
}
}
}
}
} else if (isSystemInstance) {
options = findInstanceModelFiles(project, new ArrayList<IPath>());
} else {
// Unexpected case: there is already a short circuit for the case where the reference isn't a package or classifier reference
throw new RuntimeException("Unexpected case");
}
// Don't prompt if there aren't any options.
if (options.size() == 0) {
return false;
}
final ElementSelectionDialog dlg = new ElementSelectionDialog(null, "Missing Diagram Context", "Unable to find diagram context \"" + refService.getLabel(missingContextRef) + "\".\nIf the model element has been renamed, select the new name for the model element.", options);
dlg.setFilter(searchPrefix);
if (dlg.open() != Window.OK) {
return false;
}
final CanonicalBusinessObjectReference newContextCanonicalRef;
final RelativeBusinessObjectReference newContextRelativeRef;
final Object newContext;
if (isSystemInstance) {
final IPath systemInstancePath = (IPath) dlg.getFirstSelectedElement();
newContextCanonicalRef = AadlReferenceUtil.getCanonicalBusinessObjectReferenceForSystemInstance(systemInstanceLoader, systemInstancePath);
newContextRelativeRef = AadlReferenceUtil.getRelativeBusinessObjectReferenceForSystemInstance(systemInstanceLoader, systemInstancePath);
// Create a dummy system instance. It will be replaced as part of the diagram updating process.
newContext = InstanceFactory.eINSTANCE.createSystemInstance();
} else {
final EObject newContextProxy = (EObject) dlg.getFirstSelectedElement();
// Find the live object
final ResourceSet liveResourceSet = AadlModelAccessUtil.getLiveResourceSet(project);
newContext = EcoreUtil.resolve(newContextProxy, liveResourceSet);
if (((EObject) newContext).eIsProxy()) {
throw new RuntimeException("Unable to retrieve non-proxy object for selection");
}
// Find canonical and relative reference
newContextCanonicalRef = refService.getCanonicalReference(newContext);
if (newContextCanonicalRef == null) {
throw new RuntimeException("Unable to retrieve reference for new diagram context: " + newContext);
}
newContextRelativeRef = refService.getRelativeReference(newContext);
if (newContextRelativeRef == null) {
throw new RuntimeException("Unable to retrieve relative reference for new diagram context: " + newContext);
}
}
// Update the diagram
diagram.modify("Update Diagram Context", m -> {
// Update the diagram's context
m.setDiagramConfiguration(new DiagramConfigurationBuilder(diagram.getConfiguration()).contextBoReference(newContextCanonicalRef).build());
// Update the root element
if (diagram.getChildren().size() == 1) {
m.updateBusinessObject(diagram.getChildren().stream().findAny().get(), newContext, newContextRelativeRef);
}
});
return true;
}
use of org.osate.ge.CanonicalBusinessObjectReference in project osate2 by osate.
the class AgeRenameParticipant method initialize.
@Override
protected boolean initialize(final Object element) {
originalCanRefToNewInfoMap.clear();
if (!(element instanceof IRenameElementContext)) {
return false;
}
ctx = (IRenameElementContext) element;
final URI targetElementUri = ctx.getTargetElementURI();
if (targetElementUri == null) {
return false;
}
final IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(targetElementUri.toPlatformString(true)));
if (resource == null) {
return false;
}
final XtextResourceSet tmpRs = new XtextResourceSet();
Aadl2Activator.getInstance().getInjector(Aadl2Activator.ORG_OSATE_XTEXT_AADL2_AADL2).getInstance(LiveScopeResourceSetInitializer.class).initialize(tmpRs);
targetObject = tmpRs.getEObject(targetElementUri, true);
if (targetObject == null || !(targetObject.eResource() instanceof XtextResource)) {
return false;
}
final XtextResource xtextResource = (XtextResource) targetObject.eResource();
// Get the provider for the refactoring resource set
final RefactoringResourceSetProvider refactoringResourceSetProvider = xtextResource.getResourceServiceProvider().get(RefactoringResourceSetProvider.class);
project = ProjectUtil.getProjectOrNull(targetElementUri);
if (project == null) {
return false;
}
// Get the refactoring resource set
refactoringResourceSet = refactoringResourceSetProvider.get(project);
if (refactoringResourceSet == null) {
return false;
}
// Get global services
final Bundle bundle = FrameworkUtil.getBundle(getClass());
final IEclipseContext context = EclipseContextFactory.getServiceContext(bundle.getBundleContext());
referenceService = Objects.requireNonNull(context.get(ReferenceService.class), "Unable to get reference service");
diagramService = Objects.requireNonNull(context.get(DiagramService.class), "Unable to get diagram service");
// Get projects with are affected by the refactoring.
final Set<IProject> relevantProjects = ProjectUtil.getAffectedProjects(project, new HashSet<>());
// Build a mapping between an EObject URI and the URIs of EObjects that it affects.
final Map<URI, Set<URI>> externalReferencesMap = buildExternalReferenceMap(relevantProjects);
// Find all dependent objects
final Set<EObject> dependentObjects = getDependentObjects(targetObject, targetObject.eResource().getResourceSet(), externalReferencesMap);
dependentObjects.add(targetObject);
for (final EObject dirtyObject : dependentObjects) {
final URI uri = getNameIndependentUri(dirtyObject);
if (uri != null) {
final CanonicalBusinessObjectReference canonicalReference = referenceService.getCanonicalReference(dirtyObject);
final RelativeBusinessObjectReference relativeReference = referenceService.getRelativeReference(dirtyObject);
if (canonicalReference != null && relativeReference != null) {
originalCanRefToNewInfoMap.put(canonicalReference, new UriAndRelativeReference(uri, relativeReference));
}
}
}
// Initialize is called many times so it would be best to do it before the change is made but not in the initialization phase
originalReferences = diagramService.getReferences(relevantProjects, originalCanRefToNewInfoMap.keySet());
return true;
}
use of org.osate.ge.CanonicalBusinessObjectReference in project osate2 by osate.
the class AgeRenameParticipant method createChange.
@Override
public Change createChange(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
return new Change() {
@Override
public String getName() {
return "OSATE Graphical Editor Diagram Change";
}
@Override
public void initializeValidationData(final IProgressMonitor pm) {
}
@Override
public RefactoringStatus isValid(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
return new RefactoringStatus();
}
@Override
public Change perform(final IProgressMonitor pm) throws CoreException {
// Build mappings between the canonical reference which identifies the original reference and the new canonical and relative reference for
// change and undo changes.
final Map<CanonicalBusinessObjectReference, CanonicalBusinessObjectReference> originalCanRefToNewCanRefMap = new HashMap<>();
final Map<CanonicalBusinessObjectReference, RelativeBusinessObjectReference> originalCanRefToNewRelRefMap = new HashMap<>();
final Map<CanonicalBusinessObjectReference, CanonicalBusinessObjectReference> undoOriginalCanRefToNewCanRefMap = new HashMap<>();
final Map<CanonicalBusinessObjectReference, RelativeBusinessObjectReference> undoOriginalCanRefToNewRelRefMap = new HashMap<>();
for (final Entry<CanonicalBusinessObjectReference, UriAndRelativeReference> entry : originalCanRefToNewInfoMap.entrySet()) {
final EObject newObject = refactoringResourceSet.getEObject(entry.getValue().uri, true);
if (newObject != null) {
final CanonicalBusinessObjectReference originalCanRef = entry.getKey();
final CanonicalBusinessObjectReference newCanRef = referenceService.getCanonicalReference(newObject);
final RelativeBusinessObjectReference newRelRef = referenceService.getRelativeReference(newObject);
final RelativeBusinessObjectReference originalRelRef = entry.getValue().relRef;
if (newCanRef != null && newRelRef != null) {
originalCanRefToNewCanRefMap.put(originalCanRef, newCanRef);
originalCanRefToNewRelRefMap.put(originalCanRef, newRelRef);
undoOriginalCanRefToNewCanRefMap.put(originalCanRef, originalCanRef);
undoOriginalCanRefToNewRelRefMap.put(originalCanRef, originalRelRef);
}
}
}
// Update the references
final SimpleUpdatedReferenceValueProvider mapping = new SimpleUpdatedReferenceValueProvider(originalCanRefToNewCanRefMap, originalCanRefToNewRelRefMap);
final SimpleUpdatedReferenceValueProvider undoMapping = new SimpleUpdatedReferenceValueProvider(undoOriginalCanRefToNewCanRefMap, undoOriginalCanRefToNewRelRefMap);
final UpdateReferencesChange referenceUpdateChange = new UpdateReferencesChange(originalReferences, mapping, undoMapping);
return referenceUpdateChange.perform(pm);
}
@Override
public Object getModifiedElement() {
return null;
}
};
}
Aggregations