diff --git a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/V4MDPlugin.java b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/V4MDPlugin.java index cd388b9..6b86246 100644 --- a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/V4MDPlugin.java +++ b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/V4MDPlugin.java @@ -1,6 +1,7 @@ package com.incquerylabs.v4md; import com.incquerylabs.v4md.expressions.BinaryVQLExpression; +import com.incquerylabs.v4md.internal.IProjectChangedListener; import com.nomagic.ci.persistence.IProject; import com.nomagic.magicdraw.core.Application; import com.nomagic.magicdraw.core.Project; @@ -11,8 +12,9 @@ import com.nomagic.magicdraw.expressions.evaluation.ExpressionEvaluationConfigurator; public class V4MDPlugin extends com.nomagic.magicdraw.plugins.Plugin { - + private final class V4MDProjectListener extends ProjectEventListenerAdapter implements ProjectPartListener { + @Override public void projectOpened(Project project) { ViatraQueryAdapter.getOrCreateAdapter(project); @@ -22,29 +24,29 @@ public void projectOpened(Project project) { public void projectPreClosed(Project project) { ViatraQueryAdapter.disposeAdapter(project); } - + @Override public void projectPartLoaded(Project project, IProject storage) { - ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated); + IProjectChangedListener.MANAGER.notifyProjectListeners(project); } @Override public void projectPartAttached(ModuleUsage usage) { Project project = ProjectUtilities.getProject(usage.getUsed()); - ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated); + IProjectChangedListener.MANAGER.notifyProjectListeners(project); } @Override public void projectPartDetached(ModuleUsage usage) { Project project = ProjectUtilities.getProject(usage.getUsed()); - ViatraQueryAdapter.getAdapter(project).ifPresent(ViatraQueryAdapter::projectStructureUpdated); + IProjectChangedListener.MANAGER.notifyProjectListeners(project); } @Override public void projectPartRemoved(IProject project) { Project modelProject = ProjectUtilities.getProject(project); - ViatraQueryAdapter.getAdapter(modelProject).ifPresent(ViatraQueryAdapter::projectStructureUpdated); + IProjectChangedListener.MANAGER.notifyProjectListeners(modelProject); } } diff --git a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/IProjectChangedListener.java b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/IProjectChangedListener.java new file mode 100644 index 0000000..80facf3 --- /dev/null +++ b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/IProjectChangedListener.java @@ -0,0 +1,33 @@ +package com.incquerylabs.v4md.internal; + +import com.google.common.collect.ArrayListMultimap; +import com.nomagic.magicdraw.core.Project; + +public interface IProjectChangedListener { + void modelSetUpdated(); + + public static final Manager MANAGER = new Manager(); + + public class Manager { + ArrayListMultimap listenerMap = ArrayListMultimap.create(); + + + private Manager() { + // Private constructor for singleton class + } + + public void notifyProjectListeners(Project project) { + for (IProjectChangedListener listener : listenerMap.get(project)) { + listener.modelSetUpdated(); + } + } + + public boolean addProjectChangeListener(Project project, IProjectChangedListener listener) { + return listenerMap.put(project, listener); + } + + public boolean removeProjectChangeListener(Project project, IProjectChangedListener listener) { + return listenerMap.remove(project, listener); + } + } +} \ No newline at end of file diff --git a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectEngineContext.java b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectEngineContext.java index 5d20880..25a4637 100644 --- a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectEngineContext.java +++ b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectEngineContext.java @@ -27,7 +27,6 @@ import org.eclipse.viatra.query.runtime.matchers.context.IQueryRuntimeContext; import com.google.common.collect.Sets; -import com.incquerylabs.v4md.internal.MagicDrawProjectScope.IProjectChangedListener; /** * Provides a specific engine context implementation for MagicDraw projects. @@ -101,7 +100,7 @@ public void removeRoot(Notifier root) { return; // no veto by filters - modelRoots.add(root); + modelRoots.remove(root); // TODO contentAdapter.removeAdapter(root); removeAdapter is not visible here Method method; try { @@ -120,7 +119,7 @@ public MagicDrawProjectEngineContext(MagicDrawProjectScope scope, ViatraQueryEng this.engine = engine; this.logger = logger; this.taintListener = taintListener; - scope.addProjectChangeListener(scopeListener); + IProjectChangedListener.MANAGER.addProjectChangeListener(scope.getProject(), scopeListener); } /** @@ -169,7 +168,7 @@ public void dispose() { if (runtimeContext != null) runtimeContext.dispose(); if (navHelper != null) navHelper.dispose(); - scope.removeProjectChangeListener(scopeListener); + IProjectChangedListener.MANAGER.removeProjectChangeListener(scope.getProject(), scopeListener); this.baseIndex = null; this.engine = null; diff --git a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectScope.java b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectScope.java index 153809d..5c2350c 100644 --- a/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectScope.java +++ b/com.incquerylabs.v4md/src/main/com/incquerylabs/v4md/internal/MagicDrawProjectScope.java @@ -33,9 +33,6 @@ public class MagicDrawProjectScope extends EMFScope { && ((EReference) reference).isContainment() && reference.getName().contains("_from_")) .withStrictNotificationMode(false); - public interface IProjectChangedListener { - void modelSetUpdated(); - } static Stream getProjectModels(Project projectModel) { return projectModel.getModels().stream(); } @@ -60,14 +57,6 @@ protected IEngineContext createEngineContext(ViatraQueryEngine engine, IIndexing Logger logger) { return new MagicDrawProjectEngineContext(this, engine, errorListener, logger); } - - boolean addProjectChangeListener(IProjectChangedListener listener) { - return listeners.add(listener); - } - - boolean removeProjectChangeListener(IProjectChangedListener listener) { - return listeners.remove(listener); - } public void projectStructureUpdated() { for (IProjectChangedListener listener : listeners) {