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