clear selection after trash or delete
[pithos] / src / gr / ebs / gss / client / CellTreeViewModel.java
index aba69fe..007fdc9 100644 (file)
  */
 package gr.ebs.gss.client;
 
+import static com.google.gwt.query.client.GQuery.$;
 import gr.ebs.gss.client.CellTreeView.Images;
 import gr.ebs.gss.client.CellTreeView.RefreshHandler;
 import gr.ebs.gss.client.rest.GetCommand;
 import gr.ebs.gss.client.rest.MultipleGetCommand;
 import gr.ebs.gss.client.rest.RestException;
+import gr.ebs.gss.client.rest.resource.FileResource;
 import gr.ebs.gss.client.rest.resource.FolderResource;
 import gr.ebs.gss.client.rest.resource.MyFolderResource;
 import gr.ebs.gss.client.rest.resource.OtherUserResource;
@@ -34,12 +36,19 @@ import gr.ebs.gss.client.rest.resource.SharedFolderResource;
 import gr.ebs.gss.client.rest.resource.SharedResource;
 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
 import gr.ebs.gss.client.rest.resource.TrashResource;
+import gwtquery.plugins.draggable.client.DragAndDropManager;
+import gwtquery.plugins.draggable.client.DraggableOptions;
+import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
+import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
+import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
+import gwtquery.plugins.draggable.client.events.DragContext;
 import gwtquery.plugins.droppable.client.DroppableOptions;
 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -48,8 +57,13 @@ import com.google.gwt.cell.client.AbstractCell;
 import com.google.gwt.cell.client.Cell;
 import com.google.gwt.cell.client.ValueUpdater;
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Style.Cursor;
+import com.google.gwt.query.client.plugins.GQueryUi;
+import com.google.gwt.safehtml.client.SafeHtmlTemplates;
+import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
 import com.google.gwt.user.client.DeferredCommand;
+import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.AbstractImagePrototype;
 import com.google.gwt.view.client.AsyncDataProvider;
 import com.google.gwt.view.client.HasData;
