fix node open on hover when using dnd
[pithos] / src / gr / ebs / gss / client / CellTreeViewModel.java
index 4cdad53..d0e006f 100644 (file)
@@ -36,7 +36,9 @@ 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.StopDragException;
 import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
@@ -57,10 +59,12 @@ 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;
@@ -76,11 +80,14 @@ 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);
 
@@ -88,7 +95,7 @@ public class CellTreeViewModel implements TreeViewModel{
            SafeHtml outerHelper(String cssClassName);
          }
 
-       static void configureDragOperation(DraggableOptions options) {
+       static void configureDragOperation(final DraggableOptions options) {
 
            // set a custom element as drag helper. The content of the helper will be
            // set when the drag will start
@@ -105,9 +112,21 @@ public class CellTreeViewModel implements TreeViewModel{
            options.setAppendTo("body");
            // set the revert option
            options.setRevert(RevertOption.ON_INVALID_DROP);
+           options.setOnBeforeDragStart(new DragFunction() {
+                       
+                       @Override
+                       public void f(DragContext context) {
+                                RestResource value = context.getDraggableData();
+                            if(value instanceof TrashResource || value instanceof SharedResource || value instanceof OthersResource || value instanceof OtherUserResource){
+                               throw new StopDragException();
+                             }
+                               
+                       }
+               });
            // 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();
@@ -197,8 +216,11 @@ public class CellTreeViewModel implements TreeViewModel{
        public <T> NodeInfo<?> getNodeInfo(final T value) {
                
                if(value==null){
-                       return new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
+                       DragAndDropNodeInfo n = new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
                                    selectionModel, null);
+                       configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
+                       return n;
                }
                else if (value instanceof MyFolderResource) {
                // Second level.
@@ -206,51 +228,25 @@ 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(DragAndDropContext context) {
-                                       GWT.log("-->OnActivate:"+context.getDroppableData());
-                                       if(context.getDroppableData()!=null && context.getDroppableData() instanceof RestResource){
-                                               GSS.get().getTreeView().openNodeContainingResource((RestResource) context.getDroppableData());
-                                       }
-                                       
-                               }
-                       });
-               options.setOnDrop(new DroppableFunction() {
-
-                 public void f(DragAndDropContext context) {
-                         
-                         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();
+               configureFolderDrop(n);
                configureDragOperation(n.getDraggableOptions());
+               
                return n;
                }
                else if (value instanceof SharedResource) {
                // Second level.
                        MyFolderDataProvider dataProvider = new MyFolderDataProvider(
                    ((SharedResource) value), SharedFolderResource.class);
-               return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
+                       sharedmap.put(((SharedResource) value).getUri(), dataProvider);
+                       DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
+                        configureFolderDrop(n);
+                       configureDragOperation(n.getDraggableOptions());
+                       return n;
                }
                else if (value instanceof TrashResource) {
                // Second level.
@@ -260,15 +256,21 @@ public class CellTreeViewModel implements TreeViewModel{
                                r.add(new TrashFolderResource(f));
                        }
                        trashProvider.setList(r);
-               return new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
+                       DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
+                       configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
+                       return n;
                }
                else if (value instanceof OthersResource) {
                // Second level.
                        OthersDataProvider dataProvider = new OthersDataProvider(
                    ((OthersResource) value), SharedFolderResource.class);
-               return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
+               DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, null);
+               configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
+               return n;
                }
                else if (value instanceof SharedFolderResource) {
                // Second level.
@@ -276,20 +278,9 @@ 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);
-               DroppableOptions options = n.getDroppableOptions();
-               options.setDroppableHoverClass("droppableHover");
-               // use a DroppableFunction here. We can also add a DropHandler in the tree
-               // itself
-               options.setOnDrop(new DroppableFunction() {
-
-                 public void f(DragAndDropContext context) {
-                         GWT.log("DROPPED");
-                   
-                 }
-               });
-               // permission cell are not draggable
-               n.setCellDroppableOnly();
+               sharedmap.put(((SharedFolderResource) value).getUri(), dataProvider);
+               configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
                return n;
                }
                else if (value instanceof OthersFolderResource) {
@@ -299,19 +290,9 @@ public class CellTreeViewModel implements TreeViewModel{
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
                //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
-               DroppableOptions options = n.getDroppableOptions();
-               options.setDroppableHoverClass("droppableHover");
-               // use a DroppableFunction here. We can also add a DropHandler in the tree
-               // itself
-               options.setOnDrop(new DroppableFunction() {
-
-                 public void f(DragAndDropContext context) {
-                         GWT.log("DROPPED");
-                   
-                 }
-               });
-               // permission cell are not draggable
-               n.setCellDroppableOnly();
+               othersmap.put(((OthersFolderResource) value).getUri(), dataProvider);
+               configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
                return n;
                }
                else if (value instanceof OtherUserResource) {
@@ -320,25 +301,69 @@ public class CellTreeViewModel implements TreeViewModel{
                    ((OtherUserResource) value),OthersFolderResource.class);
                DragAndDropNodeInfo<RestResource> n =  new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
                    selectionModel, new ResourceValueUpdater());
-               //nodeInfos.put(((OtherUserResource) value).getUri(), n);
-               DroppableOptions options = n.getDroppableOptions();
-               options.setDroppableHoverClass("droppableHover");
-               // use a DroppableFunction here. We can also add a DropHandler in the tree
-               // itself
-               options.setOnDrop(new DroppableFunction() {
-
-                 public void f(DragAndDropContext context) {
-                         GWT.log("DROPPED");
-                   
-                 }
-               });
-               // permission cell are not draggable
-               n.setCellDroppableOnly();
+               configureFolderDrop(n);
+               configureDragOperation(n.getDraggableOptions());
                return n;
                }
                // TODO Auto-generated method stub
                return null;
        }
+       
+       private void configureFolderDrop(DragAndDropNodeInfo<RestResource> n){
+               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().update(context);//initialize(context, GQueryUi.Event.create(com.google.gwt.user.client.Event.getCurrentEvent()));
+                                                       
+                                               }
+                                       });
+                                       
+                               }
+                               
+                               
+                               
+                       }
+               });
+        options.setOnDrop(new DroppableFunction() {
+
+          public void f(DragAndDropContext context) {
+                 
+                 DnDFolderPopupMenu popup ;
+                 if(context.getDraggableData() instanceof FileResource){
+                         if(context.getDroppableData() instanceof RestResourceWrapper)
+                                 popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
+                         else
+                                 popup = new DnDFolderPopupMenu(images, null, Arrays.asList(context.getDraggableData()));
+                 }
+                 
+                 else{
+                         if(context.getDroppableData() instanceof RestResourceWrapper)
+                                 popup = new DnDFolderPopupMenu(images, ((RestResourceWrapper) context.getDroppableData()).getResource(), context.getDraggableData());
+                         else
+                                 popup = new DnDFolderPopupMenu(images, null, context.getDraggableData());
+                 }
+                 
+                 int left = context.getDroppable().getAbsoluteLeft() + 40;
+              int top = context.getDroppable().getAbsoluteTop() + 20;
+              popup.setPopupPosition(left, top);
+                
+                 popup.show();
+          }
+        });
+       }
 
        @Override
        public boolean isLeaf(Object value) {
@@ -369,7 +394,6 @@ public class CellTreeViewModel implements TreeViewModel{
                                                ((MyFolderResource)value).setResource(rootResource);
                                                if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
                                                        selectionModel.setSelected(value, true);
-                                               GWT.log("UPDATYING");
                                                GSS.get().onResourceUpdate(value);
                                                
                                        }
@@ -383,16 +407,20 @@ 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() {
                                                //trash = getResult();
+                                               ((TrashResource)value).setFolders(getResult().getFolders());
+                                               ((TrashResource)value).setFiles(getResult().getFiles());
                                                for(RestResource r : getRootNodes().getList()){
                                                        if(r instanceof TrashResource)
                                                                getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
                                                }
                                                GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
+                                               //GSS.get().showFileList(true);
+                                               GSS.get().onResourceUpdate(value);
                                        }
 
                                        @Override
@@ -401,7 +429,8 @@ public class CellTreeViewModel implements TreeViewModel{
                                                        int statusCode = ((RestException)t).getHttpStatusCode();
                                                        // On IE status code 1223 may be returned instead of 204.
                                                        if(statusCode == 204 || statusCode == 1223){
-                                                               //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
+                                                               ((TrashResource)value).setFolders(new ArrayList<FolderResource>());
+                                                               ((TrashResource)value).setFiles(new ArrayList<FileResource>());
                                                }
                                                else{
                                                        GWT.log("", t);
@@ -412,6 +441,100 @@ public class CellTreeViewModel implements TreeViewModel{
                                }
                                });
                        }
