comparison gwt-client/src/main/java/org/dive4elements/river/client/client/ui/LoadingPanel.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-client/src/main/java/org/dive4elements/river/client/client/ui/LoadingPanel.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.client.ui;
2
3 import com.google.gwt.core.client.GWT;
4 import com.google.gwt.user.client.Timer;
5 import com.google.gwt.user.client.rpc.AsyncCallback;
6
7 import com.smartgwt.client.types.Alignment;
8 import com.smartgwt.client.types.Positioning;
9 import com.smartgwt.client.types.VerticalAlignment;
10 import com.smartgwt.client.widgets.Canvas;
11 import com.smartgwt.client.widgets.Img;
12 import com.smartgwt.client.widgets.Label;
13 import com.smartgwt.client.widgets.events.ClickEvent;
14 import com.smartgwt.client.widgets.events.ClickHandler;
15 import com.smartgwt.client.widgets.layout.HLayout;
16 import com.smartgwt.client.widgets.layout.VLayout;
17
18 import org.dive4elements.river.client.client.Config;
19 import org.dive4elements.river.client.client.FLYSConstants;
20 import org.dive4elements.river.client.client.event.HasStepBackHandlers;
21 import org.dive4elements.river.client.client.event.StepBackEvent;
22 import org.dive4elements.river.client.client.event.StepBackHandler;
23 import org.dive4elements.river.client.client.services.DescribeArtifactService;
24 import org.dive4elements.river.client.client.services.DescribeArtifactServiceAsync;
25 import org.dive4elements.river.client.shared.model.Artifact;
26 import org.dive4elements.river.client.shared.model.ArtifactDescription;
27 import org.dive4elements.river.client.shared.model.CalculationMessage;
28 import org.dive4elements.river.client.shared.model.DataList;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34 public class LoadingPanel extends Canvas implements HasStepBackHandlers {
35
36 private static final long serialVersionUID = -7806425431408987601L;
37
38 public static final int UPDATE_INTERVAL = 1000 * 3;
39
40 public static final DescribeArtifactServiceAsync describe =
41 GWT.create(DescribeArtifactService.class);
42
43 private FLYSConstants MSG = GWT.create(FLYSConstants.class);
44
45
46 protected List<StepBackHandler> handlers;
47
48 protected CollectionView parent;
49 protected Artifact artifact;
50
51 protected VLayout dialog;
52 protected HLayout cancelRow;
53 protected Label msg;
54 protected Label title;
55
56 protected int i;
57
58
59 public LoadingPanel(CollectionView parent, Artifact artifact) {
60 super();
61
62 this.handlers = new ArrayList<StepBackHandler>();
63 this.parent = parent;
64 this.artifact = artifact;
65 this.msg = new Label("");
66 this.title = new Label("");
67 this.dialog = createDialog();
68
69 this.i = 0;
70
71 initLayout();
72 startTimer();
73 }
74
75
76 private void initLayout() {
77 setWidth("100%");
78 setHeight("98%");
79 setBackgroundColor("#7f7f7f");
80 setOpacity(50);
81 setPosition(Positioning.RELATIVE);
82
83 parent.addChild(this);
84 parent.addChild(dialog);
85 dialog.moveTo(0, 20);
86 moveTo(0, 7);
87 }
88
89
90 public void setArtifact(Artifact artifact) {
91 this.artifact = artifact;
92 }
93
94
95 public Artifact getArtifact() {
96 return artifact;
97 }
98
99
100 @Override
101 public void addStepBackHandler(StepBackHandler handler) {
102 if (handler != null) {
103 handlers.add(handler);
104 }
105 }
106
107
108 /**
109 * This method is called after the user has clicked the button to cancel the
110 * current process.
111 *
112 * @param e The StepBackEvent.
113 */
114 protected void fireStepBackEvent(StepBackEvent e) {
115 for (StepBackHandler handler: handlers) {
116 handler.onStepBack(e);
117 }
118 }
119
120
121 protected VLayout createDialog() {
122
123 String baseUrl = GWT.getHostPageBaseURL();
124
125 title.setStyleName("loading-title");
126 title.setHeight(25);
127 title.setWidth100();
128
129 msg.setStyleName("loading-message");
130 msg.setValign(VerticalAlignment.TOP);
131 msg.setWidth100();
132 msg.setHeight(100);
133
134 Img img = new Img(baseUrl + MSG.loadingImg(), 25, 25);
135
136 Label cancelLabel = new Label(MSG.cancelCalculationLabel());
137 Img cancel = new Img(baseUrl + MSG.cancelCalculation(), 25, 25);
138 cancel.addClickHandler(new ClickHandler() {
139 @Override
140 public void onClick(ClickEvent e) {
141 cancel();
142 }
143 });
144
145 cancelRow = new HLayout();
146 cancelRow.setHeight(27);
147 cancelRow.setWidth100();
148 cancelRow.addMember(cancel);
149 cancelRow.addMember(cancelLabel);
150
151 VLayout box = new VLayout();
152 box.setStyleName("loading-box");
153 box.setAlign(VerticalAlignment.TOP);
154 box.setDefaultLayoutAlign(VerticalAlignment.TOP);
155 box.addMember(title);
156 box.addMember(msg);
157 box.addMember(cancelRow);
158 box.setMembersMargin(0);
159 box.setHeight(125);
160 box.setWidth(275);
161
162 dialog = new VLayout();
163 dialog.setAlign(Alignment.CENTER);
164 dialog.setDefaultLayoutAlign(Alignment.CENTER);
165 dialog.setMembersMargin(5);
166 dialog.setHeight100();
167 dialog.setWidth100();
168
169 dialog.addMember(img);
170 dialog.addMember(box);
171
172 return dialog;
173 }
174
175
176 public String getTargetState() {
177 ArtifactDescription desc = getArtifact().getArtifactDescription();
178 DataList[] oldData = desc.getOldData();
179
180 return oldData[oldData.length -1].getState();
181 }
182
183
184 private void startTimer() {
185 Timer t = new Timer() {
186 @Override
187 public void run() {
188 update();
189 }
190 };
191 t.schedule(UPDATE_INTERVAL);
192 }
193
194
195 protected void update() {
196 updateMessage();
197
198 final Config config = Config.getInstance();
199 final String locale = config.getLocale();
200
201 describe.describe(locale, artifact, new AsyncCallback<Artifact>() {
202 @Override
203 public void onFailure(Throwable t) {
204 GWT.log("Error while DESCRIBE artifact: " + t.getMessage());
205
206 startTimer();
207 }
208
209 @Override
210 public void onSuccess(Artifact artifact) {
211 GWT.log("Successfully DESCRIBE artifact.");
212
213 setArtifact(artifact);
214
215 if (artifact.isInBackground()) {
216 startTimer();
217 }
218 else {
219 finish();
220 }
221 }
222 });
223 }
224
225
226 protected void updateMessage() {
227 List<CalculationMessage> messages = artifact.getBackgroundMessages();
228 if (messages != null && messages.size() > 0) {
229 CalculationMessage calcMsg = messages.get(0);
230 title.setContents(getStepTitle(calcMsg));
231 msg.setContents(calcMsg.getMessage());
232 }
233 else {
234 title.setContents(MSG.calculationStarted());
235 }
236 }
237
238
239 protected String getStepTitle(CalculationMessage msg) {
240 return MSG.step() + " " + msg.getCurrentStep() + "/" + msg.getSteps();
241 }
242
243
244 private void cancel() {
245 fireStepBackEvent(new StepBackEvent(getTargetState()));
246 parent.removeChild(dialog);
247 parent.removeChild(this);
248 }
249
250
251 private void finish() {
252 parent.removeChild(dialog);
253 parent.removeChild(this);
254 parent.setArtifact(artifact);
255 }
256 }
257 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org