net.sf.click.extras.control
Class LinkDecorator

java.lang.Object
  extended bynet.sf.click.extras.control.LinkDecorator
All Implemented Interfaces:
Decorator, Serializable

public class LinkDecorator
extends Object
implements Decorator, Serializable

Provides a table column AbstractLink and ActionButton Decorator.

LinkDecorator Example

The example below uses a LinkDecorator to render the row ActionLinks in the tables "Action" column.
 public class EditTable extends BorderPage {

     public Table table = new Table();
     public ActionLink editLink = new ActionLink("edit", "Edit", this, "onEditClick");
     public ActionLink deleteLink = new ActionLink("delete", "Delete", this, "onDeleteClick");

     public EditTable() {
         table.addColumn(new Column("name"));
         table.addColumn(new Column("email"));
         table.addColumn(new Column("holdings"));
         table.addColumn(new Column("dateJoined"));

         Column column = new Column("Action");
         ActionLink[] links = new ActionLink[]{editLink, deleteLink};
         column.setDecorator(new LinkDecorator(table, links, "id"));
         table.addColumn(column);

         deleteLink.setAttribute("onclick", "return window.confirm('Please confirm delete');");
     }

     public boolean onEditClick() {
         Integer id = editLink.getValueInteger();
         Customer customer = getCustomerService().getCustomer(id);

         ..
     }

     public boolean onDeleteClick() {
         Integer id = deleteLink.getValueInteger();
         getCustomerService().deleteCustomer(id);
         return true;
     }

     public void onRender() {
         List customers = getCustomerService().getCustomersSortedByName(12);
         table.setRowList(customers);
     }
 } 
The LinkDecorator class automatically supports table paging.

Multiple parameters

On rare occasions it is useful to add extra parameters or even replace the default parameter.

In such cases you can override the methods renderActionLink or renderActionButton.

In the example below we want to send both the state and postCode parameters to the AddressPage:

 public class SelectPostCode extends BorderPage {

     private Table table = new Table("table");

     public SelectPostCode() {
         ...
         PageLink selectPostCode = new PageLink("select", AddressPage.class);
         Column action = new Column("action");
         String idProperty = "postCode";
         LinkDecorator decorator = new LinkDecorator(table, selectPostCode, idProperty) {
             protected void renderActionLink(HtmlStringBuffer buffer, AbstractLink link, Context context, Object row, Object value) {
                 // We want to add the PostCode "state" as an extra parameter
                 // to the action link
                 link.setParameter("state", ((PostCode) row).getState());

                 // You can manipulate the link parameters as needed, and
                 // even remove the default idProperty parameter.
                 // Object currentValue = link.getParameters().remove(this.idProperty);

                 // NB we invoke the super implementation here for default rendering to continue
                 super.renderActionLink(buffer, link, context, row, value);
             }
         }
         action.setDecorator(decorator);
         table.addColumn(action);
     };

     public void onRender() {
         table.setRowList(getPostCodeService().getPostCodes());
     }
 } 
In the above example the LinkDecorator will extract the idProperty value ("state") for each object in the table. The PageLinks will render as follows:
 <a href="/mycorp/address-page.htm?postCode=6089&state=NSW>Select</a>
 

This class was inspired by Richardo Lecheta's ViewDecorator design pattern.

Author:
Malcolm Edgar
See Also:
ActionLink, PageLink, Serialized Form

Field Summary
protected  ActionButton[] buttonsArray
          The array of ActionButtons to render.
protected  String idProperty
          The row object identifier property.
protected  AbstractLink[] linksArray
          The array of AbstractLinks to render.
protected  String linkSeparator
          The link separator string, default value is " | ".
protected  Map methodCache
          The method cached for rendering column values.
protected  Table table
          The table to render the links for.
 
Constructor Summary
LinkDecorator(Table table, AbstractLink[] links, String idProperty)
          Create a new AbstractLink table column Decorator with the given AbstractLinks array and row object identifier property name.
LinkDecorator(Table table, AbstractLink link, String idProperty)
          Create a new AbstractLink table column Decorator with the given actionLink and row object identifier property name.
