Announcement

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

    Issue with Custom Types and Change(d)Handlers

    Smart GWT EE, SC_SNAPSHOT-2010-12-14/EVAL Deployment

    Hi,

    My problem is the following:

    When the value of a certain field is changed, a custom operation needs to be performed. This seems like a trivial thing, just use the ChangedEvent or ItemChangedEvent and call the custom operation from inside.

    However, working with my own custom simple types, this seems to take it to another level. What I mean is that I can't get my head around when the framework is triggering these events and the data that is in these events.

    The ChangeEvent and ChangeDEvent are called everytime I type something in my formItem?!? Thus will trigger the operation too soon. So, I thought about checking the "event values" before calling my operation.

    But the "values" in the events are always the same between all objects in the event?!? See, the code and output below to see what I mean.

    Example, code below generates the given output when typing...

    Code:
    protected class FleetVehicleChangedHandler implements ChangedHandler {
    		
    	public void onChanged(ChangedEvent event) {
    		FormItem source = (FormItem) event.getSource();
    		FormItem item = event.getItem();
    		if(event.getValue() != null){				
    			GWT.log("event.value: " + JSOHelper.getAttribute((JavaScriptObject) event.getValue(), SilkString.FIELD_TEXT));
    		}
    			
    		if(event.getItem() != null){				
    			GWT.log("item.value: " + JSOHelper.getAttribute((JavaScriptObject) item.getValue(), SilkString.FIELD_TEXT));
    		}
    			
    		if(event.getSource() != null){				
    			GWT.log("source.value: " + JSOHelper.getAttribute((JavaScriptObject) source.getValue(), SilkString.FIELD_TEXT));
    		}
    
    		if(!source.getValue().equals(event.getValue())){
    			parent.performCustomOperation(FineOutpostService.completeIncident);	
    		}			
    	}		
    }
    
    [20110104 08:13:35] event.value: I
    [20110104 08:13:35] item.value: I
    [20110104 08:13:35] source.value: I
    [20110104 08:13:35] event.value: I 
    [20110104 08:13:35] item.value: I 
    [20110104 08:13:35] source.value: I 
    [20110104 08:13:35] event.value: I a
    [20110104 08:13:35] item.value: I a
    [20110104 08:13:35] source.value: I a
    [20110104 08:13:35] event.value: I am
    [20110104 08:13:35] item.value: I am
    [20110104 08:13:35] source.value: I am
    [20110104 08:13:36] event.value: I am 
    [20110104 08:13:36] item.value: I am 
    [20110104 08:13:36] source.value: I am 
    [20110104 08:13:36] event.value: I am t
    [20110104 08:13:36] item.value: I am t
    [20110104 08:13:36] source.value: I am t
    [20110104 08:13:37] event.value: I am ty
    [20110104 08:13:37] item.value: I am ty
    [20110104 08:13:37] source.value: I am ty
    [20110104 08:13:37] event.value: I am typ
    [20110104 08:13:37] item.value: I am typ
    [20110104 08:13:37] source.value: I am typ
    [20110104 08:13:37] event.value: I am typi
    [20110104 08:13:37] item.value: I am typi
    [20110104 08:13:37] source.value: I am typi
    [20110104 08:13:37] event.value: I am typin
    [20110104 08:13:37] item.value: I am typin
    [20110104 08:13:37] source.value: I am typin
    [20110104 08:13:38] event.value: I am typing
    [20110104 08:13:38] item.value: I am typing
    [20110104 08:13:38] source.value: I am typing
    [20110104 08:13:38] event.value: I am typing 
    [20110104 08:13:38] item.value: I am typing 
    [20110104 08:13:38] source.value: I am typing 
    [20110104 08:13:38] event.value: I am typing s
    [20110104 08:13:38] item.value: I am typing s
    [20110104 08:13:38] source.value: I am typing s
    [20110104 08:13:38] event.value: I am typing so
    [20110104 08:13:38] item.value: I am typing so
    [20110104 08:13:38] source.value: I am typing so
    [20110104 08:13:38] event.value: I am typing som
    [20110104 08:13:38] item.value: I am typing som
    [20110104 08:13:38] source.value: I am typing som
    [20110104 08:13:38] event.value: I am typing some
    [20110104 08:13:38] item.value: I am typing some
    [20110104 08:13:38] source.value: I am typing some
    [20110104 08:13:39] event.value: I am typing somet
    [20110104 08:13:39] item.value: I am typing somet
    [20110104 08:13:39] source.value: I am typing somet
    [20110104 08:13:39] event.value: I am typing someth
    [20110104 08:13:39] item.value: I am typing someth
    [20110104 08:13:39] source.value: I am typing someth
    [20110104 08:13:39] event.value: I am typing somethi
    [20110104 08:13:39] item.value: I am typing somethi
    [20110104 08:13:39] source.value: I am typing somethi
    [20110104 08:13:39] event.value: I am typing somethin
    [20110104 08:13:39] item.value: I am typing somethin
    [20110104 08:13:39] source.value: I am typing somethin
    [20110104 08:13:39] event.value: I am typing something
    [20110104 08:13:39] item.value: I am typing something
    [20110104 08:13:39] source.value: I am typing something
    [20110104 08:13:39] event.value: I am typing something 
    [20110104 08:13:39] item.value: I am typing something 
    [20110104 08:13:39] source.value: I am typing something
    The code of the custom editor

    Code:
    public class SilkStringEditor extends TextItem {
    
    	private String text;
    
    	public SilkStringEditor() {
    		super();
    
    		// link the formatter to the editor
    		this.setEditorValueFormatter(new SilkStringValueFormatter());
    		this.setEditorValueParser(new SilkStringValueParser());
    	}
    
    	public class SilkStringValueFormatter implements FormItemValueFormatter {
    
    		public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
    
    			if (value == null)
    				return SilkDBConstants.NULL_STRING;
    
    			if (value instanceof JavaScriptObject) {
    				JavaScriptObject object = (JavaScriptObject) value;
    
    				text = JSOHelper.getAttribute(object, SilkString.FIELD_TEXT);
    
    				return text;
    			}
    
    			return (String) value;
    		}
    
    	}
    
    	public class SilkStringValueParser implements FormItemValueParser {
    
    		public Object parseValue(String value, DynamicForm form, FormItem item) {
    			JavaScriptObject jsObject = (JavaScriptObject) item.getValue();
    
    			if(jsObject == null) return createJSObject(value);
    			
    			JavaScriptObject newJsObject = SilkJSOHelper.clone(jsObject);
    			JSOHelper.setAttribute(newJsObject, SilkString.FIELD_TEXT, value);
    			return newJsObject;
    		}
    
    	}
    	
    
    }

    #2
    Change/Changed fire at different times on different items, since it's not useful to, for example, receive change notifications while the user is typing in a date (it's going to be invalid during most of the keystrokes).

    For TextItem in particular, you can change the granularity of change events by setting changeOnKeypress.

    Beyond that, we don't understand what you think is surprising about those logs.

    Comment


      #3
      Alright, disabling the KeyPress did the trick, however I can see some inconsistencies between the ItemChangeEvent and the ChangeEvent.

      I changed the value from "setKeyPress(true)" to "setKeyPress(false)"

      Apparently, the oldValue coming trough in the ChangeEvent is also the NEW value.

      Code:
      [20110105 15:27:41] ------------------------------------
      [20110105 15:27:41] ChangeEvent.oldValue: setKeyPress(false)
      [20110105 15:27:41] ChangeEvent.value: setKeyPress(false)
      [20110105 15:27:41] ChangeEvent.itemValue: setKeyPress(true)
      [20110105 15:27:41] ChangeEvent.sourceValue: setKeyPress(true)
      [20110105 15:27:41] ----------------------------------------
      
      [20110105 15:27:41] ------------------------------------
      [20110105 15:27:41] ItemChangeEvent.oldValue: setKeyPress(true)
      [20110105 15:27:41] ItemChangeEvent.newValue: setKeyPress(false)
      [20110105 15:27:41] ItemChangeEvent.itemValue: setKeyPress(true)
      [20110105 15:27:41] -----------------------------------

      Comment

      Working...
      X