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