source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/com/gs3/testGXT/client/tree/MyRemoteDropTarget.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 15.9 KB
Line 
1package com.gs3.testGXT.client.tree;
2
3import java.util.LinkedList;
4import java.util.List;
5
6import com.google.gwt.core.client.GWT;
7import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
8import com.google.gwt.user.client.rpc.AsyncCallback;
9import com.gs3.testGXT.client.TestGXT;
10import com.gs3.testGXT.client.Keys.FileSystemKey;
11import com.gs3.testGXT.client.services.FileConflictCheckService;
12import com.gs3.testGXT.client.services.FileConflictCheckServiceAsync;
13import com.gs3.testGXT.client.services.FileCopyService;
14import com.gs3.testGXT.client.services.FileCopyServiceAsync;
15import com.gs3.testGXT.client.services.FileMoveService;
16import com.gs3.testGXT.client.services.FileMoveServiceAsync;
17import com.gs3.testGXT.client.services.ProgressBarHandshakeService;
18import com.gs3.testGXT.client.services.ProgressBarHandshakeServiceAsync;
19import com.gs3.testGXT.shared.SharedFileJobNode;
20import com.sencha.gxt.data.shared.TreeStore;
21import com.sencha.gxt.dnd.core.client.TreeDropTarget;
22import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
23import com.sencha.gxt.widget.core.client.button.TextButton;
24import com.sencha.gxt.widget.core.client.event.SelectEvent;
25import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
26import com.sencha.gxt.widget.core.client.tree.Tree;
27
28//NOTE: the non-merge version of this is saved in the first git commit
29public class MyRemoteDropTarget extends TreeDropTarget<FileSystemKey> {
30 //not sure I actually need to initialize these things here
31 private MyAlertMessageBox ctMsg= new MyAlertMessageBox("", "", null);
32 private MyErrorMessageBox erMsg= new MyErrorMessageBox("", "", null);
33
34 private FileConflictCheckServiceAsync fccs =null;
35 AsyncCallback<LinkedList<String[]>> fileConflictCheckCallback = null;
36
37 private ProgressBarHandshakeServiceAsync pbhs = null;
38 AsyncCallback<LinkedList<String[]>> handshakeCallback = null;
39
40 @SuppressWarnings("unused")
41 private FileMoveServiceAsync fms =null;
42 AsyncCallback<LinkedList<String[]>> fileMoveCallback = null;
43
44 private FileCopyServiceAsync fcs =null;
45 AsyncCallback<LinkedList<String[]>> fileCopyCallback = null;
46
47 private LinkedList<FileSystemKey> refreshTargets = new LinkedList<FileSystemKey>();
48 private boolean isHandShakeInProgress = false;
49
50 private void mergeWarn(List<String[]> items, String remoteTarget) {
51 //TODO: the string[] should probably be encapsulated into a type hierarchy at some point
52 LinkedList<String[]> conflicts = new LinkedList<String[]>();
53 LinkedList<String[]> resolutions = new LinkedList<String[]>();
54 LinkedList<String[]> errors = new LinkedList<String[]>();
55
56 for(String[] item : items) {
57 if(item[0].equals("Conflict")) {
58 conflicts.add(item);
59 }
60 else if (item[0].equals("No Conflict")) {
61 resolutions.add(item);
62 }
63 else if (item[0].equals("Error")) {
64 errors.add(item);
65 }
66 }
67
68 //check we actually have more than 0 conflicts
69 if(errors.size() > 0) {
70 erMsg = new MyErrorMessageBox("", "", null);
71 erMsg.setData(conflicts, resolutions, errors);
72 erMsg.setMessage(errors.get(0)[2], errors.get(0)[1], errors.size(), errors.get(0)[3]);
73 }
74 else if(conflicts.size() > 0) {
75 ctMsg = new MyAlertMessageBox("","", remoteTarget);
76 ctMsg.setData(conflicts, resolutions);
77 ctMsg.setMessage(conflicts.get(0)[2], conflicts.get(0)[1], conflicts.size(), conflicts.get(0)[3]);
78 }
79 }
80
81
82 private void handShake() {
83 isHandShakeInProgress = true;
84 handshakeCallback = new AsyncCallback<LinkedList<String[]>>() {
85
86 @Override
87 public void onFailure(Throwable caught) {
88 // TODO Auto-generated method stub
89 AlertMessageBox amb = new AlertMessageBox("Error", "Error: " + caught.getMessage()); //TODO: this is going to be messy - fix
90 amb.show();
91 }
92
93 @Override
94 public void onSuccess(LinkedList<String[]> result) {
95 // TODO Auto-generated method stub
96 final String[] res = result.get(0);
97 final double progress = Double.parseDouble(res[1]);
98
99 switch (res[0]) { //TODO: either use a class, or use shared constants
100 case "Completed" : {
101 isHandShakeInProgress = false;
102 TestGXT.setProgress(1, "Transfer Completed");
103 onComplete_refresh();
104 break;
105 }
106 case "Non-Completed" : {
107 final String[] token = result.get(1);
108 TestGXT.setProgress(progress, token[1]);
109 handShake();
110 break;
111 }
112 case "Cancelled" : {
113 TestGXT.setProgress(0, "Operation cancelled...");
114 break;
115 }
116 default : {
117 AlertMessageBox amb = new AlertMessageBox("Error", "Error: unrecognized server response - " + res[0]);
118 amb.show();
119 break;
120 }
121 }
122 }
123 };
124
125 pbhs.shakeHands(handshakeCallback);
126 }
127
128 private void onComplete_refresh() {
129 for(FileSystemKey refreshTarget : refreshTargets)
130 ((MyLocalFileSystemTree)this.getWidget()).refreshTree(refreshTarget);
131
132 refreshTargets.clear();
133 }
134
135 private LinkedList<SharedFileJobNode> convertToNode(LinkedList<String[]> buffer, int action) {
136 LinkedList<SharedFileJobNode> nodes = new LinkedList<SharedFileJobNode>();
137 for(String[] node : buffer) {
138 nodes.add(new SharedFileJobNode(action, node[1], node[2], node[3]));
139 }
140 return nodes;
141 }
142
143 private void return_call(final LinkedList<String[]> targets, int action) {
144
145 fileCopyCallback = new AsyncCallback<LinkedList<String[]>>() {
146
147 @Override
148 public void onFailure(Throwable caught) {
149 // TODO Auto-generated method stub
150 AlertMessageBox amb = new AlertMessageBox("Error", "Error: " + caught.getMessage()); //TODO: this is going to be messy - fix
151 amb.show();
152 }
153
154 @Override
155 public void onSuccess(LinkedList<String[]> result) {
156 if(!isHandShakeInProgress) //having multiple of these run at once would be pretty bad...
157 handShake();
158 }
159 };
160
161 if(!isHandShakeInProgress)
162 TestGXT.setIndeterminate(TestGXT.WAITING);
163
164 fcs.fileCopy(convertToNode(targets, action), fileCopyCallback);
165 }
166
167 public MyRemoteDropTarget(Tree<FileSystemKey, ?> tree) {
168 super(tree);
169
170 fccs = GWT.create(FileConflictCheckService.class);
171 fms = GWT.create(FileMoveService.class);
172 fcs = GWT.create(FileCopyService.class);
173 pbhs = GWT.create(ProgressBarHandshakeService.class);
174 }
175
176 @Override
177 protected void appendModel(FileSystemKey p, List<?> items, int index) {
178 if (items.size() == 0)
179 return;
180 if (items.get(0) instanceof TreeStore.TreeNode) {
181 @SuppressWarnings("unchecked")
182 List<TreeStore.TreeNode<FileSystemKey>> nodes = (List<TreeStore.TreeNode<FileSystemKey>>) items;
183
184 final String remoteTarget = p.toPath();
185
186 refreshTargets.add(p);
187
188 fileConflictCheckCallback = new AsyncCallback<LinkedList<String[]>>() {
189 public void onFailure(Throwable caught) {
190 AlertMessageBox amb = new AlertMessageBox("Error", "Error: " + caught.getMessage()); //TODO: this is going to be messy - fix
191 amb.show();
192 }
193
194 public void onSuccess(LinkedList<String[]> result) {
195 //TODO: everything
196 String[] status = result.poll();
197 switch (status[0]) {
198 case "Warning" :
199 if(!isHandShakeInProgress)
200 TestGXT.setIndeterminate(TestGXT.RESOLVING);
201 mergeWarn(result, remoteTarget);
202 break;
203 case "Success" :
204 return_call(result, SharedFileJobNode.ACTION_COPY_REPLACE);
205 break;
206 case "Error" :
207 if(!isHandShakeInProgress)
208 TestGXT.setIndeterminate(TestGXT.RESOLVING);
209 AlertMessageBox amb = new AlertMessageBox(result.get(0)[0], result.get(0)[1]);
210 amb.show();
211 break;
212 }
213 }
214 };
215
216 if(!isHandShakeInProgress)
217 TestGXT.setIndeterminate(TestGXT.REALIZING);
218 LinkedList<String> paths = new LinkedList<String>();
219 for(TreeStore.TreeNode<FileSystemKey> node : nodes) {
220 if(!node.getData().Droppable()) {
221 continue;
222 }
223 paths.add(node.getData().toPath());
224 }
225 if(paths.size() > 0) {
226 fccs.fileConflictCheck(paths, p.toPath(), fileConflictCheckCallback);
227 }
228 else {
229 //we do nothing here - we SHOULD have found a way to disable drag for root level elements by then
230 }
231 } else { // we should NEVER end up getting this unless we have items
232 // coming from another type of chart
233 @SuppressWarnings("unchecked")
234 List<FileSystemKey> models = (List<FileSystemKey>) items;
235 if (p == null) {
236 getWidget().getStore().insert(index, models);
237 } else {
238 getWidget().getStore().insert(p, index, models);
239 }
240 }
241 }
242
243 private class MyErrorMessageBox extends AlertMessageBox {
244 private TextButton btIgnore= new TextButton("Ignore");
245 private TextButton btIgnoreAllSimilarErrors = new TextButton("Ignore Similar");
246 private TextButton btIgnoreAllErrors = new TextButton("Ignore All");
247 private TextButton btCancel = new TextButton("Cancel");
248
249 private LinkedList<String[]> errors;
250 private LinkedList<String[]> conflicts;
251 private LinkedList<String[]> resolutions;
252
253 public MyErrorMessageBox(String headingText, String messageText, final String remoteTarget) {
254 super(headingText, messageText);
255
256 this.setPredefinedButtons();
257
258 //add all of the buttons to the handler
259 this.addButton(btIgnore);
260 this.addButton(btIgnoreAllSimilarErrors);
261 this.addButton(btIgnoreAllErrors);
262 this.addButton(btCancel);
263
264 btCancel.addSelectHandler(new SelectHandler() {
265 @Override public void onSelect(SelectEvent event) {
266 hideMessage();
267 conflicts = new LinkedList<String[]>();
268 resolutions = new LinkedList<String[]>();
269 errors = new LinkedList<String[]>();
270 }
271 });
272
273 btIgnore.addSelectHandler(new SelectHandler() {
274 @Override public void onSelect(SelectEvent event) {
275 errors.poll();
276 if(errors.size() == 0) {
277 hideMessage();
278 if(conflicts.size() > 0) {
279 List<String[]> output = resolutions;
280 output.addAll(conflicts);
281 mergeWarn(output, remoteTarget);
282 }
283 else if (resolutions.size() > 0) {
284 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
285 }
286 else; //in this case, no conflicts/resolutions -> no files at all to process
287 }
288 else {
289 setMessage(errors.get(0)[2], errors.get(0)[1], errors.size(), errors.get(0)[3]);
290 }
291 }
292 });
293
294 btIgnoreAllErrors.addSelectHandler(new SelectHandler() {
295 @Override public void onSelect(SelectEvent event) {
296 hideMessage();
297 errors = new LinkedList<String[]>();
298
299 if(conflicts.size() > 0) {
300 List<String[]> output = resolutions;
301 output.addAll(conflicts);
302 mergeWarn(output, remoteTarget);
303 }
304 else if (resolutions.size() > 0) {
305 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
306 }
307 else; //in this case, no conflicts/resolutions -> no files at all to process
308 }
309 });
310
311 //TODO: ignore all similar errors should be implemented (if it is to be used)
312 btIgnoreAllSimilarErrors.addSelectHandler(new SelectHandler() {
313 @Override public void onSelect(SelectEvent event) {
314 hideMessage();
315 errors = new LinkedList<String[]>();
316
317 if(conflicts.size() > 0) {
318 List<String[]> output = resolutions;
319 output.addAll(conflicts);
320 mergeWarn(output, remoteTarget);
321 }
322 else if (resolutions.size() > 0) {
323 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
324 }
325 else; //in this case, no conflicts/resolutions -> no files at all to process
326 }
327 });
328
329 btIgnoreAllSimilarErrors.setEnabled(false);
330
331 }
332
333 private void hideMessage() {
334 this.hide();
335 }
336
337 public void setData(LinkedList<String[]> conflicts, LinkedList<String[]> resolutions, LinkedList<String[]> errors) {
338 this.conflicts = conflicts;
339 this.resolutions = resolutions;
340 this.errors = errors;
341 }
342
343 public void setMessage(String key1, String key2, int len, String loc) {
344 String heading = "Error: " + len + " invalid item";
345 if(len > 1)
346 heading += "s";
347
348 this.setHeading(heading);
349
350 String message = "The file '" + key2 + "' cannot be created in the desination folder, \n '"
351 + key1 + "'.\nWould you like to skip processing this file?";
352 if(--len > 1) {
353 message += " (" + len + " more errors...)";
354 }
355 else if (len == 1) {
356 message += " (1 more error...)";
357 }
358
359 //TODO: find a more elegant solution for this
360 //btNoToAll.setEnabled(len > 0);
361 //btYesToAll.setEnabled(len > 0);
362
363 SafeHtmlBuilder builder = new SafeHtmlBuilder();
364 builder.appendEscapedLines(message);
365
366 this.setMessage(builder.toSafeHtml());
367 this.expand();
368 this.hide();
369 this.show();
370 this.setWidth(900);
371 this.setHeight(150);
372 this.show();
373 }
374 }
375
376 private class MyAlertMessageBox extends AlertMessageBox {
377 private TextButton btYes= new TextButton("Yes");
378 private TextButton btYesToAll = new TextButton("Yes to All");
379 private TextButton btNo = new TextButton("No");
380 private TextButton btCancel = new TextButton("Cancel");
381 private TextButton btNoToAll = new TextButton("No to All");
382
383 private LinkedList<String[]> conflicts;
384 private LinkedList<String[]> resolutions;
385
386 public MyAlertMessageBox(String headingText, String messageText, final String remoteTarget) {
387 super(headingText, messageText);
388
389 this.setPredefinedButtons();
390
391 //add all of the buttons to the handler
392 this.addButton(btYes);
393 this.addButton(btYesToAll);
394 this.addButton(btNo);
395 this.addButton(btNoToAll);
396 this.addButton(btCancel);
397 //deal with adding the click handlers
398 btCancel.addSelectHandler(new SelectHandler() {
399 @Override public void onSelect(SelectEvent event) {
400 hideMessage();
401 conflicts = new LinkedList<String[]>();
402 resolutions = new LinkedList<String[]>();
403 }
404 });
405
406 btNoToAll.addSelectHandler(new SelectHandler() {
407 @Override public void onSelect(SelectEvent event) {
408 hideMessage();
409 conflicts = new LinkedList<String[]>();
410
411 if(resolutions.size() > 0) {
412 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
413 }
414 }
415 });
416
417 btNo.addSelectHandler(new SelectHandler() {
418 @Override public void onSelect(SelectEvent event) {
419 conflicts.poll();
420 if(conflicts.size() == 0) {
421 hideMessage();
422 if(resolutions.size() > 0) {
423 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
424 }
425 }
426 else {
427 setMessage(conflicts.get(0)[2], conflicts.get(0)[1], conflicts.size(), conflicts.get(0)[3]);
428 }
429 }
430 });
431
432 btYes.addSelectHandler(new SelectHandler() {
433 @Override public void onSelect(SelectEvent event) {
434 resolutions.add(conflicts.poll());
435 if(conflicts.size() == 0) {
436 hideMessage();
437 if(resolutions.size() > 0) {
438 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
439 }
440 }
441 else {
442 setMessage(conflicts.get(0)[2], conflicts.get(0)[1], conflicts.size(), conflicts.get(0)[3]);
443 }
444 }
445 });
446
447 btYesToAll.addSelectHandler(new SelectHandler() {
448 @Override public void onSelect(SelectEvent event) {
449 hideMessage();
450 resolutions.addAll(conflicts);
451 conflicts = new LinkedList<String[]>();
452
453 if(resolutions.size() > 0) {
454 return_call(resolutions, SharedFileJobNode.ACTION_COPY_REPLACE);
455 }
456 }
457 });
458 }
459
460 private void hideMessage() {
461 this.hide();
462 }
463
464 public void setData(LinkedList<String[]> conflicts, LinkedList<String[]> resolutions) {
465 this.conflicts = conflicts;
466 this.resolutions = resolutions;
467 }
468
469 public void setMessage(String key1, String key2, int len, String loc) {
470 String heading = "Warning: " + len + " conflicting item";
471 if(len > 1)
472 heading += "s";
473
474 this.setHeading(heading);
475
476 String message = "The file '" + key2 + "' already exists in the destination folder, \n '"
477 + key1 + "'.\nWould you like to overwrite this file?";
478 if(--len > 1) {
479 message += " (" + len + " more conflicts...)";
480 }
481 else if (len == 1) {
482 message += " (1 more conflict...)";
483 }
484
485 //TODO: find a more elegant solution for this
486 //btNoToAll.setEnabled(len > 0);
487 //btYesToAll.setEnabled(len > 0);
488
489 SafeHtmlBuilder builder = new SafeHtmlBuilder();
490 builder.appendEscapedLines(message);
491
492 this.setMessage(builder.toSafeHtml());
493 this.expand();
494 this.hide();
495 this.show();
496 this.setWidth(900);
497 this.setHeight(150);
498 this.show();
499 }
500 }
501}
Note: See TracBrowser for help on using the repository browser.