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