Project

General

Profile

Download (1.66 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
 * @since 21 Apr 2015
19
 *
20
 */
21
public class BasicEventService {
22

    
23

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

    
26
    private final List<IBasicEventListener> listeners;
27

    
28
    private final List<BasicEvent> currentEvents;
29

    
30
    public BasicEventService() {
31
        listeners = new ArrayList<IBasicEventListener>();
32
        currentEvents = new ArrayList<BasicEvent>();
33
    }
34

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

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

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

    
53
    public void fireBasicEvent(final BasicEvent event, boolean async) {
54
        for(final IBasicEventListener listener : listeners) {
55
            if(async) {
56
                UI.getCurrent().access(new Runnable() {
57
                    @Override
58
                    public void run() {
59
                        listener.onAction(event);
60
                    }
61
                });
62
            } else {
63
                listener.onAction(event);
64
            }
65
        }
66
    }
67

    
68
}
(2-2/10)