avoid class cast exception in multiple file selection
[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.commands.ViewImageCommand;
31 import gr.ebs.gss.client.dnd.DnDTreeItem;
32 import gr.ebs.gss.client.rest.resource.FileResource;
33 import gr.ebs.gss.client.rest.resource.FolderResource;
34
35 import java.util.List;
36
37 import com.google.gwt.user.client.Command;
38 import com.google.gwt.user.client.Event;
39 import com.google.gwt.user.client.ui.AbstractImagePrototype;
40 import com.google.gwt.user.client.ui.ClickListener;
41 import com.google.gwt.user.client.ui.MenuBar;
42 import com.google.gwt.user.client.ui.MenuItem;
43 import com.google.gwt.user.client.ui.PopupPanel;
44 import com.google.gwt.user.client.ui.TreeItem;
45 import com.google.gwt.user.client.ui.Widget;
46
47 /**
48  * The 'File Context' menu implementation.
49  */
50 public class FileContextMenu extends PopupPanel implements ClickListener {
51
52         /**
53          * The widget's images.
54          */
55         private final Images images;
56
57         private MenuItem cutItem;
58
59         private MenuItem copyItem;
60
61         private MenuItem pasteItem;
62
63         private MenuItem updateItem;
64
65         private MenuItem sharingItem;
66
67         private MenuItem propItem;
68
69         private MenuItem trashItem;
70
71         private MenuItem deleteItem;
72
73         private MenuItem downloadItem;
74
75         private MenuItem saveAsItem;
76
77         private MenuItem viewImageItem;
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 FileMenu.Images, EditMenu.Images {
84
85                 @Resource("gr/ebs/gss/resources/mimetypes/document.png")
86                 AbstractImagePrototype fileContextMenu();
87
88                 @Resource("gr/ebs/gss/resources/doc_versions.png")
89                 AbstractImagePrototype versions();
90
91                 @Resource("gr/ebs/gss/resources/group.png")
92                 AbstractImagePrototype sharing();
93
94                 @Resource("gr/ebs/gss/resources/border_remove.png")
95                 AbstractImagePrototype unselectAll();
96
97                 @Resource("gr/ebs/gss/resources/demo.png")
98                 AbstractImagePrototype 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>" + 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>" + 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>" + newImages.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
142                         contextMenu.addItem("<span>" + images.refresh().getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
143                 } else if (isTrash) {
144                         contextMenu.addItem("<span>" + newImages.versions().getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(this));
145                         contextMenu.addItem("<span>" + 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>" + newImages.cut().getHTML() + "&nbsp;Cut</span>", true, new CutCommand(this));
158                         copyItem = new MenuItem("<span>" + newImages.copy().getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(this));
159
160                         updateItem = new MenuItem("<span>" + newImages.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this));
161
162                         trashItem = new MenuItem("<span>" + newImages.emptyTrash().getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
163                         deleteItem = new MenuItem("<span>" + newImages.delete().getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
164
165                         sharingItem = new MenuItem("<span>" + newImages.sharing().getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(this, images, 1));
166                         propItem = new MenuItem("<span>" + 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] + 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] + newImages.download().getHTML() + " Save As" + link[1] + "</span>", true, downloadCmd);
177                         contextMenu.addItem(saveAsItem);
178                         boolean isImage = false;
179                         if (GSS.get().getCurrentSelection() != null && GSS.get().getCurrentSelection() instanceof FileResource)
180                                 isImage = ((FileResource) GSS.get().getCurrentSelection()).getContentType().startsWith("image/");
181                         if (isImage) {
182                                 viewImageItem = new MenuItem("<span>" + newImages.viewImage().getHTML() + "&nbsp;View Image</span>", true,
183                                                         new ViewImageCommand(this, images, gss.getTopPanel().getFileMenu().getDownloadURL()));
184                                 contextMenu.addItem(viewImageItem);
185                         }
186                         contextMenu.addItem(cutItem);
187                         contextMenu.addItem(copyItem);
188                         if(currentFolder!=null && currentFolder.getUserObject() instanceof FolderResource)
189                                 contextMenu.addItem(pasteItem);
190                         contextMenu.addItem("<span>" + images.unselectAll().getHTML() + "&nbsp;Unselect</span>", true, unselectAllCommand);
191                         contextMenu.addItem(trashItem);
192                         contextMenu.addItem(deleteItem);
193                         contextMenu.addItem("<span>" + images.refresh().getHTML() + "&nbsp;Refresh</span>", true, new RefreshCommand(this, images));
194                         contextMenu.addItem(sharingItem);
195                         contextMenu.addItem(propItem);
196                 }
197                 add(contextMenu);
198                 if (gss.getClipboard().hasFileItem())
199                         pasteItem.setVisible(true);
200                 else
201                         pasteItem.setVisible(false);
202         }
203
204         void onMultipleSelection() {
205                 updateItem.setVisible(false);
206                 propItem.setVisible(false);
207                 downloadItem.setVisible(false);
208                 sharingItem.setVisible(false);
209         }
210
211         public void onClick(Widget sender) {
212                 if (GSS.get().getCurrentSelection() != null)
213                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
214                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
215                                 FileContextMenu menu;
216                                 if (res.isDeleted())
217                                         menu = new FileContextMenu(images, true, false);
218                                 else
219                                         menu = new FileContextMenu(images, false, false);
220                                 int left = sender.getAbsoluteLeft();
221                                 int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
222                                 menu.setPopupPosition(left, top);
223                                 menu.show();
224                         } else if (GSS.get().getCurrentSelection() instanceof List) {
225                                 FileContextMenu menu;
226                                 if (GSS.get().getFolders().isTrashItem(GSS.get().getFolders().getCurrent()))
227                                         menu = new FileContextMenu(images, true, false);
228                                 else {
229                                         menu = new FileContextMenu(images, false, false);
230                                         menu.onMultipleSelection();
231                                 }
232                                 int left = sender.getAbsoluteLeft();
233                                 int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
234                                 menu.setPopupPosition(left, top);
235                                 menu.show();
236                         }
237         }
238
239         public void onEvent(Event event) {
240                 if (GSS.get().getCurrentSelection() != null)
241                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
242                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
243                                 FileContextMenu menu;
244                                 if (res.isDeleted())
245                                         menu = new FileContextMenu(images, true, false);
246                                 else
247                                         menu = new FileContextMenu(images, false, false);
248                                 int left = event.getClientX();
249                                 int top = event.getClientY();
250                                 menu.setPopupPosition(left, top);
251                                 menu.show();
252                         } else if (GSS.get().getCurrentSelection() instanceof List) {
253                                 FileContextMenu menu;
254                                 if (GSS.get().getFolders().isTrashItem(GSS.get().getFolders().getCurrent()))
255                                         menu = new FileContextMenu(images, true, false);
256                                 else {
257                                         menu = new FileContextMenu(images, false, false);
258                                         menu.onMultipleSelection();
259                                 }
260                                 int left = event.getClientX();
261                                 int top = event.getClientY();
262                                 menu.setPopupPosition(left, top);
263                                 menu.show();
264                         }
265         }
266
267         public void onEmptyEvent(Event event) {
268                 FileContextMenu menu;
269                 if (GSS.get().getFolders().isTrashItem(GSS.get().getFolders().getCurrent()))
270                         menu = new FileContextMenu(images, true, true);
271                 else if(((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
272                         menu = new FileContextMenu(images, false, true);
273                 else return;
274                 int left = event.getClientX();
275                 int top = event.getClientY();
276                 menu.setPopupPosition(left, top);
277                 menu.show();
278         }
279 }