2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
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;
51 import java.util.List;
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;
65 * The 'File' menu implementation.
67 public class FileMenu extends PopupPanel implements ClickHandler {
70 * The widget's images.
72 private final Images images;
75 * An image bundle for this widgets images.
77 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
79 @Source("gr/grnet/pithos/resources/folder_new.png")
80 ImageResource folderNew();
82 @Source("gr/grnet/pithos/resources/folder_outbox.png")
83 ImageResource fileUpdate();
85 @Source("gr/grnet/pithos/resources/view_text.png")
86 ImageResource viewText();
89 @Source("gr/grnet/pithos/resources/folder_inbox.png")
90 ImageResource download();
92 @Source("gr/grnet/pithos/resources/trashcan_empty.png")
93 ImageResource emptyTrash();
95 @Source("gr/grnet/pithos/resources/internet.png")
96 ImageResource sharing();
98 @Source("gr/grnet/pithos/resources/refresh.png")
99 ImageResource refresh();
102 final MenuBar contextMenu = new MenuBar(true);
105 * The widget's constructor.
107 * @param _images the image bundle passed on by the parent object
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.
113 setAnimationEnabled(true);
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);
131 * Do some validation before downloading a file.
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");
142 * Create a download link for the respective menu item, if the currently
143 * selected object is a file.
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
148 * @param forceDownload If true, link will be such that browser should ask for filename
151 void createDownloadLink(String[] link, boolean forceDownload) {
152 String downloadURL = getDownloadURL();
153 if (!downloadURL.isEmpty()) {
154 link[0] = "<a class='hidden-link' href='" + downloadURL
155 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
160 public String getDownloadURL() {
162 Object selection = app.getCurrentSelection();
163 if (selection != null && selection instanceof FileResource) {
164 FileResource file = (FileResource) selection;
165 return getDownloadURL(file);
170 public String getDownloadURL(FileResource file) {
173 return file.getUri();
178 public MenuBar createMenu() {
179 contextMenu.clearItems();
180 contextMenu.setAutoOpen(false);
181 final Command downloadCmd = new Command() {
184 public void execute() {
189 CellTreeView treeView = GSS.get().getTreeView();
190 if (treeView == null)
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() + " New Folder</span>", true, new NewFolderCommand(this, images));
201 // newFolderItem.getElement().setId("topMenu.file.newFolder");
202 // contextMenu.addItem(newFolderItem);
205 MenuItem uploadItem = new MenuItem("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this, null));
206 contextMenu.addItem(uploadItem);
208 if (downloadVisible) {
209 String[] link = {"", ""};
210 createDownloadLink(link, false);
212 MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
213 contextMenu.addItem(downloadItem);
215 createDownloadLink(link, true);
217 MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
218 contextMenu.addItem(saveAsItem);
220 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
221 emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
222 contextMenu.addItem(emptyTrashItem);
224 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
225 refreshItem.getElement().setId("topMenu.file.refresh");
226 contextMenu.addItem(refreshItem);
228 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1));
229 sharingItem.getElement().setId("topMenu.file.sharing");
230 contextMenu.addItem(sharingItem)
231 .setVisible(propertiesVisible);
233 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0));
234 propertiesItem.getElement().setId("topMenu.file.properties");
235 contextMenu.addItem(propertiesItem)
236 .setVisible(propertiesVisible);