]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / participant / e4 / ContextUtil.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java
new file mode 100644 (file)
index 0000000..f3724ff
--- /dev/null
@@ -0,0 +1,197 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.participant.e4;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.Set;\r
+\r
+import org.eclipse.e4.ui.services.EContextService;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.utils.ObjectUtils;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+/**\r
+ * ContextUtil manages the activeness of Eclipse Workbench UI context related to\r
+ * an ICanvasContext and an IContextService (possibly local to a workbench part).\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ContextUtil extends AbstractCanvasParticipant {\r
+\r
+    private static final boolean                  DEBUG = false;\r
+\r
+    private final EContextService                 service;\r
+\r
+    private final IThreadWorkQueue                thread;\r
+\r
+    private final Set<String>                     activations = new HashSet<String>();\r
+\r
+    public ContextUtil(EContextService service, IThreadWorkQueue contextManipulationThread) {\r
+        assert service != null;\r
+        assert contextManipulationThread != null;\r
+        this.service = service;\r
+        this.thread = contextManipulationThread;\r
+    }\r
+\r
+    public void inContextThread(Runnable r) {\r
+        exec(r, true);\r
+    }\r
+\r
+    private void debug(String s) {\r
+        debug(false, s);\r
+    }\r
+\r
+    private void debug(boolean trace, String s) {\r
+        if (DEBUG) {\r
+            System.out.println(getClass().getSimpleName() + "(" + ObjectUtils.hashCode(getContext()) + "): " + s);\r
+            if (trace) {\r
+                StackTraceElement[] es = new Exception().getStackTrace();\r
+                int e = 0;\r
+                System.out.println("Invoked from: ");\r
+                for (int i = 0; i < 2 && e < es.length; ++e) {\r
+                    if (es[e].getClassName().equals(getClass().getName()))\r
+                        continue;\r
+                    System.out.println("\t" + es[e].toString());\r
+                    ++i;\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private void checkThread() {\r
+        if (!thread.currentThreadAccess()) {\r
+            throw new IllegalStateException("not in context thread, use ContextUtil.inContextThread(Runnable)");\r
+        }\r
+    }\r
+\r
+    private void exec(Runnable r) {\r
+        exec(r, false);\r
+    }\r
+\r
+    private void exec(Runnable r, boolean allowSchedule) {\r
+        if (!allowSchedule)\r
+            checkThread();\r
+\r
+        // Context access thread is disposed already?\r
+        if (thread.getThread() == null)\r
+            return;\r
+\r
+        if (thread.currentThreadAccess()) {\r
+            if (DEBUG)\r
+                debug("RUNNING " + r);\r
+            r.run();\r
+        } else {\r
+            if (DEBUG)\r
+                debug("SCHEDULING " + r);\r
+            ThreadUtils.asyncExec(thread, r);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void removedFromContext(ICanvasContext ctx) {\r
+        exec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                deactivateAll();\r
+            }\r
+        }, true);\r
+        super.removedFromContext(ctx);\r
+    }\r
+\r
+    private void doActivate(String contextId) {\r
+        if(activations.add(contextId)) {\r
+            service.activateContext(contextId);\r
+            if (DEBUG)\r
+                debug("ACTIVATED context: " + contextId);\r
+        } else {\r
+            if (DEBUG)\r
+                debug("TRIED TO ACTIVATE DUPLICATE CONTEXT: " + contextId);\r
+        }\r
+    }\r
+\r
+    public void activate(final String contextId) {\r
+        checkThread();\r
+        if (DEBUG)\r
+            debug(true, "activate(" + contextId + ")");\r
+        assert contextId != null;\r
+        exec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                doActivate(contextId);\r
+            }\r
+        });\r
+    }\r
+\r
+    public void activate(final Collection<String> contextIds) {\r
+        if (DEBUG)\r
+            debug(true, "activate contexts (" + contextIds + ")");\r
+        if (contextIds.isEmpty())\r
+            return;\r
+        exec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                for (String id : contextIds) {\r
+                    doActivate(id);\r
+                }\r
+            }\r
+        });\r
+    }\r
+\r
+    public void deactivate(Collection<String> contextIds) {\r
+        checkThread();\r
+        assert contextIds != null;\r
+        for(String id : contextIds) {\r
+            boolean a = activations.remove(id);\r
+            if (DEBUG)\r
+                debug(true, "deactivate(" + id + "): " + a);\r
+            if (a)\r
+                exec(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        if (DEBUG)\r
+                            debug("DE-ACT context: " + id);\r
+                        service.deactivateContext(id);\r
+                    }\r
+                });\r
+        }\r
+    }\r
+\r
+    public void deactivateAll() {\r
+        checkThread();\r
+        final Collection<String> contextIds = getActivatedContextIds();\r
+        if (DEBUG)\r
+            debug(true, "DE-ACTIVATE ALL INVOKED, " + contextIds.size() + " contexts to deactivate");\r
+        activations.clear();\r
+        exec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                if (DEBUG)\r
+                    debug("\tDE-ACTIVATE ALL " + contextIds.size() + " contexts:");\r
+                for (String id : contextIds) {\r
+                    if (DEBUG)\r
+                        debug("\t\tDE-ACT context: " + id);\r
+                    service.deactivateContext(id);\r
+                }\r
+            }\r
+        });\r
+    }\r
+\r
+    public synchronized Collection<String> getActivatedContextIds() {\r
+        return Collections.unmodifiableCollection(new ArrayList<String>(activations));\r
+    }\r
+\r
+}\r