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