--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.scenegraph.g2d.events;\r
+\r
+import java.util.concurrent.atomic.AtomicBoolean;\r
+\r
+import org.simantics.utils.datastructures.prioritystack.PriorityStack;\r
+import org.simantics.utils.threads.CurrentThread;\r
+import org.simantics.utils.threads.IThreadWorkQueue;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+\r
+/**\r
+ * @see IEventHandlerStack\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class EventHandlerStack extends PriorityStack<IEventHandler> implements IEventHandlerStack {\r
+\r
+ IThreadWorkQueue thread;\r
+\r
+ /**\r
+ * Creates a new event handler stack.\r
+ * \r
+ * Any event handled with this stack is handled in current thread.\r
+ */\r
+ public EventHandlerStack() {\r
+ super(IEventHandler.class);\r
+ this.thread = CurrentThread.getThreadAccess();\r
+ }\r
+\r
+ /**\r
+ * Creates a new event handler stack. Any event handled with this stack is\r
+ * handled in the given thread.\r
+ * \r
+ * @param eventHandlingThread\r
+ */\r
+ public EventHandlerStack(IThreadWorkQueue eventHandlingThread) {\r
+ super(IEventHandler.class);\r
+ this.thread = eventHandlingThread;\r
+ }\r
+\r
+ @Override\r
+ public int getEventMask() {\r
+ return EventTypes.AnyMask;\r
+ }\r
+\r
+ @Override\r
+ public boolean handleEvent(final Event e) {\r
+ final int eventType = EventTypes.toTypeMask(e);\r
+ if (thread.currentThreadAccess()) {\r
+ // Send event through interactor stack\r
+ IEventHandler list[] = toArray();\r
+ for (int i = list.length - 1; i >= 0; i--)\r
+ if (EventTypes.passes(list[i], eventType) && list[i].handleEvent(e))\r
+ return true;\r
+ return false;\r
+ } else {\r
+ final AtomicBoolean result = new AtomicBoolean(false);\r
+ Runnable run = new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ // Send event through interactor stack\r
+ IEventHandler list[] = toArray();\r
+ for (int i = list.length - 1; i >= 0; i--)\r
+ if (EventTypes.passes(list[i], eventType) && list[i].handleEvent(e)) {\r
+ result.set(true);\r
+ return;\r
+ }\r
+ }\r
+ };\r
+ ThreadUtils.syncExec(thread, run);\r
+ return result.get();\r
+ }\r
+ }\r
+\r
+}\r