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