+                       else if(value instanceof OthersFolderResource){
+                               GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
+
+                                       @Override
+                                       public void onComplete() {
+                                               FolderResource rootResource = getResult();
+                                               //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
+                                               ((OthersFolderResource)value).setResource(rootResource);
+                                               if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
+                                                       selectionModel.setSelected(value, true);
+                                               GSS.get().onResourceUpdate(value);
+                                               
+                                       }
+       
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               GWT.log("Error fetching root folder", t);
+                                               GSS.get().displayError("Unable to fetch root folder");
+                                       }
+       
+                               };
+                               DeferredCommand.addCommand(gf);
+                       }
+                       else if(value instanceof SharedFolderResource){
+                               GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
+
+                                       @Override
+                                       public void onComplete() {
+                                               FolderResource rootResource = getResult();
+                                               //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
+                                               ((SharedFolderResource)value).setResource(rootResource);
+                                               if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
+                                                       selectionModel.setSelected(value, true);
+                                               GSS.get().onResourceUpdate(value);
+                                               
+                                       }
+       
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               GWT.log("Error fetching root folder", t);
+                                               GSS.get().displayError("Unable to fetch root folder");
+                                       }
+       
+                               };
+                               DeferredCommand.addCommand(gf);
+                       }
+                       else if(value instanceof SharedResource){
+                               GetCommand<SharedResource> gf = new GetCommand<SharedResource>(SharedResource.class, value.getUri(), null) {
+
+                                       @Override
+                                       public void onComplete() {
+                                               SharedResource rootResource = getResult();
+                                               ((SharedResource)value).setFolders(getResult().getFolders());
+                                               ((SharedResource)value).setFiles(getResult().getFiles());
+                                               
+                                               if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
+                                                       selectionModel.setSelected(value, true);
+                                               GSS.get().onResourceUpdate(value);
+                                               
+                                       }
+       
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               GWT.log("Error fetching root folder", t);
+                                               GSS.get().displayError("Unable to fetch root folder");
+                                       }
+       
+                               };
+                               DeferredCommand.addCommand(gf);
+                       }
+                       else if(value instanceof OtherUserResource){
+                               GetCommand<OtherUserResource> gf = new GetCommand<OtherUserResource>(OtherUserResource.class, value.getUri(), null) {
+
+                                       @Override
+                                       public void onComplete() {
+                                               OtherUserResource rootResource = getResult();
+                                               ((OtherUserResource)value).setFolders(getResult().getFolders());
+                                               ((OtherUserResource)value).setFiles(getResult().getFiles());
+                                               
+                                               if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
+                                                       selectionModel.setSelected(value, true);
+                                               GSS.get().onResourceUpdate(value);
+                                               
+                                       }
+       
+                                       @Override
+                                       public void onError(Throwable t) {
+                                               GWT.log("Error fetching root folder", t);
+                                               GSS.get().displayError("Unable to fetch root folder");
+                                       }
+       
+                               };
+                               DeferredCommand.addCommand(gf);
+                       }
                        
                }
                
