Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / CellTreeViewModel.java @ 63366925

History | View | Annotate | Download (30.6 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import static com.google.gwt.query.client.GQuery.$;
38
import gr.grnet.pithos.web.client.CellTreeView.Images;
39
import gr.grnet.pithos.web.client.CellTreeView.RefreshHandler;
40
import gr.grnet.pithos.web.client.rest.GetCommand;
41
import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
42
import gr.grnet.pithos.web.client.rest.RestException;
43
import gr.grnet.pithos.web.client.rest.resource.FileResource;
44
import gr.grnet.pithos.web.client.rest.resource.FolderResource;
45
import gr.grnet.pithos.web.client.rest.resource.MyFolderResource;
46
import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
47
import gr.grnet.pithos.web.client.rest.resource.OthersFolderResource;
48
import gr.grnet.pithos.web.client.rest.resource.OthersResource;
49
import gr.grnet.pithos.web.client.rest.resource.RestResource;
50
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
51
import gr.grnet.pithos.web.client.rest.resource.SharedFolderResource;
52
import gr.grnet.pithos.web.client.rest.resource.SharedResource;
53
import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
54
import gr.grnet.pithos.web.client.rest.resource.TrashResource;
55
import gwtquery.plugins.draggable.client.DragAndDropManager;
56
import gwtquery.plugins.draggable.client.DraggableOptions;
57
import gwtquery.plugins.draggable.client.StopDragException;
58
import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
59
import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
60
import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
61
import gwtquery.plugins.draggable.client.events.DragContext;
62
import gwtquery.plugins.droppable.client.DroppableOptions;
63
import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
64
import gwtquery.plugins.droppable.client.events.DragAndDropContext;
65
import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
66

    
67
import java.util.ArrayList;
68
import java.util.Arrays;
69
import java.util.HashMap;
70
import java.util.List;
71
import java.util.Map;
72

    
73
import com.google.gwt.cell.client.AbstractCell;
74
import com.google.gwt.cell.client.Cell;
75
import com.google.gwt.cell.client.ValueUpdater;
76
import com.google.gwt.core.client.GWT;
77
import com.google.gwt.dom.client.Style.Cursor;
78
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
79
import com.google.gwt.safehtml.shared.SafeHtml;
80
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
81
import com.google.gwt.user.client.DeferredCommand;
82
import com.google.gwt.user.client.ui.AbstractImagePrototype;
83
import com.google.gwt.view.client.AsyncDataProvider;
84
import com.google.gwt.view.client.HasData;
85
import com.google.gwt.view.client.ListDataProvider;
86
import com.google.gwt.view.client.ProvidesKey;
87
import com.google.gwt.view.client.SingleSelectionModel;
88
import com.google.gwt.view.client.TreeViewModel;
89

    
90

    
91

    
92
public class CellTreeViewModel implements TreeViewModel{
93
        
94
        private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
95
        private Map<String,FolderResource> folderCache=new HashMap<String, FolderResource>();
96
        final Images images;
97
        SingleSelectionModel<RestResource> selectionModel;
98
        Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
99
        Map<String, MyFolderDataProvider> sharedmap = new HashMap<String, MyFolderDataProvider>();
100
        Map<String, MyFolderDataProvider> othersmap = new HashMap<String, MyFolderDataProvider>();
101
        static interface Templates extends SafeHtmlTemplates {
102
            Templates INSTANCE = GWT.create(Templates.class);
103

    
104
            @Template(" <div id='dragHelper' class='{0}'></div>")
105
            SafeHtml outerHelper(String cssClassName);
106
          }
107

    
108
        void configureDragOperation(final DraggableOptions options) {
109

    
110
            // set a custom element as drag helper. The content of the helper will be
111
            // set when the drag will start
112
            options.setHelper($(Templates.INSTANCE.outerHelper(
113
                "drag").asString()));
114
            // opacity of the drag helper
115
            options.setOpacity((float) 0.9);
116
            // cursor during the drag operation
117
            options.setCursor(Cursor.MOVE);
118
            // the cell being greater than the helper, force the position of the
119
            // helper on the mouse cursor.
120
            options.setCursorAt(new CursorAt(10, 10, null, null));
121
            // append the helper to the body element
122
            options.setAppendTo("body");
123
            options.setCancel("select");
124
            // set the revert option
125
            options.setRevert(RevertOption.ON_INVALID_DROP);
126
            
127
            options.setOnBeforeDragStart(new DragFunction() {
128
                        
129
                        @Override
130
                        public void f(DragContext context) {
131
                                 RestResource value = context.getDraggableData();
132
                             if(!CellTreeViewModel.this.selectionModel.isSelected(value)){
133
                                       throw new StopDragException();
134
                              }
135
                             if(value instanceof TrashResource || value instanceof SharedResource || value instanceof OthersResource || value instanceof OtherUserResource){
136
                                       throw new StopDragException();
137
                              }
138
                                
139
                        }
140
                });
141
            // use a Function to fill the content of the helper
142
            // we could also add a DragStartEventHandler on the DragAndDropTreeCell and
143
            // DragAndDropCellList.
144
            
145
            options.setOnDragStart(new DragFunction() {
146
              public void f(DragContext context) {
147
                RestResourceWrapper memberInfo = context.getDraggableData();
148
                context.getHelper().setInnerHTML(memberInfo.getName());
149
              }
150
            });
151

    
152
          }
153

    
154
        /**
155
         * 
156
         */
157
        public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
158
                super();
159
                images=_images;
160
                this.selectionModel=selectionModel;
161
        }
162
        
163
        private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
164
                
165
                @Override
166
                public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
167
                        String id = null;
168
                        String html = null;
169
                        String name = null;
170
                        if(arg1 instanceof TrashFolderResource){
171
                                html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
172
                                FolderResource res = ((RestResourceWrapper)arg1).getResource();
173
                                name = res.getName();
174
                                id = res.getParentName() +"."+name;
175
                        }
176
                        else if(arg1 instanceof RestResourceWrapper){
177
                                FolderResource res = ((RestResourceWrapper)arg1).getResource();
178
                                if(res.isShared())
179
                                        html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
180
                                else if(res.getParentName()==null){
181
                                        html = AbstractImagePrototype.create(images.home()).getHTML();
182
                                }
183
                                else
184
                                        html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
185
                                name = res.getName();
186
                                if(res.getParentName() != null){                                        
187
                                        id = res.getParentName()+"."+name;
188
                                }else{                                        
189
                                        id = name;
190
                                }
191
                                
192
                        }
193
                        else if(arg1 instanceof TrashResource){
194
                                html = AbstractImagePrototype.create(images.trash()).getHTML();
195
                                name="Trash";
196
                                id = name;                                
197
                        }
198
                        
199
                        else if(arg1 instanceof SharedResource){
200
                                html = AbstractImagePrototype.create(images.myShared()).getHTML();
201
                                name = "My Shared";
202
                                id = name;
203
                        }
204
                        else if(arg1 instanceof OthersResource){
205
                                html = AbstractImagePrototype.create(images.othersShared()).getHTML();
206
                                name = "Other's Shared";
207
                                id = "others";                                
208
                        }
209
                        else if(arg1 instanceof OtherUserResource){
210
                                html = AbstractImagePrototype.create(images.permUser()).getHTML();
211
                                name = ((OtherUserResource)arg1).getName();
212
                                id = name;
213
                        }
214
                        arg2.appendHtmlConstant(html);
215
                        arg2.append(FileList.Templates.INSTANCE.spanWithIdAndClass(id, "papala", name));
216
                }
