123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- * SVN: $Id$
- */
-
- package uk.org.ownage.dmdirc.parser.callbacks;
- import uk.org.ownage.dmdirc.parser.*;
- import uk.org.ownage.dmdirc.parser.callbacks.interfaces.ICallbackInterface;
- import java.util.Hashtable;
- import java.util.Enumeration;
-
- /**
- * IRC Parser Callback Manager.
- * Manages adding/removing/calling callbacks.
- *
- * @author Shane Mc Cormack
- * @version $Id$
- */
- public class CallbackManager {
- /** Reference to the parser object that owns this CallbackManager*/
- IRCParser myParser = null;
-
- /**
- * Constructor to create a CallbackManager
- *
- * @param parser IRCParser that owns this callback manager.
- */
- public CallbackManager(IRCParser parser) {
- myParser = parser;
- // Add callbacks
- addCallbackType(new CallbackOnChannelAction(myParser, this));
- addCallbackType(new CallbackOnChannelCTCP(myParser, this));
- addCallbackType(new CallbackOnChannelCTCPReply(myParser, this));
- addCallbackType(new CallbackOnChannelGotNames(myParser, this));
- addCallbackType(new CallbackOnChannelJoin(myParser, this));
- addCallbackType(new CallbackOnChannelKick(myParser, this));
- addCallbackType(new CallbackOnChannelMessage(myParser, this));
- addCallbackType(new CallbackOnChannelModeChanged(myParser, this));
- addCallbackType(new CallbackOnChannelNickChanged(myParser, this));
- addCallbackType(new CallbackOnChannelNonUserModeChanged(myParser, this));
- addCallbackType(new CallbackOnChannelNotice(myParser, this));
- addCallbackType(new CallbackOnChannelPart(myParser, this));
- addCallbackType(new CallbackOnChannelQuit(myParser, this));
- addCallbackType(new CallbackOnChannelSelfJoin(myParser, this));
- addCallbackType(new CallbackOnChannelSingleModeChanged(myParser, this));
- addCallbackType(new CallbackOnChannelTopic(myParser, this));
- addCallbackType(new CallbackOnChannelUserModeChanged(myParser, this));
- addCallbackType(new CallbackOnDataIn(myParser, this));
- addCallbackType(new CallbackOnDataOut(myParser, this));
- addCallbackType(new CallbackOnDebugInfo(myParser, this));
- addCallbackType(new CallbackOnErrorInfo(myParser, this));
- addCallbackType(new CallbackOnMOTDEnd(myParser, this));
- addCallbackType(new CallbackOnMOTDLine(myParser, this));
- addCallbackType(new CallbackOnMOTDStart(myParser, this));
- addCallbackType(new CallbackOnNickChanged(myParser, this));
- addCallbackType(new CallbackOnNickInUse(myParser, this));
- addCallbackType(new CallbackOnNoticeAuth(myParser, this));
- addCallbackType(new CallbackOnPrivateAction(myParser, this));
- addCallbackType(new CallbackOnPrivateCTCP(myParser, this));
- addCallbackType(new CallbackOnPrivateCTCPReply(myParser, this));
- addCallbackType(new CallbackOnPrivateMessage(myParser, this));
- addCallbackType(new CallbackOnPrivateNotice(myParser, this));
- addCallbackType(new CallbackOnQuit(myParser, this));
- addCallbackType(new CallbackOnServerReady(myParser, this));
- addCallbackType(new CallbackOnSocketClosed(myParser, this));
- addCallbackType(new CallbackOnUnknownAction(myParser, this));
- addCallbackType(new CallbackOnUnknownCTCP(myParser, this));
- addCallbackType(new CallbackOnUnknownCTCPReply(myParser, this));
- addCallbackType(new CallbackOnUnknownMessage(myParser, this));
- addCallbackType(new CallbackOnUnknownNotice(myParser, this));
- addCallbackType(new CallbackOnUserModeChanged(myParser, this));
- }
-
- /** Empty clone method to prevent cloning to get more copies of the CallbackManager */
- public Object clone() throws CloneNotSupportedException {
- throw new CloneNotSupportedException();
- }
-
- /** Hashtable used to store the different types of callback known. */
- private Hashtable<String,CallbackObject> callbackHash = new Hashtable<String,CallbackObject>();
- /**
- * Add new callback type.
- *
- * @param callback CallbackObject subclass for the callback.
- * @return if adding succeeded or not.
- */
- public boolean addCallbackType(CallbackObject callback) {
- if (!callbackHash.containsKey(callback.getLowerName())) {
- callbackHash.put(callback.getLowerName(), callback);
- return true;
- }
- return false;
- }
-
- /**
- * Remove a callback type.
- *
- * @param callback CallbackObject subclass to remove.
- * @return if removal succeeded or not.
- */
- public boolean delCallbackType(CallbackObject callback) {
- if (callbackHash.containsKey(callback.getLowerName())) {
- callbackHash.remove(callback.getLowerName());
- return true;
- }
- return false;
- }
-
- /**
- * Get reference to callback object
- *
- * @param callbackName Name of callback object.
- */
- public CallbackObject getCallbackType(String callbackName) {
- if (callbackHash.containsKey(callbackName.toLowerCase())) {
- CallbackObject res = callbackHash.get(callbackName.toLowerCase());
- return res;
- }
- return null;
- }
-
- /**
- * Remove all callbacks.
- *
- * @param o instance of ICallbackInterface to remove.
- */
- public void delAllCallback(ICallbackInterface o) {
- CallbackObject cb = null;
- for (Enumeration e = callbackHash.keys(); e.hasMoreElements();) {
- cb = callbackHash.get(e.nextElement());
- if (cb != null) { cb.del(o); }
- }
- }
-
- /**
- * Add all callbacks.
- *
- * @param o instance of ICallbackInterface to add.
- */
- public void addAllCallback(ICallbackInterface o) {
- CallbackObject cb = null;
- for (Enumeration e = callbackHash.keys(); e.hasMoreElements();) {
- cb = callbackHash.get(e.nextElement());
- if (cb != null) { cb.add(o); }
- }
- }
-
- /**
- * Add a callback.
- * This method will throw a CallbackNotFound Exception if the callback does not exist.
- *
- * @param callbackName Name of callback object.
- * @param o instance of ICallbackInterface to add.
- * @throws CallbackNotFound If callback is not found.
- */
- public void addCallback(String callbackName, ICallbackInterface o) throws CallbackNotFound {
- CallbackObject cb = getCallbackType(callbackName);
- if (cb != null) { cb.add(o); }
- else { throw new CallbackNotFound("Callback '"+callbackName+"' could not be found."); }
- }
-
- /**
- * Add a callback with a specific target.
- * This method will throw a CallbackNotFound Exception if the callback does not exist.
- *
- * @param callbackName Name of callback object.
- * @param o instance of ICallbackInterface to add.
- * @param target Parameter to specify that a callback should only fire for specific things
- * @throws CallbackNotFound If callback is not found.
- */
- public void addCallback(String callbackName, ICallbackInterface o, String target) throws CallbackNotFound {
- CallbackObjectSpecific cb = (CallbackObjectSpecific)getCallbackType(callbackName);
- if (cb != null) { cb.add(o,target); }
- else { throw new CallbackNotFound("Callback '"+callbackName+"' could not be found."); }
- }
-
- /**
- * Add a callback without an exception.
- * This should be used if a callback is not essential for execution (ie the DebugOut callback)
- *
- * @param callbackName Name of callback object.
- * @param o instance of ICallbackInterface to add.
- * @return true/false if the callback was added or not.
- */
- public boolean addNonCriticalCallback(String callbackName, ICallbackInterface o) {
- try {
- addCallback(callbackName, o);
- return true;
- } catch (Exception e) { return false; }
- }
-
- /**
- * Add a callback with a specific target.
- * This should be used if a callback is not essential for execution
- *
- * @param callbackName Name of callback object.
- * @param o instance of ICallbackInterface to add.
- * @param target Parameter to specify that a callback should only fire for specific things
- * @throws CallbackNotFound If callback is not found.
- */
- public boolean addNonCriticalCallback(String callbackName, ICallbackInterface o, String target) {
- try {
- addCallback(callbackName, o, target);
- return true;
- } catch (Exception e) { return false; }
- }
-
-
- /**
- * Remove a callback
- *
- * @param callbackName Name of callback object.
- * @param o instance of ICallbackInterface to remove.
- */
- public void delCallback(String callbackName, ICallbackInterface o) {
- CallbackObject cb = getCallbackType(callbackName);
- if (cb != null) { cb.del(o); }
- }
-
- /**
- * Get SVN Version information.
- *
- * @return SVN Version String
- */
- public static String getSvnInfo () { return "$Id$"; }
- }
|