javafx.concurrent.EventHelper Maven / Gradle / Ivy
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javafx.concurrent;
import com.sun.javafx.event.EventHandlerManager;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.*;
import static javafx.concurrent.WorkerStateEvent.*;
import static javafx.concurrent.WorkerStateEvent.WORKER_STATE_CANCELLED;
import static javafx.concurrent.WorkerStateEvent.WORKER_STATE_FAILED;
/**
* This utility class is used both to bucketize the event handler related
* methods and state in Service and Task, but also (and more importantly)
* to consolidate the implementation into one place.
*/
class EventHelper {
private final EventTarget target;
private final ObjectProperty> onReady;
final ObjectProperty> onReadyProperty() { return onReady; }
final EventHandler getOnReady() { return onReady.get(); }
final void setOnReady(EventHandler value) { onReady.set(value); }
private final ObjectProperty> onScheduled;
final ObjectProperty> onScheduledProperty() { return onScheduled; }
final EventHandler getOnScheduled() { return onScheduled.get(); }
final void setOnScheduled(EventHandler value) { onScheduled.set(value); }
private final ObjectProperty> onRunning;
final ObjectProperty> onRunningProperty() { return onRunning; }
final EventHandler getOnRunning() { return onRunning.get(); }
final void setOnRunning(EventHandler value) { onRunning.set(value); }
private final ObjectProperty> onSucceeded;
final ObjectProperty> onSucceededProperty() { return onSucceeded; }
final EventHandler getOnSucceeded() { return onSucceeded.get(); }
final void setOnSucceeded(EventHandler value) { onSucceeded.set(value); }
private final ObjectProperty> onCancelled;
final ObjectProperty> onCancelledProperty() { return onCancelled; }
final EventHandler getOnCancelled() { return onCancelled.get(); }
final void setOnCancelled(EventHandler value) { onCancelled.set(value); }
private final ObjectProperty> onFailed;
final ObjectProperty> onFailedProperty() { return onFailed; }
final EventHandler getOnFailed() { return onFailed.get(); }
final void setOnFailed(EventHandler value) { onFailed.set(value); }
private EventHandlerManager internalEventDispatcher;
EventHelper(EventTarget bean) {
this.target = bean;
onReady = new SimpleObjectProperty>(bean, "onReady") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_READY, handler);
}
};
onScheduled = new SimpleObjectProperty>(bean, "onScheduled") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_SCHEDULED, handler);
}
};
onRunning = new SimpleObjectProperty>(bean, "onRunning") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_RUNNING, handler);
}
};
onSucceeded = new SimpleObjectProperty>(bean, "onSucceeded") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_SUCCEEDED, handler);
}
};
onCancelled = new SimpleObjectProperty>(bean, "onCancelled") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_CANCELLED, handler);
}
};
onFailed = new SimpleObjectProperty>(bean, "onFailed") {
@Override protected void invalidated() {
EventHandler handler = get();
setEventHandler(WORKER_STATE_FAILED, handler);
}
};
}
/**
* Registers an event handler to this task. Any event filters are first
* processed, then the specified onFoo event handlers, and finally any
* event handlers registered by this method. As with other events
* in the scene graph, if an event is consumed, it will not continue
* dispatching.
*
* @param the specific event class of the handler
* @param eventType the type of the events to receive by the handler
* @param eventHandler the handler to register
*/
final void addEventHandler(
final EventType eventType,
final EventHandler super T> eventHandler) {
getInternalEventDispatcher()
.addEventHandler(eventType, eventHandler);
}
/**
* Unregisters a previously registered event handler from this task. One
* handler might have been registered for different event types, so the
* caller needs to specify the particular event type from which to
* unregister the handler.
*
* @param the specific event class of the handler
* @param eventType the event type from which to unregister
* @param eventHandler the handler to unregister
*/
final void removeEventHandler(
final EventType eventType,
final EventHandler super T> eventHandler) {
getInternalEventDispatcher()
.removeEventHandler(eventType, eventHandler);
}
/**
* Registers an event filter to this task. Registered event filters get
* an event before any associated event handlers.
*
* @param the specific event class of the filter
* @param eventType the type of the events to receive by the filter
* @param eventFilter the filter to register
*/
final void addEventFilter(
final EventType eventType,
final EventHandler super T> eventFilter) {
getInternalEventDispatcher()
.addEventFilter(eventType, eventFilter);
}
/**
* Unregisters a previously registered event filter from this task. One
* filter might have been registered for different event types, so the
* caller needs to specify the particular event type from which to
* unregister the filter.
*
* @param the specific event class of the filter
* @param eventType the event type from which to unregister
* @param eventFilter the filter to unregister
*/
final void removeEventFilter(
final EventType eventType,
final EventHandler super T> eventFilter) {
getInternalEventDispatcher()
.removeEventFilter(eventType, eventFilter);
}
/**
* Sets the handler to use for this event type. There can only be one such
* handler specified at a time. This handler is guaranteed to be called
* first. This is used for registering the user-defined onFoo event
* handlers.
*
* @param the specific event class of the handler
* @param eventType the event type to associate with the given eventHandler
* @param eventHandler the handler to register, or null to unregister
*/
final void setEventHandler(
final EventType eventType,
final EventHandler super T> eventHandler) {
getInternalEventDispatcher()
.setEventHandler(eventType, eventHandler);
}
private EventHandlerManager getInternalEventDispatcher() {
if (internalEventDispatcher == null) {
internalEventDispatcher = new EventHandlerManager(target);
}
return internalEventDispatcher;
}
/**
* Fires the specified event. Any event filter encountered will
* be notified and can consume the event. If not consumed by the filters,
* the event handlers on this task are notified. If these don't consume the
* event either, then all event handlers are called and can consume the
* event.
*
* This method must be called on the FX user thread.
*
* @param event the event to fire
*/
final void fireEvent(Event event) {
Event.fireEvent(target, event);
}
EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) {
return internalEventDispatcher == null ? tail : tail.append(getInternalEventDispatcher());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy