New button "Add everybody" to file Share dialog
[pithos-web-client] / src / gr / grnet / pithos / web / client / ToolsMenu.java
1 /*
2  * Copyright 2011-2013 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.*;
38 import gr.grnet.pithos.web.client.commands.PurgeContainerCommand;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.Folder;
41 import gr.grnet.pithos.web.client.grouptree.Group;
42 import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
43 import gr.grnet.pithos.web.client.grouptree.User;
44
45 import java.util.List;
46
47 import com.google.gwt.user.client.Command;
48 import com.google.gwt.user.client.Window;
49 import com.google.gwt.user.client.ui.AbstractImagePrototype;
50 import com.google.gwt.user.client.ui.MenuBar;
51 import com.google.gwt.user.client.ui.MenuItem;
52 import com.google.gwt.user.client.ui.PopupPanel;
53
54 /**
55  * The 'Folder Context' menu implementation.
56  */
57 public class ToolsMenu extends PopupPanel {
58
59         /**
60          * The widget's images.
61          */
62         private final Images images;
63
64         /**
65          * The image bundle for this widget's images that reuses images defined in
66          * other menus.
67          */
68         public interface Images extends GroupTreeView.Images {
69         }
70
71         private MenuItem pasteItem;
72
73         private boolean empty = true;
74         
75         /**
76          * The widget's constructor.
77          *
78          * @param newImages the image bundle passed on by the parent object
79          */
80         public ToolsMenu(final Pithos app, Images newImages, TreeView selectedTree, Folder folder, final List<File> files) {
81                 // The popup's constructor's argument is a boolean specifying that it
82                 // auto-close itself when the user clicks outside of it.
83                 super(true);
84                 images = newImages;
85         MenuBar contextMenu = new MenuBar(true);
86
87         if (folder != null) {
88                 Boolean[] permissions = folder.getPermissions().get(app.getUserID());
89                 boolean canWrite = folder.getOwnerID().equals(app.getUserID()) || (permissions!= null && permissions[1] != null && permissions[1]);
90                 boolean isFolderTreeSelected = selectedTree.equals(app.getFolderTreeView());
91                 boolean isMysharedTreeSelected = app.isMySharedSelected();
92                 
93                 if (!folder.isInTrash()) {
94                         if (canWrite) {
95
96                                 if (isFolderTreeSelected || isMysharedTreeSelected) {
97                                         MenuItem cut = null;
98                                         if (files != null && !files.isEmpty()) {
99                                                         cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut files</span>", true, new CutCommand(app, this, files));
100                                         }
101                                         else if (!folder.isContainer()) {
102                                             cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut folder</span>", true, new CutCommand(app, this, folder));
103                                         }
104                                         if (cut != null) {
105                                                 contextMenu.addItem(cut);
106                                                 empty = false;
107                                         }
108                                 }
109                         }
110         
111                         MenuItem copy = null;
112                         if (files != null && !files.isEmpty())
113                                 copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy files</span>", true, new CopyCommand(app, this, files));
114                         else if (isFolderTreeSelected && !folder.isContainer())
115                                 copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy folder</span>", true, new CopyCommand(app, this, folder));
116                         if (copy != null) {
117                                 contextMenu.addItem(copy);
118                                 empty = false;
119                         }
120                 
121                         if (canWrite) {
122                                 if (!isMysharedTreeSelected && !app.getClipboard().isEmpty()) {
123                                         Object item = app.getClipboard().getItem();
124                                         boolean showPaste = false;
125                                         if (item instanceof List) {
126                                                 @SuppressWarnings("unchecked")
127                                                         List<File> _files = (List<File>) item;
128                                                 if (_files.get(0).getOwnerID().equals(folder.getOwnerID()))
129                                                         showPaste = true;
130                                         }
131                                         else {
132                                                 Folder f = (Folder) item;
133                                                 if (f.getOwnerID().equals(folder.getOwnerID()))
134                                                         showPaste = true;
135                                         }
136                                         if (showPaste) {
137                                             pasteItem = new MenuItem("<span id = 'folderContextMenu.paste'>" + AbstractImagePrototype.create(newImages.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(app, this, folder));
138                                             contextMenu.addItem(pasteItem);
139                                                 empty = false;
140                                         }
141                                 }
142         
143                                     if (isFolderTreeSelected || isMysharedTreeSelected) {
144                                         MenuItem moveToTrash = null;
145                                         if (files != null && !files.isEmpty()) {
146                                                 moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move files to Trash</span>", true, new ToTrashCommand(app, this, files));
147                                         }
148                                         else if (!folder.isContainer()) {
149                                                 moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move folder to Trash</span>", true, new ToTrashCommand(app, this, folder));
150                                         }
151                                         if (moveToTrash != null) {
152                                                 contextMenu.addItem(moveToTrash);
153                                                 empty = false;
154                                         }
155                                 
156                                         MenuItem delete = null;
157                                         if (files != null && !files.isEmpty()) {
158                                                 delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete files</span>", true, new DeleteCommand(app, this, files, MessagePanel.images));
159                                         }
160                                         else if (!folder.isContainer())
161                                                 delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete folder</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
162                                         if (delete != null) {
163                                                 contextMenu.addItem(delete);
164                                                 empty = false;
165                                         }
166                                     }
167                                     if (isFolderTreeSelected || isMysharedTreeSelected) {
168                                         if (files != null && files.size() == 1) {
169                                                 contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;File properties</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PROPERTIES)));
170                                                 contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.group()).getHTML() + "&nbsp;Share</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PERMISSIONS)));
171                                                 contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Versions</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.VERSIONS)));
172                                                 empty = false;
173                                         }
174                                         else if (!folder.isContainer()) {
175                                                 contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Folder properties</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PROPERTIES)));
176                                                 contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Folder sharing</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PERMISSIONS)));
177                                                 empty = false;
178                                         }
179                                     }
180                                 if (files != null && !files.isEmpty()) {
181                                             contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.download()).getHTML() + "&nbsp;Download</span>", true, new Command() {
182                                                         
183                                                         @Override
184                                                         public void execute() {
185                                                         for (File f: files)
186                                                                 Window.open(app.getApiPath() + f.getOwnerID() + f.getUri(), "_blank", "");
187                                                         }
188                                                 }));
189                                         empty = false;
190                                 }
191                                 if (isFolderTreeSelected && folder.isContainer()) {
192                                         MenuItem emptyContainer = new MenuItem(
193                             "<span>" + Const.PurgeContainer(folder.getName()) + "</span>",
194                             true,
195                             new PurgeContainerCommand(app, this, folder)
196                         );
197                                         contextMenu.addItem(emptyContainer);
198                                         empty = false;
199                                 }
200                         }
201                 }
202                 else {
203                         if (!folder.isTrash()) {
204                                 MenuItem restore = null;
205                                 if (files != null && !files.isEmpty())
206                                         restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, files));
207                                 else
208                                         restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, folder));
209                                 contextMenu.addItem(restore);
210
211                                 MenuItem delete = new MenuItem("<span id = 'folderContextMenu.delete'>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
212                                 contextMenu.addItem(delete);
213                                 empty = false;
214                         }
215                         else {
216                                 MenuItem emptyTrash = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new PurgeContainerCommand(app, this, folder));
217                                 contextMenu.addItem(emptyTrash);
218                                 empty = false;
219                         }
220                 }
221         }
222         else {
223                 Object o = app.getGroupTreeView().getSelected();
224                 if (o != null) {
225                         if (o instanceof User) {
226                         MenuItem removeUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Remove User</span>", true, new RemoveUserCommand(app, this, (User) o));
227                         contextMenu.addItem(removeUser);
228                                 empty = false;
229                         }
230                         else if (o instanceof Group) {
231                         MenuItem addUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.user()).getHTML() + "&nbsp;Add User</span>", true, new AddUserCommand(app, this, (Group) o));
232                         contextMenu.addItem(addUser);
233                 
234                         MenuItem deleteGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Delete Group</span>", true, new DeleteGroupCommand(app, this, (Group) o));
235                         contextMenu.addItem(deleteGroup);
236                                 empty = false;
237                         }
238                 }
239         }
240         MenuItem createGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.group()).getHTML() + "&nbsp;Create Group</span>", true, new CreateGroupCommand(app, this));
241         contextMenu.addItem(createGroup);
242         empty = false;
243                 add(contextMenu);
244         }
245         
246         public boolean isEmpty() {
247                 return empty;
248         }
249 }