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.e4;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import org.eclipse.e4.ui.services.EContextService;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
23 import org.simantics.utils.ObjectUtils;
24 import org.simantics.utils.threads.IThreadWorkQueue;
25 import org.simantics.utils.threads.ThreadUtils;
28 * ContextUtil manages the activeness of Eclipse Workbench UI context related to
29 * an ICanvasContext and an IContextService (possibly local to a workbench part).
31 * @author Tuukka Lehtonen
33 public class ContextUtil extends AbstractCanvasParticipant {
35 private static final boolean DEBUG = false;
37 private final EContextService service;
39 private final IThreadWorkQueue thread;
41 private final Set<String> activations = new HashSet<String>();
43 public ContextUtil(EContextService service, IThreadWorkQueue contextManipulationThread) {
44 assert service != null;
45 assert contextManipulationThread != null;
46 this.service = service;
47 this.thread = contextManipulationThread;
50 public void inContextThread(Runnable r) {
54 private void debug(String s) {
58 private void debug(boolean trace, String s) {
60 System.out.println(getClass().getSimpleName() + "(" + ObjectUtils.hashCode(getContext()) + "): " + s);
62 StackTraceElement[] es = new Exception().getStackTrace();
64 System.out.println("Invoked from: ");
65 for (int i = 0; i < 2 && e < es.length; ++e) {
66 if (es[e].getClassName().equals(getClass().getName()))
68 System.out.println("\t" + es[e].toString());
75 private void checkThread() {
76 if (!thread.currentThreadAccess()) {
77 throw new IllegalStateException("not in context thread, use ContextUtil.inContextThread(Runnable)");
81 private void exec(Runnable r) {
85 private void exec(Runnable r, boolean allowSchedule) {
89 // Context access thread is disposed already?
90 if (thread.getThread() == null)
93 if (thread.currentThreadAccess()) {
95 debug("RUNNING " + r);
99 debug("SCHEDULING " + r);
100 ThreadUtils.asyncExec(thread, r);
105 public void removedFromContext(ICanvasContext ctx) {
106 exec(new Runnable() {
112 super.removedFromContext(ctx);
115 private void doActivate(String contextId) {
116 if(activations.add(contextId)) {
117 service.activateContext(contextId);
119 debug("ACTIVATED context: " + contextId);
122 debug("TRIED TO ACTIVATE DUPLICATE CONTEXT: " + contextId);
126 public void activate(final String contextId) {
129 debug(true, "activate(" + contextId + ")");
130 assert contextId != null;
131 exec(new Runnable() {
134 doActivate(contextId);
139 public void activate(final Collection<String> contextIds) {
141 debug(true, "activate contexts (" + contextIds + ")");
142 if (contextIds.isEmpty())
144 exec(new Runnable() {
147 for (String id : contextIds) {
154 public void deactivate(Collection<String> contextIds) {
156 assert contextIds != null;
157 for(String id : contextIds) {
158 boolean a = activations.remove(id);
160 debug(true, "deactivate(" + id + "): " + a);
162 exec(new Runnable() {
166 debug("DE-ACT context: " + id);
167 service.deactivateContext(id);
173 public void deactivateAll() {
175 final Collection<String> contextIds = getActivatedContextIds();
177 debug(true, "DE-ACTIVATE ALL INVOKED, " + contextIds.size() + " contexts to deactivate");
179 exec(new Runnable() {
183 debug("\tDE-ACTIVATE ALL " + contextIds.size() + " contexts:");
184 for (String id : contextIds) {
186 debug("\t\tDE-ACT context: " + id);
187 service.deactivateContext(id);
193 public synchronized Collection<String> getActivatedContextIds() {
194 return Collections.unmodifiableCollection(new ArrayList<String>(activations));