网格的字段绑定。将新记录添加到 Store 后视图仍然不一致
FieldBinding for Grid. View remains inconsistent after adding new record to Store
我们为应用程序中的一些典型网格用法实现了绑定。它工作得很好,除非你修改商店,例如添加一条记录,你会在视图中看到 n + 两条相同的记录。
当我检查商店的状态时,它显示了 n + 1 个值。
就好像我有一个网格,其中显示了一条记录并调用:grid.getStore().add(modelFactory.createModel(event.getBean()));
我现在有:
第二行和第三行相等,不能选择第三行。此外,它不存在于 grid.getStore()
.
中
来源:
freqsGrid = new AwesomeGridPanel() {
@Override
public void createColumns() {/**/}
};
freqBinding = AwesomeGridBinding.createGridBinding(freqsGrid, "frequencies");
简单的绑定源。它将模型的列表 属性 按原样映射到网格。
public class AwesomeGridBinding {
public static FieldBinding createGridBinding(AwesomeGridPanel grid, String property) {
return new FieldBinding(new AwesomeGridAdapterField(grid), property);
}
}
class AwesomeGridAdapterField<T> extends AdapterField {
protected AwesomeGridPanel grid;
private StoreListener<BeanModel> storeChangedListener;
public AwesomeGridAdapterField(AwesomeGridPanel grid) {
super(grid);
this.grid = grid;
configureGrid(grid, this);
}
@Override
public void setValue(Object value) {
List data;
if (value == null)
data = new ArrayList<>();
else if (!(value instanceof List))
throw new IllegalArgumentException();
else
data = (List) value;
grid.getStore().setMonitorChanges(false);
grid.getStore().setFiresEvents(false);
setResults(grid.getStore(), data);
grid.getStore().setFiresEvents(true);
grid.getStore().setMonitorChanges(true);
如果我删除下面的行,视图将在添加后停止显示 n+2 行,并且甚至在 formBinding.bind(createModel(bean));
之后开始显示添加的行到另一个 bean。
grid.getGrid().getView().refresh(false);
}
@Override
public Object getValue() {
List<T> result = new ArrayList<>();
for (BeanModel bm : grid.getStore().getModels())
if (isBeanForResult(bm))
result.add(extractResult(bm));
return result;
}
protected void setResults(ListStore<BeanModel> store, List data) {
store.removeAll();
for (Object obj : data)
if (obj instanceof BeanModel)
store.add((BeanModel) obj);
else
throw new IllegalArgumentException();
}
protected boolean isBeanForResult(BeanModel beanModel) {
return true;
}
protected T extractResult(BeanModel bmFromStore) {
return bmFromStore.getBean();
}
private final EventType[] STORE_EVENTS = {Store.Add, Store.Clear, Store.DataChanged, Store.Remove, Store.Update};
protected void configureGrid(final AwesomeGridPanel grid, final AdapterField field) {
grid.getStore().setMonitorChanges(true);
// grid.getStore().removeAllListeners();
if (storeChangedListener != null)
grid.getStore().removeStoreListener(storeChangedListener);
storeChangedListener = new StoreListener<BeanModel>() {
@Override
public void handleEvent(StoreEvent<BeanModel> e) {
super.handleEvent(e);
for (EventType se : STORE_EVENTS) {
if (se != e.getType())
continue;
field.fireEvent(Events.Change);
return;
}
}
};
grid.getStore().addStoreListener(storeChangedListener);
}
}
我发现解决问题的唯一方法是避免商店修改。
我使用了 BeanModel。
小部件:
schedulesGridPanel = new AwesomeGridView<RcTaskSchedule>(ListBinding.createEmptyStore(), NavigationTarget.RADIOCONTROL_TASK_DIALOG, "RCTaskDialogSchedulesGridPanel") {
@Override
public void createColumns() {...}
};
formBinding.addFieldBinding(new ListBinding(schedulesGridPanel.getGrid(), "schedules"));
...
@Override
public BeanModel getBeanModel() {
return (BeanModel) formBinding.getModel();
}
...
主持人:
eventBus.addBeanCreatedEventHandler(RcTaskSchedule.class, NavigationTarget.RC_TASK_SCHEDULE_DIALOG, new BeanCreatedEvent.Handler<RcTaskSchedule>() {
@Override
public void onBeanCreated(BeanCreatedEvent<RcTaskSchedule> event) {
ListBinding.addListItemInBeanModel(display.getBeanModel(), "schedules", schedulesFactory.createModel(event.getBean()));
}
});
eventBus.addBeanModifiedEventHandler(RcTaskSchedule.class, NavigationTarget.RC_TASK_SCHEDULE_DIALOG, new BeanModifiedEvent.Handler<RcTaskSchedule>() {
@Override
public void onBeanModified(BeanModifiedEvent<RcTaskSchedule> event) {
ListBinding.updateListItemInBeanModel(display.getBeanModel(), "schedules",
schedulesFactory.createModel(event.getOldBean()), schedulesFactory.createModel(event.getModifiedBean()));
}
});
...
public class ListBinding extends FieldBinding {
private Grid grid;
private ChangeListener listener = null;
private MemoryProxy memoryProxy = null;
public ListBinding(Grid grid, String property) {
super(new AdapterField(grid), property);
this.grid = grid;
if (!(grid.getStore().getLoader() instanceof BaseListLoader))
return;
BaseListLoader loader = (BaseListLoader) grid.getStore().getLoader();
if (!(loader.getProxy() instanceof MemoryProxy))
return;
memoryProxy = (MemoryProxy) loader.getProxy();
}
@Override
public void bind(ModelData model) {
super.bind(model);
if (memoryProxy == null)
return;
grid.getStore().removeAll();
memoryProxy.setData(getModel().get(getProperty()));
grid.getStore().getLoader().load();
if (!(model instanceof BeanModel))
return;
BeanModel bm = (BeanModel) model;
listener = new ChangeListener() {
@Override
public void modelChanged(ChangeEvent event) {
if (!(event instanceof PropertyChangeEvent))
return;
if (!property.equals(((PropertyChangeEvent) event).getName()))
return;
grid.getStore().removeAll();
memoryProxy.setData(getModel().get(getProperty()));
grid.getStore().getLoader().load();
}
};
bm.addChangeListener(listener);
}
@Override
public void unbind() {
super.unbind();
grid.getStore().removeAll();
if (listener == null)
return;
if (!(this.getModel() instanceof BeanModel))
return;
BeanModel bm = (BeanModel) this.getModel();
bm.removeChangeListener(listener);
}
public static ListStore<BeanModel> createEmptyStore() {
return new ListStore<>(new BaseListLoader(new MemoryProxy(new BoundList<>())/*, new BeanModelReader()*/));
}
public static void addListItemInBeanModel(BeanModel beanModel, String property, BeanModel newItem) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || newItem == null)
return;
List<BeanModel> list = beanModel.get(property);
list.add(newItem);
beanModel.set(property, null);
beanModel.set(property, list);
}
public static void updateListItemInBeanModel(BeanModel beanModel, String property, BeanModel oldItem, BeanModel newItem) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || newItem == null || oldItem == null)
return;
List<BeanModel> list = beanModel.get(property);
int index = list.indexOf(oldItem);
if (index < 0)
return;
list.set(index, newItem);
beanModel.set(property, list);
}
public static void removeListItemsInBeanModel(BeanModel beanModel, String property, List<BeanModel> items) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || items == null || items.isEmpty())
return;
List<BeanModel> list = beanModel.get(property);
list.removeAll(items);
beanModel.set(property, list);
}
}
我们为应用程序中的一些典型网格用法实现了绑定。它工作得很好,除非你修改商店,例如添加一条记录,你会在视图中看到 n + 两条相同的记录。 当我检查商店的状态时,它显示了 n + 1 个值。
就好像我有一个网格,其中显示了一条记录并调用:grid.getStore().add(modelFactory.createModel(event.getBean()));
我现在有:
第二行和第三行相等,不能选择第三行。此外,它不存在于 grid.getStore()
.
来源:
freqsGrid = new AwesomeGridPanel() {
@Override
public void createColumns() {/**/}
};
freqBinding = AwesomeGridBinding.createGridBinding(freqsGrid, "frequencies");
简单的绑定源。它将模型的列表 属性 按原样映射到网格。
public class AwesomeGridBinding {
public static FieldBinding createGridBinding(AwesomeGridPanel grid, String property) {
return new FieldBinding(new AwesomeGridAdapterField(grid), property);
}
}
class AwesomeGridAdapterField<T> extends AdapterField {
protected AwesomeGridPanel grid;
private StoreListener<BeanModel> storeChangedListener;
public AwesomeGridAdapterField(AwesomeGridPanel grid) {
super(grid);
this.grid = grid;
configureGrid(grid, this);
}
@Override
public void setValue(Object value) {
List data;
if (value == null)
data = new ArrayList<>();
else if (!(value instanceof List))
throw new IllegalArgumentException();
else
data = (List) value;
grid.getStore().setMonitorChanges(false);
grid.getStore().setFiresEvents(false);
setResults(grid.getStore(), data);
grid.getStore().setFiresEvents(true);
grid.getStore().setMonitorChanges(true);
如果我删除下面的行,视图将在添加后停止显示 n+2 行,并且甚至在 formBinding.bind(createModel(bean));
之后开始显示添加的行到另一个 bean。
grid.getGrid().getView().refresh(false);
}
@Override
public Object getValue() {
List<T> result = new ArrayList<>();
for (BeanModel bm : grid.getStore().getModels())
if (isBeanForResult(bm))
result.add(extractResult(bm));
return result;
}
protected void setResults(ListStore<BeanModel> store, List data) {
store.removeAll();
for (Object obj : data)
if (obj instanceof BeanModel)
store.add((BeanModel) obj);
else
throw new IllegalArgumentException();
}
protected boolean isBeanForResult(BeanModel beanModel) {
return true;
}
protected T extractResult(BeanModel bmFromStore) {
return bmFromStore.getBean();
}
private final EventType[] STORE_EVENTS = {Store.Add, Store.Clear, Store.DataChanged, Store.Remove, Store.Update};
protected void configureGrid(final AwesomeGridPanel grid, final AdapterField field) {
grid.getStore().setMonitorChanges(true);
// grid.getStore().removeAllListeners();
if (storeChangedListener != null)
grid.getStore().removeStoreListener(storeChangedListener);
storeChangedListener = new StoreListener<BeanModel>() {
@Override
public void handleEvent(StoreEvent<BeanModel> e) {
super.handleEvent(e);
for (EventType se : STORE_EVENTS) {
if (se != e.getType())
continue;
field.fireEvent(Events.Change);
return;
}
}
};
grid.getStore().addStoreListener(storeChangedListener);
}
}
我发现解决问题的唯一方法是避免商店修改。 我使用了 BeanModel。
小部件:
schedulesGridPanel = new AwesomeGridView<RcTaskSchedule>(ListBinding.createEmptyStore(), NavigationTarget.RADIOCONTROL_TASK_DIALOG, "RCTaskDialogSchedulesGridPanel") {
@Override
public void createColumns() {...}
};
formBinding.addFieldBinding(new ListBinding(schedulesGridPanel.getGrid(), "schedules"));
...
@Override
public BeanModel getBeanModel() {
return (BeanModel) formBinding.getModel();
}
... 主持人:
eventBus.addBeanCreatedEventHandler(RcTaskSchedule.class, NavigationTarget.RC_TASK_SCHEDULE_DIALOG, new BeanCreatedEvent.Handler<RcTaskSchedule>() {
@Override
public void onBeanCreated(BeanCreatedEvent<RcTaskSchedule> event) {
ListBinding.addListItemInBeanModel(display.getBeanModel(), "schedules", schedulesFactory.createModel(event.getBean()));
}
});
eventBus.addBeanModifiedEventHandler(RcTaskSchedule.class, NavigationTarget.RC_TASK_SCHEDULE_DIALOG, new BeanModifiedEvent.Handler<RcTaskSchedule>() {
@Override
public void onBeanModified(BeanModifiedEvent<RcTaskSchedule> event) {
ListBinding.updateListItemInBeanModel(display.getBeanModel(), "schedules",
schedulesFactory.createModel(event.getOldBean()), schedulesFactory.createModel(event.getModifiedBean()));
}
});
...
public class ListBinding extends FieldBinding {
private Grid grid;
private ChangeListener listener = null;
private MemoryProxy memoryProxy = null;
public ListBinding(Grid grid, String property) {
super(new AdapterField(grid), property);
this.grid = grid;
if (!(grid.getStore().getLoader() instanceof BaseListLoader))
return;
BaseListLoader loader = (BaseListLoader) grid.getStore().getLoader();
if (!(loader.getProxy() instanceof MemoryProxy))
return;
memoryProxy = (MemoryProxy) loader.getProxy();
}
@Override
public void bind(ModelData model) {
super.bind(model);
if (memoryProxy == null)
return;
grid.getStore().removeAll();
memoryProxy.setData(getModel().get(getProperty()));
grid.getStore().getLoader().load();
if (!(model instanceof BeanModel))
return;
BeanModel bm = (BeanModel) model;
listener = new ChangeListener() {
@Override
public void modelChanged(ChangeEvent event) {
if (!(event instanceof PropertyChangeEvent))
return;
if (!property.equals(((PropertyChangeEvent) event).getName()))
return;
grid.getStore().removeAll();
memoryProxy.setData(getModel().get(getProperty()));
grid.getStore().getLoader().load();
}
};
bm.addChangeListener(listener);
}
@Override
public void unbind() {
super.unbind();
grid.getStore().removeAll();
if (listener == null)
return;
if (!(this.getModel() instanceof BeanModel))
return;
BeanModel bm = (BeanModel) this.getModel();
bm.removeChangeListener(listener);
}
public static ListStore<BeanModel> createEmptyStore() {
return new ListStore<>(new BaseListLoader(new MemoryProxy(new BoundList<>())/*, new BeanModelReader()*/));
}
public static void addListItemInBeanModel(BeanModel beanModel, String property, BeanModel newItem) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || newItem == null)
return;
List<BeanModel> list = beanModel.get(property);
list.add(newItem);
beanModel.set(property, null);
beanModel.set(property, list);
}
public static void updateListItemInBeanModel(BeanModel beanModel, String property, BeanModel oldItem, BeanModel newItem) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || newItem == null || oldItem == null)
return;
List<BeanModel> list = beanModel.get(property);
int index = list.indexOf(oldItem);
if (index < 0)
return;
list.set(index, newItem);
beanModel.set(property, list);
}
public static void removeListItemsInBeanModel(BeanModel beanModel, String property, List<BeanModel> items) {
if (beanModel == null || !(beanModel.get(property) instanceof List) || items == null || items.isEmpty())
return;
List<BeanModel> list = beanModel.get(property);
list.removeAll(items);
beanModel.set(property, list);
}
}