217
                
218
                public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, RestResource value, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<RestResource> valueUpdater) {
219
                        if(event.getType().equals("contextmenu")){
220
                                selectionModel.setSelected(value, true);
221
                                GSS.get().setCurrentSelection(value);
222
                                GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
223
                        }
224
                };
225
                
226
        };
227
        
228
        
229
        @Override
230
        public <T> NodeInfo<?> getNodeInfo(final T value) {
231
                
232
                if(value==null){
233
                        DragAndDropNodeInfo n = new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
234
                                    selectionModel, null);
235
                        configureFolderDrop(n);
236
                configureDragOperation(n.getDraggableOptions());
237
                        return n;
238
                }
239
                else if (value instanceof MyFolderResource) {
240
                // Second level.
241
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
242
                    ((MyFolderResource) value),MyFolderResource.class);
243
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
244
                    selectionModel, new ResourceValueUpdater());
245
                mymap.put(((MyFolderResource) value).getUri(), dataProvider);
246
                
247
                // permission cell are not draggable
248
                //n.setCellDroppableOnly();
249
                configureFolderDrop(n);
250
                configureDragOperation(n.getDraggableOptions());
251
                
252
                return n;
253
                }
254
                else if (value instanceof SharedResource) {
255
                // Second level.
256
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
257
                    ((SharedResource) value), SharedFolderResource.class);
