Project

General

Profile

Download (1.67 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 21 Apr 2015
20
 *
21
 */
22
public class BasicEventService {
23

    
24

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

    
27
    private final List<IBasicEventListener> listeners;
28

    
29
    private final List<BasicEvent> currentEvents;
30

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

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

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

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

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

    
69
}
(2-2/9)