Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save branflake2267/f94952884d3808809170 to your computer and use it in GitHub Desktop.
Save branflake2267/f94952884d3808809170 to your computer and use it in GitHub Desktop.
ComboBox with a CheckBox selection in the selection list.
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.info.Info;
public class A_Combo_withCheckboxCellList_MultiSelection implements EntryPoint {
@Override
public void onModuleLoad() {
Data data0 = new Data("0", "zero", true);
Data data1 = new Data("1", "one", false);
Data data2 = new Data("2", "two", true);
Data data3 = new Data("3", "three", false);
Data data4 = new Data("4", "four", true);
List<Data> selected = new ArrayList<Data>();
selected.add(data0);
selected.add(data1);
selected.add(data2);
selected.add(data3);
selected.add(data4);
final MultiSelectionCheckboxComboBox<Data> combo = new MultiSelectionCheckboxComboBox<Data>();
combo.setValue(selected);
RootPanel.get().add(combo);
TextButton button = new TextButton("Get Value");
button.addSelectHandler(new SelectHandler() {
@Override
public void onSelect(SelectEvent event) {
Info.display("value=", combo.getValue() + "");
}
});
RootPanel.get().add(button);
}
public class Data implements HasChecked {
private String id;
private String name;
private boolean checked;
public Data(String id, String name, boolean checked) {
this.id = id;
this.name = name;
this.checked = checked;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
@Override
public String toString() {
return name;
}
}
}
public interface HasChecked {
// used for the internal store
void setId(String id);
String getId();
// boolean for checkbox
void setChecked(boolean checked);
// boolean for checkbox
boolean getChecked();
// label
String getName();
// label
void setName(String name);
}
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.Cell;
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.dom.client.InputElement;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.Composite;
import com.sencha.gxt.cell.core.client.form.CheckBoxCell;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.widget.core.client.ListView;
import com.sencha.gxt.widget.core.client.form.ComboBox;
public class MultiSelectionCheckboxComboBox<M extends HasChecked> extends Composite {
// cell for list view checkbox
protected class CheckBoxCellHasCell implements HasCell<M, Boolean> {
private CheckBoxCell cell = new CheckBoxCell();
@Override
public Cell<Boolean> getCell() {
return cell;
}
@Override
public FieldUpdater<M, Boolean> getFieldUpdater() {
return new FieldUpdater<M, Boolean>() {
@Override
public void update(int index, M object, Boolean value) {
object.setChecked(value);
}
};
}
@Override
public Boolean getValue(M object) {
return object.getChecked();
}
}
// cell for list view label
protected class TextHasCell implements HasCell<M, String> {
private TextCell cell = new TextCell();
@Override
public Cell<String> getCell() {
return cell;
}
@Override
public FieldUpdater<M, String> getFieldUpdater() {
return new FieldUpdater<M, String>() {
@Override
public void update(int index, M object, String value) {
object.setName(value);
}
};
}
@Override
public String getValue(M object) {
return object.getName();
}
}
// override the checkbox, so it won't collapse during selection of item
protected class CheckBoxComboBoxCell<M extends HasChecked> extends ComboBoxCell<M> {
private boolean ignoreCollapse;
public CheckBoxComboBoxCell(ListStore<M> store, LabelProvider<? super M> labelProvider, ListView<M, ?> view) {
super(store, labelProvider, view);
}
@Override
protected void onSelect(M item) {
item.setChecked(!item.getChecked());
store.update(item);
ignoreCollapse = true;
super.onSelect(item);
ignoreCollapse = false;
}
@Override
public void collapse(com.google.gwt.cell.client.Cell.Context context, XElement parent) {
if (!ignoreCollapse) {
super.collapse(context, parent);
}
}
}
protected class MultiSelectionComboBox<M extends HasChecked> extends ComboBox<M> {
public MultiSelectionComboBox(ComboBoxCell<M> cell) {
super(cell);
}
}
protected MultiSelectionComboBox<M> combo;
protected ListStore<M> store;
private LabelProvider<M> labelProvider;
public MultiSelectionCheckboxComboBox() {
initWidget(createCombo());
}
protected MultiSelectionComboBox<M> createCombo() {
ModelKeyProvider<M> modelKeyProvider = new ModelKeyProvider<M>() {
@Override
public String getKey(M item) {
return item.getId();
}
};
store = new ListStore<M>(modelKeyProvider);
// when a selection occurs, draw out this label in the input
labelProvider = new LabelProvider<M>() {
@Override
public String getLabel(M item) {
String label = "";
List<M> datas = store.getAll();
for (M data : datas) {
if (data.getChecked()) {
if (label.length() > 0) {
label += ", ";
}
label += data.getName();
}
}
return label;
}
};
List<HasCell<M, ?>> cells = new ArrayList<HasCell<M, ?>>();
cells.add(new CheckBoxCellHasCell());
cells.add(new TextHasCell());
// add two cells to layout a checkbox and label - [] label
CompositeCell<M> listViewCell = new CompositeCell<M>(cells) {
protected <X> void render(Context context, M value, SafeHtmlBuilder sb, HasCell<M, X> hasCell) {
// draw the cells in a line
Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<span style='display: inline-block; vertical-align:middle;'>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</span>");
}
};
ListView<M, M> listView = new ListView<M, M>(store, new IdentityValueProvider<M>("M"), listViewCell);
CheckBoxComboBoxCell<M> cell = new CheckBoxComboBoxCell<M>(store, labelProvider, listView);
combo = new MultiSelectionComboBox<M>(cell);
combo.setTriggerAction(TriggerAction.ALL);
return combo;
}
public void setValue(List<M> value) {
store.addAll(value);
String label = labelProvider.getLabel(null);
InputElement input = combo.getCell().getInputElement(combo.getElement());
input.setValue(label);
}
public List<M> getValue() {
List<M> all = store.getAll();
List<M> some = new ArrayList<M>();
for (M item : all) {
if (item.getChecked()) {
some.add(item);
}
}
return some;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment