Search in sources :

Example 1 with ResourceChange

use of org.eclipse.emf.ecore.change.ResourceChange in project xtext-core by eclipse.

the class ChangeTreeProvider method createChangeTree.

public ResourceSetRecording createChangeTree(ResourceSet resourceSet, Collection<IResourceSnapshot> snapshots, ChangeDescription desc) {
    ResourceSetRecording rs = new ResourceSetRecording(resourceSet);
    Map<Resource, ResourceRecording> result = Maps.newLinkedHashMap();
    Map<EObject, EObjectChange> objectChanges = Maps.newLinkedHashMap();
    for (IResourceSnapshot snap : snapshots) {
        Resource resource = snap.getResource();
        ResourceRecording recording = new ResourceRecording(rs, resource);
        recording.snapshot = snap;
        result.put(resource, recording);
    }
    for (ResourceChange rc : desc.getResourceChanges()) {
        Resource resource = rc.getResource();
        ResourceRecording recording = result.get(resource);
        if (recording == null) {
            recording = new ResourceRecording(rs, resource);
            result.put(resource, recording);
        }
        recording.resourceChange = rc;
    }
    for (Entry<EObject, EList<FeatureChange>> e : desc.getObjectChanges().entrySet()) {
        EList<FeatureChange> featureChange = e.getValue();
        EObject object = e.getKey();
        objectChanges.put(object, new EObjectChange(object, featureChange));
    }
    for (EObjectChange change : Lists.newArrayList(objectChanges.values())) {
        EObjectChange current = change;
        while (true) {
            EObject container = current.eObject.eContainer();
            if (container != null) {
                EObjectChange next = objectChanges.get(container);
                if (next == null) {
                    next = new EObjectChange(container);
                    next.children.add(current);
                    objectChanges.put(container, next);
                } else {
                    if (!next.children.add(current)) {
                        break;
                    }
                }
                current = next;
            } else {
                Resource resource = current.eObject.eResource();
                ResourceRecording resourceChange = result.get(resource);
                if (resourceChange == null) {
                    resourceChange = new ResourceRecording(rs, resource);
                    result.put(resource, resourceChange);
                // TODO: check that only recorded resources contain
                // modification
                }
                resourceChange.roots.add(current);
                break;
            }
        }
    }
    rs.resources = ImmutableList.copyOf(result.values());
    rs.objects = ImmutableMap.copyOf(objectChanges);
    return rs;
}
Also used : Resource(org.eclipse.emf.ecore.resource.Resource) FeatureChange(org.eclipse.emf.ecore.change.FeatureChange) EList(org.eclipse.emf.common.util.EList) IResourceSnapshot(org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot) EObject(org.eclipse.emf.ecore.EObject) ResourceChange(org.eclipse.emf.ecore.change.ResourceChange)

Aggregations

EList (org.eclipse.emf.common.util.EList)1 EObject (org.eclipse.emf.ecore.EObject)1 FeatureChange (org.eclipse.emf.ecore.change.FeatureChange)1 ResourceChange (org.eclipse.emf.ecore.change.ResourceChange)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 IResourceSnapshot (org.eclipse.xtext.ide.serializer.hooks.IResourceSnapshot)1