0

I'm trying to clear the user selected value on a IPickTreeItem.

This is the only solution I've found to restrict the user from selecting some of the Tree root values (not all).

To be more clear, it seems that calling event.cancel() do not stop the event from bubbling.

Am I doing something wrong ?

TreeNode treenode = new TreeNode("root", new TreeNode("Operation A"),
new TreeNode("Operation B"));

final DynamicForm dynamicForm = new DynamicForm();

Tree tree = new Tree();
tree.setRoot(treenode);

final IPickTreeItem pickTreeItem = new IPickTreeItem();
pickTreeItem.setValueTree(tree);
pickTreeItem.addChangeHandler(new ChangeHandler()
{
    @Override
    public void onChange(ChangeEvent event)
    {
        pickTreeItem.clearValue() // Not clearing the value
        pickTreeItem.setValue((String)null) // Not working neither
        event.cancel() // Not seeming to work...
    }
});

dynamicForm.setItems(pickTreeItem);
dynamicForm.draw();

This is not working either :

pickTreeItem.setInputTransformer(new FormItemInputTransformer()
{

    @Override
    public Object transformInput(DynamicForm form, FormItem item,
    Object value, Object oldValue)
    {
        return "Desired New Value (not working)...";
    }
});

This is weird because it works using an external Button to clear the value (outside the picktreeitem handler)

Button bt = new Button("click");
bt.addClickHandler(new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        pickTreeItem.setValue((Object) null);
    }
});

Expected behavior

My Tree :

-aaaa
----bbbb
----cccc
-dddd
----eeee
----ffff

If the user selects "aaaa" the PickTreeItem value should be reverted to the defaultValue ("Choose a value"), optionally inform the user that he cannot pick "aaaa".

The PickTreeItem should accept "dddd" as a valid choosen value.

Jean-Michel Garcia
  • 2,359
  • 2
  • 23
  • 44

2 Answers2

1

As with all FormItems, event.cancel() is the correct way to disallow the change. There was a framework level bug that was preventing this from behaving correctly that has now been corrected.

See this thread on the Isomorphic forums

0

I understand it is not exactly the same with what you are trying to achieve, but you could consider to define a CustomValidator, that reads the selected values and returns false and an appropriate message, when one of the parent values that shouldn't be, is selected. For this to work, you must set pickTreeItem.setCanSelectParentItems(Boolean.TRUE), to allow for parent items to be selected, and pickTreeItem.setValidateOnChange(Boolean.TRUE), to validate the selected values upon selection.

gpapaz
  • 889
  • 14
  • 23
  • Your solution is almost good. However, if validation fails, is there any way to change the value of the PickTreeItem ? I'm trying to use PickTreeItem.setValue (inside the validator) but it fails. I'm adding more details about the expected behavior in the question. – Jean-Michel Garcia May 09 '12 at 14:29
  • Yes, as I said, it is not exactly your expected functionality ;) You rely on the user to select a new option. Due to bug, limitation, feature (pick your option) SmartGWT seems to not allow to change the value inside the ChangeHandlers or the Validators. Unless, I am missing something and someone else finds a better solution. – gpapaz May 09 '12 at 23:10