01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.util;
09:
10: import net.mygwt.ui.client.event.BaseEvent;
11: import net.mygwt.ui.client.event.Listener;
12:
13: /**
14: * Abstract base class for objects that register listeners and fire events.
15: *
16: * @see Listener
17: * @see BaseEvent
18: */
19: public class Observable {
20:
21: public EventTable eventTable;
22:
23: /**
24: * Adds a listener bound by the given event type.
25: *
26: * @param eventType the eventType
27: * @param listener the listener to be added
28: */
29: public void addListener(int eventType, Listener listener) {
30: if (eventTable == null) {
31: eventTable = new EventTable();
32: }
33: eventTable.hook(eventType, listener);
34: }
35:
36: /**
37: * Removes a listener.
38: *
39: * @param eventType the event type
40: * @param listener the listener to be removed
41: */
42: public void removeListener(int eventType, Listener listener) {
43: if (eventTable != null) {
44: eventTable.unhook(eventType, listener);
45: }
46: }
47:
48: /**
49: * Removes all listeners.
50: */
51: public void removeAllListeners() {
52: if (eventTable != null) {
53: eventTable.removeAllListeners();
54: }
55: }
56:
57: /**
58: * Fires an event.
59: *
60: * @param eventType the event type
61: * @return <code>true</code> if any listeners cancel the event.
62: */
63: public boolean fireEvent(int eventType) {
64: return fireEvent(eventType, new BaseEvent());
65: }
66:
67: /**
68: * Fires an event.
69: *
70: * @param eventType eventType the event type
71: * @param be the base event
72: * @return <code>true</code> if any listeners cancel the event.
73: */
74: public boolean fireEvent(int eventType, BaseEvent be) {
75: be.type = eventType;
76: be.source = this ;
77: if (eventTable != null) {
78: return eventTable.sendEvent(be);
79: }
80: return true;
81: }
82:
83: }
|