Project

General

Profile

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

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

    
14
import com.vaadin.ui.UI;
15

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

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

    
25
    private final List<ISelectionListener> listeners;
26

    
27
    private final List<SelectionEvent> currentEvents;
28

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

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

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

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

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