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