Issue 194: CheckBoxCellEditor proposal
Status:  Accepted
Owner: ----
Reported by AlejaV...@gmail.com, May 13, 2009
Hi, I have been dealing with many tables that has boolean columns and
always look better if a checkbox is used. So tired of copy-pasting the code
to render it inside every column, I made a CheckBoxCellEditor class. I
created it so it can be used also when exporting to PDF, EXCEL,... letting
you choose the values to show when "true" or "false".
Here it is:

import org.jmesa.util.ItemUtils;
import org.jmesa.view.editor.AbstractCellEditor;
import org.jmesa.view.html.HtmlBuilder;


public class CheckBoxCellEditor extends AbstractCellEditor {
	

	String trueValue="YES";
	String falseValue="NO";
	boolean clickable=false;
	boolean disabled=false;
	
	public CheckBoxCellEditor() {
		
	}
	/**
	 * @param trueValue Value used in exporting when the boolean property is true
	 * @param falseValue Value used in exporting when the boolean property is
false
	 */
	public CheckBoxCellEditor(String trueValue, String falseValue){
		this.trueValue = trueValue;
		this.falseValue = falseValue;
	}
	
	/**
	 * @param trueValue Value used in exporting when the boolean property is true
	 * @param falseValue Value used in exporting when the boolean property is
false
	 * @param clickable Makes the checkbox clickable or not (by Javascript)
	 */
	public CheckBoxCellEditor(String trueValue, String falseValue, boolean
clickable){
		this.trueValue = trueValue;
		this.falseValue = falseValue;
		this.clickable = clickable;
	}
	
	/**
	 * 
	 * @param trueValue Value used in exporting when the boolean property is true
	 * @param falseValue Value used in exporting when the boolean property is
false
	 * @param clickable Makes the checkbox clickable or not (by Javascript)
	 * @param disabled Makes the chekbox disabled or not
	 */
	public CheckBoxCellEditor(String trueValue, String falseValue, boolean
clickable, boolean disabled ) {
		this.trueValue = trueValue;
		this.falseValue = falseValue;
		this.clickable = clickable;
		this.disabled = disabled;
	}
	
    public Object getValue(Object item, String property, int rowcount) {
    	  boolean value =  (Boolean) ItemUtils.getItemValue(item, property);
   	  
    	  boolean isExported = getCoreContext().getLimit().isExported();
    	  
    	  // If not exporting. We render the checkbox, checked or not,
clickable or not, disabled or not.
    	  if (!isExported){
    		  HtmlBuilder html = new HtmlBuilder();
    		  html.p().align("center").close();
    		  	html.input().type("checkbox");
    		  	if (value) html.checked();
    		  	if (!clickable) html.onclick("return false;");
    		  	if (disabled) html.disabled();
    		  	html.close();
    		  html.pEnd();
    		  return html.toString();
    	  }
    	  // If we are exporting, we render only the true and false values
    	  else {
    		  if (value)return trueValue;
    		  else return falseValue;
    	  }

    }
    
    
}


I also recommend this for boolean columns:

tableFacade.addFilterMatcher(new MatcherKey(boolean.class), new
StringFilterMatcher());
This applies the default StringFilterMatcher to a boolean column or bean
property. So you can filter writting "true" or "false" (or "t","f"...). But
I prefer adding also this to the boolean columns:
column.getFilterRenderer().setFilterEditor(new DroplistFilterEditor());

So you can choose between "true" and "false" in the filter editor.

Hopes this helps someone as much as Jmesa helps me with my tables.

Keep up the good work and I offer myself for testing/developing for this
project,
Alex

May 13, 2009
Project Member #1 jeff.johnston.mn@gmail.com
Thank you! I think this will be a nice addition to the API. I will bring this in over
the weekend.

Feel free to jump in and contribute for things you need and/or find interesting. I do
get more contributions nowadays and have been told that the API is pretty easy to extend.

What I do when I get code contributions is really make sure that it is consistent
with the rest of the framework. If something is too custom I like to work with the
developer and figure out how to make it generic for general use. So far that has
worked out well.
Status: Accepted
Labels: -Type-Defect Type-Enhancement
May 14, 2009
#2 AlejaV...@gmail.com
You're welcome. It's actually a pleasure contributing to this project so I can repay
the excellent work that jmesa does for my web applications. I have some other custom
methods for my tables that may be interesting and probably will post them here for
others to take advantage of.
As you said, one of the best features of jmesa is how flexible it is and allows
anyone to make it fit to their desires. 
I think I would be able to post some of my tricks before the weekend.

Cheers,
Alex