From 5c6c11abf643013976ecbc0df463a923a1f52696 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 18 Nov 2013 13:01:12 +0100 Subject: NEWT AWTAdapter: Add notion of consuming the AWT InputEvent (will be used for key events); Allow AWTAdapter to be lazily setup w/ downstream object. --- .../com/jogamp/newt/event/awt/AWTAdapter.java | 42 +++++++++++++-- .../com/jogamp/newt/event/awt/AWTKeyAdapter.java | 23 ++++++-- .../com/jogamp/newt/event/awt/AWTMouseAdapter.java | 48 +++++++++++++---- .../jogamp/newt/event/awt/AWTWindowAdapter.java | 63 ++++++++++++---------- 4 files changed, 127 insertions(+), 49 deletions(-) (limited to 'src/newt/classes/com') diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java index e3bf18448..977b38dd0 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTAdapter.java @@ -117,17 +117,19 @@ public abstract class AWTAdapter implements java.util.EventListener com.jogamp.newt.event.NEWTEventListener newtListener; com.jogamp.newt.Window newtWindow; + boolean consumeAWTEvent; /** * 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) { + protected AWTAdapter(com.jogamp.newt.event.NEWTEventListener newtListener) { if(null==newtListener) { throw new RuntimeException("Argument newtListener is null"); } this.newtListener = newtListener; this.newtWindow = null; + this.consumeAWTEvent = false; } /** @@ -135,7 +137,7 @@ public abstract class AWTAdapter implements java.util.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) { + protected AWTAdapter(com.jogamp.newt.event.NEWTEventListener newtListener, com.jogamp.newt.Window newtProxy) { if(null==newtListener) { throw new RuntimeException("Argument newtListener is null"); } @@ -144,6 +146,7 @@ public abstract class AWTAdapter implements java.util.EventListener } this.newtListener = newtListener; this.newtWindow = newtProxy; + this.consumeAWTEvent = false; } /** @@ -151,22 +154,51 @@ public abstract class AWTAdapter implements java.util.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) { + protected AWTAdapter(com.jogamp.newt.Window downstream) { + this(); + setDownstream(downstream); + } + + public AWTAdapter() { + this.newtListener = null; + this.newtWindow = null; + this.consumeAWTEvent = false; + } + + /** + * Setup 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 synchronized AWTAdapter setDownstream(com.jogamp.newt.Window downstream) { if(null==downstream) { throw new RuntimeException("Argument downstream is null"); } this.newtListener = null; this.newtWindow = downstream; + this.consumeAWTEvent = false; if( null == newtWindow.getScreen().getDisplay().getEDTUtil() ) { throw new RuntimeException("EDT not enabled"); } + return this; + } + + /** Removes all references, downstream and NEWT-EventListener. */ + public synchronized AWTAdapter clear() { + this.newtListener = null; + this.newtWindow = null; + return this; + } + + public final synchronized void setConsumeAWTEvent(boolean v) { + this.consumeAWTEvent = v; } - public final com.jogamp.newt.Window getNewtWindow() { + public final synchronized com.jogamp.newt.Window getNewtWindow() { return newtWindow; } - public final com.jogamp.newt.event.NEWTEventListener getNewtEventListener() { + public final synchronized com.jogamp.newt.event.NEWTEventListener getNewtEventListener() { return newtListener; } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java index f6f11b81f..e8545aa52 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTKeyAdapter.java @@ -49,21 +49,28 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe super(downstream); } + public AWTKeyAdapter() { + super(); + } + @Override - public AWTAdapter addTo(java.awt.Component awtComponent) { + public synchronized AWTAdapter addTo(java.awt.Component awtComponent) { awtComponent.addKeyListener(this); return this; } @Override - public AWTAdapter removeFrom(java.awt.Component awtComponent) { + public synchronized AWTAdapter removeFrom(java.awt.Component awtComponent) { awtComponent.removeKeyListener(this); return this; } @Override - public void keyPressed(java.awt.event.KeyEvent e) { + public synchronized void keyPressed(java.awt.event.KeyEvent e) { final com.jogamp.newt.event.KeyEvent event = AWTNewtEventFactory.createKeyEvent(com.jogamp.newt.event.KeyEvent.EVENT_KEY_PRESSED, e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.KeyListener)newtListener).keyPressed(event); } else { @@ -72,8 +79,11 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe } @Override - public void keyReleased(java.awt.event.KeyEvent e) { + public synchronized void keyReleased(java.awt.event.KeyEvent e) { final com.jogamp.newt.event.KeyEvent event = AWTNewtEventFactory.createKeyEvent(com.jogamp.newt.event.KeyEvent.EVENT_KEY_RELEASED, e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.KeyListener)newtListener).keyReleased(event); } else { @@ -82,7 +92,10 @@ public class AWTKeyAdapter extends AWTAdapter implements java.awt.event.KeyListe } @Override - public void keyTyped(java.awt.event.KeyEvent e) { + public synchronized void keyTyped(java.awt.event.KeyEvent e) { + if( consumeAWTEvent ) { + e.consume(); + } } } diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java index 73a02c9fc..365bc4e2a 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTMouseAdapter.java @@ -46,8 +46,12 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL super(downstream); } + public AWTMouseAdapter() { + super(); + } + @Override - public AWTAdapter addTo(java.awt.Component awtComponent) { + public synchronized AWTAdapter addTo(java.awt.Component awtComponent) { awtComponent.addMouseListener(this); awtComponent.addMouseMotionListener(this); awtComponent.addMouseWheelListener(this); @@ -55,7 +59,7 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public AWTAdapter removeFrom(java.awt.Component awtComponent) { + public synchronized AWTAdapter removeFrom(java.awt.Component awtComponent) { awtComponent.removeMouseListener(this); awtComponent.removeMouseMotionListener(this); awtComponent.removeMouseWheelListener(this); @@ -63,8 +67,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseClicked(java.awt.event.MouseEvent e) { + public synchronized void mouseClicked(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseClicked(event); } else { @@ -73,8 +80,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseEntered(java.awt.event.MouseEvent e) { + public synchronized void mouseEntered(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseEntered(event); } else { @@ -83,8 +93,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseExited(java.awt.event.MouseEvent e) { + public synchronized void mouseExited(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseExited(event); } else { @@ -93,8 +106,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mousePressed(java.awt.event.MouseEvent e) { + public synchronized void mousePressed(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mousePressed(event); } else { @@ -103,8 +119,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseReleased(java.awt.event.MouseEvent e) { + public synchronized void mouseReleased(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseReleased(event); } else { @@ -113,8 +132,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseDragged(java.awt.event.MouseEvent e) { + public synchronized void mouseDragged(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseDragged(event); } else { @@ -123,8 +145,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseMoved(java.awt.event.MouseEvent e) { + public synchronized void mouseMoved(java.awt.event.MouseEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseMoved(event); } else { @@ -133,8 +158,11 @@ public class AWTMouseAdapter extends AWTAdapter implements java.awt.event.MouseL } @Override - public void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { + public synchronized void mouseWheelMoved(java.awt.event.MouseWheelEvent e) { com.jogamp.newt.event.MouseEvent event = AWTNewtEventFactory.createMouseEvent(e, newtWindow); + if( consumeAWTEvent ) { + e.consume(); + } if(null!=newtListener) { ((com.jogamp.newt.event.MouseListener)newtListener).mouseWheelMoved(event); } else { diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java index 8a9a2a49f..57821fcb1 100644 --- a/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTWindowAdapter.java @@ -49,16 +49,17 @@ public class AWTWindowAdapter public AWTWindowAdapter(com.jogamp.newt.Window downstream) { super(downstream); } + public AWTWindowAdapter() { + super(); + } @Override - public AWTAdapter addTo(java.awt.Component awtComponent) { + public synchronized AWTAdapter addTo(java.awt.Component awtComponent) { java.awt.Window win = getWindow(awtComponent); awtComponent.addComponentListener(this); awtComponent.addFocusListener(this); - if( null == windowClosingListener ) { + if( null != win && null == windowClosingListener ) { windowClosingListener = new WindowClosingListener(); - } - if( null != win ) { win.addWindowListener(windowClosingListener); } if(awtComponent instanceof java.awt.Window) { @@ -67,7 +68,7 @@ public class AWTWindowAdapter return this; } - public AWTAdapter removeWindowClosingFrom(java.awt.Component awtComponent) { + public synchronized AWTAdapter removeWindowClosingFrom(java.awt.Component awtComponent) { java.awt.Window win = getWindow(awtComponent); if( null != win && null != windowClosingListener ) { win.removeWindowListener(windowClosingListener); @@ -76,7 +77,7 @@ public class AWTWindowAdapter } @Override - public AWTAdapter removeFrom(java.awt.Component awtComponent) { + public synchronized AWTAdapter removeFrom(java.awt.Component awtComponent) { awtComponent.removeFocusListener(this); awtComponent.removeComponentListener(this); removeWindowClosingFrom(awtComponent); @@ -97,7 +98,7 @@ public class AWTWindowAdapter } @Override - public void focusGained(java.awt.event.FocusEvent e) { + public synchronized void focusGained(java.awt.event.FocusEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { System.err.println("AWT: focusGained: "+e+" -> "+event); @@ -110,7 +111,7 @@ public class AWTWindowAdapter } @Override - public void focusLost(java.awt.event.FocusEvent e) { + public synchronized void focusLost(java.awt.event.FocusEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { System.err.println("AWT: focusLost: "+e+" -> "+event); @@ -123,7 +124,7 @@ public class AWTWindowAdapter } @Override - public void componentResized(java.awt.event.ComponentEvent e) { + public synchronized void componentResized(java.awt.event.ComponentEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { final java.awt.Component c = e.getComponent(); @@ -148,7 +149,7 @@ public class AWTWindowAdapter } @Override - public void componentMoved(java.awt.event.ComponentEvent e) { + public synchronized void componentMoved(java.awt.event.ComponentEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(DEBUG_IMPLEMENTATION) { System.err.println("AWT: componentMoved: "+e+" -> "+event); @@ -161,7 +162,7 @@ public class AWTWindowAdapter } @Override - public void componentShown(java.awt.event.ComponentEvent e) { + public synchronized void componentShown(java.awt.event.ComponentEvent e) { final java.awt.Component comp = e.getComponent(); if(DEBUG_IMPLEMENTATION) { System.err.println("AWT: componentShown: "+comp); @@ -179,7 +180,7 @@ public class AWTWindowAdapter } @Override - public void componentHidden(java.awt.event.ComponentEvent e) { + public synchronized void componentHidden(java.awt.event.ComponentEvent e) { final java.awt.Component comp = e.getComponent(); if(DEBUG_IMPLEMENTATION) { System.err.println("AWT: componentHidden: "+comp); @@ -197,7 +198,7 @@ public class AWTWindowAdapter } @Override - public void windowActivated(java.awt.event.WindowEvent e) { + public synchronized void windowActivated(java.awt.event.WindowEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowGainedFocus(event); @@ -207,13 +208,13 @@ public class AWTWindowAdapter } @Override - public void windowClosed(java.awt.event.WindowEvent e) { } + public synchronized void windowClosed(java.awt.event.WindowEvent e) { } @Override - public void windowClosing(java.awt.event.WindowEvent e) { } + public synchronized void windowClosing(java.awt.event.WindowEvent e) { } @Override - public void windowDeactivated(java.awt.event.WindowEvent e) { + public synchronized void windowDeactivated(java.awt.event.WindowEvent e) { com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); if(null!=newtListener) { ((com.jogamp.newt.event.WindowListener)newtListener).windowLostFocus(event); @@ -223,31 +224,35 @@ public class AWTWindowAdapter } @Override - public void windowDeiconified(java.awt.event.WindowEvent e) { } + public synchronized void windowDeiconified(java.awt.event.WindowEvent e) { } @Override - public void windowIconified(java.awt.event.WindowEvent e) { } + public synchronized void windowIconified(java.awt.event.WindowEvent e) { } @Override - public void windowOpened(java.awt.event.WindowEvent e) { } + public synchronized void windowOpened(java.awt.event.WindowEvent e) { } class WindowClosingListener implements java.awt.event.WindowListener { @Override public void windowClosing(java.awt.event.WindowEvent e) { - com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); - if(null!=newtListener) { - ((com.jogamp.newt.event.WindowListener)newtListener).windowDestroyNotify(event); - } else { - enqueueEvent(true, event); + synchronized( AWTWindowAdapter.this ) { + com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(null!=newtListener) { + ((com.jogamp.newt.event.WindowListener)newtListener).windowDestroyNotify(event); + } else { + enqueueEvent(true, event); + } } } @Override public void windowClosed(java.awt.event.WindowEvent e) { - com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); - if(null!=newtListener) { - ((com.jogamp.newt.event.WindowListener)newtListener).windowDestroyed(event); - } else { - enqueueEvent(true, event); + synchronized( AWTWindowAdapter.this ) { + com.jogamp.newt.event.WindowEvent event = AWTNewtEventFactory.createWindowEvent(e, newtWindow); + if(null!=newtListener) { + ((com.jogamp.newt.event.WindowListener)newtListener).windowDestroyed(event); + } else { + enqueueEvent(true, event); + } } } -- cgit v1.2.3