Hide download and property file menu options on multiple selection
[pithos] / src / gr / ebs / gss / client / FileMenu.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\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
10  *\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
15  *\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
18  */\r
19 package gr.ebs.gss.client;\r
20 \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.RefreshCommand;\r
25 import gr.ebs.gss.client.commands.UploadFileCommand;\r
26 import gr.ebs.gss.client.rest.RestCommand;\r
27 import gr.ebs.gss.client.rest.resource.FileResource;\r
28 import gr.ebs.gss.client.rest.resource.GroupUserResource;\r
29 \r
30 import com.google.gwt.http.client.URL;\r
31 import com.google.gwt.user.client.Command;\r
32 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
33 import com.google.gwt.user.client.ui.ClickListener;\r
34 import com.google.gwt.user.client.ui.MenuBar;\r
35 import com.google.gwt.user.client.ui.PopupPanel;\r
36 import com.google.gwt.user.client.ui.TreeItem;\r
37 import com.google.gwt.user.client.ui.Widget;\r
38 \r
39 /**\r
40  * The 'File' menu implementation.\r
41  */\r
42 public class FileMenu extends PopupPanel implements ClickListener {\r
43 \r
44         /**\r
45          * The widget's images.\r
46          */\r
47         private final Images images;\r
48 \r
49         /**\r
50          * An image bundle for this widgets images.\r
51          */\r
52         public interface Images extends FilePropertiesDialog.Images {\r
53 \r
54                 @Resource("gr/ebs/gss/resources/folder_new.png")\r
55                 AbstractImagePrototype folderNew();\r
56 \r
57                 @Resource("gr/ebs/gss/resources/folder_outbox.png")\r
58                 AbstractImagePrototype fileUpdate();\r
59 \r
60                 @Resource("gr/ebs/gss/resources/view_text.png")\r
61                 AbstractImagePrototype viewText();\r
62 \r
63                 @Resource("gr/ebs/gss/resources/folder_inbox.png")\r
64                 AbstractImagePrototype download();\r
65 \r
66                 @Resource("gr/ebs/gss/resources/trashcan_empty.png")\r
67                 AbstractImagePrototype emptyTrash();\r
68 \r
69                 @Resource("gr/ebs/gss/resources/internet.png")\r
70                 AbstractImagePrototype sharing();\r
71 \r
72                 @Resource("gr/ebs/gss/resources/refresh.png")\r
73                 AbstractImagePrototype refresh();\r
74 }\r
75 \r
76         final MenuBar contextMenu = new MenuBar(true);\r
77 \r
78         /**\r
79          * The widget's constructor.\r
80          *\r
81          * @param _images the image bundle passed on by the parent object\r
82          */\r
83         public FileMenu(final Images _images) {\r
84                 // The popup's constructor's argument is a boolean specifying that it\r
85                 // auto-close itself when the user clicks outside of it.\r
86                 super(true);\r
87                 setAnimationEnabled(true);\r
88                 images = _images;\r
89                 add(contextMenu);\r
90 \r
91         }\r
92 \r
93         public void onClick(final Widget sender) {\r
94                 final FileMenu menu = new FileMenu(images);\r
95                 final int left = sender.getAbsoluteLeft();\r
96                 final int top = sender.getAbsoluteTop() + sender.getOffsetHeight();\r
97                 menu.setPopupPosition(left, top);\r
98                 menu.show();\r
99         }\r
100 \r
101         /**\r
102          * Do some validation before downloading a file.\r
103          */\r
104         void preDownloadCheck() {\r
105                 Object selection = GSS.get().getCurrentSelection();\r
106                 if (selection == null || !(selection instanceof FileResource)) {\r
107                         GSS.get().displayError("You have to select a file first");\r
108                         return;\r
109                 }\r
110         }\r
111 \r
112         /**\r
113          * Create a download link for the respective menu item, if the currently\r
114          * selected object is a file.\r
115          *\r
116          * @param link a String array with two elements that is modified so that the\r
117          *            first position contains the opening tag and the second one the\r
118          *            closing tag\r
119          * @param forceDownload If true, link will be such that browser should ask for filename\r
120          *                              and save location\r
121          */\r
122         void createDownloadLink(String[] link, boolean forceDownload) {\r
123                 String downloadURL = getDownloadURL();\r
124                 if (!downloadURL.isEmpty()) {\r
125                         link[0] = "<a class='hidden-link' href='" + downloadURL\r
126                                         + (forceDownload ? "&dl=1" : "") + "' target='_blank'>";\r
127                         link[1] = "</a>";\r
128                 }\r
129         }\r
130 \r
131         public String getDownloadURL() {\r
132                 GSS app = GSS.get();\r
133                 Object selection = app.getCurrentSelection();\r
134                 if (selection != null && selection instanceof FileResource) {\r
135                         FileResource file = (FileResource) selection;\r
136                         return getDownloadURL(file);\r
137                 }\r
138                 return "";\r
139         }\r
140 \r
141         public String getDownloadURL(FileResource file) {\r
142                 GSS app = GSS.get();\r
143                 if (file != null) {\r
144                         String dateString = RestCommand.getDate();\r
145                         String resource = file.getUri().substring(app.getApiPath().length()-1,file.getUri().length());\r
146                         String sig = app.getCurrentUserResource().getUsername()+" "+RestCommand.calculateSig("GET", dateString, resource, RestCommand.base64decode(app.getToken()));\r
147                         return file.getUri() + "?Authorization=" + URL.encodeComponent(sig) + "&Date="+URL.encodeComponent(dateString);\r
148                 }\r
149                 return "";\r
150         }\r
151 \r
152         public MenuBar createMenu() {\r
153                 contextMenu.clearItems();\r
154                 contextMenu.setAutoOpen(false);\r
155                 final Command downloadCmd = new Command() {\r
156 \r
157                         public void execute() {\r
158                                 hide();\r
159                                 preDownloadCheck();\r
160                         }\r
161                 };\r
162                 Folders folders = GSS.get().getFolders();\r
163                 TreeItem selectedItem = folders.getCurrent();\r
164                 boolean downloadVisible = GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource;\r
165                 boolean propertiesVisible = !(selectedItem != null && (folders.isTrash(selectedItem) || folders.isMyShares(selectedItem) || folders.isOthersShared(selectedItem) || selectedItem.getUserObject() instanceof GroupUserResource)) && downloadVisible;\r
166                 contextMenu.addItem("<span>" + images.folderNew().getHTML() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));\r
167                 contextMenu.addItem("<span>" + images.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));\r
168                 if (downloadVisible) {\r
169                         String[] link = {"", ""};\r
170                         createDownloadLink(link, false);\r
171                         contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);\r
172                         createDownloadLink(link, true);\r
173                         contextMenu.addItem("<span>" + link[0] + images.download().getHTML() + "&nbsp;Save As" + link[1] + "</span>", true, downloadCmd);\r
174                 }\r
175                 contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(this));\r
176                 contextMenu.addItem("<span>" + images.refresh().getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));\r
177                 contextMenu.addItem("<span>" + images.sharing().getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1))\r
178                                         .setVisible(propertiesVisible);\r
179                 contextMenu.addItem("<span>" + images.viewText().getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0))\r
180                                         .setVisible(propertiesVisible);\r
181                 return contextMenu;\r
182         }\r
183 \r
184 }\r