|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.click.control.AbstractControl
public abstract class AbstractControl
Provides a default implementation of the Control
interface
to make it easier for developers to create their own controls.
getTag()
to differentiate the control. However some controls do not map cleanly
to an html tag, in which case you can override
render(org.apache.click.util.HtmlStringBuffer)
for complete control
over the output.
Below is an example of creating a new control called MyField:
public class MyField extends AbstractControl { private String value; public void setValue(String value) { this.value = value; } public String getValue() { return value; } public String getTag() { // Return the HTML tag return "input"; } public boolean onProcess() { // Bind the request parameter to the field value String requestValue = getContext().getRequestParameter(getName()); setValue(requestValue); // Invoke any listener of MyField return dispatchActionEvent(); } }By overriding
getTag()
one can specify the html tag to render.
Overriding onProcess()
allows one to bind the servlet request
parameter to MyField value. The dispatchActionEvent()
method
registers the listener for this control on the Context. Once the onProcess
event has finished, all registered listeners will be fired.
To view the html rendered by MyField invoke the control's toString()
method:
public class Test { public static void main (String args[]) { // Create mock context in which to test the control. MockContext.initContext(); String fieldName = "myfield"; MyField myfield = new MyField(fieldName); String output = myfield.toString(); System.out.println(output); } }Executing the above test results in the following output:
<input name="myfield" id="myfield"/>Also see
Control
javadoc for an explanation of the
Control execution sequence.
Field Summary | |
---|---|
protected ActionListener |
actionListener
The control's action listener. |
protected Map<String,String> |
attributes
The Control attributes Map. |
protected Set<Behavior> |
behaviors
The control's list of behaviors . |
protected List<Element> |
headElements
The list of page HTML HEAD elements including: Javascript imports, Css imports, inline Javascript and inline Css. |
protected Object |
listener
The listener target object. |
protected String |
listenerMethod
The listener method name. |
protected Map<String,String> |
messages
The Control localized messages Map. |
protected String |
name
The Control name. |
protected Object |
parent
The control's parent. |
protected Map<String,String> |
styles
Deprecated. use addStyleClass(String) and
removeStyleClass(String) instead. |
Fields inherited from interface org.apache.click.Control |
---|
CONTROL_MESSAGES |
Constructor Summary | |
---|---|
AbstractControl()
Create a control with no name defined. |
|
AbstractControl(String name)
Create a control with the given name. |
Method Summary | |
---|---|
void |
addBehavior(Behavior behavior)
Add the given Behavior to the control's Set of Behaviors . |
void |
addStyleClass(String value)
Add the CSS class attribute. |
protected void |
appendAttributes(HtmlStringBuffer buffer)
Append all the controls attributes to the specified buffer. |
protected void |
dispatchActionEvent()
Dispatch an action event to the ActionEventDispatcher . |
ActionListener |
getActionListener()
Return the control's action listener. |
String |
getAttribute(String name)
Return the control HTML attribute with the given name, or null if the attribute does not exist. |
Map<String,String> |
getAttributes()
Return the control's attributes Map. |
Set<Behavior> |
getBehaviors()
Returns the Set of Behaviors for this control. |
Context |
getContext()
Return the Page request Context of the Control. |
protected int |
getControlSizeEst()
Return the estimated rendered control size in characters. |
List<Element> |
getHeadElements()
Return the list of HEAD elements
to be included in the page. |
String |
getHtmlImports()
Deprecated. use the new getHeadElements() instead |
String |
getId()
Return the "id" attribute value if defined, or the control name otherwise. |
String |
getMessage(String name)
Return the localized message for the given key or null if not found. |
String |
getMessage(String name,
Object... args)
Return the formatted message for the given resource name and message format arguments or null if no message was found. |
Map<String,String> |
getMessages()
Return a Map of localized messages for the control. |
String |
getName()
Return the name of the Control. |
Page |
getPage()
Return the parent page of this control, or null if not defined. |
Object |
getParent()
Return the parent of the Control. |
String |
getStyle(String name)
Return the control CSS style for the given name. |
Map<String,String> |
getStyles()
Deprecated. use getAttribute(String) instead |
String |
getTag()
Returns the controls html tag. |
boolean |
hasAttribute(String name)
Returns true if specified attribute is defined, false otherwise. |
boolean |
hasAttributes()
Return true if the control has attributes or false otherwise. |
boolean |
hasBehaviors()
Returns true if this control has any Behaviors registered, false otherwise. |
boolean |
hasStyles()
Deprecated. use hasAttribute(String) instead |
boolean |
isAjaxTarget(Context context)
Returns true if this control is an AJAX target, false otherwise. |
void |
onDeploy(ServletContext servletContext)
This method does nothing. |
void |
onDestroy()
This method does nothing. |
void |
onInit()
This method does nothing. |
boolean |
onProcess()
The on process event handler. |
void |
onRender()
This method does nothing. |
void |
removeBehavior(Behavior behavior)
Remove the given Behavior from the Control's Set of Behaviors . |
void |
removeStyleClass(String value)
Removes the CSS class attribute. |
void |
render(HtmlStringBuffer buffer)
Render the control's output to the specified buffer. |
protected void |
renderTagBegin(String tagName,
HtmlStringBuffer buffer)
Render the tag and common attributes including id ,
class and style. |
protected void |
renderTagEnd(String tagName,
HtmlStringBuffer buffer)
Closes the specified tag. |
void |
setActionListener(ActionListener listener)
Set the control's action listener. |
void |
setAttribute(String name,
String value)
Set the control attribute with the given attribute name and value. |
void |
setId(String id)
Set the HTML id attribute for the control with the given value. |
void |
setListener(Object listener,
String method)
Set the controls event listener. |
void |
setName(String name)
Set the name of the Control. |
void |
setParent(Object parent)
Set the parent of the Control. |
void |
setStyle(String name,
String value)
Set the control CSS style name and value pair. |
String |
toString()
Returns the HTML representation of this control. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected ActionListener actionListener
protected Set<Behavior> behaviors
behaviors
.
protected List<Element> headElements
protected Map<String,String> attributes
protected transient Map<String,String> messages
protected String name
protected Object parent
protected Map<String,String> styles
addStyleClass(String)
and
removeStyleClass(String)
instead.
protected Object listener
protected String listenerMethod
Constructor Detail |
---|
public AbstractControl()
public AbstractControl(String name)
name
- the control nameMethod Detail |
---|
public String getTag()
public ActionListener getActionListener()
ActionListener
instance.
public void setActionListener(ActionListener listener)
listener
- the control's action listenerpublic boolean hasBehaviors()
hasBehaviors
in interface Control
public void addBehavior(Behavior behavior)
Behaviors
.
In addition, the Control will be registered with the
ControlRegistry
as a potential Ajax target control and to have it's
Behaviors processed by the Click runtime.
behavior
- the Behavior to addpublic void removeBehavior(Behavior behavior)
Behaviors
.
behavior
- the Behavior to removepublic Set<Behavior> getBehaviors()
getBehaviors
in interface Control
public boolean isAjaxTarget(Context context)
ID
is send as a request parameter.
isAjaxTarget
in interface Control
context
- the request context
public String getAttribute(String name)
name
- the name of link HTML attribute
public void setAttribute(String name, String value)
toString()
method.
For example given the ActionLink:
ActionLink addLink = new ActionLink("addLink", "Add"); addLink.setAttribute("target", "_blank");Will render the HTML as:
<a href=".." target="_blank">Add</a>Note: for style and class attributes you can also use the methods
setStyle(String, String)
and
addStyleClass(String)
.
name
- the attribute namevalue
- the attribute value
IllegalArgumentException
- if name parameter is nullsetStyle(String, String)
,
addStyleClass(String)
,
removeStyleClass(String)
public Map<String,String> getAttributes()
public boolean hasAttributes()
public boolean hasAttribute(String name)
name
- the specified attribute to check
public Context getContext()
Control
getContext
in interface Control
Control.getContext()
public String getName()
Control
getName
in interface Control
Control.getName()
public void setName(String name)
Control
setName
in interface Control
name
- of the control
IllegalArgumentException
- if the name is nullControl.setName(String)
public String getId()
getId
in interface Control
Control.getId()
public void setId(String id)
id
- the element HTML id attribute value to setpublic String getMessage(String name)
getMessages()
.
If still not found, this method will return null.
name
- the name of the message resource
public String getMessage(String name, Object... args)
getMessage(java.lang.String)
is invoked to retrieve the message
for the specified name.
name
- resource name of the messageargs
- the message arguments to format
public Map<String,String> getMessages()
getMessages
in interface Control
IllegalStateException
- if the context for the control has not be setpublic Object getParent()
Control
getParent
in interface Control
Control.getParent()
public void setParent(Object parent)
Control
setParent
in interface Control
parent
- the parent of the Control
IllegalArgumentException
- if the given parent instance is
referencing this object: if (parent == this)Control.setParent(Object)
public boolean onProcess()
Control
Container
implementations should recursively
invoke the onProcess method on each of their child controls ensuring that
all controls receive this event. However when a control onProcess method
return false, no other controls onProcess method should be invoked.
When a control is processed it should return true if the Page should
continue event processing, or false if no other controls should be
processed and the Page.onGet()
or Page.onPost()
methods
should not be invoked.
Please note: a common problem when overriding onProcess in
subclasses is forgetting to call super.onProcess(). Consider
carefully whether you should call super.onProcess() or not,
especially for Container
s which by default
call onProcess on all their child controls as well.
onProcess
in interface Control
Control.onProcess()
public void setListener(Object listener, String method)
public boolean onClick() { System.out.println("onClick called"); return true; }
setListener
in interface Control
listener
- the listener object with the named method to invokemethod
- the name of the method to invokepublic void onInit()
onInit
in interface Control
Control.onInit()
public void onDestroy()
onDestroy
in interface Control
Control.onDestroy()
public void onDeploy(ServletContext servletContext)
onDeploy
in interface Control
servletContext
- the servlet contextControl.onDeploy(ServletContext)
public void onRender()
onRender
in interface Control
Control.onRender()
public final String getHtmlImports()
getHeadElements()
instead
public List<Element> getHeadElements()
Control
elements
to be included in the page. Example HEAD elements include
JsImport
,
JsScript
,
CssImport
and
CssStyle
.
Controls can contribute their own list of HEAD elements by implementing
this method.
The recommended approach when implementing this method is to use
lazy loading to ensure the HEAD elements are only added
once and when needed. For example:
public MyControl extends AbstractControl { public List getHeadElements() { // Use lazy loading to ensure the JS is only added the // first time this method is called. if (headElements == null) { // Get the head elements from the super implementation headElements = super.getHeadElements(); // Include the control's external JavaScript resource JsImport jsImport = new JsImport("/mycorp/mycontrol/mycontrol.js"); headElements.add(jsImport); // Include the control's external Css resource CssImport cssImport = new CssImport("/mycorp/mycontrol/mycontrol.css"); headElements.add(cssImport); } return headElements; } }Alternatively one can add the HEAD elements in the Control's constructor:
public MyControl extends AbstractControl { public MyControl() { JsImport jsImport = new JsImport("/mycorp/mycontrol/mycontrol.js"); getHeadElements().add(jsImport); CssImport cssImport = new CssImport("/mycorp/mycontrol/mycontrol.css"); getHeadHeaders().add(cssImport); } }One can also add HEAD elements from event handler methods such as
Control.onInit()
, Control.onProcess()
, Control.onRender()
etc.
The order in which JS and CSS files are included will be preserved in the
page.
Note: this method must never return null. If no HEAD elements
are available this method must return an empty List
.
Also note: a common problem when overriding getHeadElements in
subclasses is forgetting to call super.getHeadElements. Consider
carefully whether you should call super.getHeadElements or not.
getHeadElements
in interface Control
Control.getHeadElements()
public Page getPage()
public String getStyle(String name)
name
- the CSS style name
public void setStyle(String name, String value)
ActionLink addLink = new ActionLink("addLink", "Add"); addLink.setStyle("color", "red"); addLink.setStyle("border", "1px solid black");Will render the HTML as:
<a href=".." style="color:red;border:1px solid black;">Add</a>To remove an existing style, set the value to null.
name
- the CSS style namevalue
- the CSS style valuepublic boolean hasStyles()
hasAttribute(String)
instead
public Map<String,String> getStyles()
getAttribute(String)
instead
public void addStyleClass(String value)
ActionLink addLink = new ActionLink("addLink", "Add"); addLink.addStyleClass("red");Will render the HTML as:
<a href=".." class="red">Add</a>
value
- the class attribute to addpublic void removeStyleClass(String value)
value
- the CSS class attributepublic void render(HtmlStringBuffer buffer)
getTag()
returns null, this method will return an empty
string.
render
in interface Control
buffer
- the specified buffer to render the control's output toControl.render(org.apache.click.util.HtmlStringBuffer)
public String toString()
render(org.apache.click.util.HtmlStringBuffer)
. The size of buffer
is determined by getControlSizeEst()
.
toString
in class Object
Object.toString()
protected void dispatchActionEvent()
ActionEventDispatcher
.
ActionEventDispatcher.dispatchActionEvent(org.apache.click.Control, org.apache.click.ActionListener)
,
ActionEventDispatcher.dispatchAjaxBehaviors(org.apache.click.Control)
protected void appendAttributes(HtmlStringBuffer buffer)
buffer
- the specified buffer to append all the attributesprotected void renderTagBegin(String tagName, HtmlStringBuffer buffer)
id
,
class and style. The name
attribute
is not rendered by this control. It is up to subclasses
whether to render the name attribute or not. Generally only
Field
controls render the name attribute.
Please note: the tag will not be closed by this method. This
enables callers of this method to append extra attributes as needed.
For example the following example:
Field field = new TextField("mytext"); HtmlStringBuffer buffer = new HtmlStringBuffer(); field.renderTagBegin("input", buffer);will render:
<input name="mytext" id="mytext"Note that the tag is not closed, so subclasses can render extra attributes.
tagName
- the name of the tag to renderbuffer
- the buffer to append the output toprotected void renderTagEnd(String tagName, HtmlStringBuffer buffer)
tagName
- the name of the tag to closebuffer
- the buffer to append the output toprotected int getControlSizeEst()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |