View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
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   * This is a selection set for one environment(server, timeline).
14   * <p>
15   * It is possible to listen for changes in selection and change selected entities.
16   * Nothing more for now.
17   *
18   * @author Honza
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  }