You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CallbackManager.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * SVN: $Id$
  23. */
  24. package uk.org.ownage.dmdirc.parser.callbacks;
  25. import uk.org.ownage.dmdirc.parser.*;
  26. import uk.org.ownage.dmdirc.parser.callbacks.interfaces.ICallbackInterface;
  27. import java.util.Hashtable;
  28. import java.util.Enumeration;
  29. /**
  30. * IRC Parser Callback Manager.
  31. * Manages adding/removing/calling callbacks.
  32. *
  33. * @author Shane Mc Cormack
  34. * @version $Id$
  35. */
  36. public class CallbackManager {
  37. /** Reference to the parser object that owns this CallbackManager*/
  38. IRCParser myParser = null;
  39. /**
  40. * Constructor to create a CallbackManager
  41. *
  42. * @param parser IRCParser that owns this callback manager.
  43. */
  44. public CallbackManager(IRCParser parser) {
  45. myParser = parser;
  46. // Add callbacks
  47. addCallbackType(new CallbackOnChannelAction(myParser, this));
  48. addCallbackType(new CallbackOnChannelCTCP(myParser, this));
  49. addCallbackType(new CallbackOnChannelCTCPReply(myParser, this));
  50. addCallbackType(new CallbackOnChannelGotNames(myParser, this));
  51. addCallbackType(new CallbackOnChannelJoin(myParser, this));
  52. addCallbackType(new CallbackOnChannelKick(myParser, this));
  53. addCallbackType(new CallbackOnChannelMessage(myParser, this));
  54. addCallbackType(new CallbackOnChannelModeChanged(myParser, this));
  55. addCallbackType(new CallbackOnChannelNickChanged(myParser, this));
  56. addCallbackType(new CallbackOnChannelNonUserModeChanged(myParser, this));
  57. addCallbackType(new CallbackOnChannelNotice(myParser, this));
  58. addCallbackType(new CallbackOnChannelPart(myParser, this));
  59. addCallbackType(new CallbackOnChannelQuit(myParser, this));
  60. addCallbackType(new CallbackOnChannelSelfJoin(myParser, this));
  61. addCallbackType(new CallbackOnChannelSingleModeChanged(myParser, this));
  62. addCallbackType(new CallbackOnChannelTopic(myParser, this));
  63. addCallbackType(new CallbackOnChannelUserModeChanged(myParser, this));
  64. addCallbackType(new CallbackOnDataIn(myParser, this));
  65. addCallbackType(new CallbackOnDataOut(myParser, this));
  66. addCallbackType(new CallbackOnDebugInfo(myParser, this));
  67. addCallbackType(new CallbackOnErrorInfo(myParser, this));
  68. addCallbackType(new CallbackOnMOTDEnd(myParser, this));
  69. addCallbackType(new CallbackOnMOTDLine(myParser, this));
  70. addCallbackType(new CallbackOnMOTDStart(myParser, this));
  71. addCallbackType(new CallbackOnNickChanged(myParser, this));
  72. addCallbackType(new CallbackOnNickInUse(myParser, this));
  73. addCallbackType(new CallbackOnNoticeAuth(myParser, this));
  74. addCallbackType(new CallbackOnPrivateAction(myParser, this));
  75. addCallbackType(new CallbackOnPrivateCTCP(myParser, this));
  76. addCallbackType(new CallbackOnPrivateCTCPReply(myParser, this));
  77. addCallbackType(new CallbackOnPrivateMessage(myParser, this));
  78. addCallbackType(new CallbackOnPrivateNotice(myParser, this));
  79. addCallbackType(new CallbackOnQuit(myParser, this));
  80. addCallbackType(new CallbackOnServerReady(myParser, this));
  81. addCallbackType(new CallbackOnSocketClosed(myParser, this));
  82. addCallbackType(new CallbackOnUnknownAction(myParser, this));
  83. addCallbackType(new CallbackOnUnknownCTCP(myParser, this));
  84. addCallbackType(new CallbackOnUnknownCTCPReply(myParser, this));
  85. addCallbackType(new CallbackOnUnknownMessage(myParser, this));
  86. addCallbackType(new CallbackOnUnknownNotice(myParser, this));
  87. addCallbackType(new CallbackOnUserModeChanged(myParser, this));
  88. }
  89. /** Empty clone method to prevent cloning to get more copies of the CallbackManager */
  90. public Object clone() throws CloneNotSupportedException {
  91. throw new CloneNotSupportedException();
  92. }
  93. /** Hashtable used to store the different types of callback known. */
  94. private Hashtable<String,CallbackObject> callbackHash = new Hashtable<String,CallbackObject>();
  95. /**
  96. * Add new callback type.
  97. *
  98. * @param callback CallbackObject subclass for the callback.
  99. * @return if adding succeeded or not.
  100. */
  101. public boolean addCallbackType(CallbackObject callback) {
  102. if (!callbackHash.containsKey(callback.getLowerName())) {
  103. callbackHash.put(callback.getLowerName(), callback);
  104. return true;
  105. }
  106. return false;
  107. }
  108. /**
  109. * Remove a callback type.
  110. *
  111. * @param callback CallbackObject subclass to remove.
  112. * @return if removal succeeded or not.
  113. */
  114. public boolean delCallbackType(CallbackObject callback) {
  115. if (callbackHash.containsKey(callback.getLowerName())) {
  116. callbackHash.remove(callback.getLowerName());
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Get reference to callback object
  123. *
  124. * @param callbackName Name of callback object.
  125. */
  126. public CallbackObject getCallbackType(String callbackName) {
  127. if (callbackHash.containsKey(callbackName.toLowerCase())) {
  128. CallbackObject res = callbackHash.get(callbackName.toLowerCase());
  129. return res;
  130. }
  131. return null;
  132. }
  133. /**
  134. * Remove all callbacks.
  135. *
  136. * @param o instance of ICallbackInterface to remove.
  137. */
  138. public void delAllCallback(ICallbackInterface o) {
  139. CallbackObject cb = null;
  140. for (Enumeration e = callbackHash.keys(); e.hasMoreElements();) {
  141. cb = callbackHash.get(e.nextElement());
  142. if (cb != null) { cb.del(o); }
  143. }
  144. }
  145. /**
  146. * Add all callbacks.
  147. *
  148. * @param o instance of ICallbackInterface to add.
  149. */
  150. public void addAllCallback(ICallbackInterface o) {
  151. CallbackObject cb = null;
  152. for (Enumeration e = callbackHash.keys(); e.hasMoreElements();) {
  153. cb = callbackHash.get(e.nextElement());
  154. if (cb != null) { cb.add(o); }
  155. }
  156. }
  157. /**
  158. * Add a callback.
  159. * This method will throw a CallbackNotFound Exception if the callback does not exist.
  160. *
  161. * @param callbackName Name of callback object.
  162. * @param o instance of ICallbackInterface to add.
  163. * @throws CallbackNotFound If callback is not found.
  164. */
  165. public void addCallback(String callbackName, ICallbackInterface o) throws CallbackNotFound {
  166. CallbackObject cb = getCallbackType(callbackName);
  167. if (cb != null) { cb.add(o); }
  168. else { throw new CallbackNotFound("Callback '"+callbackName+"' could not be found."); }
  169. }
  170. /**
  171. * Add a callback with a specific target.
  172. * This method will throw a CallbackNotFound Exception if the callback does not exist.
  173. *
  174. * @param callbackName Name of callback object.
  175. * @param o instance of ICallbackInterface to add.
  176. * @param target Parameter to specify that a callback should only fire for specific things
  177. * @throws CallbackNotFound If callback is not found.
  178. */
  179. public void addCallback(String callbackName, ICallbackInterface o, String target) throws CallbackNotFound {
  180. CallbackObjectSpecific cb = (CallbackObjectSpecific)getCallbackType(callbackName);
  181. if (cb != null) { cb.add(o,target); }
  182. else { throw new CallbackNotFound("Callback '"+callbackName+"' could not be found."); }
  183. }
  184. /**
  185. * Add a callback without an exception.
  186. * This should be used if a callback is not essential for execution (ie the DebugOut callback)
  187. *
  188. * @param callbackName Name of callback object.
  189. * @param o instance of ICallbackInterface to add.
  190. * @return true/false if the callback was added or not.
  191. */
  192. public boolean addNonCriticalCallback(String callbackName, ICallbackInterface o) {
  193. try {
  194. addCallback(callbackName, o);
  195. return true;
  196. } catch (Exception e) { return false; }
  197. }
  198. /**
  199. * Add a callback with a specific target.
  200. * This should be used if a callback is not essential for execution
  201. *
  202. * @param callbackName Name of callback object.
  203. * @param o instance of ICallbackInterface to add.
  204. * @param target Parameter to specify that a callback should only fire for specific things
  205. * @throws CallbackNotFound If callback is not found.
  206. */
  207. public boolean addNonCriticalCallback(String callbackName, ICallbackInterface o, String target) {
  208. try {
  209. addCallback(callbackName, o, target);
  210. return true;
  211. } catch (Exception e) { return false; }
  212. }
  213. /**
  214. * Remove a callback
  215. *
  216. * @param callbackName Name of callback object.
  217. * @param o instance of ICallbackInterface to remove.
  218. */
  219. public void delCallback(String callbackName, ICallbackInterface o) {
  220. CallbackObject cb = getCallbackType(callbackName);
  221. if (cb != null) { cb.del(o); }
  222. }
  223. /**
  224. * Get SVN Version information.
  225. *
  226. * @return SVN Version String
  227. */
  228. public static String getSvnInfo () { return "$Id$"; }
  229. }