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