public abstract class EventServiceAction extends AbstractAction
EventService
.
This abstract class ties the Swing Actions with the Event Bus. When fired, an ActionEvent is published on an
EventService - either the global EventBus or a Container EventService.
There are two derivatives of this class: The EventBusAction, which publishes the ActionEvent on the global EventBus,
and the ContainerEventServiceAction, which publishes the ActionEvent on the a local ContainerEventService.
By default the ActionEvent is published on an EventService topic corresponding to this action's
Action.ACTION_COMMAND_KEY. Though this behavior is highly configurable. See getTopicName(ActionEvent)
and
setTopicName(String)
for ways to customize the topic used. Override getTopicValue(ActionEvent)
to
publish an object other than the ActionEvent.
Instead of publishing on a topic, the ActionEvent can be published using class-based publication, use setPublishesOnTopic(boolean)
to set this behavior. When using class-based publication, the ActionEvent is published
by default. Override getEventServiceEvent(ActionEvent)
to publish an object other than the ActionEvent.Modifier and Type | Field and Description |
---|---|
static String |
EVENT_BUS_EVENT_CLASS_NAME |
static String |
EVENT_SERVICE_TOPIC_NAME |
changeSupport, enabled
ACCELERATOR_KEY, ACTION_COMMAND_KEY, DEFAULT, DISPLAYED_MNEMONIC_INDEX_KEY, LARGE_ICON_KEY, LONG_DESCRIPTION, MNEMONIC_KEY, NAME, SELECTED_KEY, SHORT_DESCRIPTION, SMALL_ICON
Constructor and Description |
---|
EventServiceAction() |
EventServiceAction(String actionName,
ImageIcon icon) |
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent event)
Publishes the event on the EventService returned by getEventService(event)
Gets the EventService from
getEventService(java.awt.event.ActionEvent) . |
protected abstract EventService |
getEventService(ActionEvent event)
Override to return the EventService on which to publish.
|
protected Object |
getEventServiceEvent(ActionEvent event)
If isPublishesOnTopic() returns false (i.e., when using class-based rather than topic-based publication), then
override this method to publish an on object other than the ActionEvent.
|
Object |
getName() |
boolean |
getThrowsExceptionOnNullEventService()
By default, exceptions are throw if getEventService() returns null.
|
String |
getTopicName(ActionEvent event)
The topic name is the first non-null value out of: A topic name explicitly set via
setTopicName(String) the action's getValue("event-service-topic") EVENT_SERVICE_TOPIC_NAME the
action's getValue("ID") (for compatibility with the SAM ActionManager's ID) the action's Action.ACTION_COMMAND_KEY the action event's Action.ACTION_COMMAND_KEY
the action's Action.NAME the value is used (if the value is not a String, the value's
toString() is used). |
protected Object |
getTopicValue(ActionEvent event)
By default, the ActionEvent is the object published on the topic.
|
boolean |
isPublishesOnTopic() |
void |
setPublishesOnTopic(boolean onTopic)
Sets whether this action publishes on a topic or uses class-based publication.
|
void |
setThrowsExceptionOnNullEventService(boolean throwsExceptionOnNullEventService)
By default, exceptions are thrown if getEventService() returns null.
|
void |
setTopicName(String topicName)
Explicitly sets the topic name this action publishes on.
|
addPropertyChangeListener, clone, firePropertyChange, getKeys, getPropertyChangeListeners, getValue, isEnabled, putValue, removePropertyChangeListener, setEnabled
public static final String EVENT_SERVICE_TOPIC_NAME
public static final String EVENT_BUS_EVENT_CLASS_NAME
protected abstract EventService getEventService(ActionEvent event)
event
- the event passed to #execute(ActionEvent)EventBusAction
,
ContainerEventServiceAction
public boolean isPublishesOnTopic()
public void setPublishesOnTopic(boolean onTopic)
onTopic
- true if publishes on topic (the default), false if using class-based publication.public void setTopicName(String topicName)
getTopicName(ActionEvent)
to understand how the
topic name is determined implicitly.public String getTopicName(ActionEvent event)
setTopicName(String)
EVENT_SERVICE_TOPIC_NAME
Action.ACTION_COMMAND_KEY
Action.ACTION_COMMAND_KEY
Action.NAME
the value is used (if the value is not a String, the value's
toString() is used).
To use a different name, override this method.event
- the event passed to #execute(ActionEvent)protected Object getTopicValue(ActionEvent event)
event
- the event passed to #execute(ActionEvent)public Object getName()
protected Object getEventServiceEvent(ActionEvent event)
public void actionPerformed(ActionEvent event)
getEventService(java.awt.event.ActionEvent)
. Checks isPublishesOnTopic(). If true,
gets the topic name from getTopicName(java.awt.event.ActionEvent)
and the topic value from getTopicValue(ActionEvent)
, and publishes the value on the topic on the EventService. If false, gets event from
getEventServiceEvent(java.awt.event.ActionEvent)
, and publishes the event on the EventService.
event
- the action event to publish.RuntimeException
- if getThrowsExceptionOnNullEventService() && getEventService(event) == nullpublic boolean getThrowsExceptionOnNullEventService()
public void setThrowsExceptionOnNullEventService(boolean throwsExceptionOnNullEventService)
throwsExceptionOnNullEventService
- true to suppress the exception when there is no event serviceCopyright © 2013 Bushe Enterprises, Inc.. All rights reserved.