258
                        sharedmap.put(((SharedResource) value).getUri(), dataProvider);
259
                        DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
260
                    selectionModel, new ResourceValueUpdater());
261
                         configureFolderDrop(n);
262
                        configureDragOperation(n.getDraggableOptions());
263
                        return n;
264
                }
265
                else if (value instanceof TrashResource) {
266
                // Second level.
267
                        ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
268
                        List<RestResource> r = new ArrayList<RestResource>();
269
                        for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
270
                                r.add(new TrashFolderResource(f));
271
                        }
272
                        trashProvider.setList(r);
273
                        DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
274
                    selectionModel, new ResourceValueUpdater());
275
                        configureFolderDrop(n);
276
                configureDragOperation(n.getDraggableOptions());
277
                        return n;
278
                }
279
                else if (value instanceof OthersResource) {
280
                // Second level.
281
                        OthersDataProvider dataProvider = new OthersDataProvider(
282
                    ((OthersResource) value), SharedFolderResource.class);
283
                DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
284
                    selectionModel, null);
285
                configureFolderDrop(n);
286
                configureDragOperation(n.getDraggableOptions());
287
                return n;
288
                }
289
                else if (value instanceof SharedFolderResource) {
290
                // Second level.
291
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
292
                    ((SharedFolderResource) value),SharedFolderResource.class);
293
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
294
                    selectionModel, new ResourceValueUpdater());
295
                sharedmap.put(((SharedFolderResource) value).getUri(), dataProvider);
296
                configureFolderDrop(n);
297
                configureDragOperation(n.getDraggableOptions());
298
                return n;
299
                }
300
                else if (value instanceof OthersFolderResource) {
301
                // Second level.
302
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
303
                    ((OthersFolderResource) value),OthersFolderResource.class);
304
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
305
                    selectionModel, new ResourceValueUpdater());
306
                //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
307
                othersmap.put(((OthersFolderResource) value).getUri(), dataProvider);
308
                configureFolderDrop(n);
309
                configureDragOperation(n.getDraggableOptions());
310
                return n;
311
                }
312
                else if (value instanceof OtherUserResource) {
313
                // Second level.
314
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
315
                    ((OtherUserResource) value),OthersFolderResource.class);
316
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
317
                    selectionModel, new ResourceValueUpdater());
318
                configureFolderDrop(n);
319
                configureDragOperation(n.getDraggableOptions());
320
                return n;
321
                }
322
                // TODO Auto-generated method stub
323
                return null;
324
        }
325
        
