final RowEditor<GWTJahiaUrlMapping> re = new RowEditor<GWTJahiaUrlMapping>();
// Add a cancel edit event listener to remove empty line
re.addListener(Events.CancelEdit, new Listener<RowEditorEvent>() {
public void handleEvent(RowEditorEvent ree) {
GWTJahiaUrlMapping urlMapping = store.getModels().get(ree.getRowIndex());
if (urlMapping.getUrl().length() == 0) {
store.remove(ree.getRowIndex());
}
}
});
final Grid<GWTJahiaUrlMapping> grid = new Grid<GWTJahiaUrlMapping>(store, new ColumnModel(configs));
grid.setAutoExpandColumn("url");
grid.setBorders(true);
grid.addPlugin(defaultColumn);
grid.addPlugin(activeColumn);
if (editable) {
grid.addPlugin(re);
}
store.addStoreListener(new StoreListener<GWTJahiaUrlMapping>() {
@Override
public void storeUpdate(StoreEvent<GWTJahiaUrlMapping> se) {
super.storeUpdate(se);
if (se.getOperation() == RecordUpdate.EDIT && se.getModel().isDefault()) {
clearOtherDefaults(se.getModel(), se.getStore());
}
}
private void clearOtherDefaults(GWTJahiaUrlMapping model, Store<? extends GWTJahiaUrlMapping> store) {
for (GWTJahiaUrlMapping data : store.getModels()) {
if (data.isDefault() && !data.equals(model)) {
grid.getStore().getRecord(data).set(defaultColumn.getDataIndex(), Boolean.FALSE);
}
}
}
});
ToolBar toolBar = new ToolBar();
Button add = new Button(Messages.get("label.add", "Add"), new SelectionListener<ButtonEvent>() {
@Override
public void componentSelected(ButtonEvent ce) {
re.stopEditing(false);
GWTJahiaUrlMapping mapping = new GWTJahiaUrlMapping("", locale,
store.getCount() == 0, true);
store.insert(mapping, 0);
re.startEditing(store.indexOf(mapping), true);
}
});