1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.participant;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
18 import java.util.concurrent.ConcurrentHashMap;
20 import org.eclipse.ui.contexts.IContextActivation;
21 import org.eclipse.ui.contexts.IContextService;
22 import org.simantics.g2d.canvas.ICanvasContext;
23 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
24 import org.simantics.utils.ObjectUtils;
25 import org.simantics.utils.threads.IThreadWorkQueue;
26 import org.simantics.utils.threads.ThreadUtils;
29 * ContextUtil manages the activeness of Eclipse Workbench UI context related to
30 * an ICanvasContext and an IContextService (possibly local to a workbench part).
32 * @author Tuukka Lehtonen
34 public class ContextUtil extends AbstractCanvasParticipant {
36 private static final boolean DEBUG = false;
38 private final IContextService service;
40 private final IThreadWorkQueue thread;
42 private final Map<String, IContextActivation> activations = new ConcurrentHashMap<String, IContextActivation>();
44 public ContextUtil(IContextService service, IThreadWorkQueue contextManipulationThread) {
45 assert service != null;
46 assert contextManipulationThread != null;
47 this.service = service;
48 this.thread = contextManipulationThread;
51 public void inContextThread(Runnable r) {
55 private void debug(String s) {
59 private void debug(boolean trace, String s) {
61 System.out.println(getClass().getSimpleName() + "(" + ObjectUtils.hashCode(getContext()) + "): " + s);
63 StackTraceElement[] es = new Exception().getStackTrace();
65 System.out.println("Invoked from: ");
66 for (int i = 0; i < 2 && e < es.length; ++e) {
67 if (es[e].getClassName().equals(getClass().getName()))
69 System.out.println("\t" + es[e].toString());
76 private void checkThread() {
77 if (!thread.currentThreadAccess()) {
78 throw new IllegalStateException("not in context thread, use ContextUtil.inContextThread(Runnable)");
82 private void exec(Runnable r) {
86 private void exec(Runnable r, boolean allowSchedule) {
90 // Context access thread is disposed already?
91 if (thread.getThread() == null)
94 if (thread.currentThreadAccess()) {
96 debug("RUNNING " + r);
100 debug("SCHEDULING " + r);
101 ThreadUtils.asyncExec(thread, r);
106 public void removedFromContext(ICanvasContext ctx) {
107 exec(new Runnable() {
113 super.removedFromContext(ctx);
116 private void doActivate(String contextId) {
117 if(!activations.containsKey(contextId)) {
118 IContextActivation activation = service.activateContext(contextId);
119 if (activation != null) {
121 debug("ACTIVATED context: " + contextId);
122 activations.put(contextId, activation);
126 debug("TRIED TO ACTIVATE DUPLICATE CONTEXT: " + contextId);
130 public void activate(final String contextId) {
133 debug(true, "activate(" + contextId + ")");
134 assert contextId != null;
135 exec(new Runnable() {
138 doActivate(contextId);
143 public void activate(final Collection<String> contextIds) {
145 debug(true, "activate contexts (" + contextIds + ")");
146 if (contextIds.isEmpty())
148 exec(new Runnable() {
151 for (String id : contextIds) {
158 // public void deactivate(String contextId) {
160 // assert contextId != null;
161 // final IContextActivation activation = activations.remove(contextId);
162 // if (activation == null) {
163 // debug("WARNING: cannot deactivate inactive context: " + contextId);
166 // exec(new Runnable() {
168 // public void run() {
169 // if (DEBUG_ACTIVATIONS)
170 // debug("DE-ACT context: " + activation.getContextId());
171 // service.deactivateContext(activation);
176 public void deactivate(Collection<String> contextIds) {
178 assert contextIds != null;
179 for(String id : contextIds) {
180 final IContextActivation a = activations.remove(id);
182 debug(true, "deactivate(" + id + "): " + a);
184 exec(new Runnable() {
188 debug("DE-ACT context: " + a.getContextId());
189 service.deactivateContext(a);
195 public void deactivate(IContextActivation activation) {
197 assert activation != null;
198 final IContextActivation a = activations.remove(activation.getContextId());
200 debug(true, "deactivate(" + activation + "): " + a);
202 exec(new Runnable() {
206 debug("DE-ACT context: " + a.getContextId());
207 service.deactivateContext(a);
212 public void deactivateAll() {
214 final Collection<IContextActivation> acts = getActivations();
216 debug(true, "DE-ACTIVATE ALL INVOKED, " + acts.size() + " contexts to deactivate");
218 exec(new Runnable() {
222 debug("\tDE-ACTIVATE ALL " + acts.size() + " contexts:");
223 for (IContextActivation a : acts) {
225 debug("\t\tDE-ACT context: " + a.getContextId());
226 service.deactivateContext(a);
232 public synchronized Collection<IContextActivation> getActivations() {
233 return Collections.unmodifiableCollection(new ArrayList<IContextActivation>(activations.values()));
236 public synchronized Collection<String> getActivatedContextIds() {
237 return Collections.unmodifiableCollection(new ArrayList<String>(activations.keySet()));