/******************************************************************************* * Copyright (c) 2007, 2010 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 *******************************************************************************/ /* * * @author Toni Kalajainen */ package org.simantics.scenegraph.g2d.events.adapter; import org.simantics.scenegraph.g2d.events.Event; import org.simantics.scenegraph.g2d.events.EventTypes; import org.simantics.scenegraph.g2d.events.IEventHandler; import org.simantics.scenegraph.g2d.events.IEventQueue; /** * Adapts SWT, AWT and multitouch events */ public abstract class AbstractEventAdapter { /** Used for delegating events */ protected final IEventHandler delegator; protected final IEventQueue queue; protected final Object sender; public AbstractEventAdapter(Object sender, IEventHandler delegator) { if (delegator == null) throw new IllegalArgumentException("null delegator"); this.delegator = delegator; this.queue = null; this.sender = sender; } public AbstractEventAdapter(Object sender, IEventQueue queue) { if (queue == null) throw new IllegalArgumentException("null event queue"); this.delegator = null; this.queue = queue; this.sender = sender; } protected void handleEvent(Event e) { if (queue != null) queue.queueEvent(e); else if (EventTypes.passes(delegator, e)) delegator.handleEvent(e); } protected void syncHandleEvent(Event e) { if (queue instanceof IEventHandler) ((IEventHandler) queue).handleEvent(e); else if (delegator != null && EventTypes.passes(delegator, e)) delegator.handleEvent(e); else throw new UnsupportedOperationException("Cannot handle event synchronously, no handler available: " + e); } }