326
        private void configureFolderDrop(DragAndDropNodeInfo<RestResource> n){
327
                DroppableOptions options = n.getDroppableOptions();
328
        options.setDroppableHoverClass("droppableHover");
329
        // use a DroppableFunction here. We can also add a DropHandler in the tree
330
        // itself
331
        options.setOnOver(new DroppableFunction() {
332
                        
333
                        @Override
334
                        public void f(final DragAndDropContext context) {
335
                                if(context.getDroppableData()!=null && context.getDroppableData() instanceof RestResource){
336
                                        
337
                                        GSS.get().getTreeView().getUtils().openNodeContainingResource((RestResource) context.getDroppableData(), new RefreshHandler() {
338
                                                
339
                                                @Override
340
                                                public void onRefresh() {
341
                                                        
342
                                                        DragAndDropManager.getInstance().update(context);//initialize(context, GQueryUi.Event.create(com.google.gwt.user.client.Event.getCurrentEvent()));
343
                                                        
344
                                                }
345
                                        });
346
                                        
347
                                }
348
                        }
349
                });
350
        options.setOnDrop(new DroppableFunction() {
351

    
352
          public void f(DragAndDropContext context) {
353
                  
354
                  DnDFolderPopupMenu popup ;
355
                  if(context.getDraggableData() instanceof FileResource){
356
//                          if(context.getDroppableData() instanceof RestResourceWrapper)
357
//                                  popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
358
 //                         else
359
 //                                 popup = new DnDFolderPopupMenu(images, null, Arrays.asList(context.getDraggableData()));
360
                  }
361
                  
362
                  else{
363
//                          if(context.getDroppableData() instanceof RestResourceWrapper)
364
//                                  popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), context.getDraggableData());
365
//                          else
366
//                                  popup = new DnDFolderPopupMenu(images, null, context.getDraggableData());
367
                  }
368
                  
369
                  int left = context.getDroppable().getAbsoluteLeft() + 40;
370
              int top = context.getDroppable().getAbsoluteTop() + 20;
371
//              popup.setPopupPosition(left, top);
372
                 
373
 //                 popup.show();
374
          }
375
        });
376
        }
377

    
378
        @Override
379
        public boolean isLeaf(Object value) {
380
                if(value instanceof RestResourceWrapper)
381
                        return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
382
                else if(value instanceof TrashResource)
383
                        return ((TrashResource)value).getFolders().size()==0;
384
                else if(value instanceof SharedResource)
385
                        return ((SharedResource)value).getFolders().size()==0;
386
                else if(value instanceof OthersResource)
387
                        return ((OthersResource)value).getOtherUsers().size()==0;
388
                else if(value instanceof OtherUserResource)
389
                        return ((OtherUserResource)value).getFolders().size()==0;
390
                return false;
391
        }
392
        
393
        /**
394
         * Retrieve the selectionModel.
395
         *
396
         * @return the selectionModel
397
         */
398
        public SingleSelectionModel<RestResource> getSelectionModel() {
399
                return selectionModel;
400
        }
401
        static interface ClearSelection{
402
                public void setClearSelection(boolean clearSelection);
403
        }
404
        class ResourceValueUpdater implements  ValueUpdater<RestResource>,ClearSelection{
405
                boolean clearSelection=true;
406
                
407
                
408
                /**
409
                 * Modify the clearSelection.
410
                 *
411
                 * @param clearSelection the clearSelection to set
412
                 */
413
                public void setClearSelection(boolean clearSelection) {
414
                        this.clearSelection = clearSelection;
415
                }
416
                @Override
417
                public void update(final RestResource value) {
418
                        if(value instanceof MyFolderResource){
419
                                GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
420

    
421
                                        @Override
422
                                        public void onComplete() {
423
                                                FolderResource rootResource = getResult();
424
                                                //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
425
                                                ((MyFolderResource)value).setResource(rootResource);
426
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
427
                                                        selectionModel.setSelected(value, true);
428
                                                GSS.get().onResourceUpdate(value,clearSelection);
429
                                                
430
                                        }
431
        
432
                                        @Override
433
                                        public void onError(Throwable t) {
434
                                                GWT.log("Error fetching root folder", t);
435
                                                GSS.get().displayError("Unable to fetch root folder");
436
                                        }
437
        
438
                                };
439
                                DeferredCommand.addCommand(gf);
440
                        }
441
                        else if(value instanceof TrashResource){
442
                                DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
443
                                        @Override
444
                                        public void onComplete() {
445
                                                //trash = getResult();
446
                                                ((TrashResource)value).setFolders(getResult().getFolders());
447
                                                ((TrashResource)value).setFiles(getResult().getFiles());
448
                                                for(RestResource r : getRootNodes().getList()){
449
                                                        if(r instanceof TrashResource)
450
                                                                getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
451
                                                }
452
                                                GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
453
                                                //GSS.get().showFileList(true);
454
                                                GSS.get().onResourceUpdate(value,clearSelection);
455
                                        }
456

    
457
                                        @Override
458
                                        public void onError(Throwable t) {
459
                                                if(t instanceof RestException){
460
                                                        int statusCode = ((RestException)t).getHttpStatusCode();
461
                                                        // On IE status code 1223 may be returned instead of 204.
462
                                                        if(statusCode == 204 || statusCode == 1223){
463
                                                                ((TrashResource)value).setFolders(new ArrayList<FolderResource>());
464
                                                                ((TrashResource)value).setFiles(new ArrayList<FileResource>());
465
                                                }
466
                                                else{
467
                                                        GWT.log("", t);
468
                                                        GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
469
                                                        //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
470
                                                }
471
                                        }
472
                                }
473
                                });
