2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
7 import gr.grnet.pithos.web.client.commands.NewFolderCommand;
8 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
9 import gr.grnet.pithos.web.client.commands.RefreshCommand;
10 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
11 import gr.grnet.pithos.web.client.rest.RestCommand;
12 import gr.grnet.pithos.web.client.rest.resource.FileResource;
13 import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
14 import gr.grnet.pithos.web.client.rest.resource.OthersResource;
15 import gr.grnet.pithos.web.client.rest.resource.RestResource;
16 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
17 import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
18 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
20 import java.util.List;
22 import com.google.gwt.event.dom.client.ClickEvent;
23 import com.google.gwt.event.dom.client.ClickHandler;
24 import com.google.gwt.http.client.URL;
25 import com.google.gwt.resources.client.ClientBundle;
26 import com.google.gwt.resources.client.ImageResource;
27 import com.google.gwt.user.client.Command;
28 import com.google.gwt.user.client.ui.AbstractImagePrototype;
29 import com.google.gwt.user.client.ui.MenuBar;
30 import com.google.gwt.user.client.ui.MenuItem;
31 import com.google.gwt.user.client.ui.PopupPanel;
34 * The 'File' menu implementation.
36 public class FileMenu extends PopupPanel implements ClickHandler {
39 * The widget's images.
41 private final Images images;
44 * An image bundle for this widgets images.
46 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
48 @Source("gr/grnet/pithos/resources/folder_new.png")
49 ImageResource folderNew();
51 @Source("gr/grnet/pithos/resources/folder_outbox.png")
52 ImageResource fileUpdate();
54 @Source("gr/grnet/pithos/resources/view_text.png")
55 ImageResource viewText();
58 @Source("gr/grnet/pithos/resources/folder_inbox.png")
59 ImageResource download();
61 @Source("gr/grnet/pithos/resources/trashcan_empty.png")
62 ImageResource emptyTrash();
64 @Source("gr/grnet/pithos/resources/internet.png")
65 ImageResource sharing();
67 @Source("gr/grnet/pithos/resources/refresh.png")
68 ImageResource refresh();
71 final MenuBar contextMenu = new MenuBar(true);
74 * The widget's constructor.
76 * @param _images the image bundle passed on by the parent object
78 public FileMenu(final Images _images) {
79 // The popup's constructor's argument is a boolean specifying that it
80 // auto-close itself when the user clicks outside of it.
82 setAnimationEnabled(true);
89 public void onClick(ClickEvent event) {
90 final FileMenu menu = new FileMenu(images);
91 final int left = event.getRelativeElement().getAbsoluteLeft();
92 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
93 menu.setPopupPosition(left, top);
100 * Do some validation before downloading a file.
102 void preDownloadCheck() {
103 Object selection = GSS.get().getCurrentSelection();
104 if (selection == null || !(selection instanceof FileResource)) {
105 GSS.get().displayError("You have to select a file first");
111 * Create a download link for the respective menu item, if the currently
112 * selected object is a file.
114 * @param link a String array with two elements that is modified so that the
115 * first position contains the opening tag and the second one the
117 * @param forceDownload If true, link will be such that browser should ask for filename
120 void createDownloadLink(String[] link, boolean forceDownload) {
121 String downloadURL = getDownloadURL();
122 if (!downloadURL.isEmpty()) {
123 link[0] = "<a id ='topMenu.file.download' class='hidden-link' href='" + downloadURL
124 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
129 public String getDownloadURL() {
131 Object selection = app.getCurrentSelection();
132 if (selection != null && selection instanceof FileResource) {
133 FileResource file = (FileResource) selection;
134 return getDownloadURL(file);
139 public String getDownloadURL(FileResource file) {
142 String dateString = RestCommand.getDate();
143 String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
144 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
145 return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
150 public MenuBar createMenu() {
151 contextMenu.clearItems();
152 contextMenu.setAutoOpen(false);
153 final Command downloadCmd = new Command() {
156 public void execute() {
161 CellTreeView treeView = GSS.get().getTreeView();
162 if (treeView == null)
164 RestResource selectedItem = treeView.getSelection();
165 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
166 boolean propertiesVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource
167 //|| folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource
168 || GSS.get().getCurrentSelection() instanceof List));
169 boolean newFolderVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
170 boolean uploadVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
171 if(newFolderVisible){
172 MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
173 newFolderItem.getElement().setId("topMenu.file.newFolder");
174 contextMenu.addItem(newFolderItem);
177 MenuItem uploadItem = new MenuItem("<span id='topMenu.file.upload'>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this));
178 contextMenu.addItem(uploadItem);
180 if (downloadVisible) {
181 String[] link = {"", ""};
182 createDownloadLink(link, false);
184 MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
185 contextMenu.addItem(downloadItem);
187 createDownloadLink(link, true);
189 MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
190 contextMenu.addItem(saveAsItem);
192 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
193 emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
194 contextMenu.addItem(emptyTrashItem);
196 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
197 refreshItem.getElement().setId("topMenu.file.refresh");
198 contextMenu.addItem(refreshItem);
200 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1));
201 sharingItem.getElement().setId("topMenu.file.sharing");
202 contextMenu.addItem(sharingItem)
203 .setVisible(propertiesVisible);
205 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0));
206 propertiesItem.getElement().setId("topMenu.file.properties");
207 contextMenu.addItem(propertiesItem)
208 .setVisible(propertiesVisible);