Versions are fetched before the versions dialog is shown
[pithos-web-client] / src / gr / grnet / pithos / web / client / ToolsMenu.java
index 77fae56..aa6eecb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
  */
 package gr.grnet.pithos.web.client;
 
+import gr.grnet.pithos.web.client.commands.AddUserCommand;
 import gr.grnet.pithos.web.client.commands.CopyCommand;
+import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
 import gr.grnet.pithos.web.client.commands.CutCommand;
 import gr.grnet.pithos.web.client.commands.DeleteCommand;
+import gr.grnet.pithos.web.client.commands.DeleteGroupCommand;
 import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
-import gr.grnet.pithos.web.client.commands.NewFolderCommand;
 import gr.grnet.pithos.web.client.commands.PasteCommand;
 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
-import gr.grnet.pithos.web.client.commands.RefreshCommand;
+import gr.grnet.pithos.web.client.commands.RemoveUserCommand;
 import gr.grnet.pithos.web.client.commands.RestoreTrashCommand;
 import gr.grnet.pithos.web.client.commands.ToTrashCommand;
 import gr.grnet.pithos.web.client.foldertree.File;
 import gr.grnet.pithos.web.client.foldertree.Folder;
+import gr.grnet.pithos.web.client.grouptree.Group;
+import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
+import gr.grnet.pithos.web.client.grouptree.User;
 
 import java.util.List;
 
 import com.google.gwt.user.client.Command;
+import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.AbstractImagePrototype;
 import com.google.gwt.user.client.ui.MenuBar;
 import com.google.gwt.user.client.ui.MenuItem;
@@ -69,17 +75,19 @@ public class ToolsMenu extends PopupPanel {
         * The image bundle for this widget's images that reuses images defined in
         * other menus.
         */
-       public interface Images extends FolderContextMenu.Images {
+       public interface Images extends GroupTreeView.Images {
        }
 
        private MenuItem pasteItem;
 
+       private boolean empty = true;
+       
        /**
         * The widget's constructor.
         *
         * @param newImages the image bundle passed on by the parent object
         */
-       public ToolsMenu(Pithos app, Images newImages, TreeView selectedTree, Folder folder, List<File> files) {
+       public ToolsMenu(final Pithos app, Images newImages, TreeView selectedTree, Folder folder, final List<File> files) {
                // The popup's constructor's argument is a boolean specifying that it
                // auto-close itself when the user clicks outside of it.
                super(true);
@@ -91,17 +99,9 @@ public class ToolsMenu extends PopupPanel {
                Boolean[] permissions = folder.getPermissions().get(app.getUsername());
                boolean canWrite = folder.getOwner().equals(app.getUsername()) || (permissions!= null && permissions[1] != null && permissions[1]);
                boolean isFolderTreeSelected = selectedTree.equals(app.getFolderTreeView());
-               boolean otherSharedTreeSelected = selectedTree.equals(app.getOtherSharedTreeView());
                
-               if (isFolderTreeSelected || otherSharedTreeSelected) {
-                       MenuItem refresh = new MenuItem("<span id = 'folderContextMenu.refresh'>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(app, this, folder));
-                       contextMenu.addItem(refresh);
-               }
-       
                if (!folder.isInTrash()) {
                        if (canWrite) {
-                               MenuItem newFolder = new MenuItem("<span id = 'folderContextMenu.newFolder'>" + AbstractImagePrototype.create(newImages.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(app, this, folder, images));
-                               contextMenu.addItem(newFolder);
 
                                if (isFolderTreeSelected) {
                                        MenuItem cut = null;
@@ -111,24 +111,30 @@ public class ToolsMenu extends PopupPanel {
                                        else if (!folder.isContainer()) {
                                            cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut folder</span>", true, new CutCommand(app, this, folder));
                                        }
-                                       if (cut != null)
+                                       if (cut != null) {
                                                contextMenu.addItem(cut);
+                                               empty = false;
+                                       }
                                }
                        }
        
                        MenuItem copy = null;
                        if (files != null && !files.isEmpty())
                                copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy files</span>", true, new CopyCommand(app, this, files));
-                       else
+                       else if (isFolderTreeSelected && !folder.isContainer())
                                copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy folder</span>", true, new CopyCommand(app, this, folder));
-                       contextMenu.addItem(copy);
+                       if (copy != null) {
+                               contextMenu.addItem(copy);
+                               empty = false;
+                       }
                
                        if (canWrite) {
                                if (!app.getClipboard().isEmpty()) {
                                        Object item = app.getClipboard().getItem();
                                        boolean showPaste = false;
                                        if (item instanceof List) {
-                                               List<File> _files = (List<File>) item;
+                                               @SuppressWarnings("unchecked")
+                                                       List<File> _files = (List<File>) item;
                                                if (_files.get(0).getOwner().equals(folder.getOwner()))
                                                        showPaste = true;
                                        }
@@ -140,6 +146,7 @@ public class ToolsMenu extends PopupPanel {
                                        if (showPaste) {
                                            pasteItem = new MenuItem("<span id = 'folderContextMenu.paste'>" + AbstractImagePrototype.create(newImages.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(app, this, folder));
                                            contextMenu.addItem(pasteItem);
+                                               empty = false;
                                        }
                                }
        
@@ -151,8 +158,10 @@ public class ToolsMenu extends PopupPanel {
                                        else if (!folder.isContainer()) {
                                                moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move folder to Trash</span>", true, new ToTrashCommand(app, this, folder));
                                        }
-                                       if (moveToTrash != null)
+                                       if (moveToTrash != null) {
                                                contextMenu.addItem(moveToTrash);
+                                               empty = false;
+                                       }
                                
                                        MenuItem delete = null;
                                        if (files != null && !files.isEmpty()) {
@@ -160,19 +169,34 @@ public class ToolsMenu extends PopupPanel {
                                        }
                                        else if (!folder.isContainer())
                                                delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete folder</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
-                                       if (delete != null)
+                                       if (delete != null) {
                                                contextMenu.addItem(delete);
+                                               empty = false;
+                                       }
                                
-                                       MenuItem properties = null;
-                                       if (files != null && files.size() == 1)
-                                               properties = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;File properties</span>", true, new PropertiesCommand(app, this, files, images, 0));
-                                       else if (!folder.isContainer())
-                                               properties = new MenuItem("<span id = 'folderContextMenu.properties'>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Folder properties</span>", true, new PropertiesCommand(app, this, folder, newImages, 0));
-                                       if (properties != null)
-                                               contextMenu.addItem(properties);
+                                       if (files != null && files.size() == 1) {
+                                               contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;File properties</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PROPERTIES)));
+                                               contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.group()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PERMISSIONS)));
+                                               contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Versions</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.VERSIONS)));
+                                               empty = false;
+                                       }
+                                       else if (!folder.isContainer()) {
+                                               contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Folder properties</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PROPERTIES)));
+                                               contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Folder sharing</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PERMISSIONS)));
+                                               empty = false;
+                                       }
                                    }
