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