LinkDecorator(Table table, ActionButton[] buttons, String idProperty)
          Create a new ActionButton table column Decorator with the given ActionButtons array and row object identifier property name.
LinkDecorator(Table table, ActionButton button, String idProperty)
          Create a new AbstractLink table column Decorator with the given ActionButton and row object identifier property name.
LinkDecorator(Table table, List controls, String idProperty)
          Create a new table column Decorator with the given list of AbstractLinks or ActionButtons and row object identifier property name.
 
Method Summary
 String getLinkSeparator()
          Return the link separator string.
protected  void initButton(ActionButton button, Context context, Object value)
          Initialize the button value and parameters.
protected  void initLink(AbstractLink link, Context context, Object value)
          Initialize the link value and parameters.
 String render(Object row, Context context)
          Render the given row object using the links or buttons.
protected  void renderActionButton(HtmlStringBuffer buffer, ActionButton button, Context context, Object row, Object value)
          Render the button to the specified buffer.
 String renderActionButtons(Object row, Context context)
          Render the given row object using the actionButtons array.
protected  void renderActionLink(HtmlStringBuffer buffer, AbstractLink link, Context context, Object row, Object value)
          Render the link to the specified buffer.
 String renderActionLinks(Object row, Context context)
          Render the given row object using the actionLinks array.
 void setLinkSeparator(String value)
          Set the link separator string with the given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

idProperty

protected String idProperty
The row object identifier property.


linksArray

protected AbstractLink[] linksArray
The array of AbstractLinks to render.


buttonsArray

protected ActionButton[] buttonsArray
The array of ActionButtons to render.


linkSeparator

protected String linkSeparator
The link separator string, default value is " | ".


table

protected Table table
The table to render the links for.


methodCache

protected transient Map methodCache
The method cached for rendering column values.

Constructor Detail

LinkDecorator

public LinkDecorator(Table table,
                     AbstractLink link,
                     String idProperty)
Create a new AbstractLink table column Decorator with the given actionLink and row object identifier property name.

Example usage of this constructor:

 Table table = new Table("table");

 public void onInit() {
     ... // setup other columns
     ActionLink selectState = new ActionLink("select");
     Column action = new Column("action");
     String idProperty = "state";
     LinkDecorator decorator = new LinkDecorator(table, selectState, idProperty);
     action.setDecorator(decorator);
     table.addColumn(action);
     ...
 }

 public void onRender() {
     // Populate the table rows with post code instances
     table.setRowList(getPostCodeService().getPostCodes());
 } 
In the above example the LinkDecorator will extract the idProperty value ("state") from each PostCode instance in the table.

The idProperty value will also be used as the name of the request parameter. In this example the idProperty value is "state" thus the request parameter name will also be "state".

For the PostCode "NSW" the PageLink will render as follows:

 <a href="/mycorp/postcodes.htm?state=NSW>Select</a>
 

Parameters:
table - the table to render the links for
link - the AbstractLink to render
idProperty - the row object identifier property name

LinkDecorator

public LinkDecorator(Table table,
                     AbstractLink[] links,
                     String idProperty)
Create a new AbstractLink table column Decorator with the given AbstractLinks array and row object identifier property name.

Parameters:
table - the table to render the links for
links - the array of AbstractLinks to render
idProperty - the row object identifier property name
See Also:
LinkDecorator(net.sf.click.control.Table, net.sf.click.control.AbstractLink, java.lang.String)

LinkDecorator

public LinkDecorator(Table table,
                     ActionButton button,
                     String idProperty)
Create a new AbstractLink table column Decorator with the given ActionButton and row object identifier property name. The default linkSeparator for buttons is " ".

Parameters:
table - the table to render the links for
button - the ActionButton to render
idProperty - the row object identifier property name
See Also:
LinkDecorator(net.sf.click.control.Table, net.sf.click.control.AbstractLink, java.lang.String)

LinkDecorator

public LinkDecorator(Table table,
                     ActionButton[] buttons,
                     String idProperty)
