Created
January 19, 2012 16:03
-
-
Save unthingable/1640784 to your computer and use it in GitHub Desktop.
JSNI return types with CellTable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataDictJSO extends JavaScriptObject { | |
... | |
public final native void put(String key, String obj) /*-{ | |
this[key] = obj; | |
}-*/; | |
/** | |
* A slick way to get down to the object. | |
* @param keys can be "foo" or "foo.bar.baz" | |
* @param <T> | |
* @return | |
*/ | |
public final native <T> T get(String keys) /*-{ | |
var val = this; | |
var keys = keys.split("."); | |
for (var ii=0; ii<keys.length; ii++) { | |
var key = keys[ii]; | |
val = val[key]; | |
} | |
return val; | |
}-*/; | |
} | |
public class DictColumn<K> | |
extends Column<DataDictJSO, K> { | |
String key; | |
public DictColumn(Cell cell, String fieldKey) { | |
super(cell); | |
this.key = fieldKey; | |
} | |
@Override | |
public K getValue(DataDictJSO object) { | |
return object.<K>get(key); | |
} | |
} | |
... | |
cellTable.addColumn(new DictColumn<String>(new EditTextCell(), "name"), "Name"); | |
cellTable.addColumn(new DictColumn<Boolean>(new CheckboxCell(), "is_a.administrator"), "Administrator"); | |
ListDataProvider<DataDictJSO> provider = new ListDataProvider<DataDictJSO>(); | |
provider.addDataDisplay(cellTable); | |
String data = "[{\"id\":\"admin\",\"password_sha\":\"ddf32cd4eb1caa4260d0ab626b2b3f1f7dec4a8c\",\"name\":\"Administrator\",\"is_a\":{\"administrator\":true}}]"; | |
JSONArray users = (JSONArray) JSONParser.parseLenient(data); | |
List<DataDictJSO> list = provider.getList(); | |
for (int ii = 0; ii<users.size(); ii++) { | |
list.add((DataDictJSO) ((JSONObject) users.get(ii)).getJavaScriptObject()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment