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