Source code: com/voytechs/html/event/DispatcherBroker.java
1 /*
2 * File: DispatcherBroker.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: DispatcherBroker.java,v 1.1.1.1 2002/01/23 23:52:46 voytechs Exp $
6 * Note: TABS=2
7 ********************************************
8 * $Log: DispatcherBroker.java,v $
9 * Revision 1.1.1.1 2002/01/23 23:52:46 voytechs
10 * Initial public release, BETA 1.0 - voytechs
11 *
12 */
13 package com.voytechs.html.event;
14
15 import com.voytechs.html.util.LogFacility;
16
17 import com.voytechs.html.event.*;
18
19 import java.lang.*;
20 import java.util.*;
21
22 /**
23 * Specialized container object for holding dispatcher
24 * listeners. Because of dynamic binding of HTML components and
25 * the dispatcher, addtion of listeners has to be delayed until
26 * the dispatcher bound and initlizated otherwise the binding can't
27 * occur. This object adapts to the HTML component special requirements
28 * such as generating a URL acceptable parameters or HTML Form tags.
29 */
30 public abstract class DispatcherBroker {
31
32 /* Internal attributes */
33
34 private int id = 0;
35 private int eventType = 0;
36
37 private Vector listenerRequests = new Vector();
38 private Dispatcher dispatcher = null;
39
40 /**
41 * Initialized the broker.
42 * @param eventType Type of event this broker will be brokering for.
43 */
44 public DispatcherBroker(int eventType) {
45 super();
46
47 this.eventType = eventType;
48 }
49
50 /**
51 * Sets the global dispatcher. Late bindings, adding
52 * delayed listeners, etc. Called after the full component tree is built.
53 * @param dispatcher Informs that binding of the dispatcher is complete and
54 * dispatcher is ready to receive delayed listeners any time now.
55 */
56 protected void setDispatcher(Dispatcher dispatcher)
57 throws EventException {
58
59 LogFacility.log.println(this + "::DispatcherBroker::" +
60 "setDispatcher()");
61
62 this.dispatcher = dispatcher;
63
64 if(hasListenerRequests())
65 addDelayedListeners();
66 }
67
68 /**
69 * Add listener. Can add listeners only after the init method is called which
70 * means that all of the bindings to the dispatcher have already occured and
71 * we can safely register our listener.
72 * @exception Any Dispatcher related errors.
73 */
74 public void addListener(ListenerRequest request)
75 throws EventException {
76
77
78 if(dispatcher != null) {
79 LogFacility.log.println(this + "::DispatcherBroker::addListener() - IMMEDIATE " +
80 "listener=" + request.getListener());
81 if(hasListenerRequests())
82 addDelayedListeners();
83
84 dispatcher.addListener(getEventType(), getId(), request);
85 }
86 else {
87 LogFacility.log.println(this + "::DispatcherBroker::addListener() - DELAYED " +
88 "listener=" + request.getListener());
89 listenerRequests.addElement(request);
90 }
91 }
92
93 protected boolean hasListenerRequests() {
94 return(listenerRequests.isEmpty() == false);
95 }
96
97 /**
98 * Since event listeners are added in the constructor of the element, the
99 * dispatcher isn't bound to the component at the time. The listeners are
100 * added to a queue instead and only registered with the dispatcher from
101 * the init() method which is called after the dispatcher is bound.
102 * @exception Any Dispatcher related errors.
103 */
104 protected void addDelayedListeners() throws EventException {
105 LogFacility.log.println(this + "::DispatcherBroker::" +
106 "addDelayedListeners() - adding delayed listeners");
107
108 if(listenerRequests.isEmpty() == false)
109 dispatcher.enableEvents(getEventType());
110
111 for(int i = 0; i < listenerRequests.size(); i ++) {
112
113 ListenerRequest request = (ListenerRequest)listenerRequests.elementAt(i);
114
115 dispatcher.addListener(getEventType(), getId(), request);
116 LogFacility.log.println(this + "::DispatcherBroker::" +
117 "addDelayedListeners() - listener=" + request.getListener() +
118 " id=" + getId());
119 }
120
121 if(listenerRequests.isEmpty() == false)
122 listenerRequests.removeAllElements();
123 }
124
125 /**
126 * Get event ID
127 * @return Uniq event ID.
128 */
129 public int getId() throws EventException {
130 LogFacility.log.println(this + "::DispatcherBroker::getElementId() -" +
131 " id=" + id);
132
133 if(id == 0)
134 id = dispatcher.generateUniqId();
135
136 return(id);
137 }
138
139 /**
140 * Get eventType.
141 * @return Element type.
142 */
143 public int getEventType() {
144 return(eventType);
145 }
146
147 /**
148 * Test function for DispatcherBroker
149 * @param args command line arguments
150 */
151 public static void main(String [] args) {
152 }
153
154 } /* END OF: DispatcherBroker */