VadimV1144
25 Jan 2010, 12:37 AM
Here is a little widget that may be useful. I have modified the CheckBoxListView to :
a) Allow user to check multiple boxes without holding down the 'Ctrl' key.
b) Display the Radio buttons instead of the Checkboxes.
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ListViewEvent;
import com.extjs.gxt.ui.client.widget.CheckBoxListView;
import com.extjs.gxt.ui.client.widget.ListViewSelectionModel;
import com.google.gwt.user.client.Element;
import java.util.Arrays;
import java.util.List;
public class XRadioListView<M extends ModelData> extends CheckBoxListView<M> {
private Boolean isRadio = false;
private String groupName = "";
private String selectionProperty = "";
private static final String CHECKED_PROPERTY_ID = "checked";
/**
* Default Constructor. Sets the XListViewSelectionModel as default model
* for this instance.
*/
public XRadioListView(){
super();
setSelectionModel(new XListViewSelectionModel<M>());
}
/**
* Sets the type of the buttons that will be displayed by this view.
* As wells as the button group name.
* @param isRadio - if true Radio buttons are displayed, otherwise display CheckBoxes
* @param groupName - button group name
*/
public void setRadio(Boolean isRadio, String groupName){
this.isRadio = isRadio;
this.groupName = groupName == null?"":groupName;
if(getSelectionModel() instanceof XListViewSelectionModel){
((XListViewSelectionModel)getSelectionModel()).setKeepExisting(!isRadio);
}
}
/**
* Return the selection property
* @return property of the model that will be set/unset on the selection event
*/
public String getSelectionProperty() {
return selectionProperty;
}
/**
* Set the selection property
* @param selectionProperty will be set/unset on every selection event
*/
public void setSelectionProperty(String selectionProperty) {
if(selectionProperty != null && !selectionProperty.isEmpty())
this.selectionProperty = selectionProperty;
}
/**
* Initialise the button based on the selectionProperty
* If the selectionProperty is set, the button will be selected
* @param model is the bean model that associated with the given button
*/
public void initSelection(M model){
if(!selectionProperty.isEmpty()){
if(model.<Boolean>get(selectionProperty)) {
this.getSelectionModel().select(model, true);
}
//model.set(CHECKED_PROPERTY_ID, getCheckedValue());
}
}
/**
* Sets the selectionProperty on the model based on the select value (true or false)
* Sets the button selection based on the select value (this is necessary to handle the case
* when the user clicks on the label and not the actual button)
*
* @param model - selected model
* @param select - selection type - true if selected, false if deselected
*/
protected void onSelectChange(M model, boolean select){
if(!selectionProperty.isEmpty()){
model.set(selectionProperty, select);
}
model.set(CHECKED_PROPERTY_ID,getCheckedValue(select));
super.onSelectChange(model, select);
}
protected void onRender(Element target, int index) {
String spacing = GXT.isIE ? "0" : "3";
String type = isRadio?"radio":"checkbox";
setTemplate(XTemplate.create("<tpl for=\".\"><div class='x-view-item x-view-item-check'>"
+ "<table cellspacing='" + spacing + "' cellpadding=0> "
+ "<tr>"
+ "<td>"
+ "<input class=\"x-view-item-checkbox\" type=\"" + type + "\" name=\"" + groupName + "\" {" + CHECKED_PROPERTY_ID + "}/>"
+ "</td>"
+ "<td>{" + getDisplayProperty() + "}"
+ "</td>"
+ "</tr></table></div></tpl>"));
super.onRender(target, index);
}
private String getCheckedValue(boolean checked){
return checked?"checked":"";
}
public class XListViewSelectionModel<M extends ModelData> extends ListViewSelectionModel<M> {
private Boolean keepExisting = false;
public Boolean isKeepExisting() {
return keepExisting;
}
public void setKeepExisting(Boolean keepExisting) {
this.keepExisting = keepExisting;
}
/**
* Replaces the passed argument keepExisting with the local keepExisting.
*
* Note: This is necessary to allow user to select multiple options without
* holding the Ctrl button
*/
protected void doSelect(List<M> models, boolean keepExisting, boolean supressEvent) {
super.doSelect(models, this.keepExisting, supressEvent);
}
/**
* Bypass the check for the Ctrl button press.
*
* Note: This is necessary to allow user to select multiple options without
* holding the Ctrl button
*/
@SuppressWarnings("unchecked")
protected void handleMouseClick(ListViewEvent<M> e) {
if (isLocked() || e.getIndex() == -1) {
return;
}
if (!e.isRightClick() && selectionMode == Style.SelectionMode.MULTI) {
M sel = listStore.getAt(e.getIndex());
if (isSelected(sel)) {
doDeselect(Arrays.asList(sel), false);
} else {
handleMouseClick(e);
}
}
}
}
}
In order for the checkboxes to display in a ListView format add the following to your CSS:
#checkbox-view .x-view-item {
float: left;
margin: 4px;
margin-right: 0;
padding: 5px;
}
a) Allow user to check multiple boxes without holding down the 'Ctrl' key.
b) Display the Radio buttons instead of the Checkboxes.
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.core.XTemplate;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ListViewEvent;
import com.extjs.gxt.ui.client.widget.CheckBoxListView;
import com.extjs.gxt.ui.client.widget.ListViewSelectionModel;
import com.google.gwt.user.client.Element;
import java.util.Arrays;
import java.util.List;
public class XRadioListView<M extends ModelData> extends CheckBoxListView<M> {
private Boolean isRadio = false;
private String groupName = "";
private String selectionProperty = "";
private static final String CHECKED_PROPERTY_ID = "checked";
/**
* Default Constructor. Sets the XListViewSelectionModel as default model
* for this instance.
*/
public XRadioListView(){
super();
setSelectionModel(new XListViewSelectionModel<M>());
}
/**
* Sets the type of the buttons that will be displayed by this view.
* As wells as the button group name.
* @param isRadio - if true Radio buttons are displayed, otherwise display CheckBoxes
* @param groupName - button group name
*/
public void setRadio(Boolean isRadio, String groupName){
this.isRadio = isRadio;
this.groupName = groupName == null?"":groupName;
if(getSelectionModel() instanceof XListViewSelectionModel){
((XListViewSelectionModel)getSelectionModel()).setKeepExisting(!isRadio);
}
}
/**
* Return the selection property
* @return property of the model that will be set/unset on the selection event
*/
public String getSelectionProperty() {
return selectionProperty;
}
/**
* Set the selection property
* @param selectionProperty will be set/unset on every selection event
*/
public void setSelectionProperty(String selectionProperty) {
if(selectionProperty != null && !selectionProperty.isEmpty())
this.selectionProperty = selectionProperty;
}
/**
* Initialise the button based on the selectionProperty
* If the selectionProperty is set, the button will be selected
* @param model is the bean model that associated with the given button
*/
public void initSelection(M model){
if(!selectionProperty.isEmpty()){
if(model.<Boolean>get(selectionProperty)) {
this.getSelectionModel().select(model, true);
}
//model.set(CHECKED_PROPERTY_ID, getCheckedValue());
}
}
/**
* Sets the selectionProperty on the model based on the select value (true or false)
* Sets the button selection based on the select value (this is necessary to handle the case
* when the user clicks on the label and not the actual button)
*
* @param model - selected model
* @param select - selection type - true if selected, false if deselected
*/
protected void onSelectChange(M model, boolean select){
if(!selectionProperty.isEmpty()){
model.set(selectionProperty, select);
}
model.set(CHECKED_PROPERTY_ID,getCheckedValue(select));
super.onSelectChange(model, select);
}
protected void onRender(Element target, int index) {
String spacing = GXT.isIE ? "0" : "3";
String type = isRadio?"radio":"checkbox";
setTemplate(XTemplate.create("<tpl for=\".\"><div class='x-view-item x-view-item-check'>"
+ "<table cellspacing='" + spacing + "' cellpadding=0> "
+ "<tr>"
+ "<td>"
+ "<input class=\"x-view-item-checkbox\" type=\"" + type + "\" name=\"" + groupName + "\" {" + CHECKED_PROPERTY_ID + "}/>"
+ "</td>"
+ "<td>{" + getDisplayProperty() + "}"
+ "</td>"
+ "</tr></table></div></tpl>"));
super.onRender(target, index);
}
private String getCheckedValue(boolean checked){
return checked?"checked":"";
}
public class XListViewSelectionModel<M extends ModelData> extends ListViewSelectionModel<M> {
private Boolean keepExisting = false;
public Boolean isKeepExisting() {
return keepExisting;
}
public void setKeepExisting(Boolean keepExisting) {
this.keepExisting = keepExisting;
}
/**
* Replaces the passed argument keepExisting with the local keepExisting.
*
* Note: This is necessary to allow user to select multiple options without
* holding the Ctrl button
*/
protected void doSelect(List<M> models, boolean keepExisting, boolean supressEvent) {
super.doSelect(models, this.keepExisting, supressEvent);
}
/**
* Bypass the check for the Ctrl button press.
*
* Note: This is necessary to allow user to select multiple options without
* holding the Ctrl button
*/
@SuppressWarnings("unchecked")
protected void handleMouseClick(ListViewEvent<M> e) {
if (isLocked() || e.getIndex() == -1) {
return;
}
if (!e.isRightClick() && selectionMode == Style.SelectionMode.MULTI) {
M sel = listStore.getAt(e.getIndex());
if (isSelected(sel)) {
doDeselect(Arrays.asList(sel), false);
} else {
handleMouseClick(e);
}
}
}
}
}
In order for the checkboxes to display in a ListView format add the following to your CSS:
#checkbox-view .x-view-item {
float: left;
margin: 4px;
margin-right: 0;
padding: 5px;
}