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;
30 import com.google.gwt.event.dom.client.ClickEvent;
31 import com.google.gwt.event.dom.client.ClickHandler;
32 import com.google.gwt.http.client.URL;
33 import com.google.gwt.resources.client.ClientBundle;
34 import com.google.gwt.resources.client.ImageResource;
35 import com.google.gwt.user.client.Command;
36 import com.google.gwt.user.client.ui.AbstractImagePrototype;
37 import com.google.gwt.user.client.ui.MenuBar;
38 import com.google.gwt.user.client.ui.MenuItem;
39 import com.google.gwt.user.client.ui.PopupPanel;
40 import com.google.gwt.user.client.ui.TreeItem;
43 * The 'File' menu implementation.
45 public class FileMenu extends PopupPanel implements ClickHandler {
48 * The widget's images.
50 private final Images images;
53 * An image bundle for this widgets images.
55 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
57 @Source("gr/ebs/gss/resources/folder_new.png")
58 ImageResource folderNew();
60 @Source("gr/ebs/gss/resources/folder_outbox.png")
61 ImageResource fileUpdate();
63 @Source("gr/ebs/gss/resources/view_text.png")
64 ImageResource viewText();
67 @Source("gr/ebs/gss/resources/folder_inbox.png")
68 ImageResource download();
70 @Source("gr/ebs/gss/resources/trashcan_empty.png")
71 ImageResource emptyTrash();
73 @Source("gr/ebs/gss/resources/internet.png")
74 ImageResource sharing();
76 @Source("gr/ebs/gss/resources/refresh.png")
77 ImageResource refresh();
80 final MenuBar contextMenu = new MenuBar(true);
83 * The widget's constructor.
85 * @param _images the image bundle passed on by the parent object
87 public FileMenu(final Images _images) {
88 // The popup's constructor's argument is a boolean specifying that it
89 // auto-close itself when the user clicks outside of it.
91 setAnimationEnabled(true);
98 public void onClick(ClickEvent event) {
99 final FileMenu menu = new FileMenu(images);
100 final int left = event.getRelativeElement().getAbsoluteLeft();
101 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
102 menu.setPopupPosition(left, top);
109 * Do some validation before downloading a file.
111 void preDownloadCheck() {
112 Object selection = GSS.get().getCurrentSelection();
113 if (selection == null || !(selection instanceof FileResource)) {
114 GSS.get().displayError("You have to select a file first");
120 * Create a download link for the respective menu item, if the currently
121 * selected object is a file.
123 * @param link a String array with two elements that is modified so that the
124 * first position contains the opening tag and the second one the
126 * @param forceDownload If true, link will be such that browser should ask for filename
129 void createDownloadLink(String[] link, boolean forceDownload) {
130 String downloadURL = getDownloadURL();
131 if (!downloadURL.isEmpty()) {
132 link[0] = "<a class='hidden-link' href='" + downloadURL
133 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
138 public String getDownloadURL() {
140 Object selection = app.getCurrentSelection();
141 if (selection != null && selection instanceof FileResource) {
142 FileResource file = (FileResource) selection;
143 return getDownloadURL(file);
148 public String getDownloadURL(FileResource file) {
151 String dateString = RestCommand.getDate();
152 String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
153 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
154 return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
159 public MenuBar createMenu() {
160 contextMenu.clearItems();
161 contextMenu.setAutoOpen(false);
162 final Command downloadCmd = new Command() {
165 public void execute() {
170 Folders folders = GSS.get().getFolders();
171 TreeItem selectedItem = folders.getCurrent();
172 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
173 boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource));
174 boolean newFolderVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
175 boolean uploadVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem)|| folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
176 if(newFolderVisible){
177 MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
178 newFolderItem.getElement().setId("topMenu.fileMenu.newFolder");
179 contextMenu.addItem(newFolderItem);
182 MenuItem uploadItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this));
183 uploadItem.getElement().setId("topMenu.fileMenu.upload");
184 contextMenu.addItem(uploadItem);
187 if (downloadVisible) {
188 String[] link = {"", ""};
189 createDownloadLink(link, false);
190 MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
191 contextMenu.addItem(downloadItem);
192 downloadItem.getElement().setId("topMenu.fileMenu.download");
193 createDownloadLink(link, true);
194 MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
195 saveAsItem.getElement().setId("topMenu.fileMenu.saveAs");
196 contextMenu.addItem(saveAsItem);
198 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
199 emptyTrashItem.getElement().setId("topMenu.fileMenu.emptyTrash");
200 contextMenu.addItem(emptyTrashItem);
202 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
203 refreshItem.getElement().setId("topMenu.fileMenu.refresh");
204 contextMenu.addItem(refreshItem);
206 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1));
207 sharingItem.getElement().setId("topMenu.fileMenu.sharing");
208 contextMenu.addItem(sharingItem)
209 .setVisible(propertiesVisible);
211 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0));
212 propertiesItem.getElement().setId("topMenu.fileMenu.properties");
213 contextMenu.addItem(propertiesItem)
214 .setVisible(propertiesVisible);