2 * Copyright 2007, 2008, 2009, 2010 Electronic Business Systems Ltd.
4 * This file is part of GSS.
6 * GSS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.commands.EmptyTrashCommand;
22 import gr.ebs.gss.client.commands.NewFolderCommand;
23 import gr.ebs.gss.client.commands.PropertiesCommand;
24 import gr.ebs.gss.client.commands.RefreshCommand;
25 import gr.ebs.gss.client.commands.UploadFileCommand;
26 import gr.ebs.gss.client.rest.RestCommand;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.GroupUserResource;
29 import gr.ebs.gss.client.rest.resource.OtherUserResource;
30 import gr.ebs.gss.client.rest.resource.OthersResource;
31 import gr.ebs.gss.client.rest.resource.RestResource;
32 import gr.ebs.gss.client.rest.resource.SharedResource;
33 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
34 import gr.ebs.gss.client.rest.resource.TrashResource;
36 import java.util.List;
38 import com.google.gwt.event.dom.client.ClickEvent;
39 import com.google.gwt.event.dom.client.ClickHandler;
40 import com.google.gwt.http.client.URL;
41 import com.google.gwt.resources.client.ClientBundle;
42 import com.google.gwt.resources.client.ImageResource;
43 import com.google.gwt.user.client.Command;
44 import com.google.gwt.user.client.ui.AbstractImagePrototype;
45 import com.google.gwt.user.client.ui.MenuBar;
46 import com.google.gwt.user.client.ui.MenuItem;
47 import com.google.gwt.user.client.ui.PopupPanel;
48 import com.google.gwt.user.client.ui.TreeItem;
51 * The 'File' menu implementation.
53 public class FileMenu extends PopupPanel implements ClickHandler {
56 * The widget's images.
58 private final Images images;
61 * An image bundle for this widgets images.
63 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
65 @Source("gr/ebs/gss/resources/folder_new.png")
66 ImageResource folderNew();
68 @Source("gr/ebs/gss/resources/folder_outbox.png")
69 ImageResource fileUpdate();
71 @Source("gr/ebs/gss/resources/view_text.png")
72 ImageResource viewText();
75 @Source("gr/ebs/gss/resources/folder_inbox.png")
76 ImageResource download();
78 @Source("gr/ebs/gss/resources/trashcan_empty.png")
79 ImageResource emptyTrash();
81 @Source("gr/ebs/gss/resources/internet.png")
82 ImageResource sharing();
84 @Source("gr/ebs/gss/resources/refresh.png")
85 ImageResource refresh();
88 final MenuBar contextMenu = new MenuBar(true);
91 * The widget's constructor.
93 * @param _images the image bundle passed on by the parent object
95 public FileMenu(final Images _images) {
96 // The popup's constructor's argument is a boolean specifying that it
97 // auto-close itself when the user clicks outside of it.
99 setAnimationEnabled(true);
106 public void onClick(ClickEvent event) {
107 final FileMenu menu = new FileMenu(images);
108 final int left = event.getRelativeElement().getAbsoluteLeft();
109 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
110 menu.setPopupPosition(left, top);
117 * Do some validation before downloading a file.
119 void preDownloadCheck() {
120 Object selection = GSS.get().getCurrentSelection();
121 if (selection == null || !(selection instanceof FileResource)) {
122 GSS.get().displayError("You have to select a file first");
128 * Create a download link for the respective menu item, if the currently
129 * selected object is a file.
131 * @param link a String array with two elements that is modified so that the
132 * first position contains the opening tag and the second one the
134 * @param forceDownload If true, link will be such that browser should ask for filename
137 void createDownloadLink(String[] link, boolean forceDownload) {
138 String downloadURL = getDownloadURL();
139 if (!downloadURL.isEmpty()) {
140 link[0] = "<a class='hidden-link' href='" + downloadURL
141 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
146 public String getDownloadURL() {
148 Object selection = app.getCurrentSelection();
149 if (selection != null && selection instanceof FileResource) {
150 FileResource file = (FileResource) selection;
151 return getDownloadURL(file);
156 public String getDownloadURL(FileResource file) {
159 String dateString = RestCommand.getDate();
160 String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
161 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
162 return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
167 public MenuBar createMenu() {
168 contextMenu.clearItems();
169 contextMenu.setAutoOpen(false);
170 final Command downloadCmd = new Command() {
173 public void execute() {
179 RestResource selectedItem = GSS.get().getTreeView().getSelection();
180 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
181 boolean propertiesVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource
182 //|| folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource
183 || GSS.get().getCurrentSelection() instanceof List));
184 boolean newFolderVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
185 boolean uploadVisible = !(selectedItem != null && (selectedItem instanceof TrashResource || selectedItem instanceof TrashFolderResource || selectedItem instanceof SharedResource || selectedItem instanceof OthersResource || selectedItem instanceof OtherUserResource));
186 if(newFolderVisible){
187 MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
188 newFolderItem.getElement().setId("topMenu.file.newFolder");
189 contextMenu.addItem(newFolderItem);
192 MenuItem uploadItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this));
193 uploadItem.getElement().setId("topMenu.file.upload");
194 contextMenu.addItem(uploadItem);
196 if (downloadVisible) {
197 String[] link = {"", ""};
198 createDownloadLink(link, false);
200 MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
201 contextMenu.addItem(downloadItem);
202 downloadItem.getElement().setId("topMenu.file.download");
204 createDownloadLink(link, true);
206 MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
207 saveAsItem.getElement().setId("topMenu.file.saveAs");
208 contextMenu.addItem(saveAsItem);
210 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
211 emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
212 contextMenu.addItem(emptyTrashItem);
214 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
215 refreshItem.getElement().setId("topMenu.file.refresh");
216 contextMenu.addItem(refreshItem);
218 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1));
219 sharingItem.getElement().setId("topMenu.file.sharing");
220 contextMenu.addItem(sharingItem)
221 .setVisible(propertiesVisible);
223 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0));
224 propertiesItem.getElement().setId("topMenu.file.properties");
225 contextMenu.addItem(propertiesItem)
226 .setVisible(propertiesVisible);