PDA

View Full Version : Tree not showing (using TreeStore, TreeModel, Async, Proxy)



HerrB
14 Jun 2009, 11:50 PM
[GXT V2.0 m2]
As I haven't found a working example I have collected the available code pieces to get a tree which should be filled using a JSON returning webservice. For testing purposes a Memory Proxy is used.

The data seems to get loaded and gets seems to be stored in the store, but the tree is not showing. Any ideas are appreciated.

The PersonModel used (which also contains a method to deliver the model type):

public class PersonModel extends BaseModelData {

public PersonModel() {

}

public PersonModel(long lngID, String strLastName, String strFirstName,
String strAccount, long lngPID) {
setID(lngID);
setLastName(strLastName);
setFirstName(strFirstName);
setAccount(strAccount);
setPID(lngPID);
}

public void setID(long lngID) {
set("id", lngID);
}

public void setLastName(String strLastName) {
set("lastname", strLastName);
}

public void setFirstName(String strFirstName) {
set("firstname", strFirstName);
}

public void setAccount(String strAccount) {
set("account", strAccount);
}

public void setPID(long lngPID) {
set("pid", lngPID);
}

public Long getID() {
return get("id");
}

public String getLastName() {
return get("lastname");
}

public String getFirstName() {
return get("firstname");
}

public String getAccount() {
return get("account");
}

public long getPID() {
return get("pid");
}

static public ModelType getModelType() {
ModelType objType = new ModelType();

objType.setRoot("root");

DataField objField;

objField = new DataField("id");
objField.setMap("id");
objField.setType(Long.class);
objType.addField(objField);

objField = new DataField("lastname");
objField.setMap("lastname");
objField.setType(String.class);
objType.addField(objField);

objField = new DataField("firstname");
objField.setMap("firstname");
objField.setType(String.class);
objType.addField(objField);

objField = new DataField("account");
objField.setMap("account");
objField.setType(String.class);
objType.addField(objField);

objField = new DataField("pid");
objField.setMap("pid");
objField.setType(Long.class);
objType.addField(objField);

return objType;
}

@Override
public boolean equals(Object objObject) {
if (objObject != null && objObject instanceof PersonModel) {
PersonModel objPerson = (PersonModel) objObject;

return getID().equals(objPerson.getID());
}
return super.equals(objObject);
}
}

The JSONTreeReader used to populate the TreeStore with the TreeModels (thx to the forum):

public class JsonModelReader<D> implements DataReader<List<ModelData>> {

private ModelType objModelType;

//
// Creates a new JSON reader.
//
// @param modelType the model type definition
///
public JsonModelReader(ModelType objModelType) {
this.objModelType = objModelType;
}

public List<ModelData> read(Object objLoadConfig, Object objData) {
JSONObject objJsonRoot = (JSONObject) JSONParser.parse((String) objData);
JSONArray objRoot = (JSONArray) objJsonRoot.get(objModelType.getRoot());

int intSize = objRoot.size();

ArrayList<ModelData> lstRecords = new ArrayList<ModelData>();

for (int i = 0; i < intSize; i++) {
JSONObject objItem = (JSONObject) objRoot.get(i);
ModelData objModel = newModelInstance();

for (int j = 0; j < objModelType.getFieldCount(); j++) {
DataField objField = objModelType.getField(j);
String strMap = objField.getMap() != null ? objField.getMap() : objField.getName();
JSONValue objValue = objItem.get(strMap);

if (objValue == null) continue;

if (objValue.isArray() != null) {
// nothing
} else if (objValue.isBoolean() != null) {
objModel.set(objField.getName(), objValue.isBoolean().booleanValue());
} else if (objValue.isNumber() != null) {
objModel.set(objField.getName(), objValue.isNumber().doubleValue());
} else if (objValue.isObject() != null) {
// nothing
} else if (objValue.isString() != null) {
String strValue = objValue.isString().stringValue();

if (objField.getType() != null) {
if (objField.getType().equals(Date.class)) {
DateTimeFormat format = DateTimeFormat.getFormat(objField.getFormat());
Date datValue = format.parse(strValue);
objModel.set(objField.getName(), datValue);
} else {
objModel.set(objField.getName(), strValue);
}
} else {
objModel.set(objField.getName(), strValue);
}

} else if (objValue.isNull() != null) {
objModel.set(objField.getName(), null);
}
}

lstRecords.add(objModel);
}

return lstRecords;
}

//
// Returns the new model instances. Subclasses may override to provide an
// model data subclass.
//
// @return the new model data instance
///
protected ModelData newModelInstance() {
//return new BaseModelData();
return new PersonModel();
}
}

And finally the code to create the Tree:

Tree objTree = new Tree();

MemoryProxy objProxyTest = new MemoryProxy("{root:[{id:'1',lastname:'Test',firstname:'Test2'},{id:'2',lastname:'Test4',firstname:'Test5'}]}");

JsonModelReader<PersonModel> objReader = new JsonModelReader<PersonModel>(PersonModel.getModelType());
TreeLoader<PersonModel> objLoader = new BaseTreeLoader<PersonModel>(objProxyTest, objReader);
TreeStore<PersonModel> objStore = new TreeStore<PersonModel>(objLoader);
final TreeBinder<PersonModel> objBinder = new TreeBinder<PersonModel>(objTree, objStore);
objLoader.load();
RootPanel.get().add(objTree);


The loader is called before the tree object has been added to the panel - maybe this is a problem (although debugging shows, that load is called twice - the second time the Tree object has been added to the panel).

Any idea?

Regards,

HerrB