1
2
3
4
5 package nl.tudelft.goal.ut2004.visualizer.services;
6
7 import java.util.ArrayList;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Set;
11
12
13
14
15
16
17
18
19
20 public class AgentSelection<E> {
21 private E selected = null;
22 private Set<ISelectionListener<E>> listeners = new HashSet<ISelectionListener<E>>();
23
24 public synchronized void changeSelected(E newSelection) {
25 E oldSelected = selected;
26 selected = newSelection;
27
28 fireSelectionPropertyChange(oldSelected, newSelection);
29 }
30
31 public synchronized void addSelectionListener(ISelectionListener<E> listener) {
32 this.listeners.add(listener);
33 }
34
35 public synchronized void removeSelectionListener(ISelectionListener<E> listener) {
36 this.listeners.remove(listener);
37 }
38
39 private synchronized void fireSelectionPropertyChange(E oldSelection, E newSelection) {
40 List<ISelectionListener<E>> listenersArray = new ArrayList<ISelectionListener<E>>(listeners);
41
42 for (ISelectionListener<E> listener : listenersArray) {
43 listener.selectionChanged(oldSelection, newSelection);
44 }
45 }
46
47 }