@@ -428,7 +551,6 @@ public class CellTreeViewModel implements TreeViewModel{
                                }});
                    this.restResource = department;
                    this.resourceClass=resourceClass;
-                   //CellTreeView.this.mymap.put(department.getUri(), MyFolderDataProvider.this);
                  }
 
                  @Override
@@ -456,9 +578,6 @@ public class CellTreeViewModel implements TreeViewModel{
                }
                List<RestResource> res =null;
                  public void refresh(final RefreshHandler refresh){
-                         GWT.log("******************************************");
-                         GWT.log("[REFRESHING]:"+restResource.getUri());
-                         GWT.log("******************************************");
                          FolderResource cache = null;
                          if(restResource instanceof RestResourceWrapper && !((RestResourceWrapper)restResource).getResource().isNeedsExpanding())
                                  cache = ((RestResourceWrapper)restResource).getResource();
@@ -471,8 +590,9 @@ public class CellTreeViewModel implements TreeViewModel{
                                                        ((RestResourceWrapper)restResource).getResource().setNeedsExpanding(false);
                                                }
                                                if(usedCachedVersion()&&res!=null){
-                                                       updateRowCount(res.size(), true);
-                                                       updateRowData(0,res);
+                                                       
+                                                               updateRowCount(res.size(), true);
+                                                               updateRowData(0,res);
                                                        return;
                                                }
                                                String[] folderPaths = null;
@@ -506,7 +626,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))
@@ -563,7 +682,6 @@ public class CellTreeViewModel implements TreeViewModel{
                                }});
                    this.restResource = department;
                    this.resourceClass=resourceClass;
-                   //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
                  }
 
                  @Override
@@ -591,9 +709,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) {
 
@@ -656,6 +771,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;
+       }
+       
+