X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fparticipant%2Fe4%2FContextUtil.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fparticipant%2Fe4%2FContextUtil.java;h=e5cb68010174ea4b63d82adaade00be485fec64e;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=f3724ff05080acceae8d4d5d5c9a8935623b0285;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java index f3724ff05..e5cb68010 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/participant/e4/ContextUtil.java @@ -1,197 +1,197 @@ -/******************************************************************************* - * 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 activations = new HashSet(); - - 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 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 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 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 getActivatedContextIds() { - return Collections.unmodifiableCollection(new ArrayList(activations)); - } - -} +/******************************************************************************* + * 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 activations = new HashSet(); + + 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 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 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 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 getActivatedContextIds() { + return Collections.unmodifiableCollection(new ArrayList(activations)); + } + +}