Added ids in homefolder(tree.homeFolder), Trash(tree.Trash), My Shared (tree.myShared...
[pithos] / src / gr / ebs / gss / client / EditMenu.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.ToTrashCommand;
26 import gr.ebs.gss.client.rest.resource.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.GroupUserResource;
29
30 import java.util.List;
31
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.resources.client.ClientBundle;
35 import com.google.gwt.resources.client.ImageResource;
36 import com.google.gwt.user.client.Command;
37 import com.google.gwt.user.client.ui.AbstractImagePrototype;
38 import com.google.gwt.user.client.ui.MenuBar;
39 import com.google.gwt.user.client.ui.MenuItem;
40 import com.google.gwt.user.client.ui.PopupPanel;
41
42 /**
43  * The 'Edit' menu implementation.
44  */
45 public class EditMenu extends PopupPanel implements ClickHandler {
46
47         /**
48          * The widget's images.
49          */
50         private final Images images;
51
52         private final MenuBar contextMenu  = new MenuBar(true);
53
54         /**
55          * An image bundle for this widget's images.
56          */
57         public interface Images extends ClientBundle, FileMenu.Images, MessagePanel.Images {
58
59                 /**
60                  * Will bundle the file 'editcut.png' residing in the package
61                  * 'gr.ebs.gss.resources'.
62                  *
63                  * @return the image prototype
64                  */
65                 @Source("gr/ebs/gss/resources/editcut.png")
66                 ImageResource cut();
67
68                 /**
69                  * Will bundle the file 'editcopy.png' residing in the package
70                  * 'gr.ebs.gss.resources'.
71                  *
72                  * @return the image prototype
73                  */
74                 @Source("gr/ebs/gss/resources/editcopy.png")
75                 ImageResource copy();
76
77                 /**
78                  * Will bundle the file 'editpaste.png' residing in the package
79                  * 'gr.ebs.gss.resources'.
80                  *
81                  * @return the image prototype
82                  */
83                 @Source("gr/ebs/gss/resources/editpaste.png")
84                 ImageResource paste();
85
86                 /**
87                  * Will bundle the file 'editdelete.png' residing in the package
88                  * 'gr.ebs.gss.resources'.
89                  *
90                  * @return the image prototype
91                  */
92                 @Override
93                 @Source("gr/ebs/gss/resources/editdelete.png")
94                 ImageResource delete();
95
96                 /**
97                  * Will bundle the file 'translate.png' residing in the package
98                  * 'gr.ebs.gss.resources'.
99                  *
100                  * @return the image prototype
101                  */
102                 @Source("gr/ebs/gss/resources/translate.png")
103                 ImageResource selectAll();
104
105                 /**
106                  * Will bundle the file 'border_remove.png' residing in the package
107                  * 'gr.ebs.gss.resources'.
108                  *
109                  * @return the image prototype
110                  */
111                 @Source("gr/ebs/gss/resources/border_remove.png")
112                 ImageResource unselectAll();
113         }
114
115         /**
116          * The widget's constructor.
117          *
118          * @param newImages the image bundle passed on by the parent object
119          */
120         public EditMenu(final Images newImages) {
121                 // The popup's constructor's argument is a boolean specifying that it
122                 // auto-close itself when the user clicks outside of it.
123                 super(true);
124                 setAnimationEnabled(true);
125                 images = newImages;
126                 createMenu();
127                 add(contextMenu);
128         }
129
130         @Override
131         public void onClick(ClickEvent event) {
132                 final EditMenu menu = new EditMenu(images);
133                 final int left = event.getRelativeElement().getAbsoluteLeft();
134                 final int top = event.getRelativeElement().getAbsoluteTop() + event.getRelativeElement().getOffsetHeight();
135                 menu.setPopupPosition(left, top);
136                 menu.show();
137         }
138
139         public MenuBar createMenu() {
140                 contextMenu.clearItems();
141                 contextMenu.setAutoOpen(false);
142
143                 final Command selectAllCommand = new Command() {
144
145                         @Override
146                         public void execute() {
147                                 hide();
148                                 if(GSS.get().isFileListShowing())
149                                         GSS.get().getFileList().selectAllRows();
150                                 else if(GSS.get().isSearchResultsShowing())
151                                         GSS.get().getSearchResults().selectAllRows();
152                         }
153                 };
154                 final Command unselectAllCommand = new Command() {
155
156                         @Override
157                         public void execute() {
158                                 hide();
159                                 if(GSS.get().isFileListShowing())
160                                         GSS.get().getFileList().clearSelectedRows();
161                                 else if(GSS.get().isSearchResultsShowing())
162                                         GSS.get().getSearchResults().clearSelectedRows();
163                         }
164                 };
165
166                 boolean cutcopyVisible = GSS.get().getCurrentSelection() != null && (GSS.get().getCurrentSelection() instanceof FolderResource
167                                         || GSS.get().getCurrentSelection() instanceof FileResource || GSS       .get().getCurrentSelection() instanceof GroupUserResource || GSS        .get().getCurrentSelection() instanceof List);
168                 String cutLabel = "Cut";
169                 String copyLabel ="Copy";
170                 String pasteLabel = "Paste";
171                 if(GSS.get().getCurrentSelection() != null)
172                         if(GSS.get().getCurrentSelection() instanceof FolderResource){
173                                 cutLabel = "Cut Folder";
174                                 copyLabel = "Copy Folder";
175                         }
176                         else if(GSS.get().getCurrentSelection() instanceof FileResource){
177                                 cutLabel = "Cut File";
178                                 copyLabel = "Copy File";
179                         }
180                         else if(GSS.get().getCurrentSelection() instanceof List){
181                                 cutLabel = "Cut Files";
182                                 copyLabel = "Copy Files";
183                         }
184                 if(GSS.get().getClipboard().getItem() != null)
185                         if(GSS.get().getClipboard().getItem().getFile() != null)
186                                 pasteLabel = "Paste File";
187                         else if(GSS.get().getClipboard().getItem().getFiles() != null)
188                                 pasteLabel = "Paste Files";
189                         else if(GSS.get().getClipboard().getItem().getFolderResource() != null)
190                                 pasteLabel = "Paste Folder";
191                 MenuItem cutItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.cut()).getHTML() + "&nbsp;"+cutLabel+"</span>", true, new CutCommand(this));
192                 cutItem.getElement().setId("topMenu.editMenu.cut");
193                 contextMenu.addItem(cutItem).setVisible(cutcopyVisible);
194                 
195                 MenuItem copyItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.copy()).getHTML() + "&nbsp;"+copyLabel+"</span>", true, new CopyCommand(this));
196                 copyItem.getElement().setId("topMenu.editMenu.copy");
197                 contextMenu.addItem(copyItem).setVisible(cutcopyVisible);
198                 
199                 MenuItem pasteItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.paste()).getHTML() + "&nbsp;"+pasteLabel+"</span>", true, new PasteCommand(this));                            
200                 pasteItem.getElement().setId("topMenu.editMenu.paste");
201                 if (GSS.get().getClipboard().getItem() != null)
202                         if(GSS.get().isUserListVisible() && GSS.get().getClipboard().getItem().getUser() == null){
203                                 contextMenu.addItem(pasteItem);
204                         }
205                         else if(!GSS.get().isUserListVisible() && GSS.get().getClipboard().getItem().getUser() != null){
206                                 //do not show paste
207                         }
208                         else if (GSS.get().getFolders().getCurrent().getUserObject() instanceof FolderResource){
209                                 contextMenu.addItem(pasteItem);
210                         }
211                 MenuItem moveToTrashItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Move to Trash</span>", true, new ToTrashCommand(this));
212                 moveToTrashItem.getElement().setId("topMenu.editMenu.moveToTrash");
213                 contextMenu     .addItem(moveToTrashItem)
214                                         .setVisible(cutcopyVisible);
215                 
216                 MenuItem deleteItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(this, images));
217                 deleteItem.getElement().setId("topMenu.editMenu.delete");
218                 contextMenu     .addItem(deleteItem)
219                                         .setVisible(cutcopyVisible);
220                 
221                 MenuItem selectAllItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.selectAll()).getHTML() + "&nbsp;Select All</span>", true, selectAllCommand);
222                 selectAllItem.getElement().setId("topMenu.editMenu.selectAll");
223                 contextMenu.addItem(selectAllItem);
224                 
225                 MenuItem unSelectAllItem = new MenuItem("<span>" + AbstractImagePrototype.create(images.unselectAll()).getHTML() + "&nbsp;Unselect All</span>", true, unselectAllCommand);
226                 unSelectAllItem.getElement().setId("topMenu.editMenu.unSelectAll");
227                 contextMenu.addItem(unSelectAllItem);
228                 return contextMenu;
229         }
230
231
232
233 }