/** Creates a new category, sending it to the server and adding it to the model. */ public void createCategory(String name, int parentId, AsyncCallback<Category> callback) { final Category cat = new Category(); cat.name = name; cat.parentId = parentId; cat.creator = _ctx.getMe(); Function<Integer, Category> toCat = new Function<Integer, Category>() { public Category apply(Integer catId) { cat.categoryId = catId; return cat; } }; Function<Category, Void> addCatOp = new Function<Category, Void>() { public Void apply(Category cat) { List<Category> cats = _catmap.get(cat.parentId); if (cats == null) { _catmap.put(cat.parentId, cats = new ArrayList<Category>()); } cats.add(cat); return null; } }; _editorsvc.createCategory(cat, Callbacks.map(Callbacks.before(callback, addCatOp), toCat)); }
/** Reparents the supplied category with the specified new parent. */ public void moveCategory( final Category cat, final int newParentId, AsyncCallback<Void> callback) { final int oldParentId = cat.parentId; Function<Void, Void> movedOp = new Function<Void, Void>() { public Void apply(Void result) { List<Category> cats = _catmap.get(oldParentId); if (cats != null) { cats.remove(cat); } cats = _catmap.get(newParentId); if (cats != null) { cats.add(cat); Collections.sort(cats); } return null; } }; cat.parentId = newParentId; _editorsvc.updateCategory(cat, Callbacks.before(callback, movedOp)); }