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