public class SimplePopupMenuHandler extends ActionDataImpl implements PopupMenuHandler
A specialized implementation of a PopupMenuHandler
,
which can be used out of the box for creating not too complex, mostly static
popup menus.
An instance of this class is initialized with a collection defining the content of a context menu. This collection can contain the following elements:
FormAction
interface): Actions are directly added to the
context menu constructed by this object.SimplePopupMenuHandler
implementations: If another
SimplePopupMenuHandler
instance is encountered, a sub menu is
created, and then the handler is invoked to populate it. In order to provide
the properties required for a sub menu, this class
extends ActionDataImpl
. So things like the menu text or its icon can
be set as properties. There is also a method to initialize all properties
from another ActionData
object.One advantage of this class is that instances can be fully defined in builder scripts (using the facilities provided by the dependency injection framework). As long as the context menus are static (i.e. they always display the same menu items) no programming is required, but this class can be used directly and fully defined in the application's configuration scripts.
There are some hooks allowing subclasses to influence the menu construction process: Specific methods are called before items are added to the menu under construction. Derived classes can intercept here and for instance suppress actions under certain circumstances.
Implementation note: This class is intended to be used by the event handling system of the GUI framework, i.e. to be invoked on the event dispatch thread. It is not safe to call it on multiple concurrent threads.
Constructor and Description |
---|
SimplePopupMenuHandler(Collection<?> items)
Creates a new instance of
SimplePopupMenuHandler and
initializes it with the content of the menu. |
Modifier and Type | Method and Description |
---|---|
protected void |
addAction(PopupMenuBuilder builder,
ComponentBuilderData compData,
FormAction action)
Adds an action to the menu constructed by this handler.
|
protected void |
addMenuElement(PopupMenuBuilder builder,
ComponentBuilderData compData,
Object element)
Adds an element to the menu to be constructed.
|
protected void |
addSeparator(PopupMenuBuilder builder,
ComponentBuilderData compData)
Adds a separator to the menu constructed by this handler.
|
protected void |
addSubMenu(PopupMenuBuilder builder,
ComponentBuilderData compData,
SimplePopupMenuHandler subHandler)
Adds a sub menu to the menu constructed by this handler.
|
protected void |
checkMenuElement(Object element)
Checks whether the specified menu element can be processed by this
handler.
|
void |
checkMenuItems()
Tests whether all menu elements to be processed by this handler are
supported.
|
void |
constructPopup(PopupMenuBuilder builder,
ComponentBuilderData compData)
Constructs the menu.
|
Object |
getConstructedMenu()
Returns the last menu that was constructed by this handler.
|
List<?> |
getMenuItems()
Returns a list with the content of the menu to be constructed by this
handler.
|
getAccelerator, getIcon, getMnemonicKey, getName, getTask, getText, getToolTip, setAccelerator, setActionData, setIcon, setMnemonicKey, setName, setTask, setText, setToolTip
public SimplePopupMenuHandler(Collection<?> items)
SimplePopupMenuHandler
and
initializes it with the content of the menu. The passed in collection
must not be null and must contain only valid objects, i.e. (per
default)
FormAction
interface or
SimplePopupMenuHandler
objects.checkMenuItems()
method by hand.items
- a collection with the content of the menu to constructIllegalArgumentException
- if the collection is nullcheckMenuItems()
public List<?> getMenuItems()
public void checkMenuItems() throws FormActionException
checkMenuElement()
method on each. This
ensures that only valid elements are involved. Note that this method is
not automatically called by the constructor. This design was
chosen to support derived classes that extend the number of menu elements
that can be handled. Such classes have to override methods like
checkMenuElement()
, which therefore cannot be called from
the constructor. To ensure a fail-fast approach,
checkMenuItems()
should be called manually after an instance
was constructed. However, this is optional. Invalid data will also be
detected when the menu is constructed.FormActionException
- if invalid data is found in the list of
elements to be added to the menupublic void constructPopup(PopupMenuBuilder builder, ComponentBuilderData compData) throws FormActionException
addMenuElement()
is called, which will process the element.
If an element found in the collection cannot be handled, an exception is
thrown.constructPopup
in interface PopupMenuHandler
builder
- the menu buildercompData
- the component builder data objectFormActionException
- if an invalid element is found in the
elements collectionpublic Object getConstructedMenu()
constructPopup()
for gaining access to
the created menu object.constructPopup()
invocationprotected void checkMenuElement(Object element) throws FormActionException
checkMenuItems()
. It
checks for FormAction
and
SimplePopupMenuHandler
objects. For all other objects an
exception is thrown.element
- the element to checkFormActionException
- if the element is not supportedprotected void addMenuElement(PopupMenuBuilder builder, ComponentBuilderData compData, Object element) throws FormActionException
constructPopup()
for all elements found in the collection
defining the menu. This base implementation checks for
FormAction
and SimplePopupMenuHandler
objects
and delegates to the corresponding specific add
methods.builder
- the menu buildercompData
- the component builder data objectelement
- the element to be addedFormActionException
- if the element cannot be handledprotected void addAction(PopupMenuBuilder builder, ComponentBuilderData compData, FormAction action) throws FormActionException
addMenuElement()
when an action is encountered.builder
- the menu buildercompData
- the component builder data objectaction
- the action to be addedFormActionException
- if an error occursprotected void addSubMenu(PopupMenuBuilder builder, ComponentBuilderData compData, SimplePopupMenuHandler subHandler) throws FormActionException
addMenuElement()
when another
SimplePopupMenuHandler
is encountered representing a sub
menu.builder
- the menu buildercompData
- the component builder data objectsubHandler
- the handler representing the sub menuFormActionException
- if an error occursprotected void addSeparator(PopupMenuBuilder builder, ComponentBuilderData compData) throws FormActionException
addMenuElement()
when a null element is
encountered in the elements collection.builder
- the menu buildercompData
- the component builder data objectFormActionException
- if an error occursCopyright © 2016 The JGUIraffe Team. All rights reserved.