Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    ValuesManager setValue does not trigger setValue of the item.

    here's the example which I created from a test case:

    Code:
    package com.smartgwt.sample.showcase.client.dataintegration.java.form;
    
    
    import com.smartgwt.client.widgets.Canvas;
    import com.smartgwt.client.widgets.IButton;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.ValuesManager;
    import com.smartgwt.client.widgets.form.fields.PickTreeItem;
    
    import com.smartgwt.client.widgets.layout.HStack;
    import com.smartgwt.client.widgets.layout.VLayout;
    import com.smartgwt.client.widgets.tree.Tree;
    import com.smartgwt.client.widgets.tree.TreeNode;
    import com.smartgwt.sample.showcase.client.PanelFactory;
    import com.smartgwt.sample.showcase.client.ShowcasePanel;
    
    
    public class FormValidationSample extends ShowcasePanel {
    private static final String DESCRIPTION = "<p>ValuesManager does NOT trigger setValue</p>";
    
    public static class Factory implements PanelFactory {
    
    private String id;
    
    public Canvas create() {
    FormValidationSample panel = new FormValidationSample();
    id = panel.getID();
    return panel;
    }
    
    public String getID() {
    return id;
    }
    
    public String getDescription() {
    return DESCRIPTION;
    }
    }
    
    public Canvas getViewPanel() {
    final DynamicForm form = new DynamicForm();
    
    final ValuesManager valuesManager= new ValuesManager();
    form.setValuesManager(valuesManager);
    valuesManager.addMember(form);
    
    //create tree - no values 25 or 26
    Tree tree = new Tree();
    TreeNode rootNode = new TreeNode("root");
    tree.setRoot(rootNode);
    
    TreeNode newNode = new TreeNode("Parent1");
    tree.add(newNode, rootNode);
    TreeNode childNode = new TreeNode("Child 1");
    tree.add(childNode, newNode);
    
    final PickTreeItem select = new PickTreeItem("name", "text") {
    
    @Override
    public void setValue(String str){
    System.out.println("SET str:" + str);
    super.setValue(str);
    }
    
    @Override
    public void setValue(Object str){
    System.out.println("SET obj:" + str);
    super.setValue(str);
    }
    
    };
    
    select.setValueTree(tree);
    
    form.setItems(select);
    
    //set 26 using select
    IButton clearErrorsButton = new IButton("Set 26 on Select");
    clearErrorsButton.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
    // form.clearErrors(true);
    select.setValue("26");
    }
    });
    
    //set 25 using ValuesManager
    IButton clearErrorsButton25 = new IButton("Set 25 on ValuesManager");
    clearErrorsButton25.addClickHandler(new ClickHandler() {
    public void onClick(ClickEvent event) {
    valuesManager.setValue("name", "25");
    }
    });
    
    
    HStack buttons = new HStack(10);
    buttons.setHeight(24);
    
    buttons.setMembers( clearErrorsButton, clearErrorsButton25);
    
    VLayout layout = new VLayout(10);
    layout.setMembers(form, buttons);
    return layout;
    }
    
    public String getIntro() {
    return DESCRIPTION;
    }
    
    }
    As you can see from the code if I set the value of the select using "Set 26 on Select" button
    Code:
    select.setValue("26");
    the
    Code:
    setValue(Object str)
    is called and the output is printed out:
    SET str:26
    If I use
    Code:
    valuesManager.setValue("name", "25");
    - no
    Code:
    setValue(Object str)
    (as well no
    Code:
    handleChangeEvent( ChangeEvent event )
    triggered) - the item just change it's value.
    Please advise me what function should I overrride for the PickTreeItem in order to handle setting the value from the valuesManager.setValue or valuesManager.setValues.

    For this issue I use: SmartClient Version: v10.0p_2015-08-28/PowerEdition Deployment (built 2015-08-28)

    #2
    Can you clarify what you're trying to achieve here?
    This is as expected - the change/changed handler aren't fired because this is not a user-initiated change - it's coming from the application code, and setValues() on the ValuesManager isn't expected to call setValue() on the item.

    If you're trying to do custom formatting etc, there should be an appropriate API for that (for example the setValueFormatter() method).

    Regards
    Isomorphic Software

    Comment


      #3
      Here's the more complex example of the issue:
      Code:
      
      package com.smartgwt.sample.showcase.client.dataintegration.java.form;
      
      
      import com.smartgwt.client.util.SC;
      import com.smartgwt.client.widgets.Canvas;
      import com.smartgwt.client.widgets.IButton;
      import com.smartgwt.client.widgets.events.ClickEvent;
      import com.smartgwt.client.widgets.events.ClickHandler;
      import com.smartgwt.client.widgets.form.DynamicForm;
      import com.smartgwt.client.widgets.form.ValuesManager;
      import com.smartgwt.client.widgets.form.fields.PickTreeItem;
      
      import com.smartgwt.client.widgets.layout.HStack;
      import com.smartgwt.client.widgets.layout.VLayout;
      import com.smartgwt.client.widgets.tree.Tree;
      import com.smartgwt.client.widgets.tree.TreeNode;
      import com.smartgwt.sample.showcase.client.PanelFactory;
      import com.smartgwt.sample.showcase.client.ShowcasePanel;
      
      
      public class FormValidationSample extends ShowcasePanel {
          private static final String DESCRIPTION = "<p>ValuesManager does NOT trigger setValue</p>";
          
          private String string4FutureUse = null;
      
          public static class Factory implements PanelFactory {
      
              private String id;
      
              public Canvas create() {
                  FormValidationSample panel = new FormValidationSample();
                  id = panel.getID();
                  return panel;
              }
      
              public String getID() {
                  return id;
              }
      
              public String getDescription() {
                  return DESCRIPTION;
              }
          }
      
          public Canvas getViewPanel() {
              final DynamicForm form = new DynamicForm();
       
              final ValuesManager valuesManager= new ValuesManager();
              form.setValuesManager(valuesManager);
              valuesManager.addMember(form);
      
              //create tree - no values 25 or 26
              final Tree tree = new Tree();
              TreeNode rootNode = new TreeNode("root");
              tree.setRoot(rootNode);
      
              TreeNode newNode = new TreeNode("Parent1");
              tree.add(newNode, rootNode);
              TreeNode childNode = new TreeNode("Child 1");
              tree.add(childNode, newNode);
              
              final PickTreeItem select = new PickTreeItem("name", "text") {
                  
                  @Override
                  public void setValue(String str){
                      System.out.println("SET str:" + str);
                      boolean display = false;
                      TreeNode nodes[] = tree.getAllNodes();
                      for (TreeNode node : nodes) {
                          String name = node.getName();
                          if( name.equals(str)) {
                              display = true;
                              break;
                          }
                      }
                      
                      if (display) {
                          super.setValue(str);
                          FormValidationSample.this.string4FutureUse = null;
                      } else {
                          System.out.println("Future use: " + str);
                          FormValidationSample.this.string4FutureUse = str;
                          super.setValue("Value NOT found");
                      }
                  }
                  
                  @Override
                  public void setValue(Object str){
                      System.out.println("SET obj:" + str);
                      super.setValue(str);
                  }
                  
              };
              
              select.setValueTree(tree);
                  
              form.setItems(select);
            
              //set 26 using select
              IButton clearErrorsButton = new IButton("Set 26 on Select");
              clearErrorsButton.addClickHandler(new ClickHandler() {
                  public void onClick(ClickEvent event) {
                     // form.clearErrors(true);
                      select.setValue("26");
                  }
              });
              
              //set 25 using ValuesManager
              IButton clearErrorsButton25 = new IButton("Set 25 on ValuesManager");
              clearErrorsButton25.addClickHandler(new ClickHandler() {
                  public void onClick(ClickEvent event) {
                       valuesManager.setValue("name", "25");
                  }
              });
              
              IButton buttonSetChild = new IButton("Set Child on setValue");
              buttonSetChild.addClickHandler(new ClickHandler() {
                  public void onClick(ClickEvent event) {
                      select.setValue("Child 1");
                  }
              });
              
              IButton buttonDisplayUnset = new IButton("DisplayUnset");
              buttonDisplayUnset.addClickHandler(new ClickHandler() {
                  public void onClick(ClickEvent event) {
                      SC.say("Undisplayed Value was " + FormValidationSample.this.string4FutureUse);
          
                  }
              });
              
              
      
      
              HStack buttons = new HStack(10);
              buttons.setHeight(24);
          
              buttons.setMembers( clearErrorsButton, clearErrorsButton25, buttonSetChild, buttonDisplayUnset);
      
              VLayout layout = new VLayout(10);
              layout.setMembers(form, buttons);
              return layout;
          }
      
          public String getIntro() {
              return DESCRIPTION;
          }
      
      }
      I want to display ONLY values which are in the Tree already. In this example if I call:
      select.setValue("Child 1") it would be displayed on the Select, but if i call ​select.setValue("Child 2") - the displayed value would be the "Value NOT found".
      In the same time I keep the unset value ("Child 2") in the class and can use it in the future for my own purposes, to display it on the buttonDisplayUnset click for example.
      It works just fine for the select.setValue, but it does not work at all for valuesManager.setValue("name", "25"). The value "25" is displayed on the select (which I want to prevent) and my string4FutureUse is not initiated.

      I want to be able to handle both the select.setValue and the valuesManager.setValue/s.
      Please advise how I can handle the valuesManager.setValue/s for the PickTreeItem ? I expected the select.setValue to be triggered, but as you said it is not...

      Comment


        #4
        Seems like you really want to customize the value displayed to the user to show the "Value NOT found" string in the case where the (programatically) specified value isn't present in the tree.
        A custom formatter would be the correct way to achieve this.

        Let us know if you are having trouble getting things working with that approach

        Thanks
        Isomorphic Software

        Comment


          #5
          No, I really wanted to find the "string4FutureUse" in the case the valuesManager.setValues() was used or select.setValues() was used.
          For example the following code is used:
          valuesManager.setValue("name", "25");
          or
          select.setValue("25");
          if the TreeNode with this value does not exist I want the string4FutureUse to be 25.
          This way I will know that someone tried to set the "25" even if it's not valid value.

          Right now, if the process uses the valuesManager.setValue("name", "25"); it just set into the select and my code to find string4FutureUse is not invoked at all.

          Comment


            #6
            Overriding setValue() isn't an option- it's not implemented as an override point and won't be called from setValues() on the form itself or from internal SmartClient JavaScript logic which may set the value.
            We also don't provide a notification method that fires every time a value is set programatically for FormItem, as the method is being called by application code, so customizing the value would typically be achieved in app code before form item's setValue() method is invoked at all.
            We do have notification methods fired when the user changes the value, and we have notification type methods allowing developers to customize how values are displayed (custom formatters, and "showValue" for CanvasItem).

            To achieve your result, a couple of options that occur are:
            - modify the calling code to handle the case where the specified value is invalid and modify it / store it out for future use, etc
            - allow the value to be set, but have custom formatting code such that it is not displayed to the user. Application code can still get at it, so instead of looking at a stored out "string4FutureUse", application code could look at the item value directly and at the the tree directly.

            Hopefully this gives you a starting point that allows you to come up with a solution. If not, let us know, perhaps along with a high level description of why this sort of approach isn't working for you and we'll see if we can come up with a better recommendation.

            Regards
            Isomorphic Software


            Comment

            Working...
            X