1 package siouxsie.desktop.events;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6
7 /**
8 * <p>Main bus for desktop scoped events.
9 * The bus receives events from differents sources
10 * in the desktop and forwards them to listeners.
11 * This centralized implementation of the observer
12 * pattern is used instead of using scattered
13 * event generators and listeners, which could
14 * lead to very messy event path and hard to debug
15 * code.</p>
16 * <p>This solution is inspired of the
17 * <a href="http://jedit.org">JEdit</a> text editor, where
18 * plugins register to an <a href="http://www.jedit.org/api/org/gjt/sp/jedit/EditBus.html">
19 * edit bus</a> to receive events.</p>
20 * @author Arnaud Cogoluegnes
21 * @version $Id$
22 */
23 public class DesktopBus {
24
25 private Collection<DesktopListener> listeners = new ArrayList<DesktopListener>();
26
27 private List<DesktopActorContribution> actors = new ArrayList<DesktopActorContribution>();
28
29 public void addDesktopListener(DesktopListener desktopListener) {
30 listeners.add(desktopListener);
31 }
32
33 public void removeDesktopListener(DesktopListener desktopListener) {
34 listeners.remove(desktopListener);
35 }
36
37 public void fireDesktopEvent(DesktopEvent event) {
38 for(DesktopListener listener : listeners) {
39 listener.handle(event);
40 }
41 }
42
43 /**
44 * Init the actors with the bus.
45 *
46 */
47 public void init() {
48 for(DesktopActorContribution contrib : actors) {
49 contrib.getObject().init(this);
50 }
51 }
52
53 public List<DesktopActorContribution> getActors() {
54 return actors;
55 }
56
57 public void setActors(List<DesktopActorContribution> actors) {
58 this.actors = actors;
59 }
60
61 }