1 package rero.bridges.event; 2 3 import java.util.HashMap ; 4 import java.util.List ; 5 import java.util.LinkedList ; 6 import java.util.ListIterator ; 7 8 import rero.bridges.event.CodeSnippet; 9 import rero.ircfw.interfaces.ChatListener; 11 public abstract class ScriptedEventListener 12 { 13 protected boolean registered = false; 15 protected List temporary = new LinkedList (); 16 protected List permanent = new LinkedList (); 17 18 public int dispatchEvent(HashMap eventDescription) 19 { 20 int rv = easyDispatch(temporary, eventDescription); 21 22 if (rv == ChatListener.EVENT_DONE) 23 { 24 rv = easyDispatch(permanent, eventDescription); 25 } 26 27 return rv; 28 } 29 30 private int easyDispatch(List listeners, HashMap eventDescription) 31 { 32 CodeSnippet l; 33 34 ListIterator iter = listeners.listIterator(); 35 while (iter.hasNext()) 36 { 37 l = (CodeSnippet)iter.next(); 38 39 if (l.isValid()) 40 { 41 if (listeners == temporary) 42 { 43 iter.remove(); 44 } 45 46 int rv = l.execute(eventDescription); 47 48 if ((rv & (ChatListener.REMOVE_LISTENER)) == ChatListener.REMOVE_LISTENER && listeners != temporary) 49 { 50 iter.remove(); 51 } 52 53 if ((rv & (ChatListener.EVENT_HALT)) == ChatListener.EVENT_HALT) 54 { 55 return ChatListener.EVENT_HALT; 56 } 57 } 58 else 59 { 60 iter.remove(); } 62 } 63 64 return ChatListener.EVENT_DONE; 65 } 66 67 public void addTemporaryListener(CodeSnippet c) 68 { 69 temporary.add(c); 70 } 71 72 public void addListener(CodeSnippet c) 73 { 74 permanent.add(c); 75 } 76 77 public abstract void setupListener(); 78 79 public boolean isSetup() 80 { 81 return registered; 82 } 83 84 public void setRegistered() 85 { 86 registered = true; 87 } 88 } 89 90 91 | Popular Tags |