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