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