net.sf.click.control
Class ActionLink

java.lang.Object
  extended bynet.sf.click.control.AbstractControl
      extended bynet.sf.click.control.AbstractLink
          extended bynet.sf.click.control.ActionLink
All Implemented Interfaces:
Control, Serializable

public class ActionLink
extends AbstractLink

Provides a Action Link control:   <a href=""></a>.

Action Link
This control can render the "href" URL attribute using getHref(), or the entire ActionLink anchor tag using AbstractControl.toString().

ActionLink support invoking control listeners.

ActionLink Example

An example of using ActionLink to call a logout method is illustrated below:
 public class MyPage extends Page {

     public MyPage() {
         ActionLink link = new ActionLink("logoutLink");
         link.setListener(this, "onLogoutClick");
         addControl(link);
     }

     public boolean onLogoutClick() {
         if (getContext().hasSession()) {
            getContext().getSession().invalidate();
         }
         setRedirect(LogoutPage.class);

         return false;
     }
 } 
The corresponding template code is below. Note href is evaluated by Velocity to getHref():
 <a href="$logoutLink.href" title="Click to Logout">Logout</a> 
ActionLink can also support a value parameter which is accessable using getValue().

For example a products table could include rows of products, each with a get product details ActionLink and add product ActionLink. The ActionLinks include the product's id as a parameter to the getHref(Object) method, which is then available when the control is processed:

 <table>
 #foreach ($product in $productList)
   <tr>
    <td>
      $product.name
    </td>
    <td>
      <a href="$detailsLink.getHref($product.id)" title="Get product information">Details</a>
    </td>
    <td>
      <a href="$addLink.getHref($product.id)" title="Add to basket">Add</a>
    </td>
   </tr>
 #end
 </table> 
The corresponding Page class for this template is:
 public class ProductsPage extends Page {

     public ActionLink addLink = new ActionLink("addLink", this, "onAddClick");
     public ActionLink detailsLink  = new ActionLink("detailsLink", this, "onDetailsClick");
     public List productList;

     public boolean onAddClick() {
         // Get the product clicked on by the user
         Integer productId = addLink.getValueInteger();
         Product product = getProductService().getProduct(productId);

         // Add product to basket
         List basket = (List) getContext().getSessionAttribute("basket");
         basket.add(product);
         getContext().setSessionAttribute("basket", basket);

         return true;
     }

     public boolean onDetailsClick() {
         // Get the product clicked on by the user
         Integer productId = detailsLink.getValueInteger();
         Product product = getProductService().getProduct(productId);

         // Store the product in the request and display in the details page
         getContext().setRequestAttribute("product", product);
         setForward(ProductDetailsPage.class);

         return false;
     }

     public void onRender() {
         // Display the list of available products
         productList = getProductService().getProducts();
     }
 } 
See also the W3C HTML reference: A Links

Author:
Malcolm Edgar
See Also:
AbstractLink, Submit, Serialized Form

Field Summary
static String ACTION_LINK
          The action link parameter name:   actionLink.
protected  boolean clicked
          The link is clicked.
static String VALUE
          The value parameter name:   value.
 
Fields inherited from class net.sf.click.control.AbstractLink
disabled, imageSrc, label, parameters, tabindex, title
 
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
ActionLink()
          Create an ActionLink with no name defined.
ActionLink(Object listener, String method)
          Create an ActionLink for the given listener object and listener method.
ActionLink(String name)
          Create an ActionLink for the given name.
ActionLink(String name, Object listener, String method)
          Create an ActionLink for the given name, listener object and listener method.
ActionLink(String name, String label)
          Create an ActionLink for the given name and label.
ActionLink(String name, String label, Object listener, String method)
          Create an ActionLink for the given name, label, listener object and listener method.
 
Method Summary
 void bindRequestValue()
          This method binds the submitted request value to the ActionLink's value.
 String getHref()
          Return the ActionLink anchor <a> tag href attribute value.
 String getHref(Object value)
          Return the ActionLink anchor <a> tag href attribute for the given value.
 String getValue()
          Returns the ActionLink value if the action link was processed and has a value, or null otherwise.
 Double getValueDouble()
          Returns the action link Double value if the action link was processed and has a value, or null otherwise.
 Integer getValueInteger()
          Returns the ActionLink Integer value if the ActionLink was processed and has a value, or null otherwise.
 Long getValueLong()
          Returns the ActionLink Long value if the ActionLink was processed and has a value, or null otherwise.
 boolean isClicked()
          Returns true if the ActionLink was clicked, otherwise returns false.
 boolean onProcess()
          This method will set the isClicked() property to true if the ActionLink was clicked, and if an action callback listener was set this will be invoked.
 void setName(String name)
          Set the name of the Control.
 void setParent(Object parent)
          Set the parent of the ActionLink.
 void setValue(String value)
          Set the ActionLink value.
 void setValueObject(Object object)
          Set the value of the ActionLink using the given object.
 
Methods inherited from class net.sf.click.control.AbstractLink
getHtmlImports, getId, getImageSrc, getLabel, getParameter, getParameters, getTabIndex, getTag, getTitle, hasParameters, isDisabled, render, setDisabled, setImageSrc, setLabel, setParameter, setTabIndex, setTitle
 
