com.jogamp.newt.event.awt.AWTAdapter Maven / Gradle / Ivy
Show all versions of jogl-all Show documentation
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.newt.event.awt;
import jogamp.newt.Debug;
/**
* Convenient adapter forwarding AWT events to NEWT via the event listener model.
*
* You may attach an instance of this adapter to an AWT Component. When an event happens,
* it is converted to a NEWT event and the given NEWT listener is being called.
*
* This adapter fullfills three use cases. First as a plain utility to write code AWT agnostic,
* ie write an {@link javax.media.opengl.GLEvenListener} and some appropriate NEWT {@link com.jogamp.newt.event.NEWTEventListener}.
*
* Attach the {@link javax.media.opengl.GLEvenListener} to a NEWT {@link javax.media.opengl.GLAutoDrawable}, e.g. {@link com.jogamp.newt.opengl.GLWindow},
* or to an AWT {@link javax.media.opengl.GLAutoDrawable}, e.g. {@link javax.media.opengl.awt.GLCanvas}.
*
* Attach the NEWT {@link com.jogamp.newt.event.NEWTEventListener} to a NEWT component, e.g. {@link com.jogamp.newt.Window},
* or to an AWT component, e.g. {@link java.awt.Component}.
*
* Common:
*
// your demo/render code
javax.media.opengl.GLEvenListener demo1 = new javax.media.opengl.GLEvenListener() { ... } ;
// your AWT agnostic NEWT mouse listener code
com.jogamp.newt.event.MouseListener mouseListener = new com.jogamp.newt.event.MouseAdapter() { ... } ;
*
*
* Default NEWT use case, without using the AWTAdapter:
*
// the NEWT GLAutoDrawable and Window
GLWindow glWindow = GLWindow.create();
// attach the renderer demo1
glWindow.addGLEventListener(demo1);
// attach the NEWT mouse event listener to glWindow
glWindow.addMouseListener(mouseListener);
*
*
* AWT use case, AWTAdapter used as an AWT event translator and forwarder to your NEWT listener:
*
// the AWT GLAutoDrawable and Canvas
GLCanvas glCanvas = new GLCanvas();
// attach the renderer demo1
glCanvas.addGLEventListener(demo1);
// attach the AWTMouseAdapter to glCanvas, which translates and forwards events to the NEWT mouseListener
new AWTMouseAdapter(mouseListener).addTo(glCanvas);
*
*
* Previous code in detail:
*
AWTMouseAdapter mouseAdapter = new AWTMouseAdapter(mouseListener);
glCanvas.addMouseListener(mouseAdapter);
glCanvas.addMouseMotionListener(mouseAdapter);
*
*
*
* Second use case is just a litte variation of the previous use case, where we pass a NEWT Window
* to be used as the source of the event.
*
*
com.jogamp.newt.event.MouseListener mouseListener = new com.jogamp.newt.event.MouseAdapter() { ... } ;
Component comp = ... ; // the AWT component
GLWindow glWindow = GLWindow.create(); // the NEWT component
new AWTMouseAdapter(mouseListener, glWindow).addTo(comp);
*
*
* Last but not least, the AWTAdapter maybe used as a general AWT event forwarder to NEWT.
*
*
*
com.jogamp.newt.event.MouseListener mouseListener = new com.jogamp.newt.event.MouseAdapter() { ... } ;
Component comp = ... ; // the AWT component
GLWindow glWindow = GLWindow.create(); // the NEWT component
glWindow.addMouseListener(mouseListener); // add the custom EventListener to the NEWT component
new AWTMouseAdapter(glWindow).addTo(comp); // forward all AWT events to glWindow, as NEWT events
*
*
* @see #attachTo
*/
public abstract class AWTAdapter implements java.util.EventListener
{
public static final boolean DEBUG_IMPLEMENTATION = Debug.debug("Window");
com.jogamp.newt.event.NEWTEventListener newtListener;
com.jogamp.newt.Window newtWindow;
/**
* Simply wrap aroung a NEWT EventListener, exposed as an AWT EventListener.
* The NEWT EventListener will be called when an event happens.
*/
public AWTAdapter(com.jogamp.newt.event.NEWTEventListener newtListener) {
if(null==newtListener) {
throw new RuntimeException("Argument newtListener is null");
}
this.newtListener = newtListener;
this.newtWindow = null;
}
/**
* Wrap aroung a NEWT EventListener, exposed as an AWT EventListener,
* where the given NEWT Window impersonates as the event's source.
* The NEWT EventListener will be called when an event happens.
*/
public AWTAdapter(com.jogamp.newt.event.NEWTEventListener newtListener, com.jogamp.newt.Window newtProxy) {
if(null==newtListener) {
throw new RuntimeException("Argument newtListener is null");
}
if(null==newtProxy) {
throw new RuntimeException("Argument newtProxy is null");
}
this.newtListener = newtListener;
this.newtWindow = newtProxy;
}
/**
* Create a pipeline adapter, AWT EventListener.
* Once attached to an AWT component, it sends the converted AWT events to the NEWT downstream window.
* This is only supported with EDT enabled!
*/
public AWTAdapter(com.jogamp.newt.Window downstream) {
if(null==downstream) {
throw new RuntimeException("Argument downstream is null");
}
this.newtListener = null;
this.newtWindow = downstream;
if( null == newtWindow.getScreen().getDisplay().getEDTUtil() ) {
throw new RuntimeException("EDT not enabled");
}
}
public final com.jogamp.newt.Window getNewtWindow() {
return newtWindow;
}
public final com.jogamp.newt.event.NEWTEventListener getNewtEventListener() {
return newtListener;
}
/**
* Due to the fact that some NEWT {@link com.jogamp.newt.event.NEWTEventListener}
* are mapped to more than one {@link java.util.EventListener},
* this method is for your convenience to use this Adapter as a listener for all types.
* E.g. {@link com.jogamp.newt.event.MouseListener} is mapped to {@link java.awt.event.MouseListener} and {@link java.awt.event.MouseMotionListener}.
*/
public abstract AWTAdapter addTo(java.awt.Component awtComponent);
/** @see #addTo(java.awt.Component) */
public abstract AWTAdapter removeFrom(java.awt.Component awtComponent);
/**
* Enqueues the event to the {@link #getNewtWindow()} is not null.
*/
void enqueueEvent(boolean wait, com.jogamp.newt.event.NEWTEvent event) {
if( null != newtWindow ) {
newtWindow.enqueueEvent(wait, event);
}
}
}