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