Updated copyright notice
[pithos] / web_client / src / gr / grnet / pithos / web / client / CellTreeViewModel.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client;
5
6 import static com.google.gwt.query.client.GQuery.$;
7 import gr.grnet.pithos.web.client.CellTreeView.Images;
8 import gr.grnet.pithos.web.client.CellTreeView.RefreshHandler;
9 import gr.grnet.pithos.web.client.rest.GetCommand;
10 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
11 import gr.grnet.pithos.web.client.rest.RestException;
12 import gr.grnet.pithos.web.client.rest.resource.FileResource;
13 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
14 import gr.grnet.pithos.web.client.rest.resource.MyFolderResource;
15 import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
16 import gr.grnet.pithos.web.client.rest.resource.OthersFolderResource;
17 import gr.grnet.pithos.web.client.rest.resource.OthersResource;
18 import gr.grnet.pithos.web.client.rest.resource.RestResource;
19 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
20 import gr.grnet.pithos.web.client.rest.resource.SharedFolderResource;
21 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
22 import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
23 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
24 import gwtquery.plugins.draggable.client.DragAndDropManager;
25 import gwtquery.plugins.draggable.client.DraggableOptions;
26 import gwtquery.plugins.draggable.client.StopDragException;
27 import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
28 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
29 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
30 import gwtquery.plugins.draggable.client.events.DragContext;
31 import gwtquery.plugins.droppable.client.DroppableOptions;
32 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
33 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
34 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
35
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 import com.google.gwt.cell.client.AbstractCell;
43 import com.google.gwt.cell.client.Cell;
44 import com.google.gwt.cell.client.ValueUpdater;
45 import com.google.gwt.core.client.GWT;
46 import com.google.gwt.dom.client.Style.Cursor;
47 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
48 import com.google.gwt.safehtml.shared.SafeHtml;
49 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
50 import com.google.gwt.user.client.DeferredCommand;
51 import com.google.gwt.user.client.ui.AbstractImagePrototype;
52 import com.google.gwt.view.client.AsyncDataProvider;
53 import com.google.gwt.view.client.HasData;
54 import com.google.gwt.view.client.ListDataProvider;
55 import com.google.gwt.view.client.ProvidesKey;
56 import com.google.gwt.view.client.SingleSelectionModel;
57 import com.google.gwt.view.client.TreeViewModel;
58
59
60
61 public class CellTreeViewModel implements TreeViewModel{
62         
63         private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
64         private Map<String,FolderResource> folderCache=new HashMap<String, FolderResource>();
65         final Images images;
66         SingleSelectionModel<RestResource> selectionModel;
67         Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
68         Map<String, MyFolderDataProvider> sharedmap = new HashMap<String, MyFolderDataProvider>();
69         Map<String, MyFolderDataProvider> othersmap = new HashMap<String, MyFolderDataProvider>();
70         static interface Templates extends SafeHtmlTemplates {
71             Templates INSTANCE = GWT.create(Templates.class);
72
73             @Template(" <div id='dragHelper' class='{0}'></div>")
74             SafeHtml outerHelper(String cssClassName);
75           }
76
77         void configureDragOperation(final DraggableOptions options) {
78
79             // set a custom element as drag helper. The content of the helper will be
80             // set when the drag will start
81             options.setHelper($(Templates.INSTANCE.outerHelper(
82                 "drag").asString()));
83             // opacity of the drag helper
84             options.setOpacity((float) 0.9);
85             // cursor during the drag operation
86             options.setCursor(Cursor.MOVE);
87             // the cell being greater than the helper, force the position of the
88             // helper on the mouse cursor.
89             options.setCursorAt(new CursorAt(10, 10, null, null));
90             // append the helper to the body element
91             options.setAppendTo("body");
92             options.setCancel("select");
93             // set the revert option
94             options.setRevert(RevertOption.ON_INVALID_DROP);
95             
96             options.setOnBeforeDragStart(new DragFunction() {
97                         
98                         @Override
99                         public void f(DragContext context) {
100                                  RestResource value = context.getDraggableData();
101                              if(!CellTreeViewModel.this.selectionModel.isSelected(value)){
102                                 throw new StopDragException();
103                               }
104                              if(value instanceof TrashResource || value instanceof SharedResource || value instanceof OthersResource || value instanceof OtherUserResource){
105                                 throw new StopDragException();
106                               }
107                                 
108                         }
109                 });
110             // use a Function to fill the content of the helper
111             // we could also add a DragStartEventHandler on the DragAndDropTreeCell and
112             // DragAndDropCellList.
113             
114             options.setOnDragStart(new DragFunction() {
115               public void f(DragContext context) {
116                 RestResourceWrapper memberInfo = context.getDraggableData();
117                 context.getHelper().setInnerHTML(memberInfo.getName());
118               }
119             });
120
121           }
122
123         /**
124          * 
125          */
126         public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
127                 super();
128                 images=_images;
129                 this.selectionModel=selectionModel;
130         }
131         
132         private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
133                 
134                 @Override
135                 public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
136                         String id = null;
137                         String html = null;
138                         String name = null;
139                         if(arg1 instanceof TrashFolderResource){
140                                 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
141                                 FolderResource res = ((RestResourceWrapper)arg1).getResource();
142                                 name = res.getName();
143                                 id = res.getParentName() +"."+name;
144                         }
145                         else if(arg1 instanceof RestResourceWrapper){
146                                 FolderResource res = ((RestResourceWrapper)arg1).getResource();
147                                 if(res.isShared())
148                                         html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
149                                 else if(res.getParentName()==null){
150                                         html = AbstractImagePrototype.create(images.home()).getHTML();
151                                 }
152                                 else
153                                         html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
154                                 name = res.getName();
155                                 if(res.getParentName() != null){                                        
156                                         id = res.getParentName()+"."+name;
157                                 }else{                                  
158                                         id = name;
159                                 }
160                                 
161                         }
162                         else if(arg1 instanceof TrashResource){
163                                 html = AbstractImagePrototype.create(images.trash()).getHTML();
164                                 name="Trash";
165                                 id = name;                              
166                         }
167                         
168                         else if(arg1 instanceof SharedResource){
169                                 html = AbstractImagePrototype.create(images.myShared()).getHTML();
170                                 name = "My Shared";
171                                 id = name;
172                         }
173                         else if(arg1 instanceof OthersResource){
174                                 html = AbstractImagePrototype.create(images.othersShared()).getHTML();
175                                 name = "Other's Shared";
176                                 id = "others";                          
177                         }
178                         else if(arg1 instanceof OtherUserResource){
179                                 html = AbstractImagePrototype.create(images.permUser()).getHTML();
180                                 name = ((OtherUserResource)arg1).getName();
181                                 id = name;
182                         }
183                         arg2.appendHtmlConstant(html);
184                         arg2.append(FileList.Templates.INSTANCE.spanWithIdAndClass(id, "papala", name));
185                 }
186                 
187                 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) {
188                         if(event.getType().equals("contextmenu")){
189                                 selectionModel.setSelected(value, true);
190                                 GSS.get().setCurrentSelection(value);
191                                 GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
192                         }
193                 };
194                 
195         };
196         
197         
198         @Override
199         public <T> NodeInfo<?> getNodeInfo(final T value) {
200                 
201                 if(value==null){
202                         DragAndDropNodeInfo n = new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
203                                     selectionModel, null);
204                         configureFolderDrop(n);
205                 configureDragOperation(n.getDraggableOptions());
206                         return n;
207                 }
208                 else if (value instanceof MyFolderResource) {
209                 // Second level.
210                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
211                     ((MyFolderResource) value),MyFolderResource.class);
212                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
213                     selectionModel, new ResourceValueUpdater());
214                 mymap.put(((MyFolderResource) value).getUri(), dataProvider);
215                 
216                 // permission cell are not draggable
217                 //n.setCellDroppableOnly();
218                 configureFolderDrop(n);
219                 configureDragOperation(n.getDraggableOptions());
220                 
221                 return n;
222                 }
223                 else if (value instanceof SharedResource) {
224                 // Second level.
225                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
226                     ((SharedResource) value), SharedFolderResource.class);
227                         sharedmap.put(((SharedResource) value).getUri(), dataProvider);
228                         DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
229                     selectionModel, new ResourceValueUpdater());
230                          configureFolderDrop(n);
231                         configureDragOperation(n.getDraggableOptions());
232                         return n;
233                 }
234                 else if (value instanceof TrashResource) {
235                 // Second level.
236                         ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
237                         List<RestResource> r = new ArrayList<RestResource>();
238                         for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
239                                 r.add(new TrashFolderResource(f));
240                         }
241                         trashProvider.setList(r);
242                         DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
243                     selectionModel, new ResourceValueUpdater());
244                         configureFolderDrop(n);
245                 configureDragOperation(n.getDraggableOptions());
246                         return n;
247                 }
248                 else if (value instanceof OthersResource) {
249                 // Second level.
250                         OthersDataProvider dataProvider = new OthersDataProvider(
251                     ((OthersResource) value), SharedFolderResource.class);
252                 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
253                     selectionModel, null);
254                 configureFolderDrop(n);
255                 configureDragOperation(n.getDraggableOptions());
256                 return n;
257                 }
258                 else if (value instanceof SharedFolderResource) {
259                 // Second level.
260                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
261                     ((SharedFolderResource) value),SharedFolderResource.class);
262                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
263                     selectionModel, new ResourceValueUpdater());
264                 sharedmap.put(((SharedFolderResource) value).getUri(), dataProvider);
265                 configureFolderDrop(n);
266                 configureDragOperation(n.getDraggableOptions());
267                 return n;
268                 }
269                 else if (value instanceof OthersFolderResource) {
270                 // Second level.
271                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
272                     ((OthersFolderResource) value),OthersFolderResource.class);
273                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
274                     selectionModel, new ResourceValueUpdater());
275                 //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
276                 othersmap.put(((OthersFolderResource) value).getUri(), dataProvider);
277                 configureFolderDrop(n);
278                 configureDragOperation(n.getDraggableOptions());
279                 return n;
280                 }
281                 else if (value instanceof OtherUserResource) {
282                 // Second level.
283                         MyFolderDataProvider dataProvider = new MyFolderDataProvider(
284                     ((OtherUserResource) value),OthersFolderResource.class);
285                 DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
286                     selectionModel, new ResourceValueUpdater());
287                 configureFolderDrop(n);
288                 configureDragOperation(n.getDraggableOptions());
289                 return n;
290                 }
291                 // TODO Auto-generated method stub
292                 return null;
293         }
294         
295         private void configureFolderDrop(DragAndDropNodeInfo<RestResource> n){
296                 DroppableOptions options = n.getDroppableOptions();
297         options.setDroppableHoverClass("droppableHover");
298         // use a DroppableFunction here. We can also add a DropHandler in the tree
299         // itself
300         options.setOnOver(new DroppableFunction() {
301                         
302                         @Override
303                         public void f(final DragAndDropContext context) {
304                                 if(context.getDroppableData()!=null && context.getDroppableData() instanceof RestResource){
305                                         
306                                         GSS.get().getTreeView().getUtils().openNodeContainingResource((RestResource) context.getDroppableData(), new RefreshHandler() {
307                                                 
308                                                 @Override
309                                                 public void onRefresh() {
310                                                         
311                                                         DragAndDropManager.getInstance().update(context);//initialize(context, GQueryUi.Event.create(com.google.gwt.user.client.Event.getCurrentEvent()));
312                                                         
313                                                 }
314                                         });
315                                         
316                                 }
317                         }
318                 });
319         options.setOnDrop(new DroppableFunction() {
320
321           public void f(DragAndDropContext context) {
322                   
323                   DnDFolderPopupMenu popup ;
324                   if(context.getDraggableData() instanceof FileResource){
325                           if(context.getDroppableData() instanceof RestResourceWrapper)
326                                   popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
327                           else
328                                   popup = new DnDFolderPopupMenu(images, null, Arrays.asList(context.getDraggableData()));
329                   }
330                   
331                   else{
332                           if(context.getDroppableData() instanceof RestResourceWrapper)
333                                   popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), context.getDraggableData());
334                           else
335                                   popup = new DnDFolderPopupMenu(images, null, context.getDraggableData());
336                   }
337                   
338                   int left = context.getDroppable().getAbsoluteLeft() + 40;
339               int top = context.getDroppable().getAbsoluteTop() + 20;
340               popup.setPopupPosition(left, top);
341                  
342                   popup.show();
343           }
344         });
345         }
346
347         @Override
348         public boolean isLeaf(Object value) {
349                 if(value instanceof RestResourceWrapper)
350                         return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
351                 else if(value instanceof TrashResource)
352                         return ((TrashResource)value).getFolders().size()==0;
353                 else if(value instanceof SharedResource)
354                         return ((SharedResource)value).getFolders().size()==0;
355                 else if(value instanceof OthersResource)
356                         return ((OthersResource)value).getOtherUsers().size()==0;
357                 else if(value instanceof OtherUserResource)
358                         return ((OtherUserResource)value).getFolders().size()==0;
359                 return false;
360         }
361         
362         /**
363          * Retrieve the selectionModel.
364          *
365          * @return the selectionModel
366          */
367         public SingleSelectionModel<RestResource> getSelectionModel() {
368                 return selectionModel;
369         }
370         static interface ClearSelection{
371                 public void setClearSelection(boolean clearSelection);
372         }
373         class ResourceValueUpdater implements  ValueUpdater<RestResource>,ClearSelection{
374                 boolean clearSelection=true;
375                 
376                 
377                 /**
378                  * Modify the clearSelection.
379                  *
380                  * @param clearSelection the clearSelection to set
381                  */
382                 public void setClearSelection(boolean clearSelection) {
383                         this.clearSelection = clearSelection;
384                 }
385                 @Override
386                 public void update(final RestResource value) {
387                         if(value instanceof MyFolderResource){
388                                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
389
390                                         @Override
391                                         public void onComplete() {
392                                                 FolderResource rootResource = getResult();
393                                                 //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
394                                                 ((MyFolderResource)value).setResource(rootResource);
395                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
396                                                         selectionModel.setSelected(value, true);
397                                                 GSS.get().onResourceUpdate(value,clearSelection);
398                                                 
399                                         }
400         
401                                         @Override
402                                         public void onError(Throwable t) {
403                                                 GWT.log("Error fetching root folder", t);
404                                                 GSS.get().displayError("Unable to fetch root folder");
405                                         }
406         
407                                 };
408                                 DeferredCommand.addCommand(gf);
409                         }
410                         else if(value instanceof TrashResource){
411                                 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
412                                         @Override
413                                         public void onComplete() {
414                                                 //trash = getResult();
415                                                 ((TrashResource)value).setFolders(getResult().getFolders());
416                                                 ((TrashResource)value).setFiles(getResult().getFiles());
417                                                 for(RestResource r : getRootNodes().getList()){
418                                                         if(r instanceof TrashResource)
419                                                                 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
420                                                 }
421                                                 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
422                                                 //GSS.get().showFileList(true);
423                                                 GSS.get().onResourceUpdate(value,clearSelection);
424                                         }
425
426                                         @Override
427                                         public void onError(Throwable t) {
428                                                 if(t instanceof RestException){
429                                                         int statusCode = ((RestException)t).getHttpStatusCode();
430                                                         // On IE status code 1223 may be returned instead of 204.
431                                                         if(statusCode == 204 || statusCode == 1223){
432                                                                 ((TrashResource)value).setFolders(new ArrayList<FolderResource>());
433                                                                 ((TrashResource)value).setFiles(new ArrayList<FileResource>());
434                                                 }
435                                                 else{
436                                                         GWT.log("", t);
437                                                         GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
438                                                         //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
439                                                 }
440                                         }
441                                 }
442                                 });
443                         }
444                         else if(value instanceof OthersFolderResource){
445                                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
446
447                                         @Override
448                                         public void onComplete() {
449                                                 FolderResource rootResource = getResult();
450                                                 //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
451                                                 ((OthersFolderResource)value).setResource(rootResource);
452                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
453                                                         selectionModel.setSelected(value, true);
454                                                 GSS.get().onResourceUpdate(value,clearSelection);
455                                                 
456                                         }
457         
458                                         @Override
459                                         public void onError(Throwable t) {
460                                                 GWT.log("Error fetching root folder", t);
461                                                 GSS.get().displayError("Unable to fetch root folder");
462                                         }
463         
464                                 };
465                                 DeferredCommand.addCommand(gf);
466                         }
467                         else if(value instanceof SharedFolderResource){
468                                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
469
470                                         @Override
471                                         public void onComplete() {
472                                                 FolderResource rootResource = getResult();
473                                                 //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
474                                                 ((SharedFolderResource)value).setResource(rootResource);
475                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
476                                                         selectionModel.setSelected(value, true);
477                                                 GSS.get().onResourceUpdate(value,clearSelection);
478                                                 
479                                         }
480         
481                                         @Override
482                                         public void onError(Throwable t) {
483                                                 GWT.log("Error fetching root folder", t);
484                                                 GSS.get().displayError("Unable to fetch root folder");
485                                         }
486         
487                                 };
488                                 DeferredCommand.addCommand(gf);
489                         }
490                         else if(value instanceof SharedResource){
491                                 GetCommand<SharedResource> gf = new GetCommand<SharedResource>(SharedResource.class, value.getUri(), null) {
492
493                                         @Override
494                                         public void onComplete() {
495                                                 SharedResource rootResource = getResult();
496                                                 ((SharedResource)value).setFolders(getResult().getFolders());
497                                                 ((SharedResource)value).setFiles(getResult().getFiles());
498                                                 
499                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
500                                                         selectionModel.setSelected(value, true);
501                                                 GSS.get().onResourceUpdate(value,clearSelection);
502                                                 
503                                         }
504         
505                                         @Override
506                                         public void onError(Throwable t) {
507                                                 GWT.log("Error fetching root folder", t);
508                                                 GSS.get().displayError("Unable to fetch root folder");
509                                         }
510         
511                                 };
512                                 DeferredCommand.addCommand(gf);
513                         }
514                         else if(value instanceof OtherUserResource){
515                                 GetCommand<OtherUserResource> gf = new GetCommand<OtherUserResource>(OtherUserResource.class, value.getUri(), null) {
516
517                                         @Override
518                                         public void onComplete() {
519                                                 OtherUserResource rootResource = getResult();
520                                                 ((OtherUserResource)value).setFolders(getResult().getFolders());
521                                                 ((OtherUserResource)value).setFiles(getResult().getFiles());
522                                                 
523                                                 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
524                                                         selectionModel.setSelected(value, true);
525                                                 GSS.get().onResourceUpdate(value,clearSelection);
526                                                 
527                                         }
528         
529                                         @Override
530                                         public void onError(Throwable t) {
531                                                 GWT.log("Error fetching root folder", t);
532                                                 GSS.get().displayError("Unable to fetch root folder");
533                                         }
534         
535                                 };
536                                 DeferredCommand.addCommand(gf);
537                         }
538                         
539                 }
540                 
541         }
542         class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
543                 private RestResource restResource;
544                 private Class resourceClass;
545                   public MyFolderDataProvider(RestResource department, Class resourceClass) {
546                     super(new ProvidesKey<RestResource>() {
547
548                                 @Override
549                                 public Object getKey(RestResource item) {
550                                         return item.getUri();
551                                 }});
552                     this.restResource = department;
553                     this.resourceClass=resourceClass;
554                   }
555
556                   @Override
557                   protected void onRangeChanged(final HasData<RestResource> view) {
558                         refresh(null);
559                   }
560                   
561                 /**
562                  * Retrieve the restResource.
563                  *
564                  * @return the restResource
565                  */
566                 public RestResource getRestResource() {
567                         return restResource;
568                 }
569                 
570                 
571                 /**
572                  * Modify the restResource.
573                  *
574                  * @param restResource the restResource to set
575                  */
576                 public void setRestResource(RestResource restResource) {
577                         this.restResource = restResource;
578                 }
579                 List<RestResource> res =null;
580                   public void refresh(final RefreshHandler refresh){
581                           FolderResource cache = null;
582                           if(restResource instanceof RestResourceWrapper && !((RestResourceWrapper)restResource).getResource().isNeedsExpanding())
583                                   cache = ((RestResourceWrapper)restResource).getResource();
584                           GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(),cache ) {
585
586                                         @Override
587                                         public void onComplete() {
588                                                 if(restResource instanceof RestResourceWrapper){
589                                                         ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
590                                                         ((RestResourceWrapper)restResource).getResource().setNeedsExpanding(false);
591                                                 }
592                                                 if(usedCachedVersion()&&res!=null){
593                                                         
594                                                                 updateRowCount(res.size(), true);
595                                                                 updateRowData(0,res);
596                                                         return;
597                                                 }
598                                                 String[] folderPaths = null;
599                                                 if(resourceClass.equals(MyFolderResource.class))
600                                                         folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
601                                                 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
602                                                         folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
603                                                 else if(resourceClass.equals(SharedFolderResource.class)){
604                                                         folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
605                                                         GWT.log("------------>"+folderPaths);
606                                                 }
607                                                 else if(resourceClass.equals(TrashFolderResource.class))
608                                                         folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
609                                                 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
610                                                         folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
611                                                 else if(resourceClass.equals(OthersFolderResource.class))
612                                                         folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
613                                                 MultipleGetCommand.Cached[] cached = null;
614                                                 if(restResource instanceof RestResourceWrapper)
615                                                         cached=((RestResourceWrapper)restResource).getResource().getCache();
616                                                 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
617                                                                         folderPaths, cached) {
618
619                                                         @Override
620                                                         public void onComplete() {
621                                                                 res = new ArrayList<RestResource>();
622                                                                 for(FolderResource r : getResult()){
623                                                                         if(r.isDeleted()){
624                                                                                 
625                                                                         }
626                                                                         else if(resourceClass.equals(MyFolderResource.class))
627                                                                                 res.add(new MyFolderResource(r));
628                                                                         else if(resourceClass.equals(SharedFolderResource.class)){
629                                                                                 res.add(new SharedFolderResource(r));
630                                                                         }
631                                                                         else if(resourceClass.equals(TrashFolderResource.class))
632                                                                                 res.add(new TrashFolderResource(r));
633                                                                         else if(resourceClass.equals(OthersFolderResource.class))
634                                                                                 res.add(new OthersFolderResource(r));
635                                                                 }
636                                                                 if(restResource instanceof RestResourceWrapper)
637                                                                         ((RestResourceWrapper)restResource).getResource().setFolders(getResult());
638                                                                 updateRowCount(res.size(), true);
639                                                                 updateRowData(0,res);
640                                                                 if(refresh!=null)
641                                                                         refresh.onRefresh();
642                                                         }
643
644                                                         @Override
645                                                         public void onError(Throwable t) {
646                                                                 GSS.get().displayError("Unable to fetch subfolders");
647                                                                 GWT.log("Unable to fetch subfolders", t);
648                                                         }
649
650                                                         @Override
651                                                         public void onError(String p, Throwable throwable) {
652                                                                 GWT.log("Path:"+p, throwable);
653                                                         }
654
655                                                 };
656                                                 DeferredCommand.addCommand(gf2);
657                                                 
658                                         }
659
660                                         @Override
661                                         public void onError(Throwable t) {
662                                                 
663                                                 GWT.log("Error fetching root folder", t);
664                                                 GSS.get().displayError("Unable to fetch root folder");
665                                         }
666
667                                 };
668                                 DeferredCommand.addCommand(gf);
669                   }               
670         }
671         
672         
673         class OthersDataProvider extends AsyncDataProvider<RestResource>{
674                 private RestResource restResource;
675                 private Class resourceClass;
676                   public OthersDataProvider(RestResource department, Class resourceClass) {
677                     super(new ProvidesKey<RestResource>() {
678
679                                 @Override
680                                 public Object getKey(RestResource item) {
681                                         return item.getUri();
682                                 }});
683                     this.restResource = department;
684                     this.resourceClass=resourceClass;
685                   }
686
687                   @Override
688                   protected void onRangeChanged(final HasData<RestResource> view) {
689                         refresh(null);
690                   }
691                   
692                 /**
693                  * Retrieve the restResource.
694                  *
695                  * @return the restResource
696                  */
697                 public RestResource getRestResource() {
698                         return restResource;
699                 }
700                 
701                 
702                 /**
703                  * Modify the restResource.
704                  *
705                  * @param restResource the restResource to set
706                  */
707                 public void setRestResource(RestResource restResource) {
708                         this.restResource = restResource;
709                 }
710                 
711                   public void refresh(final RefreshHandler refresh){
712                           GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
713                           restResource.getUri(), null) {
714
715                                   @Override
716                                   public void onComplete() {
717                                           final OthersResource others = getResult();
718                           MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
719                                                   others.getOthers().toArray(new String[] {}), null) {
720
721                                   @Override
722                                   public void onComplete() {
723                                           List<OtherUserResource> res = getResult();
724                                           updateRowCount(res.size(), true);
725                                           List<RestResource> r = new ArrayList<RestResource>();
726                                           r.addAll(res);
727                                                                           updateRowData(0,r);
728                                   }
729
730                                   @Override
731                                   public void onError(Throwable t) {
732                                           GWT.log("Error fetching Others Root folder", t);
733                                           GSS.get().displayError("Unable to fetch Others Root folder");
734                                   }
735
736                                   @Override
737                                   public void onError(String p, Throwable throwable) {
738                                           GWT.log("Path:"+p, throwable);
739                                   }
740                           };
741                           DeferredCommand.addCommand(gogo);
742                                   }
743                         
744                                   @Override
745                                   public void onError(Throwable t) {
746                                           GWT.log("Error fetching Others Root folder", t);
747                                           GSS.get().displayError("Unable to fetch Others Root folder");
748                                   }
749                           };
750                           DeferredCommand.addCommand(go);
751                   }               
752         }
753
754
755         
756         /**
757          * Retrieve the rootNodes.
758          *
759          * @return the rootNodes
760          */
761         public ListDataProvider<RestResource> getRootNodes() {
762                 return rootNodes;
763         }
764
765         
766         /**
767          * Retrieve the mymap.
768          *
769          * @return the mymap
770          */
771         public Map<String, MyFolderDataProvider> getMymap() {
772                 return mymap;
773         }
774
775         
776         /**
777          * Retrieve the sharedmap.
778          *
779          * @return the sharedmap
780          */
781         public Map<String, MyFolderDataProvider> getSharedmap() {
782                 return sharedmap;
783         }
784
785         
786         /**
787          * Retrieve the othersmap.
788          *
789          * @return the othersmap
790          */
791         public Map<String, MyFolderDataProvider> getOthersmap() {
792                 return othersmap;
793         }
794         
795         
796         
797         
798         
799         
800 }