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