public class ComponentBuilderData extends Object implements Composite, SimpleBeanStoreImpl.BeanContributor
A class for storing temporary data and the results of a form builder operation.
For every call of a form builder an instance of this class is created. During the build process a couple of information is created. Some of this belongs to the final result, other parts need to be accessed later to resolve references or for different purposes. This class is a home for all these kinds of data.
Especially the components created during a builder operation must be stored
somewhere so that they can be combined to a resulting Form
object. For this purpose this class provides an implementation of the
ComponentStore
interface and registers it as the default
component store. To access the component store interested parties do not
directly invoke the methods provided by ComponentStore
, but
access them through the storeXXXX()
and getXXXX()
methods defined in this class. These methods obtain the current store and
delegate the call to it. During the builder operation a different
ComponentStore
can be temporarily set using the
pushComponentStore()
and popComponentStore()
methods. This makes it possible for complex components to catch all the
components created in their context; this way sub forms can be created that
for instance represent a row in a table. With the
pushFormContext()
and popFormContext()
methods a
complete new form context can be created, i.e. all components will be added
to the passed in form, and event registration can be performed on the
components that belong to that form.
To make the current form and all of its components available to the
dependency injection framework, this class implements the
SimpleBeanStoreImpl.BeanContributor
interface and can therefore
collaborate with a SimpleBeanStoreImpl
. There is also a
getBeanContext()
method that returns a context for querying all
available beans. Through this context the global beans (as defined by the
application) can be queried and the objects created during the builder
operation as well. To access the components and their associated handler
classes the following naming scheme is used:
ComponentManager
) can be accessed using the name that is
specified in the Jelly builder script. For instance (if
builderData
is an instance of ComponentBuilderData
)
builderData.getBeanContext().getBean("txtFirstName");
would
return the input component associated with the name txtFirstName
(probably a JTextField
if Swing is used).ComponentHandler
s for the managed
components the prefix comp::
builderData.getBeanContext().getBean("comp:txtFirstName");
would
return the ComponentHandler
for the txtFirstName
component.FieldHandler
s are also accessed using a special prefix:
field:. So for obtaining the FieldHandler
for the
txtFirstName field you would write
builderData.getBeanContext().getBean("field:txtFirstName");
.WidgetHandler
. For
obtaining a component's WidgetHandler
the prefix
widget: is used, as in
builderData.getBeanContext().getBean("widget:txtFirstName");
.Form
object constructed during the current builder
operation can also be accessed under a special reserved key:
Form form = (Form) builderData.getBeanContext().getBean("CURRENT_FORM");
.BeanContext
maintained by this instance can also be
accessed (e.g. to be injected into a bean defined by the current builder
script). This can be done using the key CURRENT_CONTEXT.BuilderData
object is available under the key BUILDER_DATA.ComponentBuilderData
instance itself is
exposed through the BeanStore
implementation. The corresponding
reserved key is named COMPONENT_BUILDER_DATA.The current instance of this class for the running builder process is stored in the Jelly Context, where it can be accessed from all tags. From here also references to the factories needed for creating components can be obtained. The root container object is maintained by this object, too.
Implementation note: This class is not thread-safe. If it is accessed concurrently by multiple threads, proper synchronization must be ensured.
Modifier and Type | Field and Description |
---|---|
static String |
KEY_BUILDER_DATA
Constant for the key for accessing the current
BuilderData
object from the bean context managed by this class. |
static String |
KEY_COMPHANDLER_PREFIX
Constant for the prefix for accessing component handlers.
|
static String |
KEY_COMPONENT_BUILDER_DATA
Constant for the key for accessing the current instance of this class
from the managed bean context.
|
static String |
KEY_CURRENT_CONTEXT
Constant for the key for accessing the current bean context.
|
static String |
KEY_FIELDHANDLER_PREFIX
Constant for the prefix for accessing field handlers.
|
static String |
KEY_FORM
Constant for the key for accessing the current form from the bean context
managed by this class.
|
static String |
KEY_WIDGETHANDLER_PREFIX
Constant for the prefix for accessing widget handlers.
|
Constructor and Description |
---|
ComponentBuilderData()
Creates a new instance of
ComponentBuilderData . |
Modifier and Type | Method and Description |
---|---|
void |
addCallBack(ComponentBuilderCallBack callBack,
Object param)
Registers the specified call back at this builder data object.
|
void |
addComponent(Object comp,
Object constraints)
Adds the specified component to the root container.
|
void |
addFormContextListener(FormContextListener listener)
Adds a
FormContextListener object to this data object. |
void |
beanNames(Set<String> names)
Returns a set with the names of all contained bean.
|
protected FormEventManager |
createEventManager()
Creates the event manager object.
|
protected PlatformEventManager |
createPlatformEventManager()
Creates the platform specific event manager.
|
protected ToolTipManager |
createToolTipManager()
Creates the
ToolTipManager . |
void |
disableCallBacks()
Disables the call back mechanism.
|
void |
enableCallBacks()
Enables the call back mechanism.
|
protected ComponentManager |
fetchComponentHandler()
Returns the associated component manager.
|
static ComponentBuilderData |
get(org.apache.commons.jelly.JellyContext ctx)
Returns the instance of this class stored in the specified Jelly context.
|
Object |
getBean(String name)
Returns the bean with the given name.
|
BeanContext |
getBeanContext()
Returns the
BeanContext managed by this instance. |
String |
getBuilderName()
Returns the name of the current builder.
|
Object |
getComponent(String name)
Returns the component with the given name from the currently active
ComponentStore . |
ComponentHandler<?> |
getComponentHandler(String name)
Returns the component handler with the specified name from the current
ComponentStore . |
ComponentManager |
getComponentManager()
Returns the component manager.
|
ComponentStore |
getComponentStore()
Returns a reference to the current component store.
|
Object |
getContainer()
Returns the concrete container component.
|
ContainerSelector |
getContainerSelector()
Returns the
ContainerSelector used by this object. |
Form |
getContextForm()
Returns the form of the current form context.
|
String |
getDefaultButtonName()
Returns the name of the default button.
|
Object |
getDefaultResourceGroup()
Returns the default resource group.
|
FormEventManager |
getEventManager()
Returns the
FormEventManager used by this builder
operation. |
FormEventManager |
getEventManagerForForm(Form f)
Returns the event manager for the specified form.
|
FieldHandler |
getFieldHandler(String name)
Returns the field handler with the specified name from the current
ComponentStore object. |
FieldHandlerFactory |
getFieldHandlerFactory()
Returns the field handler factory.
|
Form |
getForm()
Returns the
Form object. |
Object |
getRootContainer()
Returns the root container.
|
ToolTipManager |
getToolTipManager()
Returns the
ToolTipManager associated with this object. |
TransformerContext |
getTransformerContext()
Returns the transformer context.
|
WidgetHandler |
getWidgetHandler(String name)
Returns a
WidgetHandler for accessing the component with
the given name. |
WidgetHandler |
getWidgetHandlerForComponent(Object component)
Returns a
WidgetHandler object for the specified
component. |
void |
initBeanStore(SimpleBeanStoreImpl store)
Initializes the specified bean store object.
|
void |
initializeForm(TransformerContext tctx,
BindingStrategy strategy)
Initializes the main form to be maintained by the
ComponentBuilderData
object. |
void |
invokeCallBacks()
Invokes all call backs that are registered at this object for the current
form context.
|
boolean |
isCallBacksEnabled()
Returns a flag whether the call back mechanism is currently enabled.
|
ComponentStore |
popComponentStore()
Removes a component store from this object.
|
Form |
popFormContext()
Removes the outer most form context.
|
Form |
popFormContext(Object source)
Removes the outer most form context passing in information about the
responsible source.
|
ComponentStore |
pushComponentStore(ComponentStore store)
Adds a new component store to this object that will replace the current
store.
|
void |
pushFormContext(Form f)
Installs a new form context for the specified form.
|
void |
pushFormContext(Form form,
Object source)
Installs a new form context for the specified form and passes information
about the responsible source.
|
void |
put(org.apache.commons.jelly.JellyContext ctx)
Stores this instance in the specified context.
|
void |
removeFormContextListener(FormContextListener listener)
Removes the specified
FormContextListener from this object. |
void |
setBeanContext(BeanContext ctx)
Allows to set a specific
BeanContext . |
void |
setBuilderName(String builderName)
Sets the name of the current builder.
|
void |
setComponentManager(ComponentManager componentManager)
Sets the component manager.
|
void |
setContainerSelector(ContainerSelector containerSelector)
Sets the
ContainerSelector to be used by this object. |
void |
setDefaultButtonName(String defaultButtonName)
Sets the name of the default button.
|
void |
setDefaultResourceGroup(Object defaultResourceGroup)
Sets the default resource group.
|
void |
setEventManager(FormEventManager evMan)
Allows to set an event manager.
|
void |
setFieldHandlerFactory(FieldHandlerFactory fieldHandlerFactory)
Sets the field handler factory.
|
void |
setLayout(Object layout)
Sets the layout for the root container.
|
void |
setRootContainer(Object rootContainer)
Sets the root container.
|
void |
setToolTipManager(ToolTipManager toolTipManager)
Sets the
ToolTipManager for this object. |
void |
storeComponent(String name,
Object component)
Stores the specified component in the current
ComponentStore . |
void |
storeComponentHandler(String name,
ComponentHandler<?> handler)
Stores the given component handler in the current
ComponentStore . |
void |
storeFieldHandler(String name,
FieldHandler fld)
Stores the specified field handler.
|
public static final String KEY_COMPHANDLER_PREFIX
ComponentHandler
of a
component from the bean context managed by this class.public static final String KEY_FIELDHANDLER_PREFIX
FieldHandler
of a component from
the bean context managed by this class.public static final String KEY_WIDGETHANDLER_PREFIX
WidgetHandler
of a component
from the bean context managed by this class.public static final String KEY_FORM
public static final String KEY_COMPONENT_BUILDER_DATA
public static final String KEY_CURRENT_CONTEXT
public static final String KEY_BUILDER_DATA
BuilderData
object from the bean context managed by this class. The
BuilderData
object allows access to some important,
application-global objects.public ComponentBuilderData()
ComponentBuilderData
.public void initializeForm(TransformerContext tctx, BindingStrategy strategy)
ComponentBuilderData
object. This method must be called after the construction of this object
to explicitly initialize the Form
object.tctx
- the TransformerContext
strategy
- the BindingStrategy
for the formIllegalArgumentException
- if a required parameter is nullpublic String getBuilderName()
public void setBuilderName(String builderName)
builderName
- the builder namepublic Object getRootContainer()
public void setRootContainer(Object rootContainer)
rootContainer
- the root container to usepublic ComponentManager getComponentManager()
public void setComponentManager(ComponentManager componentManager)
componentManager
- the component manager to usepublic FieldHandlerFactory getFieldHandlerFactory()
public void setFieldHandlerFactory(FieldHandlerFactory fieldHandlerFactory)
fieldHandlerFactory
- the handler factorypublic ContainerSelector getContainerSelector()
ContainerSelector
used by this object.ContainerSelector
public void setContainerSelector(ContainerSelector containerSelector)
ContainerSelector
to be used by this object.containerSelector
- the ContainerSelector
public Object getDefaultResourceGroup()
public void setDefaultResourceGroup(Object defaultResourceGroup)
defaultResourceGroup
- the default resource grouppublic Form getForm()
Form
object. This object is created during the builder
process. It contains all fields that have been created so far. Note:
Before calling this method initializeForm()
must have been invoked;
otherwise an exception is thrown.Form
objectIllegalStateException
- if the form has not yet been initializedpublic TransformerContext getTransformerContext()
public FormEventManager getEventManager()
FormEventManager
used by this builder
operation. This object can be used to register event handlers at
components created during the building process.public void setEventManager(FormEventManager evMan)
evMan
- the new event manager to be set (can be null, then
the default event manager for the current form will be set)public ToolTipManager getToolTipManager()
ToolTipManager
associated with this object. If no
specific ToolTipManager
has been set, a default instance is
created and returned.ToolTipManager
public void setToolTipManager(ToolTipManager toolTipManager)
ToolTipManager
for this object. This ToolTipManager
is then used for manipulating tool tips for components.
Normally it is not necessary to set a specific tool tip manager. If none
is set, a default instance is created. This method can be used to inject
a custom tool tip manager.toolTipManager
- the ToolTipManager
to be usedpublic ComponentStore getComponentStore()
ComponentStore
public ComponentStore pushComponentStore(ComponentStore store)
popComponentStore()
is called, then the
replaced component store will become the current store again. The purpose
of this method is to allow complex tags to install their own store so
that all components created in their context are put into this store.
Thus it is possible to create sub forms or things like that. Call backs
that are registered using the addCallBack()
method will
also be affected: they are always created in the context of the current
component store and executed when popComponentStore()
is
invoked.store
- the new store (must not be null)IllegalArgumentException
- if the passed in store is nullpopComponentStore()
public ComponentStore popComponentStore() throws FormBuilderException
pushComponentStore()
. It removes the last pushed
component store, making the store before to the current store again. If
any call backs have been registered for the popped component store, they
will now be invoked.FormBuilderException
- if an error occurs when invoking call backsEmptyStackException
- if there are no more stores to poppublic FormEventManager getEventManagerForForm(Form f)
Form
object; it uses the form's
ComponentStore
for retrieving the components, for
which event listeners are to be registered. With this method an event
manager for a given form can be requested. If no such event manager
exists, it will be created now. Per default there will be a single event
manager for the main form constructed during the build process. However
if complex components are involved that construct sub forms (which need
their own event handling logic), it may be necessary to have a different
event manager.f
- the form the event manager is associated withpublic void addFormContextListener(FormContextListener listener)
FormContextListener
object to this data object. The
listener receives notifications when a new form context is created or the
current context is closed. This method can be called from an arbitrary
threadlistener
- the listener to be registered (must not be null)IllegalArgumentException
- if the listener is nullpublic void removeFormContextListener(FormContextListener listener)
FormContextListener
from this object.listener
- the listener to be removedpublic void pushFormContext(Form f)
f
- the sub form of the new form context (must not be null)IllegalArgumentException
- if the form instance is nullpushComponentStore(ComponentStore)
,
getEventManagerForForm(Form)
public void pushFormContext(Form form, Object source)
pushComponentStore()
is called with the component store of
the specified form. So newly created components will be added to this
store.getEventManagerForForm()
and made to the active event manager.
This ensures that event listener registration logic for the sub form is
handled by the appropriate event manager.FormContextListener
objects are notified about the
newly created context.form
- the sub form of the new form context (must not be
null)source
- the source object responsible for the form contextIllegalArgumentException
- if the form instance is nullpushComponentStore(ComponentStore)
,
getEventManagerForForm(Form)
public Form popFormContext() throws FormBuilderException
Form
instance of the removed form contextFormBuilderException
- if an error occurs when closing the current
form contextIllegalStateException
- if pushFormContext()
has not been
called before (and the context to be removed is the root context)public Form popFormContext(Object source) throws FormBuilderException
pushFormContext()
. It makes the previous form to the active form
again (and ensures that the correct component store and event manager are
selected. This method must be called after processing of a sub form has
completed. Note: This method calls popComponentStore()
to make the component store of the previous form to the current one.
Clients must be aware that the calls to the push and pop methods must be
symmetric and correctly nested, otherwise the association between the
current forms and their component stores may get lost!source
- the source object responsible for the form contextForm
instance of the removed form contextFormBuilderException
- if an error occurs when closing the current
form contextIllegalStateException
- if pushFormContext()
has not been
called before (and the context to be removed is the root context)pushFormContext(Form, Object)
public Form getContextForm()
getForm()
method always returns the main form of this
builder operation, this method takes the current form context into
account, i.e. if pushFormContext()
has been called before,
the form passed to this method will be returned.pushFormContext(Form)
public void storeComponent(String name, Object component)
ComponentStore
.
From there it can be accessed e.g. if another component defines a
reference to it.name
- the name of this componentcomponent
- the component itselfpublic Object getComponent(String name)
ComponentStore
. If no such component can be found, the
method tries to find a component handler with this name and extract the
component object from this handler. If this fails, too, null is
returned.name
- the name of the desired componentpublic void storeComponentHandler(String name, ComponentHandler<?> handler)
ComponentStore
. From there it can later be accessed,
which is useful if it is referenced by other tags.name
- the name of this component handlerhandler
- the handler itselfpublic ComponentHandler<?> getComponentHandler(String name)
ComponentStore
. If no such handler can be found, return
value is null .name
- the name of the desired handlerpublic void storeFieldHandler(String name, FieldHandler fld)
getComponent(String)
and
getComponentHandler(String)
methods.name
- the name of the fieldfld
- the field handlerpublic FieldHandler getFieldHandler(String name)
ComponentStore
object. If no handler exists with this
name, null is returned.name
- the name of the desired field handlerpublic WidgetHandler getWidgetHandler(String name)
WidgetHandler
for accessing the component with
the given name. A component with this name is searched in the current
ComponentStore
object. If it cannot be found, null
will be returned. Otherwise the current ComponentManager
is asked to create a WidgetHandler
object for this
component. A once created WidgetHandler
object will be
cached, so that it can be directly returned if it is queried for the
second time.name
- the name of the componentWidgetHandler
object wrapping this componentpublic WidgetHandler getWidgetHandlerForComponent(Object component)
WidgetHandler
object for the specified
component. This method checks whether already a
WidgetHandler
for the passed in component has been created
(by looking it up in the internal cache). If this is the case, it can be
directly returned. Otherwise the current ComponentManager
is asked to create a new WidgetHandler
instance now. If
the passed in component is null, null will be returned.component
- the component, for which a WidgetHandler
is to be obtainedWidgetHandler
for this componentpublic void addComponent(Object comp, Object constraints) throws FormBuilderRuntimeException
addComponent
in interface Composite
comp
- the component to addconstraints
- the constraints for this componentFormBuilderRuntimeException
- if no root container was setpublic void setLayout(Object layout)
public Object getContainer()
getContainer
in interface Composite
public void disableCallBacks()
invokeCallBacks()
. Calls to this method
can be nested. A corresponding number of enableCallBacks()
is
necessary in order to enable callbacks again.public void enableCallBacks()
disableCallBacks()
.public boolean isCallBacksEnabled()
public void addCallBack(ComponentBuilderCallBack callBack, Object param)
callBack
- the call back objectparam
- a parameter object; this object is passed to the call back
when it is invokedpublic void invokeCallBacks() throws FormBuilderException
FormBuilderException
- if an exception is thrown by one of the call
backspublic void beanNames(Set<String> names)
beanNames
in interface SimpleBeanStoreImpl.BeanContributor
names
- the set in which to store the names of the managed beanspublic Object getBean(String name)
ComponentHandler
, a FieldHandler
, or a
WidgetHandler
is available, the correspondingly prefixed
name is also supported. In addition the other reserved keys as described
in the header comment can be used.getBean
in interface SimpleBeanStoreImpl.BeanContributor
name
- the name of the desired bean providerpublic void initBeanStore(SimpleBeanStoreImpl store)
BeanContext
used during the builder
operation is constructed. This implementation will add the static beans
to the given store and register this object as
BeanContributor
.store
- the store to be initializedpublic BeanContext getBeanContext()
BeanContext
managed by this instance. If not
context has been set, a default context will be returned, which allows
access only to the beans defined in this object (i.e. the components
created during the builder operation and their handlers). Typically the
builder will create a context in the initialization phase of a builder
operation.public void setBeanContext(BeanContext ctx)
BeanContext
. This is not necessary
normally, because the bean context is correctly set up automatically
taking account the appropriate hierarchy of contexts and bean stores.ctx
- the new bean context to be usedpublic String getDefaultButtonName()
public void setDefaultButtonName(String defaultButtonName)
defaultButtonName
- the name of the default button; can be
null to clear the default buttonpublic void put(org.apache.commons.jelly.JellyContext ctx)
get()
method.ctx
- the Jelly context (must not be null)IllegalArgumentException
- if the context is nullpublic static ComponentBuilderData get(org.apache.commons.jelly.JellyContext ctx)
ctx
- the Jelly contextprotected ComponentManager fetchComponentHandler() throws FormBuilderRuntimeException
FormBuilderRuntimeException
- if no component manager was setprotected FormEventManager createEventManager()
FormEventManager
and initializes it with the platform
specific event manager obtained from the component manager.FormBuilderRuntimeException
- if no component manager was setprotected PlatformEventManager createPlatformEventManager()
FormBuilderRuntimeException
- if no component manager was setprotected ToolTipManager createToolTipManager()
ToolTipManager
. This method is called when the
ToolTipManager
is accessed for the first time, but no specific
instance has been set. This implementation creates a default tool tip
manager object.ToolTipManager
instanceCopyright © 2016 The JGUIraffe Team. All rights reserved.