Saturday, 14 July 2012

Apache Click: RightCheckBox Control a Custom Checkbox Class

Click API Checkbox Customization

This customization is required to provide the labels to the right. Current available API is designed in this package org.apache.click.control.Checkbox which is providing label to the left only and if we want to use the checkbox with label on right then we have to override the current org.apache.click.control.Checkbox class and make the changes into this. Current available checkbox is available in the following options only:
Checkbox
Let's have a look on how we can customize the existing feature of the apache click org.apache.click.control.Checkbox control class.
RightCheckBox.java
package com.components.ui.click.control;

import java.text.MessageFormat;
import org.apache.click.Context;
import org.apache.click.control.Checkbox;
import org.apache.click.control.Label;
import org.apache.click.util.HtmlStringBuffer;

public class RightCheckBox extends Checkbox {

    private static final long serialVersionUID = 1L;

    // -------------------------------------------------------------- Constants
    
    private String rightLabel;
    protected final static String VALIDATE_CHECKBOX_FUNCTION =
        "function validate_{0}() '{'\n"
        + "   var msg = validateCheckbox(''{0}'',{1}, [''{2}'']);\n"
        + "   if (msg) '{'\n"
        + "      return msg + ''|{0}'';\n"
        + "   '}' else '{'\n"
        + "      return null;\n"
        + "   '}'\n"
        + "'}'\n";

    /** The field checked value. */
    protected boolean checked;

    // ----------------------------------------------------------- Constructors

    public RightCheckBox(String name) {
        super(name);
    }

    public RightCheckBox(String name, String label) {
        super(name, label);
    }

    public RightCheckBox(String name, boolean required) {
        super(name);
        setRequired(required);
    }

    public RightCheckBox() {
    }

    // ------------------------------------------------------ Public Attributes
    @Override
    public String getTag() {
        return "input";
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean value) {
        checked = value;
    }

    public String getType() {
        return "checkbox";
    }

    @Override
    public String getValue() {
        return String.valueOf(checked);
    }

    @Override
    public void setValue(String value) {
        checked = Boolean.valueOf(value);
     }

   public void setRightLabel(String label){
    rightLabel=label;
    setLabel("");
    }
    public String getRightLabel(){
      return rightLabel;
    }
    
    @Override
    public Object getValueObject() {
        if (checked) {
            return Boolean.TRUE;

        } else {
            return Boolean.FALSE;
        }
    }

    @Override
    public void setValueObject(Object object) {
        if (object != null && object instanceof Boolean) {
            checked = (Boolean) object;
        }
    }

    @Override
    public String getValidationJavaScript() {
        if (isRequired()) {
            Object[] args = new Object[3];
            args[0] = getId();
            args[1] = String.valueOf(isRequired());
            args[2] = getMessage("not-checked-error", getErrorLabel());

            return MessageFormat.format(VALIDATE_CHECKBOX_FUNCTION, args);

        } else {
            return null;
        }
    }

    // --------------------------------------------------------- Public Methods
    @Override
    public void bindRequestValue() {
        setChecked(getContext().getRequestParameter(getName()) != null);
    }

    @Override
    public boolean onProcess() {
        if (isDisabled()) {
            Context context = getContext();

            // Switch off disabled property if control has incoming request
            // parameter. Normally this means the field was enabled via JS
            if (context.hasRequestParameter(getName())) {
                setDisabled(false);
            } else {
                // If field is disabled skip process event
                return true;
            }
        }

        // In Html an unchecked Checkbox does not submit it's name/value so we
        // always validate and dispatch registered events
        bindRequestValue();

        if (getValidate()) {
            validate();
        }

        dispatchActionEvent();

        return true;
    }
    
    @Override
    public void render(HtmlStringBuffer buffer) {
     super.render(buffer);
     if(getRightLabel()!=null)
     buffer.append("

"); } @Override public void validate() { setError(null); if (isRequired() && !isChecked()) { setErrorMessage("not-checked-error"); } } }
After customization and overriding the methods the new class com.components.ui.click.control.RightCheckBox will have the backward compatibility and we have the option to use both either right or left label using RightCheckBox class. After overrriding the current class and overriding the methods we will be able to make Checkbox in the following options:
Checkbox
How to use RightCheckBox Class To use RightCheckBox class we need to create a demo java class let's assume RightCheckboxDemo is the name of the demo class which will use the above class to show the desired user interface: RightCheckboxDemo.java
package com.demo.page;

import org.apache.click.control.Form;

public class RightCheckboxDemo extends BorderPage{
 private Form form = new Form("form");
 private RightCheckBox checkbox1=new RightCheckBox("checkbox1");
 private RightCheckBox checkbox2=new RightCheckBox("checkbox2");
 private RightCheckBox checkbox3=new RightCheckBox("checkbox3");
 private RightCheckBox checkbox4=new RightCheckBox("checkbox4");
 private RightCheckBox checkbox5=new RightCheckBox("checkbox5");
 
 public RightCheckboxDemo(){
  form.add(checkbox1);
  form.add(checkbox2);
  form.add(checkbox3);
  form.add(checkbox4);
  form.add(checkbox5);
  
   //Using setLabel we can set the default label and lable will be on left side
  checkbox1.setLabel("Item 1");

 //Using setRightLabel we can set the custom label and lable will be on right side
  checkbox2.setRightLabel("Item 2");
  checkbox3.setRightLabel("Item 3");
  checkbox4.setRightLabel("Item 4");
  checkbox5.setRightLabel("Item 5");
  
  addControl(form);
 }
}

By using the addControl(form) we have provided the form to the htm page. Now we have to right one htm page to render the user interface: RightCheckboxDemo.htm
<html>
<head><title>Demo Page</title>
</head>
<body>
   $form
</body>
</html> 
Finally the user interface is ready to render and we can see how is this looks like:

Please share your comments or suggestion. You can visit our website and leave you comment over there. To visit our website visit on:
VaaCom : A Blog on Technology

Microsoft Dynamics CRM - Java Web Client Implementation

CRM - Microsoft Dynamics A Cloud based Customer Relationship Management(CRM) software package developed by Microsoft. Microsoft Dynamics ...