net.sf.click.control
Class AbstractContainer

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.AbstractContainer
All Implemented Interfaces:
Container, Control, Serializable
Direct Known Subclasses:
Form, Panel

public abstract class AbstractContainer
extends AbstractControl
implements Container

Provides a default implementation of the Container interface, to make it easier for developers to implement their own containers.

Subclasses can override AbstractControl.getTag() to return a specific HTML element.

The following example shows how to create an HTML div element:

 public class Div extends AbstractContainer {

     public String getTag() {
         // Return the HTML tag
         return "div";
     }
 }
 

Author:
Bob Schellink
See Also:
Serialized Form

Field Summary
protected  Map controlMap
          The map of controls keyed by field name.
protected  List controls
          The list of controls.
 
Fields inherited from class net.sf.click.control.AbstractControl
actionListener, attributes, listener, listenerMethod, messages, name, parent, styles
 
Fields inherited from interface net.sf.click.Control
CONTROL_MESSAGES
 
Constructor Summary
AbstractContainer()
          Create a container with no name defined.
AbstractContainer(String name)
          Create a container with the given name.
 
Method Summary
 Control add(Control control)
          Add the control to the container and return the added instance.
 boolean contains(Control control)
          Return true if the container contains the specified control.
 Control getControl(String controlName)
          Return the named control from the container if found or null otherwise.
 Map getControlMap()
          Return the map of controls where each map's key / value pair will consist of the control name and instance.
 List getControls()
          Return the sequential list of controls held by the container.
 int getControlSizeEst()
          Return the estimated rendered control size in characters.
 String getHtmlImports()
          Return the HTML import string to be include in the page.
 boolean hasControls()
          Returns true if this container has existing controls, false otherwise.
 Control insert(Control control, int index)
          Add the control to the container at the specified index, and return the added instance.
 void onDestroy()
          The on destroy request event handler.
 void onInit()
          The on initialize event handler.
 boolean onProcess()
          The on process event handler.
 void onRender()
          The on render event handler.
 boolean remove(Control control)
          Remove the given control from the container, returning true if the control was found in the container and removed, or false if the control was not found.
 void render(HtmlStringBuffer buffer)
          Render the HTML representation of the container and all its child controls to the specified buffer.
protected  void renderChildren(HtmlStringBuffer buffer)
          Render this container children to the specified buffer.
protected  void renderContent(HtmlStringBuffer buffer)
          Render this container content to the specified buffer.
protected  void renderTagEnd(String tagName, HtmlStringBuffer buffer)
          Closes the specifies tag.
 String toString()
          Returns the HTML representation of this control.
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getId, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, getTag, hasAttribute, hasAttributes, hasStyles, onDeploy, registerActionEvent, removeStyleClass, renderTagBegin, setActionListener, setAttribute, setId, setListener, setName, setParent, setStyle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.click.Control
getContext, getId, getMessages, getName, getParent, onDeploy, setListener, setName, setParent
 

Field Detail

controls

protected List controls
The list of controls.


controlMap

protected Map controlMap
The map of controls keyed by field name.

Constructor Detail

AbstractContainer

public AbstractContainer()
Create a container with no name defined.


AbstractContainer

public AbstractContainer(String name)
Create a container with the given name.

Parameters:
name - the container name
Method Detail

add

public Control add(Control control)
Description copied from interface: Container
Add the control to the container and return the added instance.

Specified by:
add in interface Container
Parameters:
control - the control to add to the container
Returns:
the control that was added to the container
Throws:
IllegalArgumentException - if the control is null or the container already contains a control with the same name
See Also:
Container.add(net.sf.click.Control).

insert

public Control insert(Control control,
                      int index)
Add the control to the container at the specified index, and return the added instance.

Please note if the specified control already has a parent assigned, it will automatically be removed from that parent and inserted into this container.

Specified by:
insert in interface Container
Parameters:
control - the control to add to the container
index - the index at which the control is to be inserted
Returns:
the control that was added to the container
Throws:
IllegalArgumentException - if the control is null or if the control and container is the same instance or the container already contains a control with the same name or if a Field name is not defined
IndexOutOfBoundsException - if index is out of range (index < 0 || index > getControls().size())
See Also:
Container.insert(net.sf.click.Control, int)

remove

public boolean remove(Control control)
Description copied from interface: Container
Remove the given control from the container, returning true if the control was found in the container and removed, or false if the control was not found.

Specified by:
remove in interface Container
Parameters:
control - the control to remove from the container
Returns:
true if the control was removed from the container
Throws:
IllegalArgumentException - if the control is null
See Also:
Container.remove(net.sf.click.Control).

getControls

public List getControls()
Description copied from interface: Container
Return the sequential list of controls held by the container.

Specified by:
getControls in interface Container
Returns:
the sequential list of controls held by the container
See Also:
Container.getControls().

getControl

public Control getControl(String controlName)
Description copied from interface: Container
Return the named control from the container if found or null otherwise.

Specified by:
getControl in interface Container
Parameters:
controlName - the name of the control to get from the container
Returns:
the named control from the container if found or null otherwise
See Also:
Container.getControl(java.lang.String)

contains

