When multiple files are selected the download option downloads all of them in multipl...
[pithos] / web_client / src / gr / grnet / pithos / web / client / ToolsMenu.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import gr.grnet.pithos.web.client.commands.AddUserCommand;
38 import gr.grnet.pithos.web.client.commands.CopyCommand;
39 import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
40 import gr.grnet.pithos.web.client.commands.CutCommand;
41 import gr.grnet.pithos.web.client.commands.DeleteCommand;
42 import gr.grnet.pithos.web.client.commands.DeleteGroupCommand;
43 import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
44 import gr.grnet.pithos.web.client.commands.NewFolderCommand;
45 import gr.grnet.pithos.web.client.commands.PasteCommand;
46 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
47 import gr.grnet.pithos.web.client.commands.RefreshCommand;
48 import gr.grnet.pithos.web.client.commands.RemoveUserCommand;
49 import gr.grnet.pithos.web.client.commands.RestoreTrashCommand;
50 import gr.grnet.pithos.web.client.commands.ToTrashCommand;
51 import gr.grnet.pithos.web.client.foldertree.File;
52 import gr.grnet.pithos.web.client.foldertree.Folder;
53 import gr.grnet.pithos.web.client.grouptree.Group;
54 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
55 import gr.grnet.pithos.web.client.grouptree.User;
56
57 import java.util.List;
58
59 import com.google.gwt.user.client.Command;
60 import com.google.gwt.user.client.Window;
61 import com.google.gwt.user.client.ui.AbstractImagePrototype;
62 import com.google.gwt.user.client.ui.MenuBar;
63 import com.google.gwt.user.client.ui.MenuItem;
64 import com.google.gwt.user.client.ui.PopupPanel;
65
66 /**
67  * The 'Folder Context' menu implementation.
68  */
69 public class ToolsMenu extends PopupPanel {
70
71         /**
72          * The widget's images.
73          */
74         private final Images images;
75
76         /**
77          * The image bundle for this widget's images that reuses images defined in
78          * other menus.
79          */
80         public interface Images extends GroupTreeView.Images {
81         }
82
83         private MenuItem pasteItem;
84
85         private boolean empty = false;;
86         
87         /**
88          * The widget's constructor.
89          *
90          * @param newImages the image bundle passed on by the parent object
91          */
92         public ToolsMenu(final Pithos app, Images newImages, TreeView selectedTree, Folder folder, final List<File> files) {
93                 // The popup's constructor's argument is a boolean specifying that it
94                 // auto-close itself when the user clicks outside of it.
95                 super(true);
96                 setAnimationEnabled(true);
97                 images = newImages;
98         MenuBar contextMenu = new MenuBar(true);
99
100         if (folder != null) {
101                 Boolean[] permissions = folder.getPermissions().get(app.getUsername());
102                 boolean canWrite = folder.getOwner().equals(app.getUsername()) || (permissions!= null && permissions[1] != null && permissions[1]);
103                 boolean isFolderTreeSelected = selectedTree.equals(app.getFolderTreeView());
104                 boolean otherSharedTreeSelected = selectedTree.equals(app.getOtherSharedTreeView());
105                 
106                 if (isFolderTreeSelected || otherSharedTreeSelected) {
107                         MenuItem refresh = new MenuItem("<span id = 'folderContextMenu.refresh'>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(app, this, folder));
108                         contextMenu.addItem(refresh);
109                 }
110         
111                 if (!folder.isInTrash()) {
112                         if (canWrite) {
113                                 MenuItem newFolder = new MenuItem("<span id = 'folderContextMenu.newFolder'>" + AbstractImagePrototype.create(newImages.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(app, this, folder, images));
114                                 contextMenu.addItem(newFolder);
115
116                                 if (isFolderTreeSelected) {
117                                         MenuItem cut = null;
118                                         if (files != null && !files.isEmpty()) {
119                                                         cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut files</span>", true, new CutCommand(app, this, files));
120                                         }
121                                         else if (!folder.isContainer()) {
122                                             cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut folder</span>", true, new CutCommand(app, this, folder));
123                                         }
124                                         if (cut != null)
125                                                 contextMenu.addItem(cut);
126                                 }
127                         }
128         
129                         MenuItem copy = null;
130                         if (files != null && !files.isEmpty())
131                                 copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy files</span>", true, new CopyCommand(app, this, files));
132                         else
133                                 copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy folder</span>", true, new CopyCommand(app, this, folder));
134                         contextMenu.addItem(copy);
135                 
136                         if (canWrite) {
137                                 if (!app.getClipboard().isEmpty()) {
138                                         Object item = app.getClipboard().getItem();
139                                         boolean showPaste = false;
140                                         if (item instanceof List) {
141                                                 List<File> _files = (List<File>) item;
142                                                 if (_files.get(0).getOwner().equals(folder.getOwner()))
143                                                         showPaste = true;
144                                         }
145                                         else {
146                                                 Folder f = (Folder) item;
147                                                 if (f.getOwner().equals(folder.getOwner()))
148                                                         showPaste = true;
149                                         }
150                                         if (showPaste) {
151                                             pasteItem = new MenuItem("<span id = 'folderContextMenu.paste'>" + AbstractImagePrototype.create(newImages.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(app, this, folder));
152                                             contextMenu.addItem(pasteItem);
153                                         }
154                                 }
155         
156                                     if (isFolderTreeSelected) {
157                                         MenuItem moveToTrash = null;
158                                         if (files != null && !files.isEmpty()) {
159                                                 moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move files to Trash</span>", true, new ToTrashCommand(app, this, files));
160                                         }
161                                         else if (!folder.isContainer()) {
162                                                 moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move folder to Trash</span>", true, new ToTrashCommand(app, this, folder));
163                                         }
164                                         if (moveToTrash != null)
165                                                 contextMenu.addItem(moveToTrash);
166                                 
167                                         MenuItem delete = null;
168                                         if (files != null && !files.isEmpty()) {
169                                                 delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete files</span>", true, new DeleteCommand(app, this, files, MessagePanel.images));
170                                         }
171                                         else if (!folder.isContainer())
172                                                 delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete folder</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
173                                         if (delete != null)
174                                                 contextMenu.addItem(delete);
175                                 
176                                         MenuItem properties = null;
177                                         if (files != null && files.size() == 1)
178                                                 properties = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;File properties</span>", true, new PropertiesCommand(app, this, files, images, 0));
179                                         else if (!folder.isContainer())
180                                                 properties = new MenuItem("<span id = 'folderContextMenu.properties'>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Folder properties</span>", true, new PropertiesCommand(app, this, folder, newImages, 0));
181                                         if (properties != null)
182                                                 contextMenu.addItem(properties);
183                                     }
184                                 if (files != null) {
185                                             contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.download()).getHTML() + "&nbsp;Download</span>", true, new Command() {
186                                                         
187                                                         @Override
188                                                         public void execute() {
189                                                         for (File f: files)
190                                                                 Window.open(app.getApiPath() + files.get(0).getOwner() + files.get(0).getUri() + "?X-Auth-Token=" + app.getToken(), "_blank", "");
191                                                         }
192                                                 }));
193                                 }
194                         }
195                 }
196                 else {
197                         if (!folder.isTrash()) {
198                                 MenuItem restore = null;
199                                 if (files != null && !files.isEmpty())
200                                         restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, files));
201                                 else
202                                         restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, folder));
203                                 contextMenu.addItem(restore);
204         
205                                 MenuItem delete = new MenuItem("<span id = 'folderContextMenu.delete'>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
206                                 contextMenu.addItem(delete);
207                         }
208                         else {
209                                 MenuItem emptyTrash = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(app, this));
210                                 contextMenu.addItem(emptyTrash);
211                         }
212                 }
213         }
214         else {
215                 Object o = app.getGroupTreeView().getSelected();
216                 if (o != null) {
217                         if (o instanceof User) {
218                         MenuItem removeUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Remove User</span>", true, new RemoveUserCommand(app, this, (User) o));
219                         contextMenu.addItem(removeUser);
220                         }
221                         else if (o instanceof Group) {
222                         MenuItem addUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.user()).getHTML() + "&nbsp;Add User</span>", true, new AddUserCommand(app, this, (Group) o));
223                         contextMenu.addItem(addUser);
224                 
225                         MenuItem deleteGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Delete Group</span>", true, new DeleteGroupCommand(app, this, (Group) o));
226                         contextMenu.addItem(deleteGroup);
227                         }
228                         else {
229                         MenuItem createGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.group()).getHTML() + "&nbsp;Create Group</span>", true, new CreateGroupCommand(app, this));
230                         contextMenu.addItem(createGroup);
231                         }
232                 }
233                 else
234                         empty = true;
235         }
236                 add(contextMenu);
237         }
238         
239         public boolean isEmpty() {
240                 return empty;
241         }
242 }