474
                        }
475
                        else if(value instanceof OthersFolderResource){
476
                                GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
477

    
478
                                        @Override
479
                                        public void onComplete() {
480
                                                FolderResource rootResource = getResult();
481
                                                //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
482
                                                ((OthersFolderResource)value).setResource(rootResource);
483
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
484
                                                        selectionModel.setSelected(value, true);
485
                                                GSS.get().onResourceUpdate(value,clearSelection);
486
                                                
487
                                        }
488
        
489
                                        @Override
490
                                        public void onError(Throwable t) {
491
                                                GWT.log("Error fetching root folder", t);
492
                                                GSS.get().displayError("Unable to fetch root folder");
493
                                        }
494
        
495
                                };
496
                                DeferredCommand.addCommand(gf);
497
                        }
498
                        else if(value instanceof SharedFolderResource){
499
                                GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
500

    
501
                                        @Override
502
                                        public void onComplete() {
503
                                                FolderResource rootResource = getResult();
504
                                                //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
505
                                                ((SharedFolderResource)value).setResource(rootResource);
506
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
507
                                                        selectionModel.setSelected(value, true);
508
                                                GSS.get().onResourceUpdate(value,clearSelection);
509
                                                
510
                                        }
511
        
512
                                        @Override
513
                                        public void onError(Throwable t) {
514
                                                GWT.log("Error fetching root folder", t);
515
                                                GSS.get().displayError("Unable to fetch root folder");
516
                                        }
517
        
518
                                };
519
                                DeferredCommand.addCommand(gf);
520
                        }
521
                        else if(value instanceof SharedResource){
522
                                GetCommand<SharedResource> gf = new GetCommand<SharedResource>(SharedResource.class, value.getUri(), null) {
523

    
524
                                        @Override
525
                                        public void onComplete() {
526
                                                SharedResource rootResource = getResult();
527
                                                ((SharedResource)value).setFolders(getResult().getFolders());
528
                                                ((SharedResource)value).setFiles(getResult().getFiles());
529
                                                
530
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
531
                                                        selectionModel.setSelected(value, true);
532
                                                GSS.get().onResourceUpdate(value,clearSelection);
533
                                                
534
                                        }
535
        
536
                                        @Override
537
                                        public void onError(Throwable t) {
538
                                                GWT.log("Error fetching root folder", t);
539
                                                GSS.get().displayError("Unable to fetch root folder");
540
                                        }
541
        
542
                                };
543
                                DeferredCommand.addCommand(gf);
544
                        }
545
                        else if(value instanceof OtherUserResource){
546
                                GetCommand<OtherUserResource> gf = new GetCommand<OtherUserResource>(OtherUserResource.class, value.getUri(), null) {
547

    
548
                                        @Override
549
                                        public void onComplete() {
550
                                                OtherUserResource rootResource = getResult();
551
                                                ((OtherUserResource)value).setFolders(getResult().getFolders());
552
                                                ((OtherUserResource)value).setFiles(getResult().getFiles());
553
                                                
554
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
555
                                                        selectionModel.setSelected(value, true);
556
                                                GSS.get().onResourceUpdate(value,clearSelection);
557
                                                
558
                                        }
559
        
560
                                        @Override
561
                                        public void onError(Throwable t) {
562
                                                GWT.log("Error fetching root folder", t);
563
                                                GSS.get().displayError("Unable to fetch root folder");
564
                                        }
565
        
566
                                };
567
                                DeferredCommand.addCommand(gf);
568
                        }
569
                        
570
                }
571
                
572
        }
573
        class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
574
                private RestResource restResource;
575
                private Class resourceClass;
