1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 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
14 * @author Toni Kalajainen
\r
16 package org.simantics.scenegraph.g2d.events;
\r
18 import java.util.concurrent.atomic.AtomicBoolean;
\r
20 import org.simantics.utils.datastructures.prioritystack.PriorityStack;
\r
21 import org.simantics.utils.threads.CurrentThread;
\r
22 import org.simantics.utils.threads.IThreadWorkQueue;
\r
23 import org.simantics.utils.threads.ThreadUtils;
\r
26 * @see IEventHandlerStack
\r
28 * @author Toni Kalajainen
\r
30 public class EventHandlerStack extends PriorityStack<IEventHandler> implements IEventHandlerStack {
\r
32 IThreadWorkQueue thread;
\r
35 * Creates a new event handler stack.
\r
37 * Any event handled with this stack is handled in current thread.
\r
39 public EventHandlerStack() {
\r
40 super(IEventHandler.class);
\r
41 this.thread = CurrentThread.getThreadAccess();
\r
45 * Creates a new event handler stack. Any event handled with this stack is
\r
46 * handled in the given thread.
\r
48 * @param eventHandlingThread
\r
50 public EventHandlerStack(IThreadWorkQueue eventHandlingThread) {
\r
51 super(IEventHandler.class);
\r
52 this.thread = eventHandlingThread;
\r
56 public int getEventMask() {
\r
57 return EventTypes.AnyMask;
\r
61 public boolean handleEvent(final Event e) {
\r
62 final int eventType = EventTypes.toTypeMask(e);
\r
63 if (thread.currentThreadAccess()) {
\r
64 // Send event through interactor stack
\r
65 IEventHandler list[] = toArray();
\r
66 for (int i = list.length - 1; i >= 0; i--)
\r
67 if (EventTypes.passes(list[i], eventType) && list[i].handleEvent(e))
\r
71 final AtomicBoolean result = new AtomicBoolean(false);
\r
72 Runnable run = new Runnable() {
\r
75 // Send event through interactor stack
\r
76 IEventHandler list[] = toArray();
\r
77 for (int i = list.length - 1; i >= 0; i--)
\r
78 if (EventTypes.passes(list[i], eventType) && list[i].handleEvent(e)) {
\r
84 ThreadUtils.syncExec(thread, run);
\r
85 return result.get();
\r