Project

General

Profile

Download (1.69 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.vaadin.session;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import com.vaadin.ui.UI;
16

    
17
/**
18
 * @author cmathew
19
 * @date 9 Apr 2015
20
 *
21
 */
22
public class SelectionService {
23

    
24
    public final static String KEY = "key_selectionService";
25

    
26
    private final List<ISelectionListener> listeners;
27

    
28
    private final List<SelectionEvent> currentEvents;
29

    
30
    public SelectionService() {
31
        listeners = new ArrayList<ISelectionListener>();
32
        currentEvents = new ArrayList<SelectionEvent>();
33
    }
34

    
35
    public void register(ISelectionListener listener) {
36
        listeners.add(listener);
37
    }
38

    
39
    public void addEvent(SelectionEvent event) {
40
        currentEvents.add(event);
41
    }
42

    
43
    public void fireCurrentSelectionEvents(boolean async) {
44
        try {
45
            for(SelectionEvent event : currentEvents) {
46
                fireSelectionEvent(event,async);
47
            }
48
        } finally {
49
            currentEvents.clear();
50
        }
51
    }
52

    
53
    public void fireSelectionEvent(final SelectionEvent event, boolean async) {
54
        for(final ISelectionListener listener : listeners) {
55
            if(async) {
56
                UI.getCurrent().access(new Runnable() {
57
                    @Override
58
                    public void run() {
59
                        listener.onSelect(event);
60
                    }
61
                });
62
            } else {
63
                listener.onSelect(event);
64
            }
65
        }
66
    }
67
}
(9-9/9)