001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.mvc;
009:
010: import java.util.ArrayList;
011: import java.util.List;
012:
013: import net.mygwt.ui.client.event.BaseEvent;
014: import net.mygwt.ui.client.util.Observable;
015:
016: /**
017: * Dispatchers are responsible for dispatching application events to
018: * controllers.
019: *
020: * <dl>
021: * <dt><b>Events:</b></dt>
022: *
023: * <dd><b>Dispatcher.BeforeDispatch</b> : (source, value)<br>
024: * <div>Fires before an event is dispatched. Listeners can set the
025: * <code>doit</code> field to <code>false</code> to cancel the action.</div>
026: * <ul>
027: * <li>source : this</li>
028: * <li>value : the app event</li>
029: * </ul>
030: * </dd>
031: *
032: * <dd><b>Dispatcher.AfterDispatch</b> : (source, data)<br>
033: * <div>Fires after an event has been dispatched.</div>
034: * <ul>
035: * <li>source : this</li>
036: * <li>value : the app event</li>
037: * </ul>
038: * </dd>
039: *
040: * </dl>
041: *
042: * @see DispatcherListener
043: */
044: public class Dispatcher extends Observable {
045:
046: /**
047: * Fires before an event is dispatched (value is 1100).
048: */
049: public static final int BeforeDispatch = 1100;
050:
051: /**
052: * Fires after an event has been dispatched (value is 1110).
053: */
054: public static final int AfterDispatch = 1110;
055:
056: private static Dispatcher instance;
057:
058: /**
059: * Forwards an app event to the dispatcher.
060: *
061: * @param event the app event
062: */
063: public static void forwardEvent(AppEvent event) {
064: instance.dispatch(event);
065: }
066:
067: /**
068: * Creates and forwards an app event to the dispatcher.
069: *
070: * @param eventType the app event type
071: */
072: public static void forwardEvent(int eventType) {
073: instance.dispatch(eventType);
074: }
075:
076: /**
077: * Returns the singleton instance.
078: *
079: * @return the dispatcher
080: */
081: public static Dispatcher get() {
082: if (instance == null) {
083: instance = new Dispatcher();
084: }
085: return instance;
086: }
087:
088: private List controllers;
089:
090: private Dispatcher() {
091: instance = this ;
092: controllers = new ArrayList();
093: }
094:
095: /**
096: * Adds a controller.
097: *
098: * @param controller the controller to be added
099: */
100: public void addController(Controller controller) {
101: if (!controllers.contains(controller)) {
102: controllers.add(controller);
103: }
104: }
105:
106: /**
107: * Adds a listener to receive dispatch events.
108: *
109: * @param listener the listener to add
110: */
111: public void addDispatcherListener(DispatcherListener listener) {
112: DispatcherTypedListener typedListener = new DispatcherTypedListener(
113: listener);
114: addListener(BeforeDispatch, typedListener);
115: addListener(AfterDispatch, typedListener);
116: }
117:
118: /**
119: * The dispatcher will query its controllers and pass the app event to any
120: * controllers that can handle the particular event type.
121: *
122: * @param event the app event
123: */
124: public void dispatch(AppEvent event) {
125: BaseEvent be = new BaseEvent();
126: be.source = this ;
127: be.value = event;
128: if (fireEvent(BeforeDispatch, be)) {
129: for (int i = 0; i < controllers.size(); i++) {
130: Controller controller = (Controller) controllers.get(i);
131: if (controller.canHandle(event)) {
132: if (!controller.initialized) {
133: controller.initialized = true;
134: controller.initialize();
135: }
136: controller.handleEvent(event);
137: }
138: }
139: fireEvent(AfterDispatch, be);
140: }
141: }
142:
143: /**
144: * The dispatcher will query its controllers and pass the app event to
145: * controllers that can handle the particular event type.
146: *
147: * @param type the event type
148: */
149: public void dispatch(int type) {
150: dispatch(new AppEvent(type));
151: }
152:
153: /**
154: * The dispatcher will query its controllers and pass the app event to
155: * controllers that can handle the particular event type.
156: *
157: * @param type the event type
158: * @param data the app event data
159: */
160: public void dispatch(int type, Object data) {
161: dispatch(new AppEvent(type, data));
162: }
163:
164: /**
165: * Returns all controllers.
166: *
167: * @return the list of controllers
168: */
169: public List getControllers() {
170: return controllers;
171: }
172:
173: /**
174: * Removes a controller.
175: *
176: * @param controller the controller to be removed
177: */
178: public void removeController(Controller controller) {
179: controllers.remove(controller);
180: }
181:
182: /**
183: * Removes a previously added listener.
184: *
185: * @param listener the listener to be removed
186: */
187: public void removeDispatcherListener(DispatcherListener listener) {
188: if (eventTable != null) {
189: eventTable.unhook(BeforeDispatch, listener);
190: eventTable.unhook(AfterDispatch, listener);
191: }
192: }
193:
194: }
|