/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.topspin.ui.client;
import com.google.gwt.events.client.EventListener;
import com.google.gwt.events.client.EventListenerRemover;
import java.util.ArrayList;
import java.util.List;
/**
* Maintains a collection of {@link EventListener event listeners}.
*
* This is most useful for synthetic events (i.e. those that derive from
* {@link WidgetEventListener}).
*/
public class WidgetEventListenerCollection {
private List<WidgetEventListener> listeners;
/**
* Adds an event to the collection.
*
* @param listener the listener to be added
* @return a handle that may be used to remove the listener
*/
public EventListenerRemover add(final WidgetEventListener listener) {
if (listeners == null) {
listeners = new ArrayList<WidgetEventListener>();
}
listeners.add(listener);
return new EventListenerRemover() {
public void remove() {
listeners.remove(listener);
}
};
}
/**
* Fires an event to all listeners in this collection.
*
* @param event the event to be fired
*/
public void fire(WidgetEvent event) {
if (listeners != null) {
for (WidgetEventListener listener : listeners) {
event.fire(listener);
}
}
}
/**
* Returns the number of listeners in this collection.
*/
public int size() {
return (listeners == null) ? 0 : listeners.size();
}
}