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.

CallbackObject.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.callbacks.interfaces.ICallbackInterface;
  26. import uk.org.ownage.dmdirc.parser.*;
  27. import uk.org.ownage.dmdirc.parser.callbacks.CallbackManager;
  28. import java.util.Hashtable;
  29. import java.util.ArrayList;
  30. /**
  31. * CallbackObject.
  32. * Superclass for all callback types.
  33. *
  34. * @author Shane Mc Cormack
  35. * @version $Id$
  36. */
  37. public abstract class CallbackObject {
  38. /** Arraylist for storing callback information related to the callback. */
  39. protected ArrayList<ICallbackInterface> callbackInfo = new ArrayList<ICallbackInterface>();
  40. /** Reference to the IRCParser that owns this callback. */
  41. protected IRCParser myParser = null;
  42. /** Reference to the CallbackManager in charge of this callback. */
  43. protected CallbackManager myManager = null;
  44. /**
  45. * Add a callback pointer to the appropriate ArrayList.
  46. *
  47. * @param eMethod OBject to callback to.
  48. */
  49. protected final void addCallback(ICallbackInterface eMethod) {
  50. for (int i = 0; i < callbackInfo.size(); i++) {
  51. if (eMethod.equals(callbackInfo.get(i))) { return; }
  52. }
  53. callbackInfo.add((ICallbackInterface)eMethod);
  54. }
  55. /**
  56. * Delete a callback pointer from the appropriate ArrayList.
  57. *
  58. * @param eMethod Object that was being called back to.
  59. */
  60. protected final void delCallback(ICallbackInterface eMethod) {
  61. for (int i = 0; i < callbackInfo.size(); i++) {
  62. if (eMethod.equals(callbackInfo.get(i))) { callbackInfo.remove(i); break; }
  63. }
  64. }
  65. /**
  66. * Call the OnErrorInfo callback.
  67. *
  68. * @param errorInfo ParserError object to pass as error.
  69. */
  70. protected final boolean callErrorInfo(ParserError errorInfo) {
  71. CallbackOnErrorInfo cb = (CallbackOnErrorInfo)myManager.getCallbackType("OnErrorInfo");
  72. if (cb != null) { return cb.call(errorInfo); }
  73. return false;
  74. }
  75. /**
  76. * Create a new instance of the Callback Object
  77. *
  78. * @param parser IRCParser That owns this callback
  79. * @param manager CallbackManager that is in charge of this callback
  80. */
  81. protected CallbackObject (IRCParser parser, CallbackManager manager) {
  82. this.myParser = parser;
  83. this.myManager = manager;
  84. }
  85. /**
  86. * Add a new callback.
  87. *
  88. * @param eMethod Object to callback to.
  89. */
  90. public void add(ICallbackInterface eMethod) { addCallback(eMethod); }
  91. /**
  92. * Remove a callback.
  93. *
  94. * @param eMethod Object to remove callback to.
  95. */
  96. public void del(ICallbackInterface eMethod) { delCallback(eMethod); }
  97. /** Get the name for this callback */
  98. public String getName() {
  99. Package thisPackage = this.getClass().getPackage();
  100. int packageLength = 0;
  101. if (thisPackage != null) {
  102. packageLength = thisPackage.getName().length()+1;
  103. }
  104. return this.getClass().getName().substring(packageLength+8); // 8 is the length of "Callback"
  105. }
  106. /** Get the name for this callback in lowercase */
  107. public final String getLowerName() {
  108. return this.getName().toLowerCase();
  109. }
  110. }