I wrote a simple generic combobox and a simple callback interface for this purpose. You don't need to deal with either Store or Model objects to use it. Feel free to use them if you like. (requires latest Beta3 build)
Use it like this:
Code:
FormPanel form = new FormPanel();
form.setFieldWidth(210);
form.setWidth(410);
final List<String> names = new ArrayList<String>();
names.add("Sarah");
names.add("Angelina");
final SimpleComboBox<String> combo1 = new SimpleComboBox<String>("Select actor");
combo1.add(names); // add list of items
combo1.add("Julia"); // add single item
combo1.addListener(new ComboBoxListener<String>() {
public void onSelectionChanged(String selected) {
Info.display("Selection", "selected item: {0}", selected);
}
});
combo1.setWidth(210);
combo1.addToForm(form);
You can use any object class as the type parameter, the SimpleComboBox uses the toString() to render the ComboBox displayfield.
Here is the code:
Code:
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import java.util.List;
/**
* Simple replacement for ExtGWT's {@link com.extjs.gxt.ui.client.widget.form.ComboBox ComboBox}.
* Usage does not depend on a ExtGWT {@link com.extjs.gxt.ui.client.store.Store Store} as the
* Store is contained within this class.
* Example usage:<br>
* <pre>
*
* final List<String> names = new ArrayList<String>();
* names.add("Sarah");
* names.add("Angelina");
*
* final ListComboBox<String> combo1 = new ListComboBox<String>("Select employee");
* combo1.add(names); // adding a list
* combo1.add("Julia"); // adding a single item
*
* combo1.addListener(new ComboBoxListener<String>() {
* public void onSelectionChanged(String selected) {
* Info.display("Selection", "selected item: {0}", selected);
* }
* });
* combo1.setWidth(210);
* combo1.addToForm(form);
*
* </pre>
*
*/
public class SimpleComboBox<T> {
ComboBox<StoreItem> combo ;
ListStore<StoreItem> store = new ListStore<StoreItem>();
private static final String VALUE_FIELD_NAME = "value";
private static final String DISPLAY_FIELD_NAME = "display";
private class StoreItem extends BaseModelData {
private StoreItem() { }
public StoreItem(T item) {
this();
set(VALUE_FIELD_NAME, item);
set(DISPLAY_FIELD_NAME, item.toString());
}
public T getValue() { return (T)get(VALUE_FIELD_NAME); }
public String getDisplayField() { return (String)get(DISPLAY_FIELD_NAME); }
public int hashCode() {
return getValue().hashCode();
}
public boolean equals(Object obj) {
if (obj == null)
return false;
StoreItem i = (StoreItem) obj;
T item1 = i.getValue();
T item2 = this.getValue();
return item1.equals(item2);
}
}
public SimpleComboBox(String fieldLabel) {
combo = new ComboBox<StoreItem>();
combo.setFieldLabel(fieldLabel);
combo.setValueField(VALUE_FIELD_NAME);
combo.setDisplayField(DISPLAY_FIELD_NAME);
combo.setStore(store);
}
public void addToForm(FormPanel form) {
form.add(combo);
}
public void addToForm(FormPanel form, Object layoutData) {
form.add(combo, layoutData);
}
public int getSelectionIndex() {
return store.indexOf(combo.getValue());
}
public T getValue() {
StoreItem storeItem = combo.getValue();
return storeItem.getValue();
}
public void add(List<T> elements) {
for (T element : elements) {
store.add(new StoreItem(element));
}
}
public void add(T element) {
store.add(new StoreItem(element));
}
public void addListener(final ComboBoxListener<T> listener) {
combo.addSelectionChangedListener(new SelectionChangedListener<StoreItem>() {
public void selectionChanged(SelectionChangedEvent<StoreItem> sce) {
if (sce.getSelectedItem() != null) {
T item = sce.getSelectedItem().getValue();
listener.onSelectionChanged(item);
}
}
});
}
public void remove(T item) {
store.remove(new StoreItem(item));
}
public void remove(int index) {
if (index >= 0 && index < store.getCount())
store.remove(store.getAt(index));
}
public void removeAll() {
store.removeAll();
}
public void setWidth(int width) {
combo.setWidth(width);
}
public void setWidth(String width) {
combo.setWidth(width);
}
public void setEditable(boolean value) {
combo.setEditable(value);
}
public void reset() {
combo.reset();
}
public ComboBox getGxtComboBox() {
return combo;
}
}
And the interface:
Code:
/**
* Callback interface used by {@link SimpleComboBox}<br>
* SimpleComboBox uses this to simply return your selected value
* instead of the standard SelectionChangedEvent that returns a GXT Model object.
*
*/
public interface ComboBoxListener<T> {
public void onSelectionChanged(T selected);
}