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