Class AbstractControl
- java.lang.Object
-
- org.openidentityplatform.openam.click.control.AbstractControl
-
- All Implemented Interfaces:
Serializable,org.openidentityplatform.openam.click.Control
- Direct Known Subclasses:
AbstractContainer,AbstractLink,Field,Table
public abstract class AbstractControl extends Object implements org.openidentityplatform.openam.click.Control
Provides a default implementation of theControlinterface to make it easier for developers to create their own controls.Subclasses are expected to at least override
getTag()to differentiate the control. However some controls do not map cleanly to an html tag, in which case you can overriderender(org.openidentityplatform.openam.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 overridinggetTag()one can specify the html tag to render.Overriding
onProcess()allows one to bind the servlet request parameter to MyField value. ThedispatchActionEvent()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 seeControljavadoc for an explanation of the Control execution sequence.Message Resources and Internationalization (i18n)
Controls support a hierarchy of resource bundles for displaying messages. These localized messages can be accessed through the methods: The order in which localized messages resolve are described in the user guide.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected org.openidentityplatform.openam.click.ActionListeneractionListenerThe control's action listener.protected Map<String,String>attributesThe Control attributes Map.protected Set<org.openidentityplatform.openam.click.Behavior>behaviorsThe control's list ofbehaviors.protected List<Element>headElementsThe list of page HTML HEAD elements including: Javascript imports, Css imports, inline Javascript and inline Css.protected ObjectlistenerThe listener target object.protected StringlistenerMethodThe listener method name.protected Map<String,String>messagesThe Control localized messages Map.protected StringnameThe Control name.protected ObjectparentThe control's parent.protected Map<String,String>stylesDeprecated.useaddStyleClass(String)andremoveStyleClass(String)instead.
-
Constructor Summary
Constructors Constructor Description AbstractControl()Create a control with no name defined.AbstractControl(String name)Create a control with the given name.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddBehavior(org.openidentityplatform.openam.click.Behavior behavior)Add the givenBehaviorto the control's Set ofBehaviors.voidaddStyleClass(String value)Add the CSS class attribute.protected voidappendAttributes(HtmlStringBuffer buffer)Append all the controls attributes to the specified buffer.protected voiddispatchActionEvent()Dispatch an action event to theActionEventDispatcher.org.openidentityplatform.openam.click.ActionListenergetActionListener()Return the control's action listener.StringgetAttribute(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<org.openidentityplatform.openam.click.Behavior>getBehaviors()Returns the Set ofBehaviorsfor this control.org.openidentityplatform.openam.click.ContextgetContext()Return the Page request Context of the Control.protected intgetControlSizeEst()Return the estimated rendered control size in characters.List<Element>getHeadElements()Return the list of HEADelementsto be included in the page.StringgetHtmlImports()Deprecated.use the newgetHeadElements()insteadStringgetId()Return the "id" attribute value if defined, or the control name otherwise.StringgetMessage(String name)Return the localized message for the given key or null if not found.StringgetMessage(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.StringgetName()Return the name of the Control.org.openidentityplatform.openam.click.PagegetPage()Return the parent page of this control, or null if not defined.ObjectgetParent()Return the parent of the Control.StringgetStyle(String name)Return the control CSS style for the given name.Map<String,String>getStyles()Deprecated.usegetAttribute(String)insteadStringgetTag()Returns the controls html tag.booleanhasAttribute(String name)Returns true if specified attribute is defined, false otherwise.booleanhasAttributes()Return true if the control has attributes or false otherwise.booleanhasBehaviors()Returnstrueif this control has anyBehaviors registered,falseotherwise.booleanhasStyles()Deprecated.usehasAttribute(String)insteadbooleanisAjaxTarget(org.openidentityplatform.openam.click.Context context)Returnstrueif this control is an AJAX target,falseotherwise.voidonDeploy(jakarta.servlet.ServletContext servletContext)This method does nothing.voidonDestroy()This method does nothing.voidonInit()This method does nothing.booleanonProcess()The on process event handler.voidonRender()This method does nothing.voidremoveBehavior(org.openidentityplatform.openam.click.Behavior behavior)Remove the givenBehaviorfrom the Control's Set ofBehaviors.voidremoveStyleClass(String value)Removes the CSS class attribute.voidrender(HtmlStringBuffer buffer)Render the control's output to the specified buffer.protected voidrenderTagBegin(String tagName, HtmlStringBuffer buffer)protected voidrenderTagEnd(String tagName, HtmlStringBuffer buffer)Closes the specified tag.voidsetActionListener(org.openidentityplatform.openam.click.ActionListener listener)Set the control's action listener.voidsetAttribute(String name, String value)Set the control attribute with the given attribute name and value.voidsetId(String id)Set the HTML id attribute for the control with the given value.voidsetListener(Object listener, String method)Set the controls event listener.voidsetName(String name)Set the name of the Control.voidsetParent(Object parent)Set the parent of the Control.voidsetStyle(String name, String value)Set the control CSS style name and value pair.StringtoString()Returns the HTML representation of this control.
-
-
-
Field Detail
-
actionListener
protected org.openidentityplatform.openam.click.ActionListener actionListener
The control's action listener.
-
behaviors
protected Set<org.openidentityplatform.openam.click.Behavior> behaviors
The control's list ofbehaviors.
-
headElements
protected List<Element> headElements
The list of page HTML HEAD elements including: Javascript imports, Css imports, inline Javascript and inline Css.
-
name
protected String name
The Control name.
-
parent
protected Object parent
The control's parent.
-
styles
protected Map<String,String> styles
Deprecated.useaddStyleClass(String)andremoveStyleClass(String)instead.The Map of CSS style attributes.
-
listener
protected Object listener
The listener target object.
-
listenerMethod
protected String listenerMethod
The listener method name.
-
-
Constructor Detail
-
AbstractControl
public AbstractControl()
Create a control with no name defined.
-
AbstractControl
public AbstractControl(String name)
Create a control with the given name.- Parameters:
name- the control name
-
-
Method Detail
-
getTag
public String getTag()
Returns the controls html tag.Subclasses should override this method and return the correct tag.
This method returns
nullby default.Example tags include
table,form,aandinput.- Returns:
- this controls html tag
-
getActionListener
public org.openidentityplatform.openam.click.ActionListener getActionListener()
Return the control's action listener. If the control has a listener target and listener method defined, this method will return anActionListenerinstance.- Returns:
- the control's action listener
-
setActionListener
public void setActionListener(org.openidentityplatform.openam.click.ActionListener listener)
Set the control's action listener.- Parameters:
listener- the control's action listener
-
hasBehaviors
public boolean hasBehaviors()
Returnstrueif this control has anyBehaviors registered,falseotherwise.- Specified by:
hasBehaviorsin interfaceorg.openidentityplatform.openam.click.Control- Returns:
trueif this control has anyBehaviors registered,falseotherwise
-
addBehavior
public void addBehavior(org.openidentityplatform.openam.click.Behavior behavior)
Add the givenBehaviorto the control's Set ofBehaviors.In addition, the Control will be registered with the
ControlRegistryas a potentialAjax target controland to have it'sBehaviorsprocessed by the Click runtime.- Parameters:
behavior- theBehaviorto add
-
removeBehavior
public void removeBehavior(org.openidentityplatform.openam.click.Behavior behavior)
Remove the givenBehaviorfrom the Control's Set ofBehaviors.- Parameters:
behavior- theBehaviorto remove
-
getBehaviors
public Set<org.openidentityplatform.openam.click.Behavior> getBehaviors()
Returns the Set ofBehaviorsfor this control.- Specified by:
getBehaviorsin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- the Set of
Behaviorsfor this control
-
isAjaxTarget
public boolean isAjaxTarget(org.openidentityplatform.openam.click.Context context)
Returnstrueif this control is an AJAX target,falseotherwise.The control is defined as an Ajax target if the control
IDis send as a request parameter.- Specified by:
isAjaxTargetin interfaceorg.openidentityplatform.openam.click.Control- Parameters:
context- the request context- Returns:
trueif this control is an AJAX target,falseotherwise
-
getAttribute
public String getAttribute(String name)
Return the control HTML attribute with the given name, or null if the attribute does not exist.- Parameters:
name- the name of link HTML attribute- Returns:
- the link HTML attribute
-
setAttribute
public void setAttribute(String name, String value)
Set the control attribute with the given attribute name and value. You would generally use attributes if you were creating the entire Control programmatically and rendering it with thetoString()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: forstyleandclassattributes you can also use the methodssetStyle(String, String)andaddStyleClass(String).- Parameters:
name- the attribute namevalue- the attribute value- Throws:
IllegalArgumentException- if name parameter is null- See Also:
setStyle(String, String),addStyleClass(String),removeStyleClass(String)
-
getAttributes
public Map<String,String> getAttributes()
Return the control's attributes Map.- Returns:
- the control's attributes Map.
-
hasAttributes
public boolean hasAttributes()
Return true if the control has attributes or false otherwise.- Returns:
- true if the control has attributes on false otherwise
-
hasAttribute
public boolean hasAttribute(String name)
Returns true if specified attribute is defined, false otherwise.- Parameters:
name- the specified attribute to check- Returns:
- true if name is a defined attribute
-
getContext
public org.openidentityplatform.openam.click.Context getContext()
Description copied from interface:org.openidentityplatform.openam.click.ControlReturn the Page request Context of the Control.- Specified by:
getContextin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- the Page request Context
- See Also:
Control.getContext()
-
getName
public String getName()
Description copied from interface:org.openidentityplatform.openam.click.ControlReturn the name of the Control. Each control name must be unique in the containing Page model or the containing Form.- Specified by:
getNamein interfaceorg.openidentityplatform.openam.click.Control- Returns:
- the name of the control
- See Also:
Control.getName()
-
setName
public void setName(String name)
Description copied from interface:org.openidentityplatform.openam.click.ControlSet the name of the Control. Each control name must be unique in the containing Page model or the parent container.Please note: changing the name of a Control after it has been added to its parent container is undefined. Thus it is best not to change the name of a Control once its been set.
- Specified by:
setNamein interfaceorg.openidentityplatform.openam.click.Control- Parameters:
name- of the control- Throws:
IllegalArgumentException- if the name is null- See Also:
Control.setName(String)
-
getId
public String getId()
Return the "id" attribute value if defined, or the control name otherwise.- Specified by:
getIdin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- HTML element identifier attribute "id" value
- See Also:
Control.getId()
-
setId
public void setId(String id)
Set the HTML id attribute for the control with the given value.- Parameters:
id- the element HTML id attribute value to set
-
getMessage
public String getMessage(String name)
Return the localized message for the given key or null if not found. The resource message returned will use the Locale obtained from the Context.This method will attempt to lookup the localized message in the parent's messages, which resolves to the Page's resource bundle.
If the message was not found, this method will attempt to look up the value in the
/click-control.propertiesmessage properties file, through the methodgetMessages().If still not found, this method will return null.
- Parameters:
name- the name of the message resource- Returns:
- the named localized message for the control, or null if not found
-
getMessage
public 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. The resource message returned will use the Locale obtained from the Context.getMessage(java.lang.String)is invoked to retrieve the message for the specified name.- Parameters:
name- resource name of the messageargs- the message arguments to format- Returns:
- the named localized message for the control or null if no message was found
-
getMessages
public Map<String,String> getMessages()
Return a Map of localized messages for the control. The messages returned will use the Locale obtained from the Context.- Specified by:
getMessagesin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- a Map of localized messages for the control
- Throws:
IllegalStateException- if the context for the control has not be set
-
getParent
public Object getParent()
Description copied from interface:org.openidentityplatform.openam.click.ControlReturn the parent of the Control.- Specified by:
getParentin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- the Control's parent
- See Also:
Control.getParent()
-
setParent
public void setParent(Object parent)
Description copied from interface:org.openidentityplatform.openam.click.ControlSet the parent of the Control.- Specified by:
setParentin interfaceorg.openidentityplatform.openam.click.Control- Parameters:
parent- the parent of the Control- Throws:
IllegalArgumentException- if the given parent instance is referencingthisobject:if (parent == this)- See Also:
Control.setParent(Object)
-
onProcess
public boolean onProcess()
Description copied from interface:org.openidentityplatform.openam.click.ControlThe 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.
Containerimplementations 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()orPage.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:
onProcessin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- true to continue Page event processing or false otherwise
- See Also:
Control.onProcess()
-
setListener
public void setListener(Object listener, String method)
Set the controls event listener.The method signature of the listener is:
- must have a valid Java method name
- takes no arguments
- returns a boolean value
An example event listener method would be:
public boolean onClick() { System.out.println("onClick called"); return true; }
- Specified by:
setListenerin interfaceorg.openidentityplatform.openam.click.Control- Parameters:
listener- the listener object with the named method to invokemethod- the name of the method to invoke
-
onInit
public void onInit()
This method does nothing. Subclasses may override this method to perform initialization.- Specified by:
onInitin interfaceorg.openidentityplatform.openam.click.Control- See Also:
Control.onInit()
-
onDestroy
public void onDestroy()
This method does nothing. Subclasses may override this method to perform clean up any resources.- Specified by:
onDestroyin interfaceorg.openidentityplatform.openam.click.Control- See Also:
Control.onDestroy()
-
onDeploy
public void onDeploy(jakarta.servlet.ServletContext servletContext)
This method does nothing. Subclasses may override this method to deploy static web resources.- Specified by:
onDeployin interfaceorg.openidentityplatform.openam.click.Control- Parameters:
servletContext- the servlet context- See Also:
Control.onDeploy(ServletContext)
-
onRender
public void onRender()
This method does nothing. Subclasses may override this method to perform pre rendering logic.- Specified by:
onRenderin interfaceorg.openidentityplatform.openam.click.Control- See Also:
Control.onRender()
-
getHtmlImports
public final String getHtmlImports()
Deprecated.use the newgetHeadElements()instead- Returns:
- the HTML includes statements for the control stylesheet and JavaScript files
-
getHeadElements
public List<Element> getHeadElements()
Description copied from interface:org.openidentityplatform.openam.click.ControlReturn the list of HEADelementsto be included in the page. Example HEAD elements includeJsImport,JsScript,CssImportandCssStyle.Controls can contribute their own list of HEAD elements by implementing this method.
The recommended approach when implementing this method is to use
lazy loadingto ensure the HEAD elements are only addedonceand whenneeded. 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 asControl.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.
- Specified by:
getHeadElementsin interfaceorg.openidentityplatform.openam.click.Control- Returns:
- the list of HEAD elements to be included in the page
- See Also:
Control.getHeadElements()
-
getPage
public org.openidentityplatform.openam.click.Page getPage()
Return the parent page of this control, or null if not defined.- Returns:
- the parent page of this control, or null if not defined
-
getStyle
public String getStyle(String name)
Return the control CSS style for the given name.- Parameters:
name- the CSS style name- Returns:
- the CSS style for the given name
-
setStyle
public void setStyle(String name, String value)
Set the control CSS style name and value pair.For example given the ActionLink:
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 tonull.- Parameters:
name- the CSS style namevalue- the CSS style value
-
hasStyles
public boolean hasStyles()
Deprecated.usehasAttribute(String)insteadReturn true if CSS styles are defined.- Returns:
- true if CSS styles are defined
-
getStyles
public Map<String,String> getStyles()
Deprecated.usegetAttribute(String)insteadReturn the Map of control CSS styles.- Returns:
- the Map of control CSS styles
-
addStyleClass
public void addStyleClass(String value)
Add the CSS class attribute. Null values will be ignored.For example given the ActionLink:
ActionLink addLink = new ActionLink("addLink", "Add"); addLink.addStyleClass("red");Will render the HTML as:<a href=".." class="red">Add</a>
- Parameters:
value- the class attribute to add
-
removeStyleClass
public void removeStyleClass(String value)
Removes the CSS class attribute.- Parameters:
value- the CSS class attribute
-
render
public void render(HtmlStringBuffer buffer)
Render the control's output to the specified buffer.If
getTag()returns null, this method will return an empty string.- Specified by:
renderin interfaceorg.openidentityplatform.openam.click.Control- Parameters:
buffer- the specified buffer to render the control's output to- See Also:
Control.render(org.apache.click.util.HtmlStringBuffer)
-
toString
public String toString()
Returns the HTML representation of this control.This method delegates the rendering to the method
render(org.openidentityplatform.openam.click.util.HtmlStringBuffer). The size of buffer is determined bygetControlSizeEst().- Overrides:
toStringin classObject- Returns:
- the HTML representation of this control
- See Also:
Object.toString()
-
dispatchActionEvent
protected void dispatchActionEvent()
Dispatch an action event to theActionEventDispatcher.- See Also:
ActionEventDispatcher.dispatchActionEvent(org.apache.click.Control, org.apache.click.ActionListener),ActionEventDispatcher.dispatchAjaxBehaviors(org.apache.click.Control)
-
appendAttributes
protected void appendAttributes(HtmlStringBuffer buffer)
Append all the controls attributes to the specified buffer.- Parameters:
buffer- the specified buffer to append all the attributes
-
renderTagBegin
protected void renderTagBegin(String tagName, HtmlStringBuffer buffer)
Render the tag and common attributes includingid,classandstyle. Thenameattribute is not rendered by this control. It is up to subclasses whether to render the name attribute or not. Generally onlyFieldcontrols 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.- Parameters:
tagName- the name of the tag to renderbuffer- the buffer to append the output to
-
renderTagEnd
protected void renderTagEnd(String tagName, HtmlStringBuffer buffer)
Closes the specified tag.- Parameters:
tagName- the name of the tag to closebuffer- the buffer to append the output to
-
getControlSizeEst
protected int getControlSizeEst()
Return the estimated rendered control size in characters.- Returns:
- the estimated rendered control size in characters
-
-