Search in sources :

Example 11 with CanonicalBusinessObjectReference

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());
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) CoreException(org.eclipse.core.runtime.CoreException) IStatus(org.eclipse.core.runtime.IStatus) Map(java.util.Map) StatusManager(org.eclipse.ui.statushandlers.StatusManager) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IEditorPart(org.eclipse.ui.IEditorPart) ImmutableSet(com.google.common.collect.ImmutableSet) PlatformUI(org.eclipse.ui.PlatformUI) Collection(java.util.Collection) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) Display(org.eclipse.swt.widgets.Display) ListDialog(org.eclipse.ui.dialogs.ListDialog) DiagramSerialization(org.osate.ge.internal.diagram.runtime.DiagramSerialization) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) Collectors(java.util.stream.Collectors) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) ProjectUtil(org.osate.ge.ProjectUtil) Objects(java.util.Objects) ReferenceService(org.osate.ge.internal.services.ReferenceService) List(java.util.List) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException) GraphicalEditor(org.osate.ge.GraphicalEditor) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) BusinessObjectProviderHelper(org.osate.ge.internal.util.BusinessObjectProviderHelper) AgeDiagramProvider(org.osate.ge.internal.AgeDiagramProvider) DiagramNode(org.osate.ge.internal.diagram.runtime.DiagramNode) DiagramElement(org.osate.ge.internal.diagram.runtime.DiagramElement) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) URI(org.eclipse.emf.common.util.URI) DiagramConfigurationBuilder(org.osate.ge.internal.diagram.runtime.DiagramConfigurationBuilder) DiagramModification(org.osate.ge.internal.diagram.runtime.DiagramModification) ExtensionRegistryService(org.osate.ge.internal.services.ExtensionRegistryService) HashMap(java.util.HashMap) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) IProject(org.eclipse.core.resources.IProject) IWorkspace(org.eclipse.core.resources.IWorkspace) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IFile(org.eclipse.core.resources.IFile) DefaultCreateDiagramModel(org.osate.ge.internal.ui.dialogs.DefaultCreateDiagramModel) DiagramService(org.osate.ge.internal.services.DiagramService) IOException(java.io.IOException) SavedDiagramIndex(org.osate.ge.internal.indexing.SavedDiagramIndex) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) EditorUtil(org.osate.ge.internal.ui.util.EditorUtil) SavedDiagramIndexInvalidator(org.osate.ge.internal.indexing.SavedDiagramIndexInvalidator) IResource(org.eclipse.core.resources.IResource) DiagramType(org.osate.ge.DiagramType) IEditorReference(org.eclipse.ui.IEditorReference) Log(org.osate.ge.internal.util.Log) CreateDiagramDialog(org.osate.ge.internal.ui.dialogs.CreateDiagramDialog) UnrecognizedDiagramType(org.osate.ge.internal.diagram.runtime.types.UnrecognizedDiagramType) Collections(java.util.Collections) FrameworkUtil(org.osgi.framework.FrameworkUtil) LabelProvider(org.eclipse.jface.viewers.LabelProvider) IFile(org.eclipse.core.resources.IFile) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException) IProject(org.eclipse.core.resources.IProject) HashSet(java.util.HashSet)

Example 12 with CanonicalBusinessObjectReference

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;
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) AgeDiagram(org.osate.ge.internal.diagram.runtime.AgeDiagram) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) AgeDiagramProvider(org.osate.ge.internal.AgeDiagramProvider)

Example 13 with CanonicalBusinessObjectReference

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ElementSelectionDialog(org.osate.ge.aadl2.ui.internal.dialogs.ElementSelectionDialog) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) EObject(org.eclipse.emf.ecore.EObject) DiagramConfigurationBuilder(org.osate.ge.internal.diagram.runtime.DiagramConfigurationBuilder) EObject(org.eclipse.emf.ecore.EObject)

Example 14 with CanonicalBusinessObjectReference

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;
}
Also used : Path(org.eclipse.core.runtime.Path) Set(java.util.Set) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) HashSet(java.util.HashSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Bundle(org.osgi.framework.Bundle) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) LiveScopeResourceSetInitializer(org.eclipse.xtext.ui.resource.LiveScopeResourceSetInitializer) IProject(org.eclipse.core.resources.IProject) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) RefactoringResourceSetProvider(org.eclipse.xtext.ui.refactoring.impl.RefactoringResourceSetProvider) EObject(org.eclipse.emf.ecore.EObject) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) IResource(org.eclipse.core.resources.IResource)

Example 15 with CanonicalBusinessObjectReference

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;
        }
    };
}
Also used : HashMap(java.util.HashMap) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RelativeBusinessObjectReference(org.osate.ge.RelativeBusinessObjectReference) Change(org.eclipse.ltk.core.refactoring.Change) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CanonicalBusinessObjectReference(org.osate.ge.CanonicalBusinessObjectReference) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

CanonicalBusinessObjectReference (org.osate.ge.CanonicalBusinessObjectReference)21 RelativeBusinessObjectReference (org.osate.ge.RelativeBusinessObjectReference)6 HashMap (java.util.HashMap)5 IProject (org.eclipse.core.resources.IProject)4 URI (org.eclipse.emf.common.util.URI)4 AgeDiagram (org.osate.ge.internal.diagram.runtime.AgeDiagram)4 DiagramConfigurationBuilder (org.osate.ge.internal.diagram.runtime.DiagramConfigurationBuilder)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 Objects (java.util.Objects)3 IResource (org.eclipse.core.resources.IResource)3 CoreException (org.eclipse.core.runtime.CoreException)3 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)3 EObject (org.eclipse.emf.ecore.EObject)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 BusinessObjectContext (org.osate.ge.BusinessObjectContext)3 AgeDiagramProvider (org.osate.ge.internal.AgeDiagramProvider)3 DiagramElement (org.osate.ge.internal.diagram.runtime.DiagramElement)3 InternalDiagramEditor (org.osate.ge.internal.ui.editor.InternalDiagramEditor)3 Collections (java.util.Collections)2