KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > ircfw > ProtocolDispatcher


1 package rero.ircfw;
2
3 import rero.ircfw.interfaces.FrameworkConstants;
4 import rero.ircfw.interfaces.ChatListener;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.ListIterator JavaDoc;
10
11 public class ProtocolDispatcher implements FrameworkConstants
12 {
13     protected LinkedList JavaDoc temporary = new LinkedList JavaDoc();
14     protected LinkedList JavaDoc permanent = new LinkedList JavaDoc();
15
16     protected ChatListener internal = null;
17
18     public void dispatchEvent(HashMap JavaDoc eventDescription)
19     {
20         String JavaDoc eventId = (String JavaDoc)eventDescription.get($EVENT$);
21
22         int rv = ChatListener.EVENT_DONE;
23
24         if (internal != null && internal.isChatEvent(eventId, eventDescription))
25         {
26             rv = internal.fireChatEvent(eventDescription);
27         }
28
29         if (rv == ChatListener.EVENT_DONE)
30         {
31             rv = easyDispatch(temporary, eventId, eventDescription);
32         }
33
34         if (rv == ChatListener.EVENT_DONE)
35         {
36             rv = easyDispatch(permanent, eventId, eventDescription);
37         }
38     }
39
40     private int easyDispatch(List JavaDoc listeners, String JavaDoc eventId, HashMap JavaDoc eventDescription)
41     {
42         ChatListener l;
43
44         ListIterator JavaDoc iter = listeners.listIterator();
45         while (iter.hasNext())
46         {
47             l = (ChatListener)iter.next();
48    
49             if (l.isChatEvent(eventId, eventDescription))
50             {
51                 int rv = l.fireChatEvent(eventDescription);
52
53                 if ((rv & (ChatListener.REMOVE_LISTENER)) == ChatListener.REMOVE_LISTENER)
54                 {
55                     iter.remove();
56                 }
57
58                 if ((rv & (ChatListener.EVENT_HALT)) == ChatListener.EVENT_HALT)
59                 {
60                     return ChatListener.EVENT_HALT;
61                 }
62             }
63         }
64        
65         return ChatListener.EVENT_DONE;
66     }
67
68     public void addTemporaryListener(ChatListener l)
69     {
70         temporary.addFirst(l);
71     }
72
73     public void addChatListener(ChatListener l)
74     {
75         permanent.addFirst(l);
76     }
77
78     public void setInternalListener(ChatListener l)
79     {
80         internal = l;
81     }
82 }
83
84
85
Popular Tags