Started implementation of new folder command
[pithos] / web_client / src / gr / grnet / pithos / web / client / FileMenu.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.EmptyTrashCommand;
38 import gr.grnet.pithos.web.client.commands.NewFolderCommand;
39 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
40 import gr.grnet.pithos.web.client.commands.RefreshCommand;
41 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
42 import gr.grnet.pithos.web.client.rest.RestCommand;
43 import gr.grnet.pithos.web.client.rest.resource.FileResource;
44 import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
45 import gr.grnet.pithos.web.client.rest.resource.OthersResource;
46 import gr.grnet.pithos.web.client.rest.resource.RestResource;
47 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
48 import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
49 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
50
51 import java.util.List;
52
53 import com.google.gwt.event.dom.client.ClickEvent;
54 import com.google.gwt.event.dom.client.ClickHandler;
55 import com.google.gwt.http.client.URL;
56 import com.google.gwt.resources.client.ClientBundle;
57 import com.google.gwt.resources.client.ImageResource;
58 import com.google.gwt.user.client.Command;
59 import com.google.gwt.user.client.ui.AbstractImagePrototype;
60 import com.google.gwt.user.client.ui.MenuBar;
61 import com.google.gwt.user.client.ui.MenuItem;
62 import com.google.gwt.user.client.ui.PopupPanel;
63
64 /**
65  * The 'File' menu implementation.
66  */
67 public class FileMenu extends PopupPanel implements ClickHandler {
68
69         /**
70          * The widget's images.
71          */
72         private final Images images;
73
74         /**
75          * An image bundle for this widgets images.
76          */
77         public interface Images extends ClientBundle,FilePropertiesDialog.Images {
78
79                 @Source("gr/grnet/pithos/resources/folder_new.png")
80                 ImageResource folderNew();
81
82                 @Source("gr/grnet/pithos/resources/folder_outbox.png")
83                 ImageResource fileUpdate();
84
85                 @Source("gr/grnet/pithos/resources/view_text.png")
86                 ImageResource viewText();
87
88                 @Override
89                 @Source("gr/grnet/pithos/resources/folder_inbox.png")
90                 ImageResource download();
91
92                 @Source("gr/grnet/pithos/resources/trashcan_empty.png")
93                 ImageResource emptyTrash();
94
95                 @Source("gr/grnet/pithos/resources/internet.png")
96                 ImageResource sharing();
97
98                 @Source("gr/grnet/pithos/resources/refresh.png")
99                 ImageResource refresh();
100 }
101
102         final MenuBar contextMenu = new MenuBar(true);
103
104         /**
105          * The widget's constructor.
106          *
107          * @param _images the image bundle passed on by the parent object
108          */
109         public FileMenu(final Images _images) {
110                 // The popup's constructor's argument is a boolean specifying that it
111                 // auto-close itself when the user clicks outside of it.
112                 super(true);
113                 setAnimationEnabled(true);
114                 images = _images;
115                 add(contextMenu);
116
117         }
118
119         @Override
120         public void onClick(ClickEvent event) {
121                 final FileMenu menu = new FileMenu(images);
122                 final int left = event.getRelativeElement().getAbsoluteLeft();
123                 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
124                 menu.setPopupPosition(left, top);
125                 menu.show();
126
127         }
128
129
130         /**
131          * Do some validation before downloading a file.
132          */
133         void preDownloadCheck() {
134                 Object selection = GSS.get().getCurrentSelection();
135                 if (selection == null || !(selection instanceof FileResource)) {
136                         GSS.get().displayError("You have to select a file first");
137                         return;
138                 }
139         }
140
141         /**
142          * Create a download link for the respective menu item, if the currently
143          * selected object is a file.
144          *
145          * @param link a String array with two elements that is modified so that the
146          *            first position contains the opening tag and the second one the
147          *            closing tag
148          * @param forceDownload If true, link will be such that browser should ask for filename
149          *                              and save location
150          */
151         void createDownloadLink(String[] link, boolean forceDownload) {
152                 String downloadURL = getDownloadURL();
153                 if (!downloadURL.isEmpty()) {
154                         link[0] = "<a id ='topMenu.file.download' class='hidden-link' href='" + downloadURL
155                                         + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
156                         link[1] = "</a>";
157                 }
158         }
159
160         public String getDownloadURL() {
161                 GSS app = GSS.get();
162                 Object selection = app.getCurrentSelection();
163                 if (selection != null && selection instanceof FileResource) {
164                         FileResource file = (FileResource) selection;
165                         return getDownloadURL(file);
166                 }
167                 return "";
168         }
169
170         public String getDownloadURL(FileResource file) {
171                 GSS app = GSS.get();
172                 if (file != null) {
173                         return file.getUri();
174                 }
175                 return "";
176         }
177
178         public MenuBar createMenu() {
179                 contextMenu.clearItems();
180                 contextMenu.setAutoOpen(false);
181                 final Command downloadCmd = new Command() {
182
183                         @Override
184                         public void execute() {
185                                 hide();
186                                 preDownloadCheck();
187                         }
188                 };
189         CellTreeView treeView = GSS.get().getTreeView();
190         if (treeView == null)
191             return contextMenu;
192                 RestResource selectedItem = treeView.getSelection();
193                 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
194                 boolean propertiesVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource 
195                                         //|| folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource 
196                                         || GSS.get().getCurrentSelection() instanceof List));
197                 boolean newFolderVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
198                 boolean uploadVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
199                 if(newFolderVisible){
200 //                      MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));
201 //                      newFolderItem.getElement().setId("topMenu.file.newFolder");
202 //                      contextMenu.addItem(newFolderItem);
203                 }
204                 if(uploadVisible){
205                         MenuItem uploadItem = new MenuItem("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));                        
206                         contextMenu.addItem(uploadItem);
207                 }
208                 if (downloadVisible) {
209                         String[] link = {"", ""};
210                         createDownloadLink(link, false);
211                         
212                         MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);
213                         contextMenu.addItem(downloadItem);
214                         
215                         createDownloadLink(link, true);
216                         
217                         MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + "&nbsp;Save As" + link[1] + "</span>", true, downloadCmd);                 
218                         contextMenu.addItem(saveAsItem);
219                 }
220                 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));
221                 emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
222                 contextMenu.addItem(emptyTrashItem);
223                 
224                 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
225                 refreshItem.getElement().setId("topMenu.file.refresh");
226                 contextMenu.addItem(refreshItem);
227                 
228                 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
229                 sharingItem.getElement().setId("topMenu.file.sharing");
230                 contextMenu.addItem(sharingItem)
231                                         .setVisible(propertiesVisible);
232                 
233                 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0));
234                 propertiesItem.getElement().setId("topMenu.file.properties");
235                 contextMenu.addItem(propertiesItem)
236                                         .setVisible(propertiesVisible);
237                 return contextMenu;
238         }
239
240 }