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.RestResource;
31 import java.util.List;
33 import com.google.gwt.event.dom.client.ClickEvent;
34 import com.google.gwt.event.dom.client.ClickHandler;
35 import com.google.gwt.http.client.URL;
36 import com.google.gwt.resources.client.ClientBundle;
37 import com.google.gwt.resources.client.ImageResource;
38 import com.google.gwt.user.client.Command;
39 import com.google.gwt.user.client.ui.AbstractImagePrototype;
40 import com.google.gwt.user.client.ui.MenuBar;
41 import com.google.gwt.user.client.ui.MenuItem;
42 import com.google.gwt.user.client.ui.PopupPanel;
43 import com.google.gwt.user.client.ui.TreeItem;
46 * The 'File' menu implementation.
48 public class FileMenu extends PopupPanel implements ClickHandler {
51 * The widget's images.
53 private final Images images;
56 * An image bundle for this widgets images.
58 public interface Images extends ClientBundle,FilePropertiesDialog.Images {
60 @Source("gr/ebs/gss/resources/folder_new.png")
61 ImageResource folderNew();
63 @Source("gr/ebs/gss/resources/folder_outbox.png")
64 ImageResource fileUpdate();
66 @Source("gr/ebs/gss/resources/view_text.png")
67 ImageResource viewText();
70 @Source("gr/ebs/gss/resources/folder_inbox.png")
71 ImageResource download();
73 @Source("gr/ebs/gss/resources/trashcan_empty.png")
74 ImageResource emptyTrash();
76 @Source("gr/ebs/gss/resources/internet.png")
77 ImageResource sharing();
79 @Source("gr/ebs/gss/resources/refresh.png")
80 ImageResource refresh();
83 final MenuBar contextMenu = new MenuBar(true);
86 * The widget's constructor.
88 * @param _images the image bundle passed on by the parent object
90 public FileMenu(final Images _images) {
91 // The popup's constructor's argument is a boolean specifying that it
92 // auto-close itself when the user clicks outside of it.
94 setAnimationEnabled(true);
101 public void onClick(ClickEvent event) {
102 final FileMenu menu = new FileMenu(images);
103 final int left = event.getRelativeElement().getAbsoluteLeft();
104 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
105 menu.setPopupPosition(left, top);
112 * Do some validation before downloading a file.
114 void preDownloadCheck() {
115 Object selection = GSS.get().getCurrentSelection();
116 if (selection == null || !(selection instanceof FileResource)) {
117 GSS.get().displayError("You have to select a file first");
123 * Create a download link for the respective menu item, if the currently
124 * selected object is a file.
126 * @param link a String array with two elements that is modified so that the
127 * first position contains the opening tag and the second one the
129 * @param forceDownload If true, link will be such that browser should ask for filename
132 void createDownloadLink(String[] link, boolean forceDownload) {
133 String downloadURL = getDownloadURL();
134 if (!downloadURL.isEmpty()) {
135 link[0] = "<a class='hidden-link' href='" + downloadURL
136 + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";
141 public String getDownloadURL() {
143 Object selection = app.getCurrentSelection();
144 if (selection != null && selection instanceof FileResource) {
145 FileResource file = (FileResource) selection;
146 return getDownloadURL(file);
151 public String getDownloadURL(FileResource file) {
154 String dateString = RestCommand.getDate();
155 String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());
156 String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));
157 return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);
162 public MenuBar createMenu() {
163 contextMenu.clearItems();
164 contextMenu.setAutoOpen(false);
165 final Command downloadCmd = new Command() {
168 public void execute() {
174 RestResource selectedItem = GSS.get().getTreeView().getSelection();
175 boolean downloadVisible = true;//GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
176 boolean propertiesVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource || GSS.get().getCurrentSelection() instanceof List));
177 boolean newFolderVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem) || folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
178 boolean uploadVisible = true;//!(selectedItem != null && (folders.isTrash(selectedItem) || folders.isTrashItem(selectedItem)|| folders.isMyShares(selectedItem)|| folders.isOthersShared(selectedItem)));
179 if(newFolderVisible){
180 MenuItem newFolderItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.folderNew()).getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
181 newFolderItem.getElement().setId("topMenu.file.newFolder");
182 contextMenu.addItem(newFolderItem);
185 MenuItem uploadItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.fileUpdate()).getHTML() + " Upload</span>", true, new UploadFileCommand(this));
186 uploadItem.getElement().setId("topMenu.file.upload");
187 contextMenu.addItem(uploadItem);
189 if (downloadVisible) {
190 String[] link = {"", ""};
191 createDownloadLink(link, false);
193 MenuItem downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
194 contextMenu.addItem(downloadItem);
195 downloadItem.getElement().setId("topMenu.file.download");
197 createDownloadLink(link, true);
199 MenuItem saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(images.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
200 saveAsItem.getElement().setId("topMenu.file.saveAs");
201 contextMenu.addItem(saveAsItem);
203 MenuItem emptyTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
204 emptyTrashItem.getElement().setId("topMenu.file.emptyTrash");
205 contextMenu.addItem(emptyTrashItem);
207 MenuItem refreshItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + " Refresh</span>", true, new RefreshCommand(this, images));
208 refreshItem.getElement().setId("topMenu.file.refresh");
209 contextMenu.addItem(refreshItem);
211 MenuItem sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.sharing()).getHTML() + " Sharing</span>", true, new PropertiesCommand(this, images, 1));
212 sharingItem.getElement().setId("topMenu.file.sharing");
213 contextMenu.addItem(sharingItem)
214 .setVisible(propertiesVisible);
216 MenuItem propertiesItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.viewText()).getHTML() + " Properties</span>", true, new PropertiesCommand(this, images, 0));
217 propertiesItem.getElement().setId("topMenu.file.properties");
218 contextMenu.addItem(propertiesItem)
219 .setVisible(propertiesVisible);