View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Created by IntelliJ IDEA.
32   * User: lguminski
33   * Date: Jun 17, 2008
34   * Time: 10:53:01 AM
35   * To change this template use File | Settings | File Templates.
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  					//swallowed
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  }