-/*******************************************************************************\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;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Map;\r
-import java.util.concurrent.ConcurrentHashMap;\r
-\r
-import org.eclipse.ui.contexts.IContextActivation;\r
-import org.eclipse.ui.contexts.IContextService;\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 IContextService service;\r
-\r
- private final IThreadWorkQueue thread;\r
-\r
- private final Map<String, IContextActivation> activations = new ConcurrentHashMap<String, IContextActivation>();\r
-\r
- public ContextUtil(IContextService 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.containsKey(contextId)) {\r
- IContextActivation activation = service.activateContext(contextId);\r
- if (activation != null) {\r
- if (DEBUG)\r
- debug("ACTIVATED context: " + contextId);\r
- activations.put(contextId, activation);\r
- }\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(String contextId) {\r
-// checkThread();\r
-// assert contextId != null;\r
-// final IContextActivation activation = activations.remove(contextId);\r
-// if (activation == null) {\r
-// debug("WARNING: cannot deactivate inactive context: " + contextId);\r
-// return;\r
-// }\r
-// exec(new Runnable() {\r
-// @Override\r
-// public void run() {\r
-// if (DEBUG_ACTIVATIONS)\r
-// debug("DE-ACT context: " + activation.getContextId());\r
-// service.deactivateContext(activation);\r
-// }\r
-// });\r
-// }\r
-\r
- public void deactivate(Collection<String> contextIds) {\r
- checkThread();\r
- assert contextIds != null;\r
- for(String id : contextIds) {\r
- final IContextActivation a = activations.remove(id);\r
- if (DEBUG)\r
- debug(true, "deactivate(" + id + "): " + a);\r
- if (a != null)\r
- exec(new Runnable() {\r
- @Override\r
- public void run() {\r
- if (DEBUG)\r
- debug("DE-ACT context: " + a.getContextId());\r
- service.deactivateContext(a);\r
- }\r
- });\r
- }\r
- }\r
-\r
- public void deactivate(IContextActivation activation) {\r
- checkThread();\r
- assert activation != null;\r
- final IContextActivation a = activations.remove(activation.getContextId());\r
- if (DEBUG)\r
- debug(true, "deactivate(" + activation + "): " + a);\r
- if (a == activation)\r
- exec(new Runnable() {\r
- @Override\r
- public void run() {\r
- if (DEBUG)\r
- debug("DE-ACT context: " + a.getContextId());\r
- service.deactivateContext(a);\r
- }\r
- });\r
- }\r
-\r
- public void deactivateAll() {\r
- checkThread();\r
- final Collection<IContextActivation> acts = getActivations();\r
- if (DEBUG)\r
- debug(true, "DE-ACTIVATE ALL INVOKED, " + acts.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 " + acts.size() + " contexts:");\r
- for (IContextActivation a : acts) {\r
- if (DEBUG)\r
- debug("\t\tDE-ACT context: " + a.getContextId());\r
- service.deactivateContext(a);\r
- }\r
- }\r
- });\r
- }\r
-\r
- public synchronized Collection<IContextActivation> getActivations() {\r
- return Collections.unmodifiableCollection(new ArrayList<IContextActivation>(activations.values()));\r
- }\r
-\r
- public synchronized Collection<String> getActivatedContextIds() {\r
- return Collections.unmodifiableCollection(new ArrayList<String>(activations.keySet()));\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;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.eclipse.ui.contexts.IContextActivation;
+import org.eclipse.ui.contexts.IContextService;
+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 IContextService service;
+
+ private final IThreadWorkQueue thread;
+
+ private final Map<String, IContextActivation> activations = new ConcurrentHashMap<String, IContextActivation>();
+
+ public ContextUtil(IContextService 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.containsKey(contextId)) {
+ IContextActivation activation = service.activateContext(contextId);
+ if (activation != null) {
+ if (DEBUG)
+ debug("ACTIVATED context: " + contextId);
+ activations.put(contextId, activation);
+ }
+ } 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(String contextId) {
+// checkThread();
+// assert contextId != null;
+// final IContextActivation activation = activations.remove(contextId);
+// if (activation == null) {
+// debug("WARNING: cannot deactivate inactive context: " + contextId);
+// return;
+// }
+// exec(new Runnable() {
+// @Override
+// public void run() {
+// if (DEBUG_ACTIVATIONS)
+// debug("DE-ACT context: " + activation.getContextId());
+// service.deactivateContext(activation);
+// }
+// });
+// }
+
+ public void deactivate(Collection<String> contextIds) {
+ checkThread();
+ assert contextIds != null;
+ for(String id : contextIds) {
+ final IContextActivation a = activations.remove(id);
+ if (DEBUG)
+ debug(true, "deactivate(" + id + "): " + a);
+ if (a != null)
+ exec(new Runnable() {
+ @Override
+ public void run() {
+ if (DEBUG)
+ debug("DE-ACT context: " + a.getContextId());
+ service.deactivateContext(a);
+ }
+ });
+ }
+ }
+
+ public void deactivate(IContextActivation activation) {
+ checkThread();
+ assert activation != null;
+ final IContextActivation a = activations.remove(activation.getContextId());
+ if (DEBUG)
+ debug(true, "deactivate(" + activation + "): " + a);
+ if (a == activation)
+ exec(new Runnable() {
+ @Override
+ public void run() {
+ if (DEBUG)
+ debug("DE-ACT context: " + a.getContextId());
+ service.deactivateContext(a);
+ }
+ });
+ }
+
+ public void deactivateAll() {
+ checkThread();
+ final Collection<IContextActivation> acts = getActivations();
+ if (DEBUG)
+ debug(true, "DE-ACTIVATE ALL INVOKED, " + acts.size() + " contexts to deactivate");
+ activations.clear();
+ exec(new Runnable() {
+ @Override
+ public void run() {
+ if (DEBUG)
+ debug("\tDE-ACTIVATE ALL " + acts.size() + " contexts:");
+ for (IContextActivation a : acts) {
+ if (DEBUG)
+ debug("\t\tDE-ACT context: " + a.getContextId());
+ service.deactivateContext(a);
+ }
+ }
+ });
+ }
+
+ public synchronized Collection<IContextActivation> getActivations() {
+ return Collections.unmodifiableCollection(new ArrayList<IContextActivation>(activations.values()));
+ }
+
+ public synchronized Collection<String> getActivatedContextIds() {
+ return Collections.unmodifiableCollection(new ArrayList<String>(activations.keySet()));
+ }
+
+}