001: /*
002: * EventMediator.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.executequery;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import java.lang.reflect.Method;
026: import java.util.ArrayList;
027: import java.util.EventObject;
028: import java.util.List;
029: import org.executequery.event.ConnectionEvent;
030: import org.executequery.event.ConnectionListener;
031: import org.executequery.event.KeywordEvent;
032: import org.executequery.event.KeywordListener;
033:
034: /* ----------------------------------------------------------
035: * CVS NOTE: Changes to the CVS repository prior to the
036: * release of version 3.0.0beta1 has meant a
037: * resetting of CVS revision numbers.
038: * ----------------------------------------------------------
039: */
040:
041: /**
042: * Event controller class.
043: * Global application events are registered and mediated through this class.
044: *
045: * @author Takis Diakoumis
046: * @version $Revision: 1.4 $
047: * @date $Date: 2006/05/14 06:56:53 $
048: */
049: public class EventMediator {
050:
051: public static final int CONNECTION_EVENT = 0;
052:
053: public static final int KEYWORD_EVENT = 1;
054:
055: private static List<ConnectionListener> connectionListeners;
056:
057: private static List<KeywordListener> keywordListeners;
058:
059: /*
060: public static void fireConnectionEvent(ConnectionEvent event, String method) {
061: try {
062: Object[] arguments = new Object[]{event};
063: Class[] argumentTypes = new Class[]{event.getClass()};
064: for (int i = 0, k = connectionListeners.size(); i < k; i++) {
065: ConnectionListener listener = connectionListeners.get(i);
066: Method _method = listener.getClass().
067: getMethod(method, argumentTypes);
068: _method.invoke(listener, arguments);
069: }
070: }
071: catch (NoSuchMethodException e) {
072: e.printStackTrace();
073: }
074: catch (IllegalAccessException e) {
075: e.printStackTrace();
076: }
077: catch (InvocationTargetException e) {
078: e.printStackTrace();
079: }
080: }
081: */
082:
083: public static void fireEvent(EventObject event, String method) {
084: try {
085: Object[] arguments = new Object[] { event };
086: Class[] argumentTypes = new Class[] { event.getClass() };
087: List list = null;
088:
089: // check the event type
090: if (event instanceof ConnectionEvent) {
091: list = connectionListeners;
092: } else if (event instanceof KeywordEvent) {
093: list = keywordListeners;
094: } else {
095: throw new IllegalArgumentException("Unknown event type");
096: }
097:
098: for (int i = 0, k = list.size(); i < k; i++) {
099: Object object = list.get(i);
100: Method _method = object.getClass().getMethod(method,
101: argumentTypes);
102: _method.invoke(object, arguments);
103: }
104: } catch (NoSuchMethodException e) {
105: e.printStackTrace();
106: } catch (IllegalAccessException e) {
107: e.printStackTrace();
108: } catch (InvocationTargetException e) {
109: e.printStackTrace();
110: }
111: }
112:
113: public static void registerListener(int type, Object object) {
114: switch (type) {
115: case CONNECTION_EVENT:
116: if (connectionListeners == null) {
117: connectionListeners = new ArrayList<ConnectionListener>();
118: }
119: connectionListeners.add((ConnectionListener) object);
120: break;
121:
122: case KEYWORD_EVENT:
123: if (keywordListeners == null) {
124: keywordListeners = new ArrayList<KeywordListener>();
125: }
126: keywordListeners.add((KeywordListener) object);
127: break;
128: }
129: }
130:
131: public static void deregisterListener(int type, Object object) {
132: switch (type) {
133: case CONNECTION_EVENT:
134: connectionListeners.remove((ConnectionListener) object);
135: break;
136: case KEYWORD_EVENT:
137: keywordListeners.remove((KeywordListener) object);
138: break;
139: }
140: }
141:
142: /** Prevent instantiation */
143: private EventMediator() {
144: }
145:
146: }
|