Last active
July 28, 2021 18:59
-
-
Save Aadi1/4949994 to your computer and use it in GitHub Desktop.
A GWT / GXT3 compositecell with Checkbox which has customizable text rendering. Uses XTemplate. To use only GWT, use Template instead of XTemplate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.gwt.cell.client.Cell; | |
import com.google.gwt.cell.client.CheckboxCell; | |
import com.google.gwt.cell.client.CompositeCell; | |
import com.google.gwt.cell.client.FieldUpdater; | |
import com.google.gwt.cell.client.HasCell; | |
import com.google.gwt.cell.client.TextCell; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.safehtml.shared.SafeHtml; | |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; | |
import com.google.gwt.user.cellview.client.CellTable; | |
import com.google.gwt.user.cellview.client.Column; | |
import com.google.gwt.user.client.ui.IsWidget; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.google.gwt.view.client.SingleSelectionModel; | |
import com.sencha.gxt.core.client.XTemplates; | |
import com.sencha.gxt.legacy.client.data.ModelData; | |
/** | |
* This widget will create a Checkbox List view. | |
* See MyCheckBoxListExample for usage. | |
*/ | |
public class MyCheckBoxList implements IsWidget { | |
private SingleSelectionModel<ModelData> ssm = new SingleSelectionModel<ModelData>(); | |
HasCell<ModelData, String> textCell; | |
List<ModelData> data; | |
public void setData(List<ModelData> data) { | |
this.data = data; | |
} | |
/** | |
* Create a singleton instance of the templates used to render the cell. | |
*/ | |
private DefaultRenderer cellRenderer = GWT.create(DefaultRenderer.class); | |
/** | |
* Default text layout | |
*/ | |
public interface DefaultRenderer extends XTemplates { | |
@XTemplate("<span> {pName}</span>") | |
public SafeHtml render(String pName); | |
} | |
/** | |
* Default constructor. Uses default text cell implementation of this class | |
*/ | |
public MyCheckBoxList() { | |
textCell = new DefaultTextCellImpl(); | |
} | |
/** | |
* To customize the text cell user can create their own text Cell and pass it. | |
* See MyCheckBoxListExample for usage. | |
* @param customTextCell | |
*/ | |
public MyCheckBoxList(HasCell<ModelData, String> customTextCell) { | |
textCell = customTextCell; | |
} | |
@Override | |
public Widget asWidget() { | |
// Now create a Table which takes an object i.e BaseDataMode | |
final CellTable<ModelData> table = new CellTable<ModelData>(); | |
// Create a list of cell. These cells will make up the composite cell | |
// Here I am constructing a composite cell with 2 parts that includes a checkbox. | |
List<HasCell<ModelData, ?>> cellComponents = new ArrayList<HasCell<ModelData, ?>>(); | |
// 1st part of Composite cell - Show a checkbox and select it "selected property is true | |
cellComponents.add(new HasCell<ModelData, Boolean>() { | |
// These booleans (false,true) are very important for right behavior of CBCell selection. | |
private CheckboxCell cell = new CheckboxCell(false, true); | |
public Cell<Boolean> getCell() { | |
return cell; | |
} | |
public FieldUpdater<ModelData, Boolean> getFieldUpdater() { | |
return new FieldUpdater<ModelData, Boolean>() { | |
public void update(int index, ModelData object, Boolean isCBChecked) { | |
ssm.setSelected(object, isCBChecked); | |
} | |
}; | |
} | |
public Boolean getValue(ModelData object) { | |
return object.get("selected"); | |
} | |
}); | |
// 2nd part of Composite cell - Show Text for the CB Cell | |
cellComponents.add(textCell); | |
// Create a composite cell and pass the definition of | |
// individual cells that the composite cell should render. | |
CompositeCell<ModelData> compositeCell = new CompositeCell<ModelData>(cellComponents); | |
Column colCC = new Column<ModelData, ModelData>(compositeCell) { | |
@Override | |
public ModelData getValue(ModelData object) { | |
return object; | |
} | |
}; | |
// Here we are adding a Column. This column is rendered using the composite cell | |
table.addColumn(colCC); | |
table.setRowData(data); | |
table.setSelectionModel(ssm); | |
return table; | |
} | |
/** | |
* The user is responsible to get this ModelData and do something with it. Like update the selected items, etc. | |
* @return | |
*/ | |
public SingleSelectionModel<ModelData> getSelectionModel() { | |
return ssm; | |
} | |
/** | |
* This is the default TextCell implementation. Will be used if the user don't plan to customize the text cell | |
*/ | |
class DefaultTextCellImpl implements HasCell<ModelData, String> { | |
TextCell txtCell = new TextCell() { | |
@Override | |
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) { | |
if (value == null) return; | |
SafeHtml rendered = cellRenderer.render(value.asString()); | |
sb.append(rendered); | |
} | |
}; | |
public Cell<String> getCell() { | |
return txtCell; | |
} | |
public FieldUpdater<ModelData, String> getFieldUpdater() { | |
return null; | |
} | |
@Override | |
public String getValue(ModelData object) { | |
return object.get("name"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.gwt.cell.client.Cell; | |
import com.google.gwt.cell.client.FieldUpdater; | |
import com.google.gwt.cell.client.HasCell; | |
import com.google.gwt.cell.client.TextCell; | |
import com.google.gwt.core.client.EntryPoint; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.safehtml.shared.SafeHtml; | |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; | |
import com.google.gwt.user.client.ui.HorizontalPanel; | |
import com.google.gwt.user.client.ui.RootPanel; | |
import com.google.gwt.user.client.ui.Widget; | |
import com.sencha.gxt.core.client.XTemplates; | |
import com.sencha.gxt.legacy.client.data.BaseModelData; | |
import com.sencha.gxt.legacy.client.data.ModelData; | |
import com.sencha.gxt.widget.core.client.FramedPanel; | |
public class MyCheckBoxListExample implements EntryPoint{ | |
List<ModelData> data; | |
@Override | |
public void onModuleLoad() { | |
HorizontalPanel hp = new HorizontalPanel(); | |
Widget w1 = getDefaultWidget(); | |
hp.add(w1); | |
Widget w2 = getCustomWidget(); | |
hp.add(w2); | |
RootPanel.get().add(hp); | |
} | |
private List<ModelData> _getData() { | |
BaseModelData data1 = new BaseModelData(); | |
data1.set("id", Long.toString(2222222103l)); | |
data1.set("name", "ProductItem_101"); | |
data1.set("selected", true); | |
BaseModelData data2 = new BaseModelData(); | |
data2.set("id", Long.toString(5555555102l)); | |
data2.set("name", "ProductItem_102"); | |
data2.set("selected", false); | |
BaseModelData data3 = new BaseModelData(); | |
data3.set("id", Long.toString(1111111101l)); | |
data3.set("name", "ProductItem_103"); | |
data3.set("selected", true); | |
data = new ArrayList<ModelData>(); | |
data.add(data1); | |
data.add(data2); | |
data.add(data3); | |
return data; | |
} | |
public FramedPanel getDefaultWidget() { | |
final MyCheckBoxList acbList = new MyCheckBoxList(); | |
acbList.setData(_getData()); | |
FramedPanel fp = new FramedPanel(); | |
fp.setBorders(false); | |
fp.setHeadingHtml("Default List"); | |
fp.setWidth(250); | |
fp.setBorders(true); | |
fp.add(acbList.asWidget()); | |
return fp; | |
} | |
// ---------------------------- If you want the text to be rendered in a special way do as below------------------- | |
public interface TwoLineRenderer extends XTemplates { | |
@XTemplates.XTemplate("<span> {pName}</span> <div style=\"color:maroon;" + | |
"font-style:italic;" + | |
"font-size:11px;" + | |
"padding-left: 20px;\">{pid}</div>") | |
public SafeHtml render(String pName, String pid); | |
} | |
private TwoLineRenderer cellRenderer = GWT.create(TwoLineRenderer.class); | |
class CustomTextCellImpl implements HasCell<ModelData, String> { | |
TextCell txtCell = new TextCell() { | |
@Override | |
public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) { | |
if (value == null) return; | |
String[] params = value.asString().split("#"); | |
SafeHtml rendered = cellRenderer.render(params[0], params[1]); | |
sb.append(rendered); | |
} | |
}; | |
public Cell<String> getCell() { | |
return txtCell; | |
} | |
public FieldUpdater<ModelData, String> getFieldUpdater() { | |
return null; | |
} | |
@Override | |
public String getValue(ModelData object) { | |
//Concatenate id and name. TextCell takes only String object hence have to | |
// do this to pass various Strings. Would like to know if there is a better way. | |
return object.get("name") + "#" + object.get("id"); | |
} | |
} | |
public FramedPanel getCustomWidget() { | |
final MyCheckBoxList acbList = new MyCheckBoxList(new CustomTextCellImpl()); | |
acbList.setData(_getData()); | |
FramedPanel fp = new FramedPanel(); | |
fp.setBorders(false); | |
fp.setHeadingHtml("Custom List"); | |
fp.setWidth(250); | |
fp.setBorders(true); | |
fp.add(acbList.asWidget()); | |
return fp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment