NativePreviewEvent class
Represents a preview of a native {@link Event}.
class NativePreviewEvent extends DwtEvent implements HasNativeEvent {
/**
* Handler type.
*/
static EventType<NativePreviewHandler> TYPE = new EventType<NativePreviewHandler>();
/**
* The singleton instance of {@link NativePreviewEvent}.
*/
static NativePreviewEvent singleton;
/**
* Fire a {@link NativePreviewEvent} for the native event.
*
* @param handlers the {@link HandlerManager}
* @param nativeEvent the native event
* @return true to fire the event normally, false to cancel the event
*/
static bool fire(EventBus handlers, dart_html.Event nativeEvent) {
if (TYPE != null && handlers != null) { // && handlers.isEventHandled(TYPE)) {
// Cache the current values in the singleton in case we are in the
// middle of handling another event.
bool lastIsCanceled = singleton._isCanceled;
bool lastIsConsumed = singleton._isConsumed;
bool lastIsFirstHandler = singleton._isFirstHandler;
dart_html.Event lastNativeEvent = singleton._nativeEvent;
// Revive the event
singleton.revive();
singleton.setNativeEvent(nativeEvent);
// Fire the event
handlers.fireEvent(singleton);
bool ret = !(singleton.isCanceled() && !singleton.isConsumed());
// Restore the state of the singleton.
singleton._isCanceled = lastIsCanceled;
singleton._isConsumed = lastIsConsumed;
singleton._isFirstHandler = lastIsFirstHandler;
singleton._nativeEvent = lastNativeEvent;
return ret;
}
return true;
}
/**
* A bool indicating that the native event should be canceled.
*/
bool _isCanceled = false;
/**
* A bool indicating whether or not canceling the native event should be
* prevented. This supercedes {@link #isCanceled}.
*/
bool _isConsumed = false;
/**
* A bool indicating that the current handler is at the top of the event
* preview stack.
*/
bool _isFirstHandler = false;
/**
* The event being previewed.
*/
dart_html.Event _nativeEvent;
/**
* Cancel the native event and prevent it from firing. Note that the event
* can still fire if another handler calls {@link #consume()}.
*
* Classes overriding this method should still call super.cancel().
*/
void cancel() {
_isCanceled = true;
}
/**
* Consume the native event and prevent it from being canceled, even if it
* has already been canceled by another handler.
* {@link NativePreviewHandler} that fire first have priority over later
* handlers, so all handlers should check if the event has already been
* canceled before calling this method.
*/
void consume() {
_isConsumed = true;
}
EventType<NativePreviewHandler> getAssociatedType() {
return TYPE;
}
dart_html.Event getNativeEvent() {
return _nativeEvent;
}
// /**
// * Gets the type int corresponding to the native event that triggered this
// * preview.
// *
// * @return the type int associated with this native event
// */
// int getTypeInt() {
// return Event.as(getNativeEvent()).getTypeInt();
// }
/**
* Has the event already been canceled? Note that {@link #isConsumed()} will
* still return true if the native event has also been consumed.
*
* @return true if the event has been canceled
* @see #cancel()
*/
bool isCanceled() {
return _isCanceled;
}
/**
* Has the native event been consumed? Note that {@link #isCanceled()} will
* still return true if the native event has also been canceled.
*
* @return true if the event has been consumed
* @see #consume()
*/
bool isConsumed() {
return _isConsumed;
}
/**
* Is the current handler the first to preview this event?
*
* @return true if the current handler is the first to preview the event
*/
bool isFirstHandler() {
return _isFirstHandler;
}
void dispatch(NativePreviewHandler handler) {
handler.onPreviewNativeEvent(this);
singleton._isFirstHandler = false;
}
void revive() {
super.revive();
_isCanceled = false;
_isConsumed = false;
_isFirstHandler = true;
_nativeEvent = null;
}
/**
* Set the native event.
*
* @param nativeEvent the native {@link Event} being previewed.
*/
void setNativeEvent(dart_html.Event nativeEvent) {
this._nativeEvent = nativeEvent;
}
}
Extends
IEvent<H> > DwtEvent > NativePreviewEvent
Implements
Static Properties
NativePreviewEvent singleton #
The singleton instance of {@link NativePreviewEvent}.
static NativePreviewEvent singleton
EventType<NativePreviewHandler> TYPE #
Handler type.
static EventType<NativePreviewHandler> TYPE = new EventType<NativePreviewHandler>()
Static Methods
bool fire(EventBus handlers, Event nativeEvent) #
Fire a {@link NativePreviewEvent} for the native event.
@param handlers the {@link HandlerManager} @param nativeEvent the native event @return true to fire the event normally, false to cancel the event
static bool fire(EventBus handlers, dart_html.Event nativeEvent) {
if (TYPE != null && handlers != null) { // && handlers.isEventHandled(TYPE)) {
// Cache the current values in the singleton in case we are in the
// middle of handling another event.
bool lastIsCanceled = singleton._isCanceled;
bool lastIsConsumed = singleton._isConsumed;
bool lastIsFirstHandler = singleton._isFirstHandler;
dart_html.Event lastNativeEvent = singleton._nativeEvent;
// Revive the event
singleton.revive();
singleton.setNativeEvent(nativeEvent);
// Fire the event
handlers.fireEvent(singleton);
bool ret = !(singleton.isCanceled() && !singleton.isConsumed());
// Restore the state of the singleton.
singleton._isCanceled = lastIsCanceled;
singleton._isConsumed = lastIsConsumed;
singleton._isFirstHandler = lastIsFirstHandler;
singleton._nativeEvent = lastNativeEvent;
return ret;
}
return true;
}
Methods
void assertLive() #
Asserts that the event still should be accessed. All events are considered to be "dead" after their original handler manager finishes firing them. An event can be revived by calling {@link GwtEvent#revive()}.
void assertLive() {
assert (!_dead) ; //: "This event has already finished being processed by its original handler manager, so you can no longer access it";
}
void cancel() #
Cancel the native event and prevent it from firing. Note that the event can still fire if another handler calls {@link #consume()}.
Classes overriding this method should still call super.cancel().
void cancel() {
_isCanceled = true;
}
void consume() #
Consume the native event and prevent it from being canceled, even if it has already been canceled by another handler. {@link NativePreviewHandler} that fire first have priority over later handlers, so all handlers should check if the event has already been canceled before calling this method.
void consume() {
_isConsumed = true;
}
void dispatch(NativePreviewHandler handler) #
Implemented by subclasses to invoke their handlers in a type safe
manner. Intended to be called by EventBus#fireEvent(Event) or
EventBus#fireEventFromSource(Event, Object).
@param handler handler @see EventBus#dispatchEvent(Event, Object)
void dispatch(NativePreviewHandler handler) {
handler.onPreviewNativeEvent(this);
singleton._isFirstHandler = false;
}
EventType<NativePreviewHandler> getAssociatedType() #
Event getNativeEvent() #
dart_html.Event getNativeEvent() {
return _nativeEvent;
}
Object getSource() #
Returns the source for this event. The type and meaning of the source is
arbitrary, and is most useful as a secondary key for handler registration.
(See EventBus#addHandlerToSource, which allows a handler to
register for events of a particular type, tied to a particular source.)
Note that the source is actually set at dispatch time, e.g. via
EventBus#fireEventFromSource(Event, Object).
@return object representing the source of this event
Object getSource() {
assertLive();
return super.getSource();
}
bool isCanceled() #
Has the event already been canceled? Note that {@link #isConsumed()} will still return true if the native event has also been consumed.
@return true if the event has been canceled @see #cancel()
bool isCanceled() {
return _isCanceled;
}
bool isConsumed() #
Has the native event been consumed? Note that {@link #isCanceled()} will still return true if the native event has also been canceled.
@return true if the event has been consumed @see #consume()
bool isConsumed() {
return _isConsumed;
}
bool isFirstHandler() #
Is the current handler the first to preview this event?
@return true if the current handler is the first to preview the event
bool isFirstHandler() {
return _isFirstHandler;
}
bool isLive() #
Is the event current live?
@return whether the event is live
bool isLive() {
return !_dead;
}
void kill() #
Kill the event. After the event has been killed, users cannot really on its values or functions being available.
void kill() {
_dead = true;
setSource(null);
}
void overrideSource(Object source) #
void overrideSource(Object source) {
super.setSource(source);
}
void revive() #
Revives the event. Used when recycling event instances.
void revive() {
super.revive();
_isCanceled = false;
_isConsumed = false;
_isFirstHandler = true;
_nativeEvent = null;
}
void setNativeEvent(Event nativeEvent) #
Set the native event.
@param nativeEvent the native {@link Event} being previewed.
void setNativeEvent(dart_html.Event nativeEvent) {
this._nativeEvent = nativeEvent;
}
void setSource(source) #
Set the source that triggered this event. Intended to be called by the EventBus during dispatch.
@param source the source of this event. @see EventBus#fireEventFromSource(Event, Object) @see EventBus#setSourceOfEvent(Event, Object)
void setSource(dynamic source) {
this._source = source;
}