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