Configurable service URLs via the otherProperties JS mechanism
[pithos-web-client] / src / gr / grnet / pithos / web / client / FileContextMenu.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.CopyCommand;
38 import gr.grnet.pithos.web.client.commands.CutCommand;
39 import gr.grnet.pithos.web.client.commands.DeleteCommand;
40 import gr.grnet.pithos.web.client.commands.PasteCommand;
41 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
42 import gr.grnet.pithos.web.client.commands.RestoreTrashCommand;
43 import gr.grnet.pithos.web.client.commands.ToTrashCommand;
44 import gr.grnet.pithos.web.client.foldertree.File;
45 import gr.grnet.pithos.web.client.foldertree.Folder;
46
47 import java.util.List;
48
49 import com.google.gwt.resources.client.ClientBundle;
50 import com.google.gwt.resources.client.ImageResource;
51 import com.google.gwt.user.client.Command;
52 import com.google.gwt.user.client.Window;
53 import com.google.gwt.user.client.ui.AbstractImagePrototype;
54 import com.google.gwt.user.client.ui.MenuBar;
55 import com.google.gwt.user.client.ui.MenuItem;
56 import com.google.gwt.user.client.ui.PopupPanel;
57
58 /**
59  * The 'File Context' menu implementation.
60  */
61 public class FileContextMenu extends PopupPanel {
62
63         /**
64          * The widget's images.
65          */
66         private final Images images;
67
68         private MenuItem cutItem;
69
70         private MenuItem copyItem;
71
72         private MenuItem pasteItem;
73
74         private MenuItem updateItem;
75
76         private MenuItem sharingItem;
77
78         private MenuItem propItem;
79
80         private MenuItem trashItem;
81
82         private MenuItem deleteItem;
83
84         private MenuItem downloadItem;
85
86         private MenuItem saveAsItem;
87
88         /**
89          * The image bundle for this widget's images that reuses images defined in
90          * other menus.
91          */
92         public interface Images extends ClientBundle {
93
94                 @Source("gr/grnet/pithos/resources/mimetypes/document.png")
95                 ImageResource fileContextMenu();
96
97                 @Source("gr/grnet/pithos/resources/doc_versions.png")
98                 ImageResource versions();
99
100                 @Source("gr/grnet/pithos/resources/groups22.png")
101                 ImageResource sharing();
102
103                 @Source("gr/grnet/pithos/resources/border_remove.png")
104                 ImageResource unselectAll();
105
106                 @Source("gr/grnet/pithos/resources/demo.png")
107                 ImageResource viewImage();
108
109         @Source("gr/grnet/pithos/resources/folder_new.png")
110         ImageResource folderNew();
111
112         @Source("gr/grnet/pithos/resources/folder_outbox.png")
113         ImageResource fileUpdate();
114
115         @Source("gr/grnet/pithos/resources/view_text.png")
116         ImageResource viewText();
117
118         @Source("gr/grnet/pithos/resources/folder_inbox.png")
119         ImageResource download();
120
121         @Source("gr/grnet/pithos/resources/trash.png")
122         ImageResource emptyTrash();
123
124         @Source("gr/grnet/pithos/resources/refresh.png")
125         ImageResource refresh();
126
127         /**
128          * Will bundle the file 'editcut.png' residing in the package
129          * 'gr.grnet.pithos.web.resources'.
130          *
131          * @return the image prototype
132          */
133         @Source("gr/grnet/pithos/resources/editcut.png")
134         ImageResource cut();
135
136         /**
137          * Will bundle the file 'editcopy.png' residing in the package
138          * 'gr.grnet.pithos.web.resources'.
139          *
140          * @return the image prototype
141          */
142         @Source("gr/grnet/pithos/resources/editcopy.png")
143         ImageResource copy();
144
145         /**
146          * Will bundle the file 'editpaste.png' residing in the package
147          * 'gr.grnet.pithos.web.resources'.
148          *
149          * @return the image prototype
150          */
151         @Source("gr/grnet/pithos/resources/editpaste.png")
152         ImageResource paste();
153
154         /**
155          * Will bundle the file 'editdelete.png' residing in the package
156          * 'gr.grnet.pithos.web.resources'.
157          *
158          * @return the image prototype
159          */
160         @Source("gr/grnet/pithos/resources/editdelete.png")
161         ImageResource delete();
162
163         /**
164          * Will bundle the file 'translate.png' residing in the package
165          * 'gr.grnet.pithos.web.resources'.
166          *
167          * @return the image prototype
168          */
169         @Source("gr/grnet/pithos/resources/translate.png")
170         ImageResource selectAll();
171         
172         @Source("gr/grnet/pithos/resources/internet.png")
173         ImageResource internet();
174     }
175
176         public static native String getDate()/*-{
177                 return (new Date()).toUTCString();
178         }-*/;
179
180         /**
181          * The widget's constructor.
182          *
183          * @param newImages the image bundle passed on by the parent object
184          */
185         public FileContextMenu(final Pithos app, Images newImages, TreeView selectedTree, Folder selectedFolder, final List<File> selectedFiles) {
186                 // The popup's constructor's argument is a boolean specifying that it
187                 // auto-close itself when the user clicks outside of it.
188                 super(true);
189                 images = newImages;
190         MenuBar contextMenu = new MenuBar(true);
191         Boolean[] permissions = null;
192         boolean canWrite = true;
193         if (selectedFolder != null) {
194                 permissions = selectedFolder.getPermissions().get(app.getUserID());
195                 canWrite = selectedFolder.getOwnerID().equals(app.getUserID()) || (permissions!= null && permissions[1] != null && permissions[1]);
196                 }
197         else {
198                 for (File f : selectedFiles) {
199                         permissions = f.getPermissions().get(app.getUserID());
200                         canWrite &= (f.getOwnerID().equals(app.getUserID()) || (permissions!= null && permissions[1] != null && permissions[1]));
201                 }
202         }
203         boolean isFolderTreeSelected = selectedTree.equals(app.getFolderTreeView());
204         boolean isMysharedSelected = selectedTree.equals(app.getMySharedTreeView());
205
206         Pithos.LOG("FileContextMenu(), selectedFolder=", selectedFolder);
207         Pithos.LOG("FileContextMenu(), isFolderTreeSelected=", isFolderTreeSelected);
208         Pithos.LOG("FileContextMenu(), isMysharedSelected=", isMysharedSelected);
209
210         if (selectedFolder != null) {
211                     if (!selectedFolder.isInTrash()) {
212                         if (canWrite && app.getClipboard().hasFiles() && !isMysharedSelected) {
213                                 pasteItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(app, this, selectedFolder));
214                                 contextMenu.addItem(pasteItem);
215                         }
216                 
217                         if (isFolderTreeSelected || isMysharedSelected) {
218                                         cutItem = new MenuItem("<span id='fileContextMenu.cut'>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut</span>", true, new CutCommand(app, this, selectedFiles));
219                             contextMenu.addItem(cutItem);
220                         }
221                 
222                                 copyItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(app, this, selectedFiles));
223                         contextMenu.addItem(copyItem);
224                 
225                         if (isFolderTreeSelected || isMysharedSelected) {
226                                         trashItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(app, this, selectedFiles));
227                             contextMenu.addItem(trashItem);
228                         }
229                     }
230                     else {
231                                 MenuItem restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, selectedFiles));
232                                 contextMenu.addItem(restore);
233
234                 deleteItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(app, this, selectedFiles, MessagePanel.images));
235                 contextMenu.addItem(deleteItem);
236                     }
237         }
238
239         if (selectedFolder != null && !selectedFolder.isInTrash()) {
240                 if ((isFolderTreeSelected || isMysharedSelected) && selectedFiles.size() == 1) {
241                         contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(app, this, selectedFiles, PropertiesCommand.PROPERTIES)));
242                         contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Share</span>", true, new PropertiesCommand(app, this, selectedFiles, PropertiesCommand.PERMISSIONS)));
243                         contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Versions</span>", true, new PropertiesCommand(app, this, selectedFiles, PropertiesCommand.VERSIONS)));
244                 }
245
246         }
247             contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.download()).getHTML() + "&nbsp;Download</span>", true, new Command() {
248                         
249                         @Override
250                         public void execute() {
251                                 for (File f : selectedFiles)
252                                         Window.open(Pithos.getStorageAPIURL() + f.getOwnerID() + f.getUri(), "_blank", "");
253                         }
254                 }));
255
256                 add(contextMenu);
257         }
258 }