recover admin interface
[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 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
34 import gr.ebs.gss.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/ebs/gss/resources/mimetypes/document.png")
87                 ImageResource fileContextMenu();
88
89                 @Source("gr/ebs/gss/resources/doc_versions.png")
90                 ImageResource versions();
91
92                 @Override
93                 @Source("gr/ebs/gss/resources/group.png")
94                 ImageResource sharing();
95
96                 @Override
97                 @Source("gr/ebs/gss/resources/border_remove.png")
98                 ImageResource unselectAll();
99
100                 @Source("gr/ebs/gss/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                 RestResource sel = GSS.get().getTreeView().getSelection();
133                 MenuBar contextMenu = new MenuBar(true);
134                 if (isEmpty) {
135                         contextMenu.addItem(pasteItem);
136                         if (sel != null)
137                                 /*TODO:CELLTREE
138                                 if (GSS.get().getTreeView().isFileItem(GSS.get().getTreeView().getCurrent()))
139                                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
140                                 else if (GSS.get().getTreeView().isMySharedItem(GSS.get().getTreeView().getCurrent()) || GSS    .get()
141                                                                                                                                                                                                                         .getTreeView()
142                                                                                                                                                                                                                         .isOthersSharedItem(GSS .get()
143                                                                                                                                                                                                                                                                         .getTreeView()
144                                                                                                                                                                                                                                                                         .getCurrent()))
145                                         if(sel instanceof FolderResource)
146                                                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
147                         */
148                         if(sel instanceof RestResourceWrapper && !(sel instanceof TrashFolderResource)){
149                                 contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
150                         }
151                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
152                         
153                 } else if (isTrash) {
154                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(this));
155                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
156                 } else {
157                         final Command unselectAllCommand = new Command() {
158
159                                 @Override
160                                 public void execute() {
161                                         hide();
162                                         if(GSS.get().isFileListShowing())
163                                                 GSS.get().getFileList().clearSelectedRows();
164                                         else if(GSS.get().isSearchResultsShowing())
165                                                 GSS.get().getSearchResults().clearSelectedRows();
166                                 }
167                         };
168                         cutItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut</span>", true, new CutCommand(this));
169                         copyItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(this));
170
171                         updateItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.fileUpdate()).getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
172
173                         trashItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
174                         deleteItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
175
176                         sharingItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
177                         propItem = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images, 0));
178
179                         
180                         if(sel!=null && sel instanceof FolderResource)
181                                 contextMenu.addItem(updateItem);
182                         String[] link = {"", ""};
183                         gss.getTopPanel().getFileMenu().createDownloadLink(link, false);
184                         downloadItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(newImages.download()).getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
185                         contextMenu.addItem(downloadItem);
186                         gss.getTopPanel().getFileMenu().createDownloadLink(link, true);
187                         saveAsItem = new MenuItem("<span>" + link[0] + AbstractImagePrototype.create(newImages.download()).getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
188                         contextMenu.addItem(saveAsItem);
189                         contextMenu.addItem(cutItem);
190                         contextMenu.addItem(copyItem);
191                         if(sel!=null && sel instanceof FolderResource)
192                                 contextMenu.addItem(pasteItem);
193                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.unselectAll()).getHTML() + "&nbsp;Unselect</span>", true, unselectAllCommand);
194                         contextMenu.addItem(trashItem);
195                         contextMenu.addItem(deleteItem);
196                         contextMenu.addItem("<span>" + AbstractImagePrototype.create(images.refresh()).getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
197                         contextMenu.addItem(sharingItem);
198                         contextMenu.addItem(propItem);
199                 }
200                 add(contextMenu);
201                 if (gss.getClipboard().hasFileItem())
202                         pasteItem.setVisible(true);
203                 else
204                         pasteItem.setVisible(false);
205         }
206
207         void onMultipleSelection() {
208                 updateItem.setVisible(false);
209                 downloadItem.setVisible(false);
210                 saveAsItem.setVisible(false);
211                 sharingItem.setVisible(false);
212         }
213         @Override
214         public void onClick(ClickEvent event) {
215                 if (GSS.get().getCurrentSelection() != null)
216                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
217                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
218                                 FileContextMenu menu;
219                                 if (res.isDeleted())
220                                         menu = new FileContextMenu(images, true, false);
221                                 else
222                                         menu = new FileContextMenu(images, false, false);
223                                 int left = event.getRelativeElement().getAbsoluteLeft();
224                                 int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
225                                 menu.setPopupPosition(left, top);
226                                 menu.show();
227                         } else if (GSS.get().getCurrentSelection() instanceof List) {
228                                 FileContextMenu menu;
229                                 /*TODO: CELLTREE
230                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
231                                         menu = new FileContextMenu(images, true, false);
232                                 else {
233                                         menu = new FileContextMenu(images, false, false);
234                                         menu.onMultipleSelection();
235                                 }
236                                 */
237                                 menu = new FileContextMenu(images, false, false);
238                                 menu.onMultipleSelection();
239                                 int left = event.getRelativeElement().getAbsoluteLeft();
240                                 int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
241                                 menu.setPopupPosition(left, top);
242                                 menu.show();
243                         }
244         }
245
246         
247         public void onContextEvent(ContextMenuEvent 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.getNativeEvent().getClientX();
257                                 int top = event.getNativeEvent().getClientY();
258                                 menu.setPopupPosition(left, top);
259                                 menu.show();
260
261                         } else if (GSS.get().getCurrentSelection() instanceof List) {
262                                 FileContextMenu menu;
263                                 /*TODO: CELLTREE
264                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
265                                         menu = new FileContextMenu(images, true, false);
266                                 else {
267                                         menu = new FileContextMenu(images, false, false);
268                                         menu.onMultipleSelection();
269                                 }
270                                 */
271                                 int left = event.getNativeEvent().getClientX();
272                                 int top = event.getNativeEvent().getClientY();
273                                 //menu.setPopupPosition(left, top);
274                                 //menu.show();
275                         }
276         }
277
278         public FileContextMenu onEvent(Event event) {
279                 FileContextMenu menu=null;
280                 if (GSS.get().getCurrentSelection() != null)
281                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
282                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
283
284                                 if (res.isDeleted())
285                                         menu = new FileContextMenu(images, true, false);
286                                 else
287                                         menu = new FileContextMenu(images, false, false);
288                                 int left = event.getClientX();
289                                 int top = event.getClientY();
290                                 menu.setPopupPosition(left, top);
291                                 menu.show();
292                         } else if (GSS.get().getCurrentSelection() instanceof List) {
293                                 /*TODO: CELLTREE
294                                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getSelection()))
295                                         menu = new FileContextMenu(images, true, false);
296                                 else {
297                                         menu = new FileContextMenu(images, false, false);
298                                         menu.onMultipleSelection();
299                                 }*/
300                                 menu = new FileContextMenu(images, false, false);
301                                 menu.onMultipleSelection();
302                                 int left = event.getClientX();
303                                 int top = event.getClientY();
304                                 menu.setPopupPosition(left, top);
305                                 menu.show();
306                         }
307                 return menu;
308         }
309
310         public FileContextMenu onEmptyEvent(Event event) {
311                 FileContextMenu menu=null;
312                 /*TODO: CELLTREE
313                 if (GSS.get().getTreeView().isTrashItem(GSS.get().getTreeView().getCurrent()))
314                         menu = new FileContextMenu(images, true, true);
315                 else if(((DnDTreeItem)GSS.get().getTreeView().getCurrent()).getFolderResource() != null)
316                         menu = new FileContextMenu(images, false, true);
317                 else return menu;
318                 */
319                 menu = new FileContextMenu(images, false, true);
320                 int left = event.getClientX();
321                 int top = event.getClientY();
322                 menu.setPopupPosition(left, top);
323                 menu.show();
324                 return menu;
325         }
326
327
328 }