JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
NEWTEvent.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 */
34
35package com.jogamp.newt.event;
36
37/**
38 * NEWT events are provided for notification purposes ONLY;<br>
39 * The NEWT will automatically handle the event semantics internally, regardless of whether a program is receiving these events or not.<br>
40 * The actual event semantic is processed before the event is send.<br>
41 *
42 * Event type registry:<br>
43 * <ul>
44 * <li> WindowEvent <code>100..10x</code></li>
45 * <li> MouseEvent <code>200..20x</code></li>
46 * <li> KeyEvent <code>300..30x</code></li>
47 * <li> GestureEvent <code>400..5xx</code></li>
48 * <li> MonitorEvent <code>600..60x</code></li>
49 * </ul><br>
50 */
51@SuppressWarnings("serial")
52public class NEWTEvent extends java.util.EventObject {
53 /**
54 * See {@link #setConsumed(boolean)} for description.
55 */
56 public static final Object consumedTag = new Object();
57
58 private final short eventType;
59 private final long when;
60 private Object attachment;
61
62 static final boolean DEBUG = false;
63
64 protected NEWTEvent(final short eventType, final Object source, final long when) {
65 super(source);
66 this.eventType = eventType;
67 this.when = when;
68 this.attachment=null;
69 }
70
71 /** Returns the event type of this event. */
72 public final short getEventType() {
73 return eventType;
74 }
75
76 /** Returns the timestamp, in milliseconds, of this event. */
77 public final long getWhen() {
78 return when;
79 }
80
81 /**
82 * Attach the passed object to this event.<br>
83 * If an object was previously attached, it will be replaced.<br>
84 * Attachments to NEWT events allow users to pass on information
85 * from one custom listener to another, ie custom listener to listener
86 * communication.
87 * @param attachment User application specific object
88 */
89 public final void setAttachment(final Object attachment) {
90 this.attachment = attachment;
91 }
92
93 /**
94 * @return The user application specific attachment, or null
95 */
96 public final Object getAttachment() {
97 return attachment;
98 }
99
100 /**
101 * Returns <code>true</code> if this events has been {@link #setConsumed(boolean) consumed},
102 * otherwise <code>false</code>.
103 * @see #setConsumed(boolean)
104 */
105 public final boolean isConsumed() {
106 return consumedTag == attachment;
107 }
108
109 /**
110 * If <code>consumed</code> is <code>true</code>, this event is marked as consumed,
111 * ie. the event will not be propagated any further to potential <i>other</i> event listener.
112 * Otherwise the event will be propagated to other event listener, the default.
113 * <p>
114 * The event is marked as being consumed while {@link #setAttachment(Object) attaching}
115 * the {@link #consumedTag}.
116 * </p>
117 * <p>
118 * Events with platform specific actions will be supressed if marked as consumed.
119 * Examples are:
120 * <ul>
121 * <li>{@link KeyEvent#VK_ESCAPE} on Android's BACK button w/ Activity::finish()</li>
122 * <li>{@link KeyEvent#VK_HOME} on Android's HOME button w/ Intend.ACTION_MAIN[Intend.CATEGORY_HOME]</li>
123 * </ul>
124 * </p>
125 */
126 public final void setConsumed(final boolean consumed) {
127 if( consumed ) {
128 setAttachment( consumedTag );
129 } else if( consumedTag == attachment ) {
130 setAttachment( null );
131 }
132 }
133
134 @Override
135 public String toString() {
136 return toString(null).toString();
137 }
138
139 public StringBuilder toString(StringBuilder sb) {
140 if(null == sb) {
141 sb = new StringBuilder();
142 }
143 return sb.append("NEWTEvent[source:").append(getSource().getClass().getName()).append(", consumed ").append(isConsumed()).append(", when:").append(getWhen()).append(" d ").append((System.currentTimeMillis()-getWhen())).append("ms]");
144 }
145
146 public static String toHexString(final short hex) {
147 return "0x" + Integer.toHexString( hex & 0x0000FFFF );
148 }
149}
NEWT events are provided for notification purposes ONLY; The NEWT will automatically handle the even...
Definition: NEWTEvent.java:52
final boolean isConsumed()
Returns true if this events has been consumed, otherwise false.
Definition: NEWTEvent.java:105
final void setConsumed(final boolean consumed)
If consumed is true, this event is marked as consumed, ie.
Definition: NEWTEvent.java:126
StringBuilder toString(StringBuilder sb)
Definition: NEWTEvent.java:139
NEWTEvent(final short eventType, final Object source, final long when)
Definition: NEWTEvent.java:64
final short getEventType()
Returns the event type of this event.
Definition: NEWTEvent.java:72
static String toHexString(final short hex)
Definition: NEWTEvent.java:146
final long getWhen()
Returns the timestamp, in milliseconds, of this event.
Definition: NEWTEvent.java:77
final void setAttachment(final Object attachment)
Attach the passed object to this event.
Definition: NEWTEvent.java:89