Announcement

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

    Registering event handler

    I am trying to make a ToolStrip fire an event when its content has changed,
    what I do not understand is how to register the handler, I must be doing something wrong since the handler count is 0 both before and after the call to addDataChangedHandler.

    Also I have not been able to figure out how to actually fire the
    event in _test(); I tried something like
    Code:
    fireEvent(new DataChangedEvent(jsObj));
    but I do not know what to give as argument.

    Anyone know what is wrong with this picture ?

    Code:
    import com.google.gwt.event.shared.HandlerRegistration;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    import com.smartgwt.client.data.events.HasDataChangedHandlers;
    import com.smartgwt.client.widgets.toolbar.ToolStrip;
    
    public class DataSourceToolbar extends ToolStrip implements HasDataChangedHandlers {
    	
    	public void _test(){
    		Util.logMessage("FIRING EVENT");
    	}
    	
    	@Override
    	public HandlerRegistration addDataChangedHandler(DataChangedHandler handler) {
    		Util.logMessage("Registering handler");
    		return addHandler(handler, DataChangedEvent.getType());
    	}
    }
    Code:
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    
    public class Test implements EntryPoint {
    	
    	/**
    	 * The DataSourceObject contains information which describes what
    	 * information to load, and with it we can recreate the request url.
    	 */
    	DataSourceObject ds;
    
    	/* (non-Javadoc)
    	 * @see com.google.gwt.core.client.EntryPoint#onModuleLoad()
    	 */
    	public void onModuleLoad() {
    
    		DataSourceToolbar t = new DataSourceToolbar();
    		Util.logMessage(String.valueOf(t.getHandlerCount(DataChangedEvent.getType())));
    		t.addDataChangedHandler(new DataChangedHandler() {
    			@Override
    			public void onDataChanged(DataChangedEvent event) {
    				Util.logMessage("EVENT RECEIVED");
    			}
    		});
    		Util.logMessage(String.valueOf(t.getHandlerCount(DataChangedEvent.getType())));
    		t._test();
    		t._test();
    		t._test();
    	}
    }

    #2
    Solved using the following code

    Note the use of doAddHandler instead of addHandler in the implementation of addDataChangedHandler

    Code:
    package test.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    import com.smartgwt.client.widgets.HTMLPane;
    
    public class CustomEvents implements EntryPoint {
    
    	static HTMLPane log;
    	
    	public void onModuleLoad() {
    		log = new HTMLPane();
    		log.setContents("Logging started");
    		log.setHeight100();
    		log.setWidth100();
    		log.draw();
    		
    		CustomEvents.log("Creating ToolStrip");
    		MyStrip t = new MyStrip();
    		t.addDataChangedHandler(new DataChangedHandler () {
    			@Override
    			public void onDataChanged(DataChangedEvent event) {
    				CustomEvents.log("EVENT RECEIVED !!!");
    			}
    		});
    		t.test();
    	}
    	
    	public static void log(String s){
    		String tmp = log.getContents();
    		log.setContents(tmp + "<br/>" + s);
    	}
    }
    
    package test.client;
    
    import com.google.gwt.event.shared.HandlerRegistration;
    import com.google.gwt.event.shared.HasHandlers;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    import com.smartgwt.client.data.events.HasDataChangedHandlers;
    import com.smartgwt.client.widgets.toolbar.ToolStrip;
    
    public class MyStrip extends ToolStrip implements HasDataChangedHandlers, HasHandlers {
    	
    	public void test(){
    		CustomEvents.log("Firing event!");
    		fireEvent(new DataChangedEvent(this.getJsObj()));
    	}
    	
    	@Override
    	public HandlerRegistration addDataChangedHandler(DataChangedHandler handler) {
    		CustomEvents.log("Adding handler");
    		HandlerRegistration r = doAddHandler(handler, DataChangedEvent.getType());
    		return r;
    	}
    }

    Comment

    Working...
    X