9e3066b42d8d0f121acae4053bcefe9b3a2abd7a
[pithos] / gss / 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.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
28 \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
37 \r
38 /**\r
39  * The 'File' menu implementation.\r
40  */\r
41 public class FileMenu extends PopupPanel implements ClickListener {\r
42 \r
43         /**\r
44          * The path info portion of the URL that provides the file download service.\r
45          */\r
46         static final String FILE_DOWNLOAD_PATH = "/gss/fileDownload";\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 FilePropertiesDialog.Images {\r
57 \r
58                 /**\r
59                  * Will bundle the file 'folder_new.png' residing in the package\r
60                  * 'gr.ebs.gss.resources'.\r
61                  *\r
62                  * @return the image prototype\r
63                  */\r
64                 @Resource("gr/ebs/gss/resources/folder_new.png")\r
65                 AbstractImagePrototype folderNew();\r
66 \r
67                 /**\r
68                  * Will bundle the file 'filenew.png' residing in the package\r
69                  * 'gr.ebs.gss.resources'.\r
70                  *\r
71                  * @return the image prototype\r
72                  */\r
73                 @Resource("gr/ebs/gss/resources/filenew.png")\r
74                 AbstractImagePrototype fileNew();\r
75 \r
76                 /**\r
77                  * Will bundle the file 'update.png' residing in the package\r
78                  * 'gr.ebs.gss.resources'.\r
79                  *\r
80                  * @return the image prototype\r
81                  */\r
82                 @Resource("gr/ebs/gss/resources/update.png")\r
83                 AbstractImagePrototype fileUpdate();\r
84 \r
85                 /**\r
86                  * Will bundle the file 'view_text.png' residing in the package\r
87                  * 'gr.ebs.gss.resources'.\r
88                  *\r
89                  * @return the image prototype\r
90                  */\r
91                 @Resource("gr/ebs/gss/resources/view_text.png")\r
92                 AbstractImagePrototype viewText();\r
93 \r
94                 /**\r
95                  * Will bundle the file 'download_manager.png' residing in the package\r
96                  * 'gr.ebs.gss.resources'.\r
97                  *\r
98                  * @return the image prototype\r
99                  */\r
100                 @Resource("gr/ebs/gss/resources/download_manager.png")\r
101                 AbstractImagePrototype download();\r
102 \r
103                 /**\r
104                  * Will bundle the file 'trashcan_empty.png' residing in the package\r
105                  * 'gr.ebs.gss.resources'.\r
106                  *\r
107                  * @return the image prototype\r
108                  */\r
109                 @Resource("gr/ebs/gss/resources/trashcan_empty.png")\r
110                 AbstractImagePrototype emptyTrash();\r
111         }\r
112 \r
113         final MenuBar contextMenu = new MenuBar(true);\r
114 \r
115         /**\r
116          * The widget's constructor.\r
117          *\r
118          * @param _images the image bundle passed on by the parent object\r
119          */\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
123                 super(true);\r
124                 setAnimationEnabled(true);\r
125                 images = _images;\r
126                 // createMenu();\r
127                 add(contextMenu);\r
128 \r
129         }\r
130 \r
131         /*\r
132          * (non-Javadoc)\r
133          *\r
134          * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)\r
135          */\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
141 \r
142                 menu.show();\r
143         }\r
144 \r
145         /**\r
146          * Do some validation before downloading a file.\r
147          */\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
152                         return;\r
153                 }\r
154         }\r
155 \r
156         /**\r
157          * Create a download link for the respective menu item, if the currently\r
158          * selected object is a file.\r
159          *\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
162          *            closing tag\r
163          */\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
172                         link[1] = "</a>";\r
173                 }\r
174         }\r
175 \r
176         public MenuBar createMenu() {\r
177                 contextMenu.clearItems();\r
178                 contextMenu.setAutoOpen(false);\r
179                 final Command downloadCmd = new Command() {\r
180 \r
181                         public void execute() {\r
182                                 hide();\r
183                                 preDownloadCheck();\r
184                         }\r
185                 };\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() + "&nbsp;New Folder</span>", true, new NewFolderCommand(this, images));\r
191                 contextMenu.addItem("<span>" + images.fileNew().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));\r
192                 contextMenu     .addItem("<span>" + images.viewText().getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images))\r
193                                         .setVisible(!propertiesNotVisible);\r
194                 contextMenu.addItem("<span>" + images.emptyTrash().getHTML() + "&nbsp;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() + "&nbsp;Download" + link[1] + "</span>", true, downloadCmd);\r
199                 }\r
200                 return contextMenu;\r
201         }\r
202 \r
203 }\r