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