Skip to content
Snippets Groups Projects
Commit b3c69067 authored by Simon Barner's avatar Simon Barner
Browse files

- Use EContentAdapter instead of manual management of non-recursive AdapterImpl

refs 1841
parent 0888161d
No related branches found
No related tags found
No related merge requests found
......@@ -19,14 +19,13 @@ package org.fortiss.tooling.base.ui.annotation.view;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Composite;
......@@ -59,19 +58,22 @@ public abstract class AnnotationViewPartBase extends ViewPart implements ISelect
private IModelElement currentlySelectedObject;
/**
* Set of {@link IModelElement}s for which the {@link #changeListener} has been installed to
* Root element for which the {@link #changeListener} has been installed to (recursively)
* watch for model changes (to trigger an update of this {@link AnnotationViewPartBase}).
*/
Set<EObject> watchedModelElements = new HashSet<EObject>();
EObject watchedElement;
/**
* {@link Adapter} to watch for the addition, removal or value change of model elements
* to/from/in elements visible in this {@link AnnotationViewPartBase} (to trigger according
* updates).
*/
private final Adapter changeListener = new AdapterImpl() {
private final Adapter changeListener = new EContentAdapter() {
/** {@inheritDoc} */
@Override
public void notifyChanged(Notification notification) {
super.notifyChanged(notification);
if(notification.getEventType() == Notification.ADD ||
notification.getEventType() == Notification.REMOVE ||
notification.getEventType() == Notification.SET) {
......@@ -113,38 +115,6 @@ public abstract class AnnotationViewPartBase extends ViewPart implements ISelect
}
}
/** Update registration of model listeners. */
private void updateChangeListener(EObject rootElement) {
// Determine all IModelElements, and their specifications below the current root elements.
// Specifications must be watched, too, since the state of an editing support for a given
// specification can depend on the state of one or more other specifications.
Set<EObject> currentWatchedModelElements = new HashSet<EObject>();
for(EObject modelElement : EcoreUtils.getChildrenWithType(rootElement, IModelElement.class)) {
currentWatchedModelElements.add(modelElement);
currentWatchedModelElements.addAll(((IModelElement)modelElement).getSpecifications());
}
if(rootElement instanceof IModelElement) {
currentWatchedModelElements.add(rootElement);
currentWatchedModelElements.addAll(((IModelElement)rootElement).getSpecifications());
}
// Add change listeners to IModelElements that are about to appear in the view
Set<EObject> tmpWatchedModelElements = new HashSet<EObject>(currentWatchedModelElements);
tmpWatchedModelElements.removeAll(watchedModelElements);
for(EObject modelElement : tmpWatchedModelElements) {
modelElement.eAdapters().add(changeListener);
}
// Remove change listeners IModelElements that are about to disappear from the view
tmpWatchedModelElements = new HashSet<EObject>(watchedModelElements);
tmpWatchedModelElements.removeAll(currentWatchedModelElements);
for(EObject modelElement : tmpWatchedModelElements) {
modelElement.eAdapters().remove(changeListener);
}
watchedModelElements = currentWatchedModelElements;
}
/**
* Update the {@link AnnotationViewPartBase}: delegate update of concrete view to
* {@link #update(Collection)}, and update model change listeners.
......@@ -181,9 +151,15 @@ public abstract class AnnotationViewPartBase extends ViewPart implements ISelect
annotationEntries.addAll(IAnnotationValueService.INSTANCE.getValues(childModelElement));
}
// Update the view and change listener registration
// Update the view...
update(annotationEntries);
updateChangeListener(rootElement);
// ... and change listener registration
if(watchedElement != null) {
watchedElement.eAdapters().remove(changeListener);
}
watchedElement = rootElement;
watchedElement.eAdapters().add(changeListener);
}
/** Update concrete view */
......@@ -205,11 +181,10 @@ public abstract class AnnotationViewPartBase extends ViewPart implements ISelect
@Override
public void dispose() {
getSite().getWorkbenchWindow().getSelectionService().removeSelectionListener(this);
for(EObject modelElement : watchedModelElements) {
modelElement.eAdapters().remove(changeListener);
if(watchedElement != null) {
watchedElement.eAdapters().remove(changeListener);
watchedElement = null;
}
watchedModelElements.clear();
super.dispose();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment