Renamed all packages to gr.grnet.pithos.*
[pithos] / web_client / src / gr / grnet / pithos / web / client / FileContextMenu.java
1 /*
2  * Copyright 2007, 2008, 2009 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.grnet.pithos.web.client;
20
21 import gr.grnet.pithos.web.client.commands.CopyCommand;
22 import gr.grnet.pithos.web.client.commands.CutCommand;
23 import gr.grnet.pithos.web.client.commands.DeleteCommand;
24 import gr.grnet.pithos.web.client.commands.PasteCommand;
25 import gr.grnet.pithos.web.client.commands.PropertiesCommand;
26 import gr.grnet.pithos.web.client.commands.RefreshCommand;
27 import gr.grnet.pithos.web.client.commands.RestoreTrashCommand;
28 import gr.grnet.pithos.web.client.commands.ToTrashCommand;
29 import gr.grnet.pithos.web.client.commands.UploadFileCommand;
30 import gr.grnet.pithos.web.client.rest.resource.FileResource;
31 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
32 import gr.grnet.pithos.web.client.rest.resource.RestResource;
33 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
34 import gr.grnet.pithos.web.client.rest.resource.TrashFolderResource;
35
36 import java.util.List;
37
38 import com.google.gwt.event.dom.client.ClickEvent;
39 import com.google.gwt.event.dom.client.ClickHandler;
40 import com.google.gwt.event.dom.client.ContextMenuEvent;
41 import com.google.gwt.resources.client.ClientBundle;
42 import com.google.gwt.resources.client.ImageResource;
43 import com.google.gwt.user.client.Command;
44 import com.google.gwt.user.client.Event;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.user.client.ui.MenuBar;
47 import com.google.gwt.user.client.ui.MenuItem;
48 import com.google.gwt.user.client.ui.PopupPanel;
49
50 /**
51  * The 'File Context' menu implementation.
52  */
53 public class FileContextMenu extends PopupPanel implements ClickHandler {
54
55         /**
56          * The widget's images.
57          */
58         private final Images images;
59
60         private MenuItem cutItem;
61
62         private MenuItem copyItem;
63
64         private MenuItem pasteItem;
65
66         private MenuItem updateItem;
67
68         private MenuItem sharingItem;
69
70         private MenuItem propItem;
71
72         private MenuItem trashItem;
73
74         private MenuItem deleteItem;
75
76         private MenuItem downloadItem;
77
78         private MenuItem saveAsItem;
79
80         /**
81          * The image bundle for this widget's images that reuses images defined in
82          * other menus.
83          */
84         public interface Images extends ClientBundle,FileMenu.Images, EditMenu.Images {
85
86                 @Source("gr/grnet/pithos/resources/mimetypes/document.png")
87                 ImageResource fileContextMenu();
88
89                 @Source("gr/grnet/pithos/resources/doc_versions.png")
90                 ImageResource versions();
91
92                 @Override
93                 @Source("gr/grnet/pithos/resources/group.png")
94                 ImageResource sharing();
95
96                 @Override
97                 @Source("gr/grnet/pithos/resources/border_remove.png")
98                 ImageResource unselectAll();
99
100                 @Source("gr/grnet/pithos/resources/demo.png")
101                 ImageResource viewImage();
102 }
103
104         public static native String getDate()/*-{
105                 return (new Date()).toUTCString();
106         }-*/;
107
108         /**
109          * The widget's constructor.
110          *
111          * @param newImages the image bundle passed on by the parent object
112          */
113         public FileContextMenu(Images newImages, boolean isTrash, boolean isEmpty) {
114                 // The popup's constructor's argument is a boolean specifying that it
115                 // auto-close itself when the user clicks outside of it.
116                 super(true);
117                 GSS gss = GSS.get();
118                 setAnimationEnabled(true);
119                 images = newImages;
120
121                 // The command that does some validation before downloading a file.
122                 Command downloadCmd = new Command() {
123
124                         @Override
125                         public void execute() {
126                                 hide();
127                                 GSS.get().getTopPanel().getFileMenu().preDownloadCheck();
128                         }
129                 };
130
131                 pasteItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(this));
132                 pasteItem.getElement().setId("FileContextMenu.paste");
133                 RestResource sel = GSS.get().getTreeView().getSelection();
134                 MenuBar contextMenu = new MenuBar(true);
135                 if (isEmpty) {
136                         contextMenu.addItem(pasteItem);
137                         if (sel != null)
138                                 /*TODO:CELLTREE
139                                 if (GSS.get().getTreeView().isFileItem(GSS.get().getTreeView().getCurrent()))
140                                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
141                                 else if (GSS.get().getTreeView().isMySharedItem(GSS.get().getTreeView().getCurrent()) || GSS    .get()
142                                                                                                                                                                                                                         .getTreeView()
143                                                                                                                                                                                                                         .isOthersSharedItem(GSS .get()
144                                                                                                                                                                                                                                                                         .getTreeView()
145                                                                                                                                                                                                                                                                         .getCurrent()))
146                                         if(sel instanceof FolderResource)
147                                                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
148                         */
149                         if(sel instanceof RestResourceWrapper && !(sel instanceof TrashFolderResource)){
150                                 MenuItem upload = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
151                                 upload.getElement().setId("fileContextMenu.upload");
152                                 contextMenu.addItem(upload);
153                         }
154                         MenuItem refresh = new MenuItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
155                         refresh.getElement().setId("fileContextMenu.refresh");
156                         contextMenu.addItem(refresh);
157                         
158                 } else if (isTrash) {
159                         MenuItem restore = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(this));
160                         restore.getElement().setId("fileContextMenu.restore");
161                         contextMenu.addItem(restore);
162                         
163                         MenuItem delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
164                         delete.getElement().setId("fileContextMenu.delete");
165                         contextMenu.addItem(delete);
166                 } else {
167                         final Command unselectAllCommand = new Command() {
168
169                                 @Override
170                                 public void execute() {
171                                         hide();
172                                         if(GSS.get().isFileListShowing())
173                                                 GSS.get().getFileList().clearSelectedRows();
174                                         else if(GSS.get().isSearchResultsShowing())
175                                                 GSS.get().getSearchResults().clearSelectedRows();
176                                 }
177                         };
178                         cutItem = new MenuItem("<span id='fileContextMenu.cut'>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut</span>", true, new CutCommand(this));
179                         cutItem.getElement().setId("fileContextMenu.cut");
180                         
181                         copyItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(this));
182                         copyItem.getElement().setId("fileContextMenu.copy");
183                         
184                         updateItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
185                         updateItem.getElement().setId("fileContextMenu.upload");
186
187                         trashItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
188                         trashItem.getElement().setId("fileContextMenu.moveToTrash");
189                         
190                         deleteItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
191                         deleteItem.getElement().setId("fileContextMenu.delete");
192
193                         sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
194                         sharingItem.getElement().setId("fileContextMenu.sharing");
195                         
196                         propItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0));
197                         propItem.getElement().setId("fileContextMenu.properties");
198
199                         
200                         if(sel!=null && sel instanceof FolderResource)
201                                 contextMenu.addItem(updateItem);
202                         String[] link = {"", ""};
203                         gss.getTopPanel().getFileMenu().createDownloadLink(link, false);
204                         downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(newImages.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
205                         downloadItem.getElement().setId("fileContextMenu.download");
206                         contextMenu.addItem(downloadItem);
207                         
208                         gss.getTopPanel().getFileMenu().createDownloadLink(link, true);
209                         saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(newImages.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
210                         saveAsItem.getElement().setId("fileContextMenu.saveAs");
211                         contextMenu.addItem(saveAsItem);
212                         contextMenu.addItem(cutItem);
213                         contextMenu.addItem(copyItem);
214                         if(sel!=null && sel instanceof FolderResource)
215                                 contextMenu.addItem(pasteItem);
216                         MenuItem unSelect = new MenuItem("<span>" + AbstractImagePrototype.create(images.unselectAll()).getHTML() + "&nbsp;Unselect</span>", true, unselectAllCommand);
217                         unSelect.getElement().setId("fileContextMenu.unSelect");
218                         contextMenu.addItem(unSelect);
219                         
220                         contextMenu.addItem(trashItem);
221                         contextMenu.addItem(deleteItem);
222                         
223                         MenuItem refresh = new MenuItem("<span id='fileContextMenu.refresh'>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
224                         refresh.getElement().setId("fileContextMenu.refresh");
225                         contextMenu.addItem(refresh);
226                         
227                         contextMenu.addItem(sharingItem);
228                         contextMenu.addItem(propItem);
229                 }
230                 add(contextMenu);
231                 if (gss.getClipboard().hasFileItem())
232                         pasteItem.setVisible(true);
233                 else
234                         pasteItem.setVisible(false);
235         }
236
237         void onMultipleSelection() {
238                 updateItem.setVisible(false);
239                 downloadItem.setVisible(false);
240                 saveAsItem.setVisible(false);
241                 sharingItem.setVisible(false);
242         }
243         @Override
244         public void onClick(ClickEvent event) {
245                 if (GSS.get().getCurrentSelection() != null)
246                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
247                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
248                                 FileContextMenu menu;
249                                 if (res.isDeleted())
250                                         menu = new FileContextMenu(images, true, false);
251                                 else
252                                         menu = new FileContextMenu(images, false, false);
253                                 int left = event.getRelativeElement().getAbsoluteLeft();
254                                 int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
255                                 menu.setPopupPosition(left, top);
256                                 menu.show();
257                         } else if (GSS.get().getCurrentSelection() instanceof List) {
258                                 FileContextMenu menu;
259                                 /*TODO: CELLTREE
260                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
261                                         menu = new FileContextMenu(images, true, false);
262                                 else {
263                                         menu = new FileContextMenu(images, false, false);
264                                         menu.onMultipleSelection();
265                                 }
266                                 */
267                                 menu = new FileContextMenu(images, false, false);
268                                 menu.onMultipleSelection();
269                                 int left = event.getRelativeElement().getAbsoluteLeft();
270                                 int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
271                                 menu.setPopupPosition(left, top);
272                                 menu.show();
273                         }
274         }
275
276         
277         public void onContextEvent(ContextMenuEvent event) {
278                 if (GSS.get().getCurrentSelection() != null)
279                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
280                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
281                                 FileContextMenu menu;
282                                 if (res.isDeleted())
283                                         menu = new FileContextMenu(images, true, false);
284                                 else
285                                         menu = new FileContextMenu(images, false, false);
286                                 int left = event.getNativeEvent().getClientX();
287                                 int top = event.getNativeEvent().getClientY();
288                                 menu.setPopupPosition(left, top);
289                                 menu.show();
290
291                         } else if (GSS.get().getCurrentSelection() instanceof List) {
292                                 FileContextMenu menu;
293                                 /*TODO: CELLTREE
294                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
295                                         menu = new FileContextMenu(images, true, false);
296                                 else {
297                                         menu = new FileContextMenu(images, false, false);
298                                         menu.onMultipleSelection();
299                                 }
300                                 */
301                                 int left = event.getNativeEvent().getClientX();
302                                 int top = event.getNativeEvent().getClientY();
303                                 //menu.setPopupPosition(left, top);
304                                 //menu.show();
305                         }
306         }
307
308         public FileContextMenu onEvent(Event event) {
309                 FileContextMenu menu=null;
310                 if (GSS.get().getCurrentSelection() != null)
311                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
312                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
313
314                                 if (res.isDeleted())
315                                         menu = new FileContextMenu(images, true, false);
316                                 else
317                                         menu = new FileContextMenu(images, false, false);
318                                 int left = event.getClientX();
319                                 int top = event.getClientY();
320                                 menu.setPopupPosition(left, top);
321                                 menu.show();
322                         } else if (GSS.get().getCurrentSelection() instanceof List) {
323                                 /*TODO: CELLTREE
324                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getSelection()))
325                                         menu = new FileContextMenu(images, true, false);
326                                 else {
327                                         menu = new FileContextMenu(images, false, false);
328                                         menu.onMultipleSelection();
329                                 }*/
330                                 menu = new FileContextMenu(images, false, false);
331                                 menu.onMultipleSelection();
332                                 int left = event.getClientX();
333                                 int top = event.getClientY();
334                                 menu.setPopupPosition(left, top);
335                                 menu.show();
336                         }
337                 return menu;
338         }
339
340         public FileContextMenu onEmptyEvent(Event event) {
341                 FileContextMenu menu=null;
342                 /*TODO: CELLTREE
343                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
344                         menu = new FileContextMenu(images, true, true);
345                 else if(((DnDTreeItem)GSS.get().getTreeView().getCurrent()).getFolderResource() != null)
346                         menu = new FileContextMenu(images, false, true);
347                 else return menu;
348                 */
349                 menu = new FileContextMenu(images, false, true);
350                 int left = event.getClientX();
351                 int top = event.getClientY();
352                 menu.setPopupPosition(left, top);
353                 menu.show();
354                 return menu;
355         }
356
357
358 }