diff options
author | Cedric Dumoulin | 2013-12-22 22:03:16 +0000 |
---|---|---|
committer | Cedric Dumoulin | 2014-01-27 13:57:10 +0000 |
commit | 78aeed90433cc283fb521a3b4f6c4d51b86c9de3 (patch) | |
tree | 6dda592eafeeb67ab1477246c37a36947558317b /extraplugins/layers | |
parent | cfcc88475333a2a6d19969ede230e03079c3c17e (diff) | |
download | org.eclipse.papyrus-78aeed90433cc283fb521a3b4f6c4d51b86c9de3.tar.gz org.eclipse.papyrus-78aeed90433cc283fb521a3b4f6c4d51b86c9de3.tar.xz org.eclipse.papyrus-78aeed90433cc283fb521a3b4f6c4d51b86c9de3.zip |
bug 330199 - Improve init of ExpressionMatcher
Diffstat (limited to 'extraplugins/layers')
3 files changed, 151 insertions, 6 deletions
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java index 9280527e5ae..09e45735281 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java +++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ExpressionMatcher.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
@@ -36,8 +37,6 @@ import org.eclipse.ocl.ecore.OCL; import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.util.ObservableListView;
-import com.google.common.collect.Lists;
-
/**
* This class evaluate its associated expression against the associated models.
* It provide a list of elements matching the expression in the model.
@@ -49,7 +48,7 @@ import com.google.common.collect.Lists; * @author cedric dumoulin
*
*/
-public class ExpressionMatcher {
+public class ExpressionMatcher implements IValueChangedEventListener {
protected String expression="";
@@ -87,7 +86,7 @@ public class ExpressionMatcher { */
public ExpressionMatcher(List<EObject> searchRoots) {
this.expression = "";
- this.searchRoots = searchRoots;
+ setSearchRoots(searchRoots);
// init matchingElements
matchingElements = new ObservableListView<View>(new ArrayList<View>());
}
@@ -283,10 +282,17 @@ public class ExpressionMatcher { * @param searchRoots
*/
public void setSearchRoots(List<EObject> searchRoots) {
+
+ // Remove any existing observers
+ removeSearchRootsObservers();
+
if( searchRoots == null) {
searchRoots = Collections.emptyList();
}
this.searchRoots = searchRoots;
+ // add observers on roots changes
+ addSearchRootsObservers();
+
// Do not refresh. Let user do it.
}
@@ -296,6 +302,8 @@ public class ExpressionMatcher { */
public void setSearchRoots(EObject searchRoot) {
if( searchRoot == null) {
+ // Remove any existing observers
+ removeSearchRootsObservers();
searchRoots = Collections.emptyList();
return;
}
@@ -303,5 +311,47 @@ public class ExpressionMatcher { setSearchRoots( Collections.singletonList(searchRoot) );
}
+ /**
+ * Observes all searchRoots for changes. If a change occurs, refresh the matching elements.
+ *
+ */
+ protected void addSearchRootsObservers() {
+
+ if( searchRoots == null) {
+ return;
+ }
+
+
+ for( EObject root : searchRoots) {
+ ValueChangedEventNotifier notifier = ValueChangedEventNotifierFactory.instance.adapt(root);
+ notifier.addEventListener(this);
+ }
+ }
+
+ /**
+ * Observes all searchRoots for changes. If a change occurs, refresh the matching elements.
+ *
+ */
+ protected void removeSearchRootsObservers() {
+
+ if( searchRoots == null) {
+ return;
+ }
+
+ for( EObject root : searchRoots) {
+ ValueChangedEventNotifier notifier = ValueChangedEventNotifierFactory.instance.adapt(root);
+ notifier.removeEventListener(this);
+ }
+ }
+
+ /**
+ * Called when a value change in one of the elements of the observed roots.
+ * @param msg
+ */
+ @Override
+ public void valueChanged(Notification msg) {
+ refreshMatchingElements();
+ }
+
}
diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifier.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifier.java index 6ba78501945..0a32cbf7c07 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifier.java +++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/src/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifier.java @@ -11,13 +11,21 @@ package org.eclipse.papyrus.layers.stackmodel.exprmatcher; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import org.eclipse.emf.common.notify.Adapter; import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.common.notify.Notifier; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.util.EContentAdapter; +import org.eclipse.emf.ecore.util.InternalEList; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.View; import org.eclipse.papyrus.layers.stackmodel.layers.LayersStack; import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication; import org.eclipse.papyrus.layers.stackmodel.notifier.LayersTreeEventNotifier; @@ -56,6 +64,9 @@ public class ValueChangedEventNotifier extends EContentAdapter { // Self atttach super.notifyChanged(notification); + // Take into account the domain hierarchy found from the notation. + + // Now, filter if( isDiagramRootView(notification)) { fireValueChangedEvent(notification); } @@ -234,4 +245,88 @@ public class ValueChangedEventNotifier extends EContentAdapter { } } + /** + * Handle View::element hierarchy in the self adapt mechanism. + * Handles a notification by calling {@link #handleContainment handleContainment} + * for any containment-based notification. + */ + @Override + protected void selfAdapt(Notification notification) + { + System.err.println( this.getClass().getSimpleName() + ".selfAdapt("+ notification+")" ); + // Handle the View::element tree + if (notification.getFeature() == NotationPackage.eINSTANCE.getView_Element()) + { + handleContainment(notification); + } + else { + super.selfAdapt(notification); + } + } + + /** + * Handle View::element hierarchy in the self adapt mechanism. + * @see org.eclipse.emf.ecore.util.EContentAdapter#setTarget(org.eclipse.emf.ecore.EObject) + * + * @param target + */ + @Override + protected void setTarget(EObject target) { + System.err.println( this.getClass().getSimpleName() + ".setTarget("+ target+")" ); + super.setTarget(target); + // Handle the View::element tree + if( target instanceof View ) { + EObject extraTarget = ((View)target).getElement(); + if( extraTarget != null) { + System.err.println( this.getClass().getSimpleName() + ".setExtraTarget("+ extraTarget+")" ); + // copied from org.eclipse.emf.ecore.util.EContentAdapter.setTarget(EObject) +// basicSetTarget(target); + // Add the extra object + addAdapter(extraTarget); + // Add the content of the extra object +// for (Iterator<? extends Notifier> i = resolve() ? +// extraTarget.eContents().iterator() : +// ((InternalEList<? extends Notifier>)extraTarget.eContents()).basicIterator(); +// i.hasNext(); ) +// { +// Notifier notifier = i.next(); +// addAdapter(notifier); +// } + + } + } + } + + /** + * Handle View::element hierarchy in the self adapt mechanism. + * @see org.eclipse.emf.ecore.util.EContentAdapter#unsetTarget(org.eclipse.emf.ecore.EObject) + * + * @param target + */ + @Override + protected void unsetTarget(EObject target) { + // TODO Auto-generated method stub + super.unsetTarget(target); + // Handle the View::element tree + if( target instanceof View ) { + EObject extraTarget = ((View)target).getElement(); + if( extraTarget != null) { + // copied from org.eclipse.emf.ecore.util.EContentAdapter.setTarget(EObject) +// basicSetTarget(target); + // Remove the extra object + removeAdapter(extraTarget); + // Remove contents of the extra object +// for (Iterator<? extends Notifier> i = resolve() ? +// extraTarget.eContents().iterator() : +// ((InternalEList<? extends Notifier>)extraTarget.eContents()).basicIterator(); +// i.hasNext(); ) +// { +// Notifier notifier = i.next(); +// removeAdapter(notifier); +// } + + } + } + } + } diff --git a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifierTest.java b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifierTest.java index 0ca2083725d..c67a61e8d72 100644 --- a/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifierTest.java +++ b/extraplugins/layers/org.eclipse.papyrus.layers.stackmodel/test/org/eclipse/papyrus/layers/stackmodel/exprmatcher/ValueChangedEventNotifierTest.java @@ -108,7 +108,7 @@ public class ValueChangedEventNotifierTest { * Test method for {@link org.eclipse.papyrus.layers.stackmodel.notifier.ValueChangedEventNotifier#fireDiagramViewAddedEvent(org.eclipse.emf.common.notify.Notification)}.
*/
@Test
- public void testNameChangedEvent() {
+ public void testClassNameChangedEvent() {
NotationAndUmlModelsFactory modelsFactory = new NotationAndUmlModelsFactory();
Diagram diagram1 = modelsFactory.newDiagram();
Class c1 = modelsFactory.newClass(diagram1, "C1");
@@ -173,7 +173,7 @@ public class ValueChangedEventNotifierTest { Property p1 = modelsFactory.newProperty(c1, "p1");
ValueChangedEventNotifierFactory notifierFactory = new ValueChangedEventNotifierFactory();
- ValueChangedEventNotifier eventNotifier = (ValueChangedEventNotifier)notifierFactory.adapt(diagram1, ValueChangedEventNotifier.class);
+ ValueChangedEventNotifier eventNotifier = (ValueChangedEventNotifier)notifierFactory.adapt(diagram1);
TraceValueChangedEventListener listener = new TraceValueChangedEventListener();
eventNotifier.addEventListener(listener);
|