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.util;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Date;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
|
18
|
import com.vaadin.server.Page;
|
19
|
import com.vaadin.ui.Notification;
|
20
|
import com.vaadin.ui.Notification.Type;
|
21
|
import com.vaadin.ui.UI;
|
22
|
|
23
|
import eu.etaxonomy.cdm.vaadin.component.CdmProgressComponent;
|
24
|
import eu.etaxonomy.cdm.vaadin.session.CdmChangeEvent;
|
25
|
|
26
|
/**
|
27
|
* @author cmathew
|
28
|
* @date 14 Apr 2015
|
29
|
*
|
30
|
*/
|
31
|
public abstract class CdmVaadinOperation implements Runnable {
|
32
|
private static final Logger logger = Logger.getLogger(CdmVaadinOperation.class);
|
33
|
|
34
|
private int pollInterval = -1;
|
35
|
private CdmProgressComponent progressComponent;
|
36
|
|
37
|
private boolean opDone = false;
|
38
|
|
39
|
List<CdmChangeEvent> events = new ArrayList<CdmChangeEvent>();
|
40
|
|
41
|
private Date now = new java.util.Date();
|
42
|
|
43
|
private Exception exception;
|
44
|
|
45
|
|
46
|
|
47
|
public CdmVaadinOperation(int pollInterval, CdmProgressComponent progressComponent) {
|
48
|
this.pollInterval = pollInterval;
|
49
|
this.progressComponent = progressComponent;
|
50
|
|
51
|
UI.getCurrent().setPollInterval(pollInterval);
|
52
|
|
53
|
// comment out below for debugging
|
54
|
// logger.warn(new Timestamp(now.getTime()) + " : set polling interval to " + pollInterval);
|
55
|
// UI.getCurrent().addPollListener(new UIEvents.PollListener() {
|
56
|
// @Override
|
57
|
// public void poll(UIEvents.PollEvent event) {
|
58
|
// logger.warn( new Timestamp(now.getTime()) + " : polling");
|
59
|
// }
|
60
|
// });
|
61
|
}
|
62
|
|
63
|
public CdmVaadinOperation() {
|
64
|
|
65
|
}
|
66
|
|
67
|
|
68
|
|
69
|
public void setProgress(final String progressText) {
|
70
|
if(progressComponent == null) {
|
71
|
return;
|
72
|
}
|
73
|
if(isAsync()) {
|
74
|
UI.getCurrent().access(new Runnable() {
|
75
|
@Override
|
76
|
public void run() {
|
77
|
progressComponent.setProgress(progressText);
|
78
|
//logger.warn("set progress spinner");
|
79
|
}
|
80
|
});
|
81
|
} else {
|
82
|
progressComponent.setProgress(progressText);
|
83
|
}
|
84
|
|
85
|
|
86
|
}
|
87
|
|
88
|
public void setProgress(final String progressText, final float progress) {
|
89
|
if(progressComponent == null) {
|
90
|
return;
|
91
|
}
|
92
|
if(isAsync()) {
|
93
|
UI.getCurrent().access(new Runnable() {
|
94
|
@Override
|
95
|
public void run() {
|
96
|
progressComponent.setProgress(progressText, progress);
|
97
|
}
|
98
|
});
|
99
|
} else {
|
100
|
progressComponent.setProgress(progressText, progress);
|
101
|
}
|
102
|
}
|
103
|
|
104
|
public void endProgress() {
|
105
|
if(progressComponent == null) {
|
106
|
return;
|
107
|
}
|
108
|
if(isAsync()) {
|
109
|
UI.getCurrent().access(new Runnable() {
|
110
|
@Override
|
111
|
public void run() {
|
112
|
progressComponent.setVisible(false);
|
113
|
}
|
114
|
});
|
115
|
} else {
|
116
|
progressComponent.setVisible(false);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/* (non-Javadoc)
|
121
|
* @see java.lang.Runnable#run()
|
122
|
*/
|
123
|
@Override
|
124
|
public void run() {
|
125
|
try {
|
126
|
final boolean success = execute();
|
127
|
//logger.warn(new Timestamp(now.getTime()) + " : ran execute");
|
128
|
|
129
|
if(isAsync()) {
|
130
|
UI.getCurrent().access(new Runnable() {
|
131
|
@Override
|
132
|
public void run() {
|
133
|
try {
|
134
|
if(success) {
|
135
|
fireDelayedEvents();
|
136
|
}
|
137
|
if(exception != null) {
|
138
|
Notification notification = new Notification(exception.getMessage(), "", Type.WARNING_MESSAGE);
|
139
|
notification.show(Page.getCurrent());
|
140
|
exception = null;
|
141
|
}
|
142
|
postOpUIUpdate(success);
|
143
|
//logger.warn(new Timestamp(now.getTime()) + " : ran postOpUIUpdate ");
|
144
|
} finally {
|
145
|
UI.getCurrent().setPollInterval(-1);
|
146
|
opDone = true;
|
147
|
//logger.warn(new Timestamp(now.getTime()) + " : switched off pollling");
|
148
|
}
|
149
|
}
|
150
|
});
|
151
|
} else {
|
152
|
postOpUIUpdate(success);
|
153
|
}
|
154
|
} finally {
|
155
|
endProgress();
|
156
|
}
|
157
|
}
|
158
|
|
159
|
public abstract boolean execute();
|
160
|
|
161
|
public void postOpUIUpdate(boolean isOpSuccess) {}
|
162
|
|
163
|
public void fireEvent(CdmChangeEvent event, boolean async) {
|
164
|
CdmVaadinSessionUtilities.getCurrentCdmDataChangeService().fireChangeEvent(event, async);
|
165
|
}
|
166
|
|
167
|
public void registerDelayedEvent(CdmChangeEvent event) {
|
168
|
events.add(event);
|
169
|
}
|
170
|
|
171
|
private void fireDelayedEvents() {
|
172
|
for(CdmChangeEvent event : events) {
|
173
|
fireEvent(event, false);
|
174
|
}
|
175
|
events.clear();
|
176
|
}
|
177
|
|
178
|
public boolean isAsync() {
|
179
|
return pollInterval > 0;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* @return the exception
|
184
|
*/
|
185
|
public Exception getException() {
|
186
|
return exception;
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* @param exception the exception to set
|
191
|
*/
|
192
|
public void setException(Exception exception) {
|
193
|
this.exception = exception;
|
194
|
}
|
195
|
|
196
|
|
197
|
}
|