1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.idea.crucible.comments;
18
19 import com.atlassian.theplugin.commons.util.Logger;
20 import com.atlassian.theplugin.idea.crucible.events.CrucibleEvent;
21 import com.atlassian.theplugin.util.PluginUtil;
22 import com.intellij.openapi.project.Project;
23
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Queue;
27 import java.util.Set;
28 import java.util.concurrent.LinkedBlockingQueue;
29
30
31
32
33
34
35
36
37 public final class ReviewActionEventBroker {
38 private Set<CrucibleReviewActionListener> listeners =
39 new HashSet<CrucibleReviewActionListener>();
40 private Queue<CrucibleEvent> events = new LinkedBlockingQueue<CrucibleEvent>();
41 public static final Logger LOGGER = PluginUtil.getLogger();
42 private Project project;
43
44 public ReviewActionEventBroker(final Project project) {
45 this.project = project;
46 new Thread(new Runnable() {
47 public void run() {
48 try {
49 while (true) {
50 CrucibleEvent event = ((LinkedBlockingQueue<CrucibleEvent>) events).take();
51 event.run(ReviewActionEventBroker.this);
52 }
53 } catch (InterruptedException e) {
54
55 }
56 }
57 }, "atlassian-idea-plugin Crucible events processor"
58 ).start();
59 }
60
61 public void registerListener(CrucibleReviewActionListener listener) {
62 synchronized (listeners) {
63 listeners.add(listener);
64 }
65 }
66
67 public void unregisterListener(CrucibleReviewActionListener listener) {
68 synchronized (listeners) {
69 listeners.remove(listener);
70 }
71 }
72
73 public void trigger(CrucibleEvent event) {
74 events.add(event);
75 }
76
77 public Iterable<? extends CrucibleReviewActionListener> getListeners() {
78 synchronized (listeners) {
79 return Collections.unmodifiableSet(new HashSet<CrucibleReviewActionListener>(listeners));
80 }
81 }
82 }