Announcement

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

    DataSource Listener Utility

    Here's a utility class I made to make it easier to create listeners for DataSource changes. Hope it is helpful - and also if you see any issues with this class (or can suggest improvements) would be grateful for any feedback.

    Code:
    package com.kisschedule.app.client;
    
    import com.smartgwt.client.data.Criteria;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.ResultSet;
    import com.smartgwt.client.data.events.DataArrivedEvent;
    import com.smartgwt.client.data.events.DataArrivedHandler;
    import com.smartgwt.client.data.events.DataChangedEvent;
    import com.smartgwt.client.data.events.DataChangedHandler;
    
    public class DataSourceListener implements DataArrivedHandler,
    		DataChangedHandler {
    
    	private ResultSet resultSet;
    	private DataSourceListenerCallback callback;
    
    	public DataSourceListener(DataSource dataSource,
    			DataSourceListenerCallback callback) {
    		this(dataSource, callback, null);
    	}
    
    	public DataSourceListener(DataSource dataSource,
    			DataSourceListenerCallback callback, Criteria criteria) {
    		this.callback = callback;
    		resultSet = new ResultSet(dataSource);
    		if (criteria != null)
    			resultSet.setCriteria(criteria);
    		resultSet.addDataArrivedHandler(this);
    		resultSet.addDataChangedHandler(this);
    		resultSet.get(0);
    	}
    
    	@Override
    	public void onDataChanged(DataChangedEvent event) {
    		callback.execute((ResultSet) event.getSource(), null, null);
    	}
    
    	@Override
    	public void onDataArrived(DataArrivedEvent event) {
    		callback.execute((ResultSet) event.getSource(), event.getStartRow(),
    				event.getEndRow());
    	}
    
    }
    and the interface spec
    Code:
    import com.smartgwt.client.data.ResultSet;
    
    public interface DataSourceListenerCallback {
    	public void execute(ResultSet resultSet, Integer start, Integer end);
    }

    Sample usage:

    Code:
     
     		DataSourceListenerCallback callback = new DataSourceListenerCallback() {
    			@Override
    			public void execute(ResultSet resultSet, Integer start, Integer end) {
    				if (start!=null)
    				   updateSettings(resultSet.get(start));
    			}
    		};
    
    		settingsListener = new DataSourceListener(
    				DataSource.get("settings"), callback);

    One usage, I haven't tried yet is to update a particular string tag in the UI. For example

    Code:
     		DataSourceListenerCallback callback = new DataSourceListenerCallback() {
    			@Override
    			public void execute(ResultSet resultSet, Integer start, Integer end) {
    				if (start!=null)
    				   updateLabel(resultSet.get(start));
    			}
    		};
    
    		settingsListener = new DataSourceListener(
    				DataSource.get("settings"), callback, new Criteria("setting","labelSetting");
    My assumption is that the callback will not fire unless the criteria is matched.

    An alternative, and perhaps more general mechanism would be to use GWT Events and instead of a callback, register listeners on those events.
Working...
X