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