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