From 98f6f99ddc6643cfa540d6c85c64c7f8510cc1ea Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 21 Mar 2013 17:38:26 +0100 Subject: NEWTEvent: Add isConsumed() and setConsumed(boolean) methods to simply usage, using the existing consumedTag attachment for compatibility and efficiency. --- .../classes/com/jogamp/newt/awt/NewtCanvasAWT.java | 5 ++- .../classes/com/jogamp/newt/event/NEWTEvent.java | 41 +++++++++++++++++++--- src/newt/classes/jogamp/newt/WindowImpl.java | 10 ++---- 3 files changed, 42 insertions(+), 14 deletions(-) (limited to 'src/newt') diff --git a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java index a3c2a5dc8..d22e50cb8 100644 --- a/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java +++ b/src/newt/classes/com/jogamp/newt/awt/NewtCanvasAWT.java @@ -60,7 +60,6 @@ import com.jogamp.newt.Display; import com.jogamp.newt.Window; import com.jogamp.newt.event.KeyEvent; import com.jogamp.newt.event.KeyListener; -import com.jogamp.newt.event.NEWTEvent; import com.jogamp.newt.event.WindowAdapter; import com.jogamp.newt.event.WindowEvent; import com.jogamp.newt.event.WindowListener; @@ -214,7 +213,7 @@ public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProto } public void keyTyped(KeyEvent e) { if(suppress) { - e.setAttachment(NEWTEvent.consumedTag); + e.setConsumed(true); suppress = false; // reset } } @@ -244,7 +243,7 @@ public class NewtCanvasAWT extends java.awt.Canvas implements WindowClosingProto } } if(suppress) { - evt.setAttachment(NEWTEvent.consumedTag); + evt.setConsumed(true); } if(DEBUG) { System.err.println("NewtCanvasAWT.focusKey: XXX: "+ks); diff --git a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java index 17210cef8..67bc73652 100644 --- a/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java +++ b/src/newt/classes/com/jogamp/newt/event/NEWTEvent.java @@ -49,8 +49,7 @@ package com.jogamp.newt.event; @SuppressWarnings("serial") public class NEWTEvent extends java.util.EventObject { /** - * Object when attached via {@link #setAttachment(Object)} marks the event consumed, - * ie. stops propagating the event any further to the other event listener. + * See {@link #setConsumed(boolean)} for description. */ public static final Object consumedTag = new Object(); @@ -86,7 +85,7 @@ public class NEWTEvent extends java.util.EventObject { * @param attachment User application specific object */ public final void setAttachment(Object attachment) { - this.attachment=attachment; + this.attachment = attachment; } /** @@ -95,7 +94,41 @@ public class NEWTEvent extends java.util.EventObject { public final Object getAttachment() { return attachment; } - + + /** + * Returns true if this events has been {@link #setConsumed(boolean) consumed}, + * otherwise false. + * @see #setConsumed(boolean) + */ + public final boolean isConsumed() { + return consumedTag == attachment; + } + + /** + * If consumed is true, this event is marked as consumed, + * ie. the event will not be propagated any further to potential other event listener. + * Otherwise the event will be propagated to other event listener, the default. + *

+ * The event is marked as being consumed while {@link #setAttachment(Object) attaching} + * the {@link #consumedTag}. + *

+ *

+ * Events with platform specific actions will be supressed if marked as consumed. + * Examples are: + *

+ *

+ */ + public final void setConsumed(boolean consumed) { + if( consumed ) { + setAttachment( consumedTag ); + } else if( consumedTag == attachment ) { + setAttachment( null ); + } + } + public String toString() { return toString(null).toString(); } diff --git a/src/newt/classes/jogamp/newt/WindowImpl.java b/src/newt/classes/jogamp/newt/WindowImpl.java index 45100421a..e8dc0d7d7 100644 --- a/src/newt/classes/jogamp/newt/WindowImpl.java +++ b/src/newt/classes/jogamp/newt/WindowImpl.java @@ -2194,8 +2194,7 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer if(DEBUG_MOUSE_EVENT) { System.err.println("consumeMouseEvent: event: "+e); } - boolean consumed = false; - for(int i = 0; !consumed && i < mouseListeners.size(); i++ ) { + for(int i = 0; !e.isConsumed() && i < mouseListeners.size(); i++ ) { MouseListener l = mouseListeners.get(i); switch(e.getEventType()) { case MouseEvent.EVENT_MOUSE_CLICKED: @@ -2225,7 +2224,6 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer default: throw new NativeWindowException("Unexpected mouse event type " + e.getEventType()); } - consumed = NEWTEvent.consumedTag == e.getAttachment(); } } @@ -2354,7 +2352,7 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer default: throw new NativeWindowException("Unexpected key event type " + e.getEventType()); } - return NEWTEvent.consumedTag == e.getAttachment(); + return e.isConsumed(); } @SuppressWarnings("deprecation") @@ -2460,8 +2458,7 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer if(DEBUG_IMPLEMENTATION) { System.err.println("consumeWindowEvent: "+e+", visible "+isVisible()+" "+getX()+"/"+getY()+" "+getWidth()+"x"+getHeight()); } - boolean consumed = false; - for(int i = 0; !consumed && i < windowListeners.size(); i++ ) { + for(int i = 0; !e.isConsumed() && i < windowListeners.size(); i++ ) { WindowListener l = windowListeners.get(i); switch(e.getEventType()) { case WindowEvent.EVENT_WINDOW_RESIZED: @@ -2490,7 +2487,6 @@ public abstract class WindowImpl implements Window, NEWTEventConsumer new NativeWindowException("Unexpected window event type " + e.getEventType()); } - consumed = NEWTEvent.consumedTag == e.getAttachment(); } } -- cgit v1.2.3