}
public void addElement(Component cmp) {
viewStateSnapshot = TreeViewStateSnapshot.takeSnapshot(tree);
Textbox name = (Textbox) cmp.getFellow("newOrderElementName");
Intbox hours = (Intbox) cmp.getFellow("newOrderElementHours");
if (StringUtils.isEmpty(name.getValue())) {
throw new WrongValueException(name, _("cannot be empty"));
}
if (hours.getValue() == null) {
hours.setValue(0);
}
Textbox nameTextbox = null;
// Parse hours
try {
if (tree.getSelectedCount() == 1) {
T node = getSelectedNode();
T newNode = getModel().addElementAt(node, name.getValue(),
hours.getValue());
getRenderer().refreshHoursValueForThisNodeAndParents(newNode);
getRenderer().refreshBudgetValueForThisNodeAndParents(newNode);
// Moved here in order to have items already renderer in order
// to select the proper element to focus
reloadTreeUIAfterChanges();
if (node.isLeaf() && !node.isEmptyLeaf()) {
// Then a new container will be created
nameTextbox = getRenderer().getNameTextbox(node);
} else {
// select the parent row to add new children ASAP
tree.setSelectedItem(getRenderer().getTreeitemForNode(
newNode.getParent().getThis()));
}
} else {
getModel().addElement(name.getValue(), hours.getValue());
// This is needed in both parts of the if, but it's repeated in
// order to simplify the code
reloadTreeUIAfterChanges();
}
} catch (IllegalStateException e) {
LOG.warn("exception ocurred adding element", e);
messagesForUser.showMessage(Level.ERROR, e.getMessage());
}
name.setValue("");
hours.setValue(0);
if (nameTextbox != null) {
nameTextbox.focus();
} else {
name.focus();