PDA

View Full Version : [FIXED] Store losing setMonitorChanges(true) on reload



negge
27 Jun 2008, 10:04 AM
I have a simple list and editor. When you edit an item, the change is shown in the list by use of setModifedChanges(true) on the store. If you reload the store using loader.load() and then edit an object, the changes are not shown.


import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binder.DataListBinder;
import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.ListLoadConfig;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.DataList;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Item extends BaseModel {

public Item(long _id,String _name) {
super();
setId(_id);
setName(_name);
}

public long getId() {
return((Long)get("id"));
}

public void setId(long _id) {
set("id",_id);
}

public String getName() {
return((String)get("name"));
}

public void setName(String _name) {
set("name",_name);
}

};

class ItemListEditor extends ContentPanel {

protected DataList list;

public ItemListEditor(ListStore<Item> _items,final ListLoader<ListLoadConfig> _itemLoader) {
setHeading("Items");
setFrame(true);
setInsetBorder(false);
setSize(150,250);
setLayout(new FitLayout());

list=new DataList();
list.setFlatStyle(true);
add(list);

final DataListBinder<Item> itemBinder=new DataListBinder<Item>(list,_items);
itemBinder.setDisplayProperty("name");
itemBinder.addSelectionChangedListener(new SelectionChangedListener<Item>() {
public void selectionChanged(SelectionChangedEvent<Item> _se) {
Item item=_se.getSelectedItem();
onItemSelected(item);
}
});
itemBinder.init();

getHeader().addTool(new ToolButton("x-tool-refresh", new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent ce) {
_itemLoader.load();
}
}));

ToolBar toolBar=new ToolBar();
setTopComponent(toolBar);

TextToolItem addItem=new TextToolItem("Add","icon-add");
toolBar.add(addItem);

TextToolItem removeItem=new TextToolItem("Remove","icon-delete");
toolBar.add(removeItem);
}

protected void onItemSelected(Item _item) {

}
};

class ItemEditor extends FormPanel {

protected Item item;

protected TextField<String> nameTextField;

public ItemEditor(Item _item) {
item=_item;

setHeaderVisible(false);
setBodyBorder(false);
setFrame(true);
setWidth(400);

nameTextField=new TextField<String>();
nameTextField.setFieldLabel("Name");
nameTextField.setAllowBlank(false);
nameTextField.setMinLength(3);
nameTextField.setValue(item.getName());
add(nameTextField);

Button updateButton=new Button("Update",new SelectionListener<ComponentEvent>() {
public void componentSelected(ComponentEvent ce) {
if (isValid()) {
onValidSubmit();
}
}
});
addButton(updateButton);
}

protected void onValidSubmit() {
Database.updateItem(item.getId(),nameTextField.getValue());
item.setName(nameTextField.getValue());
}

};

class Database {

static Map<Long,String> ITEMS=new HashMap<Long,String>() {
{
put(1L,"Apple");
put(2L,"Orange");
put(3L,"Grape");
}
};

static List<Item> getItems() {
List<Item> items=new ArrayList<Item>();
for (Map.Entry<Long,String> entry : ITEMS.entrySet()) {
items.add(new Item(entry.getKey(),entry.getValue()));
}
return(items);
}

static void updateItem(long _id,String _name) {
ITEMS.put(_id,_name);
}

};

class SettingsTabItem extends TabItem {

public SettingsTabItem() {
setText("Settings");

setLayout(new RowLayout(Orientation.HORIZONTAL));

final ContentPanel itemPanel=new ContentPanel();
itemPanel.setHeading("Item Editor");
itemPanel.setLayout(new RowLayout(Orientation.VERTICAL));

ListLoader<ListLoadConfig> itemLoader=new BaseListLoader<ListLoadConfig,ListLoadResult<Item>>(new RpcProxy<ListLoadConfig,ListLoadResult<Item>>() {
protected void load(ListLoadConfig _loadConfig,final AsyncCallback<ListLoadResult<Item>> _callback) {
Timer t=new Timer() {
public void run() {
_callback.onSuccess(new BaseListLoadResult<Item>(Database.getItems()));
}
};
t.schedule(250);
}
});

ListStore<Item> itemStore=new ListStore<Item>(itemLoader);
itemStore.setMonitorChanges(true);
itemStore.add(Database.getItems());

ItemListEditor itemListEditor=new ItemListEditor(itemStore,itemLoader) {
protected void onItemSelected(Item _item) {
itemPanel.removeAll();
if (_item!=null) {
itemPanel.add(new ItemEditor(_item));
}
itemPanel.layout();
}
};
//RowData data=new RowData();
RowData data=new RowData(150,250);
data.setMargins(new Margins(10));
add(itemListEditor,data);

add(itemPanel,new RowData(1,1,new Margins(10,10,10,0)));
}
}

public class TestProject implements EntryPoint {

public void onModuleLoad() {
final Viewport viewport=new Viewport();
viewport.setLayout(new BorderLayout());
RootPanel.get().add(viewport);

ContentPanel navigation=new ContentPanel();
navigation.setHeading("Users");
navigation.setScrollMode(Scroll.AUTO);
navigation.setLayout(new FitLayout());

BorderLayoutData data=new BorderLayoutData(Style.LayoutRegion.WEST,200,150,300);
data.setMargins(new Margins(5,5,5,5));
data.setCollapsible(true);
viewport.add(navigation,data);

ContentPanel content=new ContentPanel();
content.setBorders(false);
content.setHeaderVisible(false);
content.setLayout(new FitLayout());

data=new BorderLayoutData(Style.LayoutRegion.CENTER);
data.setMargins(new Margins(5,5,5,0));
viewport.add(content,data);

TabPanel outer=new TabPanel();
outer.setTabScroll(true);
outer.setBorderStyle(false);
outer.setBodyBorder(false);
content.add(outer);

TabItem page=new TabItem();
page.setItemId("p1");
page.setText("Project 1");
page.setLayout(new FitLayout());
outer.add(page);

ContentPanel main=new ContentPanel();
main.setBodyBorder(false);
main.setHeaderVisible(false);
main.setLayout(new FitLayout());
page.add(main);

ToolBar toolBar=new ToolBar();
main.setTopComponent(toolBar);

TabPanel tabPanel=new TabPanel();
tabPanel.setTabScroll(true);
tabPanel.setBorderStyle(false);
tabPanel.setBodyBorder(false);
tabPanel.setTabPosition(TabPanel.TabPosition.BOTTOM);
main.add(tabPanel);

tabPanel.add(new SettingsTabItem());
}

};Nathan

darrellmeyer
29 Jun 2008, 9:06 PM
Fix is in SVN.