Class EnablerBuilder
- java.lang.Object
-
- net.sf.jguiraffe.gui.builder.enablers.EnablerBuilder
-
public class EnablerBuilder extends Object
A helper class for the convenient creation of standard
ElementEnablerobjects.This class supports the creation of
ElementEnablerobjects based on textual specifications. It is intended to be used where a compact notation of enabler objects is required. One example are bean declarations for the dependency injection framework: Action tasks that issue commands can be associated with anElementEnablerto disable certain elements while the action task is running. The XML-based declaration of complex enabler can be pretty inconvenient - especially if aChainElementEnablerwith multiple child elements is used. Here a shorter form for declaring enablers is helpful.The basic usage of
EnablerBuilderis to call theaddSpecification(String)method an arbitrary number of times. Each invocation adds a string with the specification for a concreteElementEnabler. When all specifications have been added thebuild()method is called which creates the resultingElementEnabler. The result of this method depends on the number of specifications that have been added:- If there is no specification, a
NullEnableris returned. - If there is exactly one specification, an
ElementEnablercorresponding to the specification is returned. - In all other cases result is a
ChainElementEnablerwith corresponding childElementEnablerobjects.
A valid specification for an
ElementEnablerhas the following form: It starts with a prefix (see below), followed by a colon. After the colon a parameter to be passed to theElementEnabler's constructor is expected. The support prefix values are shown in the following table:Prefix Element enabler class action ActionEnableractiongroup ActionGroupEnablercomp ComponentEnablerThe prefix is not case sensitive. Whitespace are ignored. Multiple specifications can be concatenated to a single one using a comma (","). So the following are valid examples for
ElementEnablerspecifications:- action:testAction
- defines an
ActionEnablerfor an action named testAction - ActionGroup : testGroup
- defines an
ActionGroupEnablerfor an action group named testGroup - COMP : myButton
- defines an
ComponentEnablerfor the myButton component - action:action1, action:action2,comp:button1, comp:button2
- defines multiple enablers, two action enablers and two component enablers
This class only supports the standard
ElementEnablerimplementations. There is no extension mechanism. This is because this class exists only for convenience. If access to otherElementEnablerimplementations is needed, the objects have to be created directly.Implementation note: This class is not thread-safe.
- Version:
- $Id: EnablerBuilder.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
- If there is no specification, a
-
-
Constructor Summary
Constructors Constructor Description EnablerBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description EnablerBuilderaddSpecification(String spec)Adds the given specification to this builder.ElementEnablerbuild()Creates anElementEnablerbased on the specifications that have been added to this builder.protected ElementEnablercreateEnabler(String prefix, String name)Creates anElementEnablerfor the specified prefix.protected ElementEnablerprocessSpecification(String spec)Processes the specified specification and creates a correspondingElementEnabler.voidreset()Resets this builder.
-
-
-
Method Detail
-
addSpecification
public EnablerBuilder addSpecification(String spec)
Adds the given specification to this builder. The specification must conform to the format defined in the class comment. It must not be null or empty.- Parameters:
spec- the specification to add- Returns:
- a reference to this builder for method chaining
- Throws:
IllegalArgumentException- if the specification is undefined
-
build
public ElementEnabler build()
Creates anElementEnablerbased on the specifications that have been added to this builder. This method also resets the state of this builder; all specifications are removed, so that this instance can be used for defining anotherElementEnabler.- Returns:
- an
ElementEnablerbased on the so far added specifications - Throws:
IllegalArgumentException- if an invalid specification is encountered
-
reset
public void reset()
Resets this builder. All specifications that have been added so far are removed. Note: This method is also invoked bybuild(), so it is not necessary to reset the builder after an object was created.
-
processSpecification
protected ElementEnabler processSpecification(String spec)
Processes the specified specification and creates a correspondingElementEnabler. This method is called for each specification that was added to this builder.- Parameters:
spec- the specification for a singleElementEnabler(already trimmed)- Returns:
- the corresponding
ElementEnabler - Throws:
IllegalArgumentException- if the specification is invalid
-
createEnabler
protected ElementEnabler createEnabler(String prefix, String name)
Creates anElementEnablerfor the specified prefix. This method is called byprocessSpecification(String)after the specification was passed. It creates the actual enabler.- Parameters:
prefix- the prefix indicating the enabler typename- the name of the element to be enabled- Returns:
- the corresponding
ElementEnabler - Throws:
IllegalArgumentException- if no enabler can be created for this prefix
-
-