Events

Managing Composite events.


Adding and Removing Listeners

Listeners can be added to any composite through the addListener method. In the following code snippet, Listeners are only notified of changes that occur within the composite, so the chosen event type is SWT.Modify.
Listeners must implement org.eclipse.swt.widgets.Listener.

	public void add(Listener listener) {
		this.addListener(SWT.Modify, listener);
	}

	public void remove(Listener listener) {
		this.removeListener(SWT.Modify, listener);
	}

Sending Event Notifications
Event notifications to Listeners are sent through the notifyListeners method, which takes two parameters: the event type from the SWT constants and an Event object. The Event object allows you to add an application object that would be useful to the Listeners–in this case, a dimSet object:

	protected void add(DimLine line) {
		this.dimSet.add(line);
		this.tableViewer.add(line);
		this.tableViewer.setSelection(new StructuredSelection(line));

		//Notify Listeners
		Event e = new Event();
		e.data= this.dimSet;
		this.notifyListeners(SWT.Modify,e);
	}

Override the dispose method
To avoid memory leaks override the dispose method to make sure you remove all listeners. Notice the use of the getListeners method to obtain an array of Listeners.

	public void dispose() {
		Listener[] array = this.getListeners(SWT.Modify);
		for(Listener l : array){
			this.remove(l);           // remove calls this.removeListener(SWT.Modify, listener)
		}
		super.dispose();
	}

org.eclipse.swt.widgets.Listener
Listeners will have to implement a handleEvent method in order to process event notifications. Notice in the following snippet I make use of the Event data object, which must be cast to the apropriate type.

	public void handleEvent(Event event) {
		DimSet ds = (DimSet) event.data;
		if(ds.getLineSet().size()>0){
			this.setPageComplete(true);
		}
		else{
			this.setPageComplete(false);
		}
	}

You must be logged in to post a comment.