-                               if (files != null && files.size() == 1)
-                                           contextMenu.addItem(new MenuItem("<span><a class='hidden-link' href='" + app.getApiPath() + files.get(0).getOwner() + files.get(0).getUri() + "?X-Auth-Token=" + app.getToken() + "' target='_blank'>" + AbstractImagePrototype.create(newImages.download()).getHTML() + " Download</a></span>", true, (Command) null));
+                               if (files != null && !files.isEmpty()) {
+                                           contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.download()).getHTML() + "&nbsp;Download</span>", true, new Command() {
+                                                       
+                                                       @Override
+                                                       public void execute() {
+                                                       for (File f: files)
+                                                               Window.open(app.getApiPath() + f.getOwner() + f.getUri() + "?X-Auth-Token=" + app.getToken(), "_blank", "");
+                                                       }
+                                               }));
+                                       empty = false;
+                               }
                        }
                }
                else {
@@ -183,16 +207,45 @@ public class ToolsMenu extends PopupPanel {
                                else
                                        restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, folder));
                                contextMenu.addItem(restore);
-       
+
                                MenuItem delete = new MenuItem("<span id = 'folderContextMenu.delete'>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
                                contextMenu.addItem(delete);
+                               empty = false;
                        }
                        else {
                                MenuItem emptyTrash = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(app, this));
                                contextMenu.addItem(emptyTrash);
+                               empty = false;
                        }
                }
-                       add(contextMenu);
         }
+        else {
+               Object o = app.getGroupTreeView().getSelected();
+               if (o != null) {
+                       if (o instanceof User) {
+                       MenuItem removeUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Remove User</span>", true, new RemoveUserCommand(app, this, (User) o));
+                       contextMenu.addItem(removeUser);
+                               empty = false;
+                       }
+                       else if (o instanceof Group) {
+                       MenuItem addUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.user()).getHTML() + "&nbsp;Add User</span>", true, new AddUserCommand(app, this, (Group) o));
+                       contextMenu.addItem(addUser);
+               
+                       MenuItem deleteGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Delete Group</span>", true, new DeleteGroupCommand(app, this, (Group) o));
+                       contextMenu.addItem(deleteGroup);
+                               empty = false;
+                       }
+                       else {
+                       MenuItem createGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.group()).getHTML() + "&nbsp;Create Group</span>", true, new CreateGroupCommand(app, this));
+                       contextMenu.addItem(createGroup);
+                               empty = false;
+                       }
+               }
+        }
+               add(contextMenu);
+       }
+       
+       public boolean isEmpty() {
+               return empty;
        }
 }