2 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
\r
4 * This file is part of GSS.
\r
6 * GSS is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * GSS is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with GSS. If not, see <http://www.gnu.org/licenses/>.
\r
19 package gr.ebs.gss.client;
\r
21 import gr.ebs.gss.client.commands.EmptyTrashCommand;
\r
22 import gr.ebs.gss.client.commands.NewFolderCommand;
\r
23 import gr.ebs.gss.client.commands.PropertiesCommand;
\r
24 import gr.ebs.gss.client.commands.UploadFileCommand;
\r
25 import gr.ebs.gss.client.rest.AbstractRestCommand;
\r
26 import gr.ebs.gss.client.rest.resource.FileResource;
\r
27 import gr.ebs.gss.client.rest.resource.GroupUserResource;
\r
29 import com.google.gwt.http.client.URL;
\r
30 import com.google.gwt.user.client.Command;
\r
31 import com.google.gwt.user.client.ui.AbstractImagePrototype;
\r
32 import com.google.gwt.user.client.ui.ClickListener;
\r
33 import com.google.gwt.user.client.ui.MenuBar;
\r
34 import com.google.gwt.user.client.ui.PopupPanel;
\r
35 import com.google.gwt.user.client.ui.TreeItem;
\r
36 import com.google.gwt.user.client.ui.Widget;
\r
39 * The 'File' menu implementation.
\r
41 public class FileMenu extends PopupPanel implements ClickListener {
\r
44 * The path info portion of the URL that provides the file download service.
\r
46 static final String FILE_DOWNLOAD_PATH = "/gss/fileDownload";
\r
49 * The widget's images.
\r
51 private final Images images;
\r
54 * An image bundle for this widgets images.
\r
56 public interface Images extends FilePropertiesDialog.Images {
\r
59 * Will bundle the file 'folder_new.png' residing in the package
\r
60 * 'gr.ebs.gss.resources'.
\r
62 * @return the image prototype
\r
64 @Resource("gr/ebs/gss/resources/folder_new.png")
\r
65 AbstractImagePrototype folderNew();
\r
68 * Will bundle the file 'filenew.png' residing in the package
\r
69 * 'gr.ebs.gss.resources'.
\r
71 * @return the image prototype
\r
73 @Resource("gr/ebs/gss/resources/filenew.png")
\r
74 AbstractImagePrototype fileNew();
\r
77 * Will bundle the file 'update.png' residing in the package
\r
78 * 'gr.ebs.gss.resources'.
\r
80 * @return the image prototype
\r
82 @Resource("gr/ebs/gss/resources/update.png")
\r
83 AbstractImagePrototype fileUpdate();
\r
86 * Will bundle the file 'view_text.png' residing in the package
\r
87 * 'gr.ebs.gss.resources'.
\r
89 * @return the image prototype
\r
91 @Resource("gr/ebs/gss/resources/view_text.png")
\r
92 AbstractImagePrototype viewText();
\r
95 * Will bundle the file 'download_manager.png' residing in the package
\r
96 * 'gr.ebs.gss.resources'.
\r
98 * @return the image prototype
\r
100 @Resource("gr/ebs/gss/resources/download_manager.png")
\r
101 AbstractImagePrototype download();
\r
104 * Will bundle the file 'trashcan_empty.png' residing in the package
\r
105 * 'gr.ebs.gss.resources'.
\r
107 * @return the image prototype
\r
109 @Resource("gr/ebs/gss/resources/trashcan_empty.png")
\r
110 AbstractImagePrototype emptyTrash();
\r
113 final MenuBar contextMenu = new MenuBar(true);
\r
116 * The widget's constructor.
\r
118 * @param _images the image bundle passed on by the parent object
\r
120 public FileMenu(final Images _images) {
\r
121 // The popup's constructor's argument is a boolean specifying that it
\r
122 // auto-close itself when the user clicks outside of it.
\r
124 setAnimationEnabled(true);
\r
134 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
\r
136 public void onClick(final Widget sender) {
\r
137 final FileMenu menu = new FileMenu(images);
\r
138 final int left = sender.getAbsoluteLeft();
\r
139 final int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
\r
140 menu.setPopupPosition(left, top);
\r
146 * Do some validation before downloading a file.
\r
148 void preDownloadCheck() {
\r
149 Object selection = GSS.get().getCurrentSelection();
\r
150 if (selection == null || !(selection instanceof FileResource)) {
\r
151 GSS.get().displayError("You have to select a file first");
\r
157 * Create a download link for the respective menu item, if the currently
\r
158 * selected object is a file.
\r
160 * @param link a String array with two elements that is modified so that the
\r
161 * first position contains the opening tag and the second one the
\r
164 void createDownloadLink(String[] link) {
\r
165 Object selection = GSS.get().getCurrentSelection();
\r
166 if (selection != null && selection instanceof FileResource) {
\r
167 FileResource file = (FileResource) selection;
\r
168 String dateString = AbstractRestCommand.getDate();
\r
169 String resource = file.getPath().substring(GSS.GSS_REST_PATH.length()-1,file.getPath().length());
\r
170 String sig = GSS.get().getCurrentUserResource().getUsername()+" "+AbstractRestCommand.calculateSig("GET", dateString, resource, AbstractRestCommand.base64decode(GSS.get().getToken()));
\r
171 link[0] = "<a class='hidden-link' href='" + file.getPath() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString) + "' target='_blank'>";
\r
176 public MenuBar createMenu() {
\r
177 contextMenu.clearItems();
\r
178 contextMenu.setAutoOpen(false);
\r
179 final Command downloadCmd = new Command() {
\r
181 public void execute() {
\r
183 preDownloadCheck();
\r
186 Folders folders = GSS.get().getFolders();
\r
187 TreeItem selectedItem = folders.getCurrent();
\r
188 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;
\r
189 boolean propertiesNotVisible = selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource);
\r
190 contextMenu.addItem("<span>" + images.folderNew().getHTML() + " New Folder</span>", true, new NewFolderCommand(this, images));
\r
191 contextMenu.addItem("<span>" + images.fileNew().getHTML() + " Upload</span>", true, new UploadFileCommand(this, images));
\r
192 contextMenu .addItem("<span>" + images.viewText().getHTML() + " Properties</span>", true, new PropertiesCommand(this, images))
\r
193 .setVisible(!propertiesNotVisible);
\r
194 contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + " Empty Trash</span>", true, new EmptyTrashCommand(this));
\r
195 if (downloadVisible) {
\r
196 String[] link = {"", ""};
\r
197 createDownloadLink(link);
\r
198 contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
\r
200 return contextMenu;
\r