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 java.util.List;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.http.client.URL;
35 import com.google.gwt.resources.client.ClientBundle;
36 import com.google.gwt.resources.client.ImageResource;
37 import com.google.gwt.user.client.Command;
38 import com.google.gwt.user.client.ui.AbstractImagePrototype;
39 import com.google.gwt.user.client.ui.MenuBar;
40 import com.google.gwt.user.client.ui.PopupPanel;
41 import com.google.gwt.user.client.ui.TreeItem;
44 * The 'File' menu implementation.
46 public class FileMenu extends PopupPanel implements ClickHandler {
49 * The widget's images.
51 private final Images images;
54 * An image bundle for this widgets images.
56 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
58 @Source("gr/ebs/gss/resources/folder_new.png")
59 ImageResource folderNew();
61 @Source("gr/ebs/gss/resources/folder_outbox.png")
62 ImageResource fileUpdate();
64 @Source("gr/ebs/gss/resources/view_text.png")
65 ImageResource viewText();
68 @Source("gr/ebs/gss/resources/folder_inbox.png")
69 ImageResource download();
71 @Source("gr/ebs/gss/resources/trashcan_empty.png")
72 ImageResource emptyTrash();
74 @Source("gr/ebs/gss/resources/internet.png")
75 ImageResource sharing();
77 @Source("gr/ebs/gss/resources/refresh.png")
78 ImageResource refresh();
81 final MenuBar contextMenu = new MenuBar(true);
84 * The widget's constructor.
86 * @param _images the image bundle passed on by the parent object
88 public FileMenu(final Images _images) {
89 // The popup's constructor's argument is a boolean specifying that it
90 // auto-close itself when the user clicks outside of it.
92 setAnimationEnabled(true);
99 public void onClick(ClickEvent event) {
100 final FileMenu menu = new FileMenu(images);
101 final int left = event.getRelativeElement().getAbsoluteLeft();
102 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
103 menu.setPopupPosition(left, top);
110 * Do some validation before downloading a file.
112 void preDownloadCheck() {
113 Object selection = GSS.get().getCurrentSelection();
114 if (selection == null || !(selection instanceof FileResource)) {
115 GSS.get().displayError("You have to select a file first");
121 * Create a download link for the respective menu item, if the currently
122 * selected object is a file.
124 * @param link a String array with two elements that is modified so that the
125 * first position contains the opening tag and the second one the
127 * @param forceDownload If true, link will be such that browser should ask for filename
130 void createDownloadLink(String[] link, boolean forceDownload) {
131 String downloadURL = getDownloadURL();
132 if (!downloadURL.isEmpty()) {
133 link[0] = "<a class='hidden-link' href='" + downloadURL
134 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
139 public String getDownloadURL() {
141 Object selection = app.getCurrentSelection();
142 if (selection != null && selection instanceof FileResource) {
143 FileResource file = (FileResource) selection;
144 return getDownloadURL(file);
149 public String getDownloadURL(FileResource file) {
152 String dateString = RestCommand.getDate();
153 String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
154 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
155 return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
160 public MenuBar createMenu() {
161 contextMenu.clearItems();
162 contextMenu.setAutoOpen(false);
163 final Command downloadCmd = new Command() {
166 public void execute() {
171 Folders folders = GSS.get().getFolders();
172 TreeItem selectedItem = folders.getCurrent();
173 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
174 boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List));
175 boolean newFolderVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
176 boolean uploadVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem)|| folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
178 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
180 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this));
181 if (downloadVisible) {
182 String[] link = {"", ""};
183 createDownloadLink(link, false);
184 contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
185 createDownloadLink(link, true);
186 contextMenu.addItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
188 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
189 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
190 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1))
191 .setVisible(propertiesVisible);
192 contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0))
193 .setVisible(propertiesVisible);