@@ -66,9 +80,48 @@ import com.google.gwt.view.client.TreeViewModel;
  */
 public class CellTreeViewModel implements TreeViewModel{
        private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
+       private Map<String,FolderResource> folderCache=new HashMap<String, FolderResource>();
        final Images images;
        SingleSelectionModel<RestResource> selectionModel;
        Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
+       Map<String, MyFolderDataProvider> sharedmap = new HashMap<String, MyFolderDataProvider>();
+       Map<String, MyFolderDataProvider> othersmap = new HashMap<String, MyFolderDataProvider>();
+       static interface Templates extends SafeHtmlTemplates {
+           Templates INSTANCE = GWT.create(Templates.class);
+
+           @Template(" <div id='dragHelper' class='{0}'></div>")
+           SafeHtml outerHelper(String cssClassName);
+         }
+
+       static void configureDragOperation(DraggableOptions options) {
+
+           // set a custom element as drag helper. The content of the helper will be
+           // set when the drag will start
+           options.setHelper($(Templates.INSTANCE.outerHelper(
+               "drag").asString()));
+           // opacity of the drag helper
+           options.setOpacity((float) 0.9);
+           // cursor during the drag operation
+           options.setCursor(Cursor.MOVE);
+           // the cell being greater than the helper, force the position of the
+           // helper on the mouse cursor.
+           options.setCursorAt(new CursorAt(10, 10, null, null));
+           // append the helper to the body element
+           options.setAppendTo("body");
+           // set the revert option
+           options.setRevert(RevertOption.ON_INVALID_DROP);
+           // use a Function to fill the content of the helper
+           // we could also add a DragStartEventHandler on the DragAndDropTreeCell and
+           // DragAndDropCellList.
+           options.setOnDragStart(new DragFunction() {
+             public void f(DragContext context) {
+               RestResourceWrapper memberInfo = context.getDraggableData();
+               context.getHelper().setInnerHTML(memberInfo.getName());
+             }
+           });
+
+         }
+
        /**
         * 
         */
@@ -82,12 +135,14 @@ public class CellTreeViewModel implements TreeViewModel{
                
                @Override
                public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
-                       String html=null;
-                       String name=null;
+                       String id = null;
+                       String html = null;
+                       String name = null;
                        if(arg1 instanceof TrashFolderResource){
                                html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
                                FolderResource res = ((RestResourceWrapper)arg1).getResource();
-                               name=res.getName();
+                               name = res.getName();
+                               id = res.getParentName() +"."+name;
                        }
                        else if(arg1 instanceof RestResourceWrapper){
                                FolderResource res = ((RestResourceWrapper)arg1).getResource();
@@ -99,27 +154,36 @@ public class CellTreeViewModel implements TreeViewModel{
                                else
                                        html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
                                name = res.getName();
+                               if(res.getParentName() != null){                                        
+                                       id = res.getParentName()+"."+name;
+                               }else{                                  
+                                       id = name;
+                               }
                                
                        }
                        else if(arg1 instanceof TrashResource){
                                html = AbstractImagePrototype.create(images.trash()).getHTML();
                                name="Trash";
+                               id = name;                              
                        }
                        
                        else if(arg1 instanceof SharedResource){
                                html = AbstractImagePrototype.create(images.myShared()).getHTML();
-                               name="My Shared";
+                               name = "My Shared";
+                               id = name;
                        }
                        else if(arg1 instanceof OthersResource){
                                html = AbstractImagePrototype.create(images.othersShared()).getHTML();
                                name = "Other's Shared";
+                               id = "others";                          
                        }
                        else if(arg1 instanceof OtherUserResource){
                                html = AbstractImagePrototype.create(images.permUser()).getHTML();
                                name = ((OtherUserResource)arg1).getName();
+                               id = name;
                        }
                        arg2.appendHtmlConstant(html);
-                       arg2.appendHtmlConstant("<span class='papala'>");
+                       arg2.appendHtmlConstant("<span id='"+id +"'"+ " class='papala'>");
                        arg2.appendEscaped(name);
                        arg2.appendHtmlConstant("</span>");
                }
@@ -135,7 +199,7 @@ public class CellTreeViewModel implements TreeViewModel{
        
        
        @Override
-       public <T> NodeInfo<?> getNodeInfo(T value) {
+       public <T> NodeInfo<?> getNodeInfo(final T value) {
                
                if(value==null){
                        return new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
@@ -147,27 +211,62 @@ public class CellTreeViewModel implements TreeViewModel{
                    ((MyFolderResource) value),MyFolderResource.class);
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
+               
                //nodeInfos.put(((MyFolderResource) value).getUri(), n);
                mymap.put(((MyFolderResource) value).getUri(), dataProvider);
                DroppableOptions options = n.getDroppableOptions();
                options.setDroppableHoverClass("droppableHover");
                // use a DroppableFunction here. We can also add a DropHandler in the tree
                // itself
+               options.setOnOver(new DroppableFunction() {
+                               
+                               @Override
+                               public void f(final DragAndDropContext context) {
+                                       if(context.getDroppableData()!=null && context.getDroppableData() instanceof RestResource){
+                                               GSS.get().getTreeView().getUtils().openNodeContainingResource((RestResource) context.getDroppableData(), new RefreshHandler() {
+                                                       
+                                                       @Override
+                                                       public void onRefresh() {
+                                                               
+                                                               DragAndDropManager.getInstance().initialize(context, GQueryUi.Event.create(com.google.gwt.user.client.Event.getCurrentEvent()));
+                                                               
+                                                       }
+                                               });
+                                               
+                                       }
+                                       
+                                       
+                                       
+                               }
+                       });
                options.setOnDrop(new DroppableFunction() {
 
                  public void f(DragAndDropContext context) {
-                         GWT.log("DROPPED");
-                   
+                         
+                         DnDFolderPopupMenu popup ;
+                         if(context.getDraggableData() instanceof FileResource){
+                                 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
+                         }
+                         else
+                                 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), context.getDraggableData());
+                         int left = context.getDroppable().getAbsoluteLeft() + 40;
+                  int top = context.getDroppable().getAbsoluteTop() + 20;
+                  popup.setPopupPosition(left, top);
+                        
+                         popup.show();
                  }
                });
                // permission cell are not draggable
-               n.setCellDroppableOnly();
+               //n.setCellDroppableOnly();
+               configureDragOperation(n.getDraggableOptions());
+               
                return n;
                }
                else if (value instanceof SharedResource) {
                // Second level.
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
                    ((SharedResource) value), SharedFolderResource.class);
+                       sharedmap.put(((SharedResource) value).getUri(), dataProvider);
                return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
                }
@@ -195,7 +294,7 @@ public class CellTreeViewModel implements TreeViewModel{
                    ((SharedFolderResource) value),SharedFolderResource.class);
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
-               //nodeInfos.put(((SharedFolderResource) value).getUri(), n);
+               sharedmap.put(((SharedFolderResource) value).getUri(), dataProvider);
                DroppableOptions options = n.getDroppableOptions();
                options.setDroppableHoverClass("droppableHover");
                // use a DroppableFunction here. We can also add a DropHandler in the tree
@@ -218,6 +317,7 @@ public class CellTreeViewModel implements TreeViewModel{
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
                //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
+               othersmap.put(((SharedFolderResource) value).getUri(), dataProvider);
                DroppableOptions options = n.getDroppableOptions();
                options.setDroppableHoverClass("droppableHover");
                // use a DroppableFunction here. We can also add a DropHandler in the tree
@@ -284,10 +384,10 @@ public class CellTreeViewModel implements TreeViewModel{
                                        @Override
                                        public void onComplete() {
                                                FolderResource rootResource = getResult();
-                                               ((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
+                                               //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
+                                               ((MyFolderResource)value).setResource(rootResource);
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
                                                        selectionModel.setSelected(value, true);
-                                               GWT.log("UPDATYING");
                                                GSS.get().onResourceUpdate(value);
                                                
                                        }
@@ -301,7 +401,7 @@ public class CellTreeViewModel implements TreeViewModel{
                                };
                                DeferredCommand.addCommand(gf);
                        }
-                       if(value instanceof TrashResource){
+                       else if(value instanceof TrashResource){
                                DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
                                        @Override
                                        public void onComplete() {
@@ -330,6 +430,9 @@ public class CellTreeViewModel implements TreeViewModel{
                                }
                                });
                        }
+                       else if(value instanceof OthersFolderResource){
+                               
+                       }
                        
                }
                
@@ -372,20 +475,26 @@ public class CellTreeViewModel implements TreeViewModel{
                public void setRestResource(RestResource restResource) {
                        this.restResource = restResource;
                }
-               
+               List<RestResource> res =null;
                  public void refresh(final RefreshHandler refresh){
-                         GWT.log("******************************************");
-                         GWT.log("[REFRESHING]:"+restResource.getUri());
-                         GWT.log("******************************************");
-                         GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(), null) {
+                         FolderResource cache = null;
+                         if(restResource instanceof RestResourceWrapper && !((RestResourceWrapper)restResource).getResource().isNeedsExpanding())
+                                 cache = ((RestResourceWrapper)restResource).getResource();
+                         GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(),cache ) {
 
                                        @Override
                                        public void onComplete() {
-                                               if(restResource instanceof RestResourceWrapper)
+                                               if(restResource instanceof RestResourceWrapper){
                                                        ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
-                                               
-                                               //if(CellTreeView.this.mymap.get(restResource.getUri())!=null)
-                                                       //CellTreeView.this.mymap.get(restResource.getUri()).setRestResource(restResource);
+                                                       ((RestResourceWrapper)restResource).getResource().setNeedsExpanding(false);
+                                               }
+                                               if(usedCachedVersion()&&res!=null){
+                                                       
+                                                               updateRowCount(res.size(), true);
+                                                               updateRowData(0,res);
+                                                       
+                                                       return;
+                                               }
                                                String[] folderPaths = null;
                                                if(resourceClass.equals(MyFolderResource.class))
                                                        folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
@@ -401,12 +510,15 @@ public class CellTreeViewModel implements TreeViewModel{
                                                        folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
                                                else if(resourceClass.equals(OthersFolderResource.class))
                                                        folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
+                                               MultipleGetCommand.Cached[] cached = null;
+                                               if(restResource instanceof RestResourceWrapper)
+                                                       cached=((RestResourceWrapper)restResource).getResource().getCache();
                                                MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
-                                                                       folderPaths, null) {
+                                                                       folderPaths, cached) {
 
                                                        @Override
                                                        public void onComplete() {
-                                                               List<RestResource> res = new ArrayList<RestResource>();
+                                                               res = new ArrayList<RestResource>();
                                                                for(FolderResource r : getResult()){
                                                                        if(r.isDeleted()){
                                                                                
@@ -414,7 +526,6 @@ public class CellTreeViewModel implements TreeViewModel{
                                                                        else if(resourceClass.equals(MyFolderResource.class))
                                                                                res.add(new MyFolderResource(r));
                                                                        else if(resourceClass.equals(SharedFolderResource.class)){
-                                                                               GWT.log("ADDING:"+r.getUri());
                                                                                res.add(new SharedFolderResource(r));
                                                                        }
                                                                        else if(resourceClass.equals(TrashFolderResource.class))
@@ -422,6 +533,8 @@ public class CellTreeViewModel implements TreeViewModel{
                                                                        else if(resourceClass.equals(OthersFolderResource.class))
                                                                                res.add(new OthersFolderResource(r));
                                                                }
+                                                               if(restResource instanceof RestResourceWrapper)
+                                                                       ((RestResourceWrapper)restResource).getResource().setFolders(getResult());
                                                                updateRowCount(res.size(), true);
                                                                updateRowData(0,res);
                                                                if(refresh!=null)
@@ -446,6 +559,7 @@ public class CellTreeViewModel implements TreeViewModel{
 
                                        @Override
                                        public void onError(Throwable t) {
+                                               
                                                GWT.log("Error fetching root folder", t);
                                                GSS.get().displayError("Unable to fetch root folder");
                                        }
@@ -496,9 +610,6 @@ public class CellTreeViewModel implements TreeViewModel{
                }
                
                  public void refresh(final RefreshHandler refresh){
-                         GWT.log("******************************************");
-                         GWT.log("[REFRESHING]:"+restResource.getUri());
-                         GWT.log("******************************************");
                          GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
                           restResource.getUri(), null) {
 
@@ -561,6 +672,28 @@ public class CellTreeViewModel implements TreeViewModel{
        public Map<String, MyFolderDataProvider> getMymap() {
                return mymap;
        }
+
+       
+       /**
+        * Retrieve the sharedmap.
+        *
+        * @return the sharedmap
+        */
+       public Map<String, MyFolderDataProvider> getSharedmap() {
+               return sharedmap;
+       }
+
+       
+       /**
+        * Retrieve the othersmap.
+        *
+        * @return the othersmap
+        */
+       public Map<String, MyFolderDataProvider> getOthersmap() {
+               return othersmap;
+       }
+       
+