001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/event/EventProcessor.java,v 1.1 2005/04/20 21:04:25 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.event;
019:
020: import java.util.HashMap;
021: import java.util.LinkedList;
022: import java.util.ListIterator;
023:
024: /**
025: * Handles distribution of events throughout the application
026: *
027: * This class is thread safe. Any changes made to the set of events
028: * during an event processing cycle will not appear in the event
029: * list until the next cycle
030: *
031: * @author paulby
032: * @version
033: */
034: public class EventProcessor extends Object {
035:
036: private HashMap eventStore;
037: private static EventProcessor eventProcessor = null;
038: private int busy = 0;
039: private LinkedList changeList = new LinkedList();
040:
041: /** Creates new EventProcessor */
042: public EventProcessor() {
043: eventStore = new HashMap();
044: }
045:
046: /*
047: public static EventProcessor processor() {
048: if (eventProcessor==null)
049: eventProcessor=new EventProcessor();
050:
051: return eventProcessor;
052: }
053: */
054:
055: public void postEvent(FlyEvent evt) {
056: LinkedList list = (LinkedList) eventStore.get(evt.getClass());
057:
058: if (list == null)
059: return;
060:
061: //System.out.println("Posting event "+evt);
062:
063: synchronized (eventStore) {
064: busy++;
065: }
066:
067: ListIterator listIterator = list.listIterator();
068: while (listIterator.hasNext()) {
069: FlyEventListener listener = (FlyEventListener) listIterator
070: .next();
071: //System.out.println("Calling Listener "+listener);
072: listener.processFlyEvent(evt);
073: }
074:
075: synchronized (eventStore) {
076: busy--;
077: if (busy == 0 && changeList.size() != 0)
078: processChangeList();
079: }
080: }
081:
082: private void processChangeList() {
083: //System.out.println("Processing change list ");
084:
085: ListIterator listIterator = changeList.listIterator();
086: while (listIterator.hasNext()) {
087: ChangeObject c = (ChangeObject) listIterator.next();
088: if (c.addObject)
089: c.list.add(c.listener);
090: else
091: c.list.remove(c.listener);
092: }
093: changeList.clear();
094:
095: }
096:
097: /**
098: * Add a listener which is called whenever events of the specified
099: * class are posted
100: */
101: public void addListener(FlyEventListener listener, Class evtClass) {
102: LinkedList list = (LinkedList) eventStore.get(evtClass);
103: if (list == null) {
104: list = new LinkedList();
105: eventStore.put(evtClass, list);
106: }
107:
108: synchronized (eventStore) {
109: //System.out.println("Adding "+listener+" "+busy);
110:
111: if (busy != 0)
112: changeList.add(new ChangeObject(true, listener, list));
113: else
114: list.add(listener);
115: }
116: }
117:
118: public void removeListener(FlyEventListener listener, Class evtClass) {
119: LinkedList list = (LinkedList) eventStore.get(evtClass);
120: if (list == null)
121: return;
122:
123: synchronized (eventStore) {
124: //System.out.println("Removing "+listener+" "+busy);
125: if (busy != 0)
126: changeList.add(new ChangeObject(false, listener, list));
127: else
128: list.remove(listener);
129: }
130: }
131:
132: class ChangeObject {
133: boolean addObject;
134: FlyEventListener listener;
135: LinkedList list;
136:
137: public ChangeObject(boolean add, FlyEventListener listener,
138: LinkedList list) {
139: this.addObject = add;
140: this.listener = listener;
141: this.list = list;
142: }
143: }
144: }
|