Project

General

Profile

Download (5.38 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.util;
10

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

    
15
import com.vaadin.server.Page;
16
import com.vaadin.ui.Notification;
17
import com.vaadin.ui.Notification.Type;
18
import com.vaadin.ui.UI;
19

    
20
import eu.etaxonomy.cdm.vaadin.component.CdmProgressComponent;
21
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
22

    
23
/**
24
 * @author cmathew
25
 * @since 14 Apr 2015
26
 */
27
public abstract class CdmVaadinOperation implements Runnable {
28

    
29
    private int pollInterval = -1;
30
    private CdmProgressComponent progressComponent;
31

    
32
    private boolean opDone = false;
33

    
34
    List<CdmChangeEvent> events = new ArrayList<CdmChangeEvent>();
35

    
36
    private Date now = new java.util.Date();
37

    
38
    private Exception exception;
39

    
40

    
41

    
42
    public CdmVaadinOperation(int pollInterval, CdmProgressComponent progressComponent) {
43
        this.pollInterval = pollInterval;
44
        this.progressComponent = progressComponent;
45

    
46
        UI.getCurrent().setPollInterval(pollInterval);
47

    
48
        // comment out below for debugging
49
//        logger.warn(new Timestamp(now.getTime()) + " : set polling interval to " + pollInterval);
50
//        UI.getCurrent().addPollListener(new UIEvents.PollListener() {
51
//            @Override
52
//            public void poll(UIEvents.PollEvent event) {
53
//                logger.warn( new Timestamp(now.getTime()) + " : polling");
54
//            }
55
//        });
56
    }
57

    
58
    public CdmVaadinOperation() {
59

    
60
    }
61

    
62

    
63

    
64
    public void setProgress(final String progressText) {
65
        if(progressComponent == null) {
66
            return;
67
        }
68
        if(isAsync()) {
69
            UI.getCurrent().access(new Runnable() {
70
                @Override
71
                public void run() {
72
                    progressComponent.setProgress(progressText);
73
                    //logger.warn("set progress spinner");
74
                }
75
            });
76
        } else {
77
            progressComponent.setProgress(progressText);
78
        }
79

    
80

    
81
    }
82

    
83
    public void setProgress(final String progressText, final float progress) {
84
        if(progressComponent == null) {
85
            return;
86
        }
87
        if(isAsync()) {
88
            UI.getCurrent().access(new Runnable() {
89
                @Override
90
                public void run() {
91
                    progressComponent.setProgress(progressText, progress);
92
                }
93
            });
94
        } else {
95
            progressComponent.setProgress(progressText, progress);
96
        }
97
    }
98

    
99
    public void endProgress() {
100
        if(progressComponent == null) {
101
            return;
102
        }
103
        if(isAsync()) {
104
            UI.getCurrent().access(new Runnable() {
105
                @Override
106
                public void run() {
107
                    progressComponent.setVisible(false);
108
                }
109
            });
110
        } else {
111
            progressComponent.setVisible(false);
112
        }
113
    }
114

    
115
    /* (non-Javadoc)
116
     * @see java.lang.Runnable#run()
117
     */
118
    @Override
119
    public void run() {
120
        try {
121
            final boolean success = execute();
122
            //logger.warn(new Timestamp(now.getTime()) + " : ran execute");
123

    
124
            if(isAsync()) {
125
                UI.getCurrent().access(new Runnable() {
126
                    @Override
127
                    public void run() {
128
                        try {
129
                            if(success) {
130
                                fireDelayedEvents();
131
                            }
132
                            if(exception != null) {
133
                                Notification notification = new Notification(exception.getMessage(), "", Type.WARNING_MESSAGE);
134
                                notification.show(Page.getCurrent());
135
                                exception = null;
136
                            }
137
                            postOpUIUpdate(success);
138
                            //logger.warn(new Timestamp(now.getTime()) + " : ran postOpUIUpdate ");
139
                        } finally {
140
                            UI.getCurrent().setPollInterval(-1);
141
                            opDone = true;
142
                            //logger.warn(new Timestamp(now.getTime()) + " : switched off pollling");
143
                        }
144
                    }
145
                });
146
            } else {
147
                postOpUIUpdate(success);
148
            }
149
        } finally {
150
            endProgress();
151
        }
152
    }
153

    
154
    public abstract  boolean execute();
155

    
156
    public void postOpUIUpdate(boolean isOpSuccess) {}
157

    
158
    public void fireEvent(CdmChangeEvent event, boolean async) {
159
        CdmVaadinSessionUtilities.getCurrentCdmDataChangeService().fireChangeEvent(event, async);
160
    }
161

    
162
    public void registerDelayedEvent(CdmChangeEvent event) {
163
        events.add(event);
164
    }
165

    
166
    private void fireDelayedEvents() {
167
        for(CdmChangeEvent event : events) {
168
            fireEvent(event, false);
169
        }
170
        events.clear();
171
    }
172

    
173
    public boolean isAsync() {
174
        return pollInterval > 0;
175
    }
176

    
177
    /**
178
     * @return the exception
179
     */
180
    public Exception getException() {
181
        return exception;
182
    }
183

    
184
    /**
185
     * @param exception the exception to set
186
     */
187
    public void setException(Exception exception) {
188
        this.exception = exception;
189
    }
190

    
191

    
192
}
(6-6/17)