Create a new ActionButton table column Decorator with the given ActionButtons array and row object identifier property name. The default linkSeparator for buttons is " ".

Parameters:
table - the table to render the links for
buttons - the array of ActionButtons to render
idProperty - the row object identifier property name
See Also:
LinkDecorator(net.sf.click.control.Table, net.sf.click.control.AbstractLink, java.lang.String)

LinkDecorator

public LinkDecorator(Table table,
                     List controls,
                     String idProperty)
Create a new table column Decorator with the given list of AbstractLinks or ActionButtons and row object identifier property name. The default linkSeparator for buttons are " ".

Please note you must provide either AbstractLink objects or ActionButton objects in the controls array, but not a mixture of both.

Parameters:
table - the table to render the links for
controls - the list of AbstractLink or ActionButtons to render
idProperty - the row object identifier property name
See Also:
LinkDecorator(net.sf.click.control.Table, net.sf.click.control.AbstractLink, java.lang.String)
Method Detail

getLinkSeparator

public String getLinkSeparator()
Return the link separator string. The default value is " | ".

Returns:
the link separator string.

setLinkSeparator

public void setLinkSeparator(String value)
Set the link separator string with the given value.

Parameters:
value - the link separator string value

render

public String render(Object row,
                     Context context)
Render the given row object using the links or buttons.

Specified by:
render in interface Decorator
Parameters:
row - the row object to render
context - the request context
Returns:
the rendered links for the given row object and request context
See Also:
Decorator.render(java.lang.Object, net.sf.click.Context)

renderActionLinks

public String renderActionLinks(Object row,
                                Context context)
Render the given row object using the actionLinks array.

This method initializes each link in actionLinks array by invoking initLink(net.sf.click.control.AbstractLink, net.sf.click.Context, java.lang.Object).

This method also renders each link in the array by invoking renderActionLink(net.sf.click.util.HtmlStringBuffer, net.sf.click.control.AbstractLink, net.sf.click.Context, java.lang.Object, java.lang.Object).

Parameters:
row - the row object to render
context - the request context
Returns:
the rendered links for the given row object and request context

renderActionButtons

public String renderActionButtons(Object row,
                                  Context context)
Render the given row object using the actionButtons array.

This method initializes each button in actionButtons array by invoking initButton(net.sf.click.control.ActionButton, net.sf.click.Context, java.lang.Object).

This method also renders each button in the array by invoking renderActionButton(net.sf.click.util.HtmlStringBuffer, net.sf.click.control.ActionButton, net.sf.click.Context, java.lang.Object, java.lang.Object).

Parameters:
row - the row object to render
context - the request context
Returns:
the rendered buttons for the given row object and request context

renderActionLink

protected void renderActionLink(HtmlStringBuffer buffer,
                                AbstractLink link,
                                Context context,
                                Object row,
                                Object value)
Render the link to the specified buffer.

If this method is overridden to add extra parameters to the link, remember to invoke super.renderActionLink so default rendering can continue.

Parameters:
buffer - the specified buffer to render the link output to
link - the link to render
context - the request context
row - the table row being rendered
value - the value of the link

renderActionButton

protected void renderActionButton(HtmlStringBuffer buffer,
                                  ActionButton button,
                                  Context context,
                                  Object row,
                                  Object value)
Render the button to the specified buffer.

If this method is overridden to add extra parameters to the button, remember to invoke super.renderActionButton so default rendering can continue.

Parameters:
buffer - the specified buffer to render the button output to
button - the button to render
context - the request context
row - the table row being rendered
value - the value of the button
See Also:
renderActionLink(net.sf.click.util.HtmlStringBuffer, net.sf.click.control.AbstractLink, net.sf.click.Context, java.lang.Object, java.lang.Object)

initLink

protected void initLink(AbstractLink link,
                        Context context,
                        Object value)
Initialize the link value and parameters.

Parameters:
link - the link to initialize
context - the request context
value - the value of the link

initButton

protected void initButton(ActionButton button,
                          Context context,
                          Object value)
Initialize the button value and parameters.

Parameters:
button - the button to initialize
context - the request context
value - the value of the button