Methods inherited from class net.sf.click.control.AbstractControl
addStyleClass, appendAttributes, getActionListener, getAttribute, getAttributes, getContext, getControlSizeEst, getMessage, getMessage, getMessage, getMessages, getName, getPage, getParent, getStyle, getStyles, hasAttribute, hasAttributes, hasStyles, onDeploy, onDestroy, onInit, onRender, registerActionEvent, removeStyleClass, renderTagBegin, renderTagEnd, setActionListener, setAttribute, setId, setListener, setStyle, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ACTION_LINK

public static final String ACTION_LINK
The action link parameter name:   actionLink.

See Also:
Constant Field Values

VALUE

public static final String VALUE
The value parameter name:   value.

See Also:
Constant Field Values

clicked

protected boolean clicked
The link is clicked.

Constructor Detail

ActionLink

public ActionLink(String name)
Create an ActionLink for the given name.

Please note the name 'actionLink' is reserved as a control request parameter name and cannot be used as the name of the control.

Parameters:
name - the action link name
Throws:
IllegalArgumentException - if the name is null

ActionLink

public ActionLink(String name,
                  String label)
Create an ActionLink for the given name and label.

Please note the name 'actionLink' is reserved as a control request parameter name and cannot be used as the name of the control.

Parameters:
name - the action link name
label - the action link label
Throws:
IllegalArgumentException - if the name is null

ActionLink

public ActionLink(Object listener,
                  String method)
Create an ActionLink for the given listener object and listener method.

Parameters:
listener - the listener target object
method - the listener method to call
Throws:
IllegalArgumentException - if the name, listener or method is null or if the method is blank

ActionLink

public ActionLink(String name,
                  Object listener,
                  String method)
Create an ActionLink for the given name, listener object and listener method.

Please note the name 'actionLink' is reserved as a control request parameter name and cannot be used as the name of the control.

Parameters:
name - the action link name
listener - the listener target object
method - the listener method to call
Throws:
IllegalArgumentException - if the name, listener or method is null or if the method is blank

ActionLink

public ActionLink(String name,
                  String label,
                  Object listener,
                  String method)
Create an ActionLink for the given name, label, listener object and listener method.

Please note the name 'actionLink' is reserved as a control request parameter name and cannot be used as the name of the control.

Parameters:
name - the action link name
label - the action link label
listener - the listener target object
method - the listener method to call
Throws:
IllegalArgumentException - if the name, listener or method is null or if the method is blank

ActionLink

public ActionLink()
Create an ActionLink with no name defined. Please note the control's name must be defined before it is valid.

Method Detail

isClicked

public boolean isClicked()
Returns true if the ActionLink was clicked, otherwise returns false.

Returns:
true if the ActionLink was clicked, otherwise returns false.

getHref

public String getHref(Object value)
Return the ActionLink anchor <a> tag href attribute for the given value. This method will encode the URL with the session ID if required using HttpServletResponse.encodeURL().

Parameters:
value - the ActionLink value parameter
Returns:
the ActionLink HTML href attribute

getHref

public String getHref()
Return the ActionLink anchor <a> tag href attribute value.

Specified by:
getHref in class AbstractLink
Returns:
the ActionLink anchor <a> tag HTML href attribute value

setName

public void setName(String name)
Set the name of the Control. Each control name must be unique in the containing Page model or the containing Form.

Please note the name 'actionLink' is reserved as a control request parameter name and cannot be used as the name of the control.

Specified by:
setName in interface Control
Overrides:
setName in class AbstractControl
Parameters:
name - of the control
Throws:
IllegalArgumentException - if the name is null
See Also:
Control.setName(String)

setParent

public void setParent(Object parent)
Set the parent of the ActionLink.

Specified by:
setParent in interface Control
Overrides:
setParent in class AbstractControl
Parameters:
parent - the parent of the Control
Throws:
IllegalStateException - if AbstractControl.name is not defined
IllegalArgumentException - if the given parent instance is referencing this object: if (parent == this)
See Also:
Control.setParent(Object)

getValue

public String getValue()
Returns the ActionLink value if the action link was processed and has a value, or null otherwise.

Returns:
the ActionLink value if the ActionLink was processed

getValueDouble

public Double getValueDouble()
Returns the action link Double value if the action link was processed and has a value, or null otherwise.

Returns:
the action link Double value if the action link was processed
Throws:
NumberFormatException - if the value cannot be parsed into a Double

getValueInteger

public Integer getValueInteger()
Returns the ActionLink Integer value if the ActionLink was processed and has a value, or null otherwise.

Returns:
the ActionLink Integer value if the action link was processed
Throws:
NumberFormatException - if the value cannot be parsed into an Integer

getValueLong

public Long getValueLong()
Returns the ActionLink Long value if the ActionLink was processed and has a value, or null otherwise.

Returns:
the ActionLink Long value if the action link was processed
Throws:
NumberFormatException - if the value cannot be parsed into a Long

setValue

public void setValue(String value)
Set the ActionLink value.

Parameters:
value - the ActionLink value

setValueObject

public void setValueObject(Object object)
Set the value of the ActionLink using the given object.

Parameters:
object - the object value to set

bindRequestValue

public void bindRequestValue()
This method binds the submitted request value to the ActionLink's value.


onProcess

public boolean onProcess()
This method will set the isClicked() property to true if the ActionLink was clicked, and if an action callback listener was set this will be invoked.

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()