@SuppressWarnings("unchecked")
private void init() {
treeModel = new MenuTreeModel(getModelObject().getRoot());
final TreeGrid tg;
add(tg = new TreeGrid("treeGrid", treeModel, newColumns()) {
@Override
protected void onItemSelectionChanged(IModel item, boolean newValue) {
super.onItemSelectionChanged(item, newValue);
//if (newValue == false)
setItemEdit(item, newValue);
selectionChanged(AjaxRequestTarget.get());
// update();
}
@Override
public void setItemEdit(IModel rowModel, boolean edit) {
if (edit == true) {
selectItem(rowModel, true);
}
super.setItemEdit(rowModel, edit);
}
});
tree = tg.getTree();
tg.setClickRowToSelect(true);
tg.setClickRowToDeselect(true);
tg.setSelectToEdit(false);
tree.setRootLess(true);
tg.setContentHeight(34, SizeUnit.EM);
tree.getTreeState().expandAll();
tree.getTreeState().setAllowSelectMultiple(false);
// tree.getTreeState().selectNode(treeModel.getRoot(), true);
links = new WebMarkupContainer("links");
links.setOutputMarkupId(true);
links.add(new AjaxLink("addTopLevel") {
@Override
public void onClick(AjaxRequestTarget target) {
MenuTreeNode parent = (MenuTreeNode) treeModel.getRoot();
ChildEntry entry = new ChildEntry(parent.getEntry());
entry.setTitle(getString("newEntry"));
parent.getEntry().getChildren().add(entry);
MenuTreeNode node = new MenuTreeNode(entry);
treeModel.nodeInserted(tree, parent, node);
tree.getTreeState().selectNode(node, true);
tree.updateTree();
}
});
links.add(new AjaxLink("add") {
@Override
public void onClick(AjaxRequestTarget target) {
ChildEntry entry = new ChildEntry(getSelected().getEntry());
entry.setTitle(getString("newEntry"));
getSelected().getEntry().getChildren().add(entry);
MenuTreeNode node = new MenuTreeNode(entry);
treeModel.nodeInserted(tree, getSelected(), node);
tree.getTreeState().selectNode(node, true);
tree.updateTree();
}
@Override
public boolean isEnabled() {
return !getSelected().equals(treeModel.getRoot());
}
});
links.add(new AjaxLink("remove") {
@Override
public void onClick(AjaxRequestTarget target) {
MenuTreeNode selected = getSelected();
MenuTreeNode parent = (MenuTreeNode) tree.getParentNode(selected);
boolean editing = tg.isItemEdited(new Model<MenuTreeNode>(selected));
int index = parent.getChildren().indexOf(selected);
treeModel.nodeDeleted(tree, selected);
parent.getEntry().getChildren().remove(selected.getEntry());
if (index > parent.getChildren().size() - 1) {
--index;
}
MenuTreeNode newSelected = (MenuTreeNode) ((index >= 0) ? parent.getChildren().get(index) : parent);
if (newSelected.equals(treeModel.getRoot()) == false) {
tree.getTreeState().selectNode(newSelected, true);
tg.setItemEdit(new Model<MenuTreeNode>(newSelected), editing);
}
tree.updateTree();
}
@Override
public boolean isEnabled() {
return getSelected() != treeModel.getRoot();
}
});
links.add(new AjaxLink("moveUp") {
@Override
public void onClick(AjaxRequestTarget target) {
MenuTreeNode selected = getSelected();
int index = getIndex(selected);
if (index > 0) {
boolean editing = tg.isItemEdited(new Model<MenuTreeNode>(selected));
MenuTreeNode parent = (MenuTreeNode) tree.getParentNode(selected);
treeModel.nodeDeleted(tree, selected);
parent.getEntry().getChildren().remove(selected.getEntry());
parent.getEntry().getChildren().add(index - 1, (ChildEntry) selected.getEntry());
treeModel.nodeInserted(tree, parent, selected);
tree.getTreeState().selectNode(selected, true);
tg.setItemEdit(new Model<MenuTreeNode>(selected), editing);
tree.updateTree();
}
target.addComponent(links);
}
@Override
public boolean isEnabled() {
return getIndex(getSelected()) > 0;
}
});
links.add(new AjaxLink("moveDown") {
@Override
public void onClick(AjaxRequestTarget target) {
MenuTreeNode selected = getSelected();
MenuTreeNode parent = (MenuTreeNode) tree.getParentNode(selected);
int index = getIndex(selected);
if (index < parent.getChildren().size() - 1) {
boolean editing = tg.isItemEdited(new Model<MenuTreeNode>(selected));
treeModel.nodeDeleted(tree, selected);
parent.getEntry().getChildren().remove(selected.getEntry());
parent.getEntry().getChildren().add(index + 1, (ChildEntry) selected.getEntry());
treeModel.nodeInserted(tree, parent, selected);
tree.getTreeState().selectNode(selected, true);
tg.setItemEdit(new Model<MenuTreeNode>(selected), editing);
tree.updateTree();
}
target.addComponent(links);
}