|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.click.control.AbstractControl
Provides a default implementation of the Control
interface,
to make it easier for developers to implement their own controls.
getTag()
to differentiate the control. However some controls does not map cleanly
to a html tag, in which case you can override
render(net.sf.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().getRequestParamter(getName()); setValue(requestValue); // Invoke any listener of MyField return registerActionEvent(); } }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 registerActionEvent()
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 |
attributes
The Control attributes Map. |
protected Object |
listener
The listener target object. |
protected String |
listenerMethod
The listener method name. |
protected Map |
messages
The Control localized messages Map. |
protected String |
name
The Control name. |
protected Object |
parent
The control's parent. |
protected Map |
styles
Deprecated. use addStyleClass(String) and
removeStyleClass(String) instead. |
Fields inherited from interface net.sf.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 |
addStyleClass(String value)
Add the CSS class attribute. |
protected void |
appendAttributes(HtmlStringBuffer buffer)
Append all the controls attributes to the specified buffer. |
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 |
getAttributes()
Return the control's attributes Map. |
Context |
getContext()
Return the Page request Context of the Control. |
protected int |
getControlSizeEst()
Return the estimated rendered control size in characters. |
String |
getHtmlImports()
Return the HTML import string to be include in the page. |
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 arg)
Return the formatted message for the given resource name, message format argument and the context request locale, or null if no message was found. |
String |
getMessage(String name,
Object[] args)
Return the formatted message for the given resource name, message format arguments and the context request locale, or null if no message was found. |
Map |
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 |
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 |
hasStyles()
Deprecated. use hasAttribute(String) instead |
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. |
protected void |
registerActionEvent()
Register this control's listener with the ControlRegistry
registry. |
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. |
protected void |
renderTagEnd(String tagName,
HtmlStringBuffer buffer)
Closes the specifies 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 Map attributes
protected transient Map messages
protected String name
protected Object parent
protected Map 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()
ActionListenerAdaptor
instance.
public void setActionListener(ActionListener listener)
listener
- the control's action listenerpublic 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 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 arg)
getMessage(java.lang.String, java.lang.Object[])
is invoked to
retrieve the message for the specified name.
name
- resource name of the messagearg
- the message argument to format
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 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 String getHtmlImports()
Control
protected static final String HTML_IMPORT = "<script type=\"text/javascript\" src=\"{0}/click/custom.js\"></script>"; public String getHtmlImports() { return ClickUtils.createHtmlImport(HTML_IMPORTS, getResourceVersionIndicator(), getContext()); }Note multiple import lines should be separated by a '\n' char, as the
PageImports
will
parse multiple import lines on the '\n' char and ensure that
imports are not included twice.
The order in which JS and CSS files are include will be preserved in the
page.
Also note: a common problem when overriding getHtmlImports in
subclasses is forgetting to call super.getHtmlImports. Consider
carefully whether you should call super.getHtmlImports or not.
getHtmlImports
in interface Control
Control.getHtmlImports()
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 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(net.sf.click.util.HtmlStringBuffer)
public String toString()
render(net.sf.click.util.HtmlStringBuffer)
. The size of buffer
is determined by getControlSizeEst()
.
Object.toString()
protected void registerActionEvent()
ControlRegistry
registry.
ControlRegistry.registerActionEvent(net.sf.click.Control, net.sf.click.ActionListener)
protected void appendAttributes(HtmlStringBuffer buffer)
buffer
- the specified buffer to append all the attributesprotected void renderTagBegin(String tagName, HtmlStringBuffer buffer)
Field field = new TextField("mytext"); HtmlStringBuffer buffer = new HtmlStringBuffer(); field.renderTagBegin("div", buffer);will be:
<div name="mytext" id="mytext"Note that the tag is not closed, so subclasses can easily add more 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 |