public boolean contains(Control control)
Description copied from interface: Container
Return true if the container contains the specified control.

Specified by:
contains in interface Container
Parameters:
control - the control whose presence in this container is to be tested
Returns:
true if the container contains the specified control
See Also:
Container.contains(net.sf.click.Control)

hasControls

public boolean hasControls()
Returns true if this container has existing controls, false otherwise.

Specified by:
hasControls in interface Container
Returns:
true if the container has existing controls, false otherwise.

getControlMap

public Map getControlMap()
Return the map of controls where each map's key / value pair will consist of the control name and instance.

Controls added to the container that did not specify a AbstractControl.name, will not be included in the returned map.

Returns:
the map of controls

getControlSizeEst

public int getControlSizeEst()
Description copied from class: AbstractControl
Return the estimated rendered control size in characters.

Overrides:
getControlSizeEst in class AbstractControl
Returns:
the estimated rendered control size in characters
See Also:
AbstractControl.getControlSizeEst().

onProcess

public boolean onProcess()
Description copied from interface: Control
The on process event handler. Each control will be processed when the Page is requested.

ClickServlet will process all Page controls in the order they were added to the Page.

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 Containers which by default call onProcess on all their child controls as well.

Specified by:
onProcess in interface Control
Overrides:
onProcess in class AbstractControl
Returns:
true to continue Page event processing or false otherwise
See Also:
Control.onProcess().

onDestroy

public void onDestroy()
Description copied from interface: Control
The on destroy request event handler. Control classes should use this method to add any resource clean up code.

This method is guaranteed to be called before the Page object reference goes out of scope and is available for garbage collection.

Container implementations should recursively invoke the onDestroy method on each of their child controls ensuring that all controls receive this event.

Please note: a common problem when overriding onDestroy in subclasses is forgetting to call super.onDestroy(). Consider carefully whether you should call super.onDestroy() or not, especially for Containers which by default call onDestroy on all their child controls as well.

Specified by:
onDestroy in interface Control
Overrides:
onDestroy in class AbstractControl
See Also:
Control.onDestroy()

onInit

public void onInit()
Description copied from interface: Control
The on initialize event handler. Each control will be initialized before its Control.onProcess() method is called.

Container implementations should recursively invoke the onInit method on each of their child controls ensuring that all controls receive this event.

Please note: a common problem when overriding onInit in subclasses is forgetting to call super.onInit(). Consider carefully whether you should call super.onInit() or not, especially for Containers which by default call onInit on all their child controls as well.

Specified by:
onInit in interface Control
Overrides:
onInit in class AbstractControl
See Also:
Control.onInit()

onRender

public void onRender()
Description copied from interface: Control
The on render event handler. This event handler is invoked prior to the control being rendered, and is useful for providing pre rendering logic.

The on render method is typically used to populate tables performing some database intensive operation. By putting the intensive operations in the on render method they will not be performed if the user navigates away to a different page.

Container implementations should recursively invoke the onRender method on each of their child controls ensuring that all controls receive this event.

Please note: a common problem when overriding onRender in subclasses is forgetting to call super.onRender(). Consider carefully whether you should call super.onRender() or not, especially for Containers which by default call onRender on all their child controls as well.

Specified by:
onRender in interface Control
Overrides:
onRender in class AbstractControl
See Also:
Control.onRender()

getHtmlImports

public String getHtmlImports()
Description copied from interface: Control
Return the HTML import string to be include in the page.

Override this method to specify JavaScript and CSS includes for the page. For example:

 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.

Specified by:
getHtmlImports in interface Control
Overrides:
getHtmlImports in class AbstractControl
Returns:
the HTML includes statements for the container and child Controls, or null if no includes are available
See Also:
Control.getHtmlImports()

render

public void render(HtmlStringBuffer buffer)
Render the HTML representation of the container and all its child controls to the specified buffer.

If AbstractControl.getTag() returns null, this method will render only its child controls.

Specified by:
render in interface Control
Overrides:
render in class AbstractControl
Parameters:
buffer - the specified buffer to render the control's output to
See Also:
AbstractControl.render(net.sf.click.util.HtmlStringBuffer)

toString

public String toString()
Returns the HTML representation of this control.

This method delegates the rendering to the method render(net.sf.click.util.HtmlStringBuffer). The size of buffer is determined by getControlSizeEst().

Overrides:
toString in class AbstractControl
Returns:
the HTML representation of this control
See Also:
Object.toString()

renderTagEnd

protected void renderTagEnd(String tagName,
                            HtmlStringBuffer buffer)
Description copied from class: AbstractControl
Closes the specifies tag.

Overrides:
renderTagEnd in class AbstractControl
Parameters:
tagName - the name of the tag to close
buffer - the buffer to append the output to
See Also:
AbstractControl.renderTagEnd(java.lang.String, net.sf.click.util.HtmlStringBuffer).

renderContent

protected void renderContent(HtmlStringBuffer buffer)
Render this container content to the specified buffer.

Parameters:
buffer - the buffer to append the output to

renderChildren

protected void renderChildren(HtmlStringBuffer buffer)
Render this container children to the specified buffer.

Parameters:
buffer - the buffer to append the output to
See Also:
getControls()