576
                  public MyFolderDataProvider(RestResource department, Class resourceClass) {
577
                    super(new ProvidesKey<RestResource>() {
578

    
579
                                @Override
580
                                public Object getKey(RestResource item) {
581
                                        return item.getUri();
582
                                }});
583
                    this.restResource = department;
584
                    this.resourceClass=resourceClass;
585
                  }
586

    
587
                  @Override
588
                  protected void onRangeChanged(final HasData<RestResource> view) {
589
                        refresh(null);
590
                  }
591
                  
592
                /**
593
                 * Retrieve the restResource.
594
                 *
595
                 * @return the restResource
596
                 */
597
                public RestResource getRestResource() {
598
                        return restResource;
599
                }
600
                
601
                
602
                /**
603
                 * Modify the restResource.
604
                 *
605
                 * @param restResource the restResource to set
606
                 */
607
                public void setRestResource(RestResource restResource) {
608
                        this.restResource = restResource;
609
                }
610
                List<RestResource> res =null;
611
                  public void refresh(final RefreshHandler refresh){
612
                          FolderResource cache = null;
613
                          if(restResource instanceof RestResourceWrapper && !((RestResourceWrapper)restResource).getResource().isNeedsExpanding())
614
                                  cache = ((RestResourceWrapper)restResource).getResource();
615
                          GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(),cache ) {
616

    
617
                                        @Override
618
                                        public void onComplete() {
619
                                                if(restResource instanceof RestResourceWrapper){
620
                                                        ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
621
                                                        ((RestResourceWrapper)restResource).getResource().setNeedsExpanding(false);
622
                                                }
623
                                                if(usedCachedVersion()&&res!=null){
624
                                                        
625
                                                                updateRowCount(res.size(), true);
626
                                                                updateRowData(0,res);
627
                                                        return;
628
                                                }
629
                                                String[] folderPaths = null;
630
                                                if(resourceClass.equals(MyFolderResource.class))
631
                                                        folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
632
                                                else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
633
                                                        folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
634
                                                else if(resourceClass.equals(SharedFolderResource.class)){
635
                                                        folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
636
                                                        GWT.log("------------>"+folderPaths);
637
                                                }
638
                                                else if(resourceClass.equals(TrashFolderResource.class))
639
                                                        folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
640
                                                else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
641
                                                        folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
642
                                                else if(resourceClass.equals(OthersFolderResource.class))
643
                                                        folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
644
                                                MultipleGetCommand.Cached[] cached = null;
645
                                                if(restResource instanceof RestResourceWrapper)
646
                                                        cached=((RestResourceWrapper)restResource).getResource().getCache();
647
                                                MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
648
                                                                        folderPaths, cached) {
649

    
650
                                                        @Override
651
                                                        public void onComplete() {
652
                                                                res = new ArrayList<RestResource>();
653
                                                                for(FolderResource r : getResult()){
654
                                                                        if(r.isDeleted()){
655
                                                                                
656
                                                                        }
657
                                                                        else if(resourceClass.equals(MyFolderResource.class))
658
                                                                                res.add(new MyFolderResource(r));
659
                                                                        else if(resourceClass.equals(SharedFolderResource.class)){
660
                                                                                res.add(new SharedFolderResource(r));
661
                                                                        }
662
                                                                        else if(resourceClass.equals(TrashFolderResource.class))
663
                                                                                res.add(new TrashFolderResource(r));
664
                                                                        else if(resourceClass.equals(OthersFolderResource.class))
665
                                                                                res.add(new OthersFolderResource(r));
666
                                                                }
667
                                                                if(restResource instanceof RestResourceWrapper)
668
                                                                        ((RestResourceWrapper)restResource).getResource().setFolders(getResult());
669
                                                                updateRowCount(res.size(), true);
670
                                                                updateRowData(0,res);
671
                                                                if(refresh!=null)
672
                                                                        refresh.onRefresh();
673
                                                        }
674

    
675
                                                        @Override
676
                                                        public void onError(Throwable t) {
677
                                                                GSS.get().displayError("Unable to fetch subfolders");
678
                                                                GWT.log("Unable to fetch subfolders", t);
679
                                                        }
680

    
681
                                                        @Override
682
                                                        public void onError(String p, Throwable throwable) {
683
                                                                GWT.log("Path:"+p, throwable);
684
                                                        }
685

    
686
                                                };
687
                                                DeferredCommand.addCommand(gf2);
688
                                                
689
                                        }
