Announcement

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

    Some FormItem Types w/ Default Value Not Triggering Form ItemChangedHandler

    It appears that some FormItem types do not trigger the parent Form's ItemChangedHandler when default values are set?

    Issue demonstrated here, though not exhaustively.
    Code:
    package com.smartgwt.sample.client;
    
    import java.util.Date;
    import java.util.Map;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.types.Alignment;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.events.ItemChangedEvent;
    import com.smartgwt.client.widgets.form.events.ItemChangedHandler;
    import com.smartgwt.client.widgets.form.fields.DateTimeItem;
    import com.smartgwt.client.widgets.form.fields.DoubleItem;
    import com.smartgwt.client.widgets.form.fields.FormItem;
    import com.smartgwt.client.widgets.form.fields.SelectItem;
    import com.smartgwt.client.widgets.form.fields.TextAreaItem;
    
    public class DemoFormItemChangedSomeNot implements EntryPoint {
    
        @Override
        public void onModuleLoad() {
    
            final DynamicForm dynamicForm = new DynamicForm() {
                {
                    setShowEdges(Boolean.TRUE);
                    // setShowShadow(Boolean.TRUE);
                    setAlign(Alignment.CENTER);
                    setNumCols(4);
                    setCellBorder(1);
                }
            };
    
            dynamicForm.setFields(makeFormItems());
    
            dynamicForm.addItemChangedHandler(new ItemChangedHandler() {
    
                @Override
                public void onItemChanged(ItemChangedEvent event) {
    
                    Map<String, String> oldValues = dynamicForm.getOldValues();
                    String fieldName = event.getItem().getName();
                    String oldValue = oldValues.get(fieldName);
    
                    String newValue = event.getNewValue().toString();
    
                    String msg = "Item Changed:  " + event.getItem().getName();
                    msg += "<br />";
                    msg += "Old Value:  " + oldValue;
                    msg += "<br />";
                    msg += "New Value:  " + newValue;
    
                    SC.say(msg);
                }
            });
    
            dynamicForm.draw();
        }
    
        private FormItem[] makeFormItems() {
    
            FormItem[] formItems = new FormItem[] {
    
            makeSelectItem1(),
    
            makeTextAreaItem1(),
    
            makeDateTimeItem1(),
    
            makeDoubleItem()
    
            };
    
            return formItems;
        }
    
        private FormItem makeDoubleItem() {
    
            DoubleItem doubleItem1 = new DoubleItem("doubleItem1", "DoubleItem #1") {
                {
                    // Comment this line, and Form ItemChangedHandler works.
                    setDefaultValue(0.1D);
                }
            };
    
            return doubleItem1;
        }
    
        private FormItem makeDateTimeItem1() {
    
            DateTimeItem dateTimeItem1 = new DateTimeItem("dateTimeItem1",
                    "DateTime #1") {
                {
                    // Comment this line, and Form ItemChangedHandler works.
                    setDefaultValue(new Date());
                }
            };
    
            return dateTimeItem1;
        }
    
        private FormItem makeTextAreaItem1() {
    
            TextAreaItem textAreaItem1 = new TextAreaItem("textAreaItem1",
                    "Text Area #1") {
                {
                    setWrapTitle(Boolean.FALSE);
                    // No problem here
                    setDefaultValue("Not all FormItem types trigger Form ItemChanged handler, when default values are set?");
                }
            };
    
            return textAreaItem1;
        }
    
        private FormItem makeSelectItem1() {
    
            SelectItem selectItem = new SelectItem("selectItem1", "Select Item #1") {
                {
                    setWrapTitle(Boolean.FALSE);
                    setValueMap("Red", "Green", "Blue");
                    setDefaultToFirstOption(Boolean.TRUE); // No problem here.
                }
            };
    
            return selectItem;
        }
    
    }
    SmartClient Version: v9.1p_2016-07-08/PowerEdition Deployment (built 2016-07-08)
    Chrome 51.0.2704.106 m



    #2
    Can you clarify what you'er expecting here?

    Programmatic calls to setDefaultValue() or setValue() are not expected to trigger change notifications. Change notifications are for end user changes.

    Are you saying you are seeing an absence of a change notification for an end user change? If so, be specific about how to change the value without seeing the change notification: how you are entering the field (mouse or keyboard), what you type or click, whether and how you exit the field (note that some items fire change only on blur, some sooner, and this is by design).

    Comment


      #3
      Yes, I'm referring to changes made by user GUI interaction.

      Upon launch, the demo page will show a DynamicForm with pre-populated default values.

      Here's what I see, using the mouse to initially select each FormItem:

      SelectItem: ItemChangedHandler fires as expected (as demonstrated by dialog pop-up).
      - Use mouse cursor to select TextEditor or Picker.
      - Dialog appears.
      - After Dialog is dismissed, focus returns to TextEditor.

      TextAreaItem: ItemChangedHandler fires as expected.
      - Place mouse cursor in TextAreaEditor.
      - Delete or Add a character.
      - Dialog appears.
      - After Dialog is dismissed, focus returns to TextAreaEditor.

      DateTimeItem:
      ItemChangedHandler fired by TextEditor...
      - Place mouse cursor in TextEditor.
      - Delete a single character.
      - Dialog appears. OK.
      ...but not by Picker.
      - Select Picker w/ mouse cursor.
      - Use mouse to change date or time.
      - Click Apply. No dialog.

      DoubleItem: ItemChangedHandler never fired by TextEditor.
      - Place mouse cursor in TextEditor.
      - Make edits.
      - Exit by tab-out or by using mouse cursor to select another FormItem
      - No dialog.

      Again, the DateTimeItem and DoubleItem work as expected when no default value is set.

      I can work around this, just a) thought you'd want to know, and b) always hoping to learn more if I'm not getting it correctly.

      Comment


        #4
        The problem is that your item changed handler is generating a ClassCastException, as should be shown in the Dev Console even in Production Mode. (Specifically, the values in the Map obtained from DynamicForm.getOldValues() aren't exclusively String data.) It's useful to open the Dev Console and check for any warnings when you're noticing unexpected behavior.

        Try using the followed modified code:
        Code:
        @Override
        public void onItemChanged(ItemChangedEvent event) {
        
            Map<String, Object> oldValues = dynamicForm.getOldValues();
            String fieldName = event.getItem().getName();
            String oldValue = oldValues.get(fieldName).toString();
            String newValue = event.getNewValue().toString();
        
            String msg = "Item Changed:  " + event.getItem().getName();
            msg += "<br />";
            msg += "Old Value:  " + oldValue;
            msg += "<br />";
            msg += "New Value:  " + newValue;
        
            SC.say(msg);
        }

        Comment

        Working...
        X