edcdd511ea2f612706860f657bc57bfa189bb954
[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.PropertiesCommand;
25 import gr.ebs.gss.client.commands.RestoreTrashCommand;
26 import gr.ebs.gss.client.commands.ToTrashCommand;
27 import gr.ebs.gss.client.commands.UploadFileCommand;
28 import gr.ebs.gss.client.dnd.DnDTreeItem;
29 import gr.ebs.gss.client.rest.resource.FileResource;
30 import gr.ebs.gss.client.rest.resource.FolderResource;
31
32 import java.util.List;
33
34 import com.google.gwt.user.client.Command;
35 import com.google.gwt.user.client.Event;
36 import com.google.gwt.user.client.Window;
37 import com.google.gwt.user.client.ui.AbstractImagePrototype;
38 import com.google.gwt.user.client.ui.ClickListener;
39 import com.google.gwt.user.client.ui.MenuBar;
40 import com.google.gwt.user.client.ui.MenuItem;
41 import com.google.gwt.user.client.ui.PopupPanel;
42 import com.google.gwt.user.client.ui.Widget;
43
44 /**
45  * The 'File Context' menu implementation.
46  */
47 public class FileContextMenu extends PopupPanel implements ClickListener {
48
49         /**
50          * The widget's images.
51          */
52         private final Images images;
53
54         private MenuItem cutItem;
55
56         private MenuItem copyItem;
57
58         private MenuItem updateItem;
59
60         private MenuItem propItem;
61
62         private MenuItem trashItem;
63
64         private MenuItem deleteItem;
65
66         private MenuItem downloadItem;
67
68         /**
69          * The image bundle for this widget's images that reuses images defined in
70          * other menus.
71          */
72         public interface Images extends FileMenu.Images, EditMenu.Images {
73
74                 /**
75                  * Will bundle the file 'document_menu.png' residing in the package
76                  * 'gr.ebs.gss.resources.mimetypes'.
77                  *
78                  * @return the image prototype
79                  */
80                 @Resource("gr/ebs/gss/resources/mimetypes/document_menu.png")
81                 AbstractImagePrototype fileContextMenu();
82
83                 /**
84                  * Will bundle the file 'doc_versions.png' residing in the package
85                  * 'gr.ebs.gss.resources'.
86                  *
87                  * @return the image prototype
88                  */
89                 @Resource("gr/ebs/gss/resources/doc_versions.png")
90                 AbstractImagePrototype versions();
91         }
92
93         public static native String getDate()/*-{
94                         return (new Date()).toUTCString();
95                 }-*/;
96
97         /**
98          * The widget's constructor.
99          *
100          * @param newImages the image bundle passed on by the parent object
101          */
102         public FileContextMenu(final Images newImages, boolean isTrash, boolean isEmpty) {
103                 // The popup's constructor's argument is a boolean specifying that it
104                 // auto-close itself when the user clicks outside of it.
105                 super(true);
106                 setAnimationEnabled(true);
107                 images = newImages;
108
109                 // A dummy command that we will execute from unimplemented leaves.
110                 final Command cmd = new Command() {
111
112                         public void execute() {
113                                 hide();
114                                 Window.alert("You selected a menu item!");
115                         }
116                 };
117
118                 // The command that does some validation before downloading a file.
119                 final Command downloadCmd = new Command() {
120
121                         public void execute() {
122                                 hide();
123                                 GSS.get().getTopPanel().getFileMenu().preDownloadCheck();
124                         }
125                 };
126
127                 final MenuBar contextMenu = new MenuBar(true);
128                 if (isEmpty) {
129                         if (GSS.get().getFolders().getCurrent() != null)
130                                 if (GSS.get().getFolders().isFileItem(GSS.get().getFolders().getCurrent()))
131                                         contextMenu.addItem("<span>" + newImages.fileNew().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));
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.fileNew().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));
139                 } else if (isTrash) {
140                         contextMenu.addItem("<span>" + newImages.versions().getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(this));
141                         contextMenu.addItem("<span>" + newImages.delete().getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
142                 } else {
143
144                         cutItem = new MenuItem("<span>" + newImages.cut().getHTML() + "&nbsp;Cut</span>", true, new CutCommand(this));
145                         copyItem = new MenuItem("<span>" + newImages.copy().getHTML() + "&nbsp;Copy</span>", true, new CopyCommand(this));
146                         updateItem = new MenuItem("<span>" + newImages.fileUpdate().getHTML() + "&nbsp;Upload</span>", true, new UploadFileCommand(this, images));
147
148                         propItem = new MenuItem("<span>" + newImages.viewText().getHTML() + "&nbsp;Properties</span>", true, new PropertiesCommand(this, images));
149                         trashItem = new MenuItem("<span>" + newImages.emptyTrash().getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
150                         deleteItem = new MenuItem("<span>" + newImages.delete().getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
151
152                         contextMenu.addItem(cutItem);
153                         contextMenu.addItem(copyItem);
154                         contextMenu.addItem(updateItem);
155
156                         contextMenu.addItem(propItem);
157                         contextMenu.addItem(trashItem);
158                         contextMenu.addItem(deleteItem);
159                         String[] link = {"", ""};
160                         GSS.get().getTopPanel().getFileMenu().createDownloadLink(link);
161                         downloadItem = new MenuItem("<span>" + link[0] + newImages.download().getHTML() + " Download" + link[1] + "</span>", true, downloadCmd);
162                         contextMenu.addItem(downloadItem);
163
164                 }
165
166                 add(contextMenu);
167
168         }
169
170         void onMultipleSelection() {
171                 updateItem.setVisible(false);
172                 propItem.setVisible(false);
173                 downloadItem.setVisible(false);
174         }
175
176         /*
177          * (non-Javadoc)
178          *
179          * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
180          */
181         public void onClick(final Widget sender) {
182
183                 if (GSS.get().getCurrentSelection() != null)
184                         if (GSS.get().getCurrentSelection() instanceof FileResource) {
185                                 FileResource res = (FileResource) GSS.get().getCurrentSelection();
186                                 FileContextMenu menu;
187                                 if (res.isDeleted())
188                                         menu = new FileContextMenu(images, true, false);
189                                 else
190                                         menu = new FileContextMenu(images, false, false);
191                                 int left = sender.getAbsoluteLeft();
192                                 int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
193                                 menu.setPopupPosition(left, top);
194                                 menu.show();
195                         } else if (GSS.get().getCurrentSelection() instanceof List) {
196                                 List<FileResource> dto = (List<FileResource>) GSS.get().getCurrentSelection();
197                                 FileContextMenu menu;
198                                 if (GSS.get().getFolders().isTrashItem(GSS.get().getFolders().getCurrent()))
199                                         menu = new FileContextMenu(images, true, false);
200                                 else {
201                                         menu = new FileContextMenu(images, false, false);
202                                         menu.onMultipleSelection();
203                                 }
204                                 int left = sender.getAbsoluteLeft();
205                                 int top = sender.getAbsoluteTop() + sender.getOffsetHeight();
206                                 menu.setPopupPosition(left, top);
207                                 menu.show();
208                         }
209         }
210
211         public void onEmptyEvent(Event event) {
212                 FileContextMenu menu;
213                 if (GSS.get().getFolders().isTrashItem(GSS.get().getFolders().getCurrent()))
214                         menu = new FileContextMenu(images, true, true);
215                 else if(((DnDTreeItem)GSS.get().getFolders().getCurrent()).getFolderResource() != null)
216                         menu = new FileContextMenu(images, false, true);
217                 else return;
218                 int left = event.getClientX();
219                 int top = event.getClientY();
220                 menu.setPopupPosition(left, top);
221                 menu.show();
222         }
223 }