litesoft
@ 625
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / foundation / widgets / support / NonDomHandlerManager.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.temp.foundation.widgets.support; import java.util.*; import com.google.gwt.event.shared.*; import com.temp.foundation.widgets.support.events.*; import com.temp.shared.utils.*; /** * @param <H> interface implemented by handlers of this kind of event */ public class NonDomHandlerManager<H extends EventHandler> implements HasHandlers { private final List<H> handlers = new ArrayList<H>(); private final Object source; public NonDomHandlerManager(Object source) { this.source = source; } @Override public void fireEvent(GwtEvent<?> event) { if (!(event instanceof NonDomEvent)) { throw new IllegalStateException("NonDomHandlerManager can currently only dispatch Events than implement NonDomEvent"); } NonDomEvent<H> ndEvent = ObjectUtils.cast(event); ndEvent.setSource(source); for (H handler : handlers) { if (handler != null) { ndEvent.dispatch(handler); } } } /** * Adds a handler. * * @param handler the handler * @return the handler registration */ public HandlerRegistration addHandler(H handler) { if (handler == null) { return null; } int index = handlers.size(); handlers.add(handler); return new IndexHandlerRegistration(index); } private class IndexHandlerRegistration implements HandlerRegistration { private final int index; private IndexHandlerRegistration(int index) { this.index = index; } @Override public void removeHandler() { if (index < handlers.size()) { handlers.set(index, null); } } } } |