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
Labels: -Type-Defect Type-Enhancement