690

    
691
                                        @Override
692
                                        public void onError(Throwable t) {
693
                                                
694
                                                GWT.log("Error fetching root folder", t);
695
                                                GSS.get().displayError("Unable to fetch root folder");
696
                                        }
697

    
698
                                };
699
                                DeferredCommand.addCommand(gf);
700
                  }                  
701
        }
702
        
703
        
704
        class OthersDataProvider extends AsyncDataProvider<RestResource>{
705
                private RestResource restResource;
706
                private Class resourceClass;
707
                  public OthersDataProvider(RestResource department, Class resourceClass) {
708
                    super(new ProvidesKey<RestResource>() {
709

    
710
                                @Override
711
                                public Object getKey(RestResource item) {
712
                                        return item.getUri();
713
                                }});
714
                    this.restResource = department;
715
                    this.resourceClass=resourceClass;
716
                  }
717

    
718
                  @Override
719
                  protected void onRangeChanged(final HasData<RestResource> view) {
720
                        refresh(null);
721
                  }
722
                  
723
                /**
724
                 * Retrieve the restResource.
725
                 *
726
                 * @return the restResource
727
                 */
728
                public RestResource getRestResource() {
729
                        return restResource;
730
                }
731
                
732
                
733
                /**
734
                 * Modify the restResource.
735
                 *
736
                 * @param restResource the restResource to set
737
                 */
738
                public void setRestResource(RestResource restResource) {
739
                        this.restResource = restResource;
740
                }
741
                
742
                  public void refresh(final RefreshHandler refresh){
743
                          GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
744
                          restResource.getUri(), null) {
745

    
746
                                  @Override
747
                                  public void onComplete() {
748
                                          final OthersResource others = getResult();
749
                          MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
750
                                                  others.getOthers().toArray(new String[] {}), null) {
751

    
752
                                  @Override
753
                                  public void onComplete() {
754
                                          List<OtherUserResource> res = getResult();
755
                                          updateRowCount(res.size(), true);
756
                                          List<RestResource> r = new ArrayList<RestResource>();
757
                                          r.addAll(res);
758
                                                                            updateRowData(0,r);
759
                                  }
760

    
761
                                  @Override
762
                                  public void onError(Throwable t) {
763
                                          GWT.log("Error fetching Others Root folder", t);
764
                                          GSS.get().displayError("Unable to fetch Others Root folder");
765
                                  }
766

    
767
                                  @Override
768
                                  public void onError(String p, Throwable throwable) {
769
                                          GWT.log("Path:"+p, throwable);
770
                                  }
771
                          };
772
                          DeferredCommand.addCommand(gogo);
773
                                  }
774
                        
775
                                  @Override
776
                                  public void onError(Throwable t) {
777
                                          GWT.log("Error fetching Others Root folder", t);
778
                                          GSS.get().displayError("Unable to fetch Others Root folder");
779
                                  }
780
                          };
781
                          DeferredCommand.addCommand(go);
782
                  }                  
783
        }
784

    
785

    
786
        
787
        /**
788
         * Retrieve the rootNodes.
789
         *
790
         * @return the rootNodes
791
         */
792
        public ListDataProvider<RestResource> getRootNodes() {
793
                return rootNodes;
794
        }
795

    
796
        
797
        /**
798
         * Retrieve the mymap.
799
         *
800
         * @return the mymap
801
         */
802
        public Map<String, MyFolderDataProvider> getMymap() {
803
                return mymap;
804
        }
805

    
806
        
807
        /**
808
         * Retrieve the sharedmap.
809
         *
810
         * @return the sharedmap
811
         */
812
        public Map<String, MyFolderDataProvider> getSharedmap() {
813
                return sharedmap;
814
        }
815

    
816
        
817
        /**
818
         * Retrieve the othersmap.
819
         *
820
         * @return the othersmap
821
         */
822
        public Map<String, MyFolderDataProvider> getOthersmap() {
823
                return othersmap;
824
        }
825
        
826
        
827
        
828
        
829
        
830
        
831
}