Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / ToolsMenu.java @ ebead1b5

History | View | Annotate | Download (11.5 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.commands.AddUserCommand;
38
import gr.grnet.pithos.web.client.commands.CopyCommand;
39
import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
40
import gr.grnet.pithos.web.client.commands.CutCommand;
41
import gr.grnet.pithos.web.client.commands.DeleteCommand;
42
import gr.grnet.pithos.web.client.commands.DeleteGroupCommand;
43
import gr.grnet.pithos.web.client.commands.EmptyTrashCommand;
44
import gr.grnet.pithos.web.client.commands.PasteCommand;
45
import gr.grnet.pithos.web.client.commands.PropertiesCommand;
46
import gr.grnet.pithos.web.client.commands.RemoveUserCommand;
47
import gr.grnet.pithos.web.client.commands.RestoreTrashCommand;
48
import gr.grnet.pithos.web.client.commands.ToTrashCommand;
49
import gr.grnet.pithos.web.client.foldertree.File;
50
import gr.grnet.pithos.web.client.foldertree.Folder;
51
import gr.grnet.pithos.web.client.grouptree.Group;
52
import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
53
import gr.grnet.pithos.web.client.grouptree.User;
54

    
55
import java.util.List;
56

    
57
import com.google.gwt.user.client.Command;
58
import com.google.gwt.user.client.Window;
59
import com.google.gwt.user.client.ui.AbstractImagePrototype;
60
import com.google.gwt.user.client.ui.MenuBar;
61
import com.google.gwt.user.client.ui.MenuItem;
62
import com.google.gwt.user.client.ui.PopupPanel;
63

    
64
/**
65
 * The 'Folder Context' menu implementation.
66
 */
67
public class ToolsMenu extends PopupPanel {
68

    
69
        /**
70
         * The widget's images.
71
         */
72
        private final Images images;
73

    
74
        /**
75
         * The image bundle for this widget's images that reuses images defined in
76
         * other menus.
77
         */
78
        public interface Images extends GroupTreeView.Images {
79
        }
80

    
81
        private MenuItem pasteItem;
82

    
83
        private boolean empty = false;;
84
        
85
        /**
86
         * The widget's constructor.
87
         *
88
         * @param newImages the image bundle passed on by the parent object
89
         */
90
        public ToolsMenu(final Pithos app, Images newImages, TreeView selectedTree, Folder folder, final List<File> files) {
91
                // The popup's constructor's argument is a boolean specifying that it
92
                // auto-close itself when the user clicks outside of it.
93
                super(true);
94
                setAnimationEnabled(true);
95
                images = newImages;
96
        MenuBar contextMenu = new MenuBar(true);
97

    
98
        if (folder != null) {
99
                Boolean[] permissions = folder.getPermissions().get(app.getUsername());
100
                    boolean canWrite = folder.getOwner().equals(app.getUsername()) || (permissions!= null && permissions[1] != null && permissions[1]);
101
                    boolean isFolderTreeSelected = selectedTree.equals(app.getFolderTreeView());
102
                    
103
                if (!folder.isInTrash()) {
104
                        if (canWrite) {
105

    
106
                                if (isFolderTreeSelected) {
107
                                        MenuItem cut = null;
108
                                        if (files != null && !files.isEmpty()) {
109
                                                        cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut files</span>", true, new CutCommand(app, this, files));
110
                                        }
111
                                        else if (!folder.isContainer()) {
112
                                            cut = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.cut()).getHTML() + "&nbsp;Cut folder</span>", true, new CutCommand(app, this, folder));
113
                                        }
114
                                        if (cut != null)
115
                                                contextMenu.addItem(cut);
116
                                }
117
                        }
118
        
119
                        MenuItem copy = null;
120
                        if (files != null && !files.isEmpty())
121
                                copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy files</span>", true, new CopyCommand(app, this, files));
122
                        else if (!folder.isContainer())
123
                                copy = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.copy()).getHTML() + "&nbsp;Copy folder</span>", true, new CopyCommand(app, this, folder));
124
                        if (copy != null)
125
                                contextMenu.addItem(copy);
126
                
127
                        if (canWrite) {
128
                                if (!app.getClipboard().isEmpty()) {
129
                                        Object item = app.getClipboard().getItem();
130
                                        boolean showPaste = false;
131
                                        if (item instanceof List) {
132
                                                @SuppressWarnings("unchecked")
133
                                                        List<File> _files = (List<File>) item;
134
                                                if (_files.get(0).getOwner().equals(folder.getOwner()))
135
                                                        showPaste = true;
136
                                        }
137
                                        else {
138
                                                Folder f = (Folder) item;
139
                                                if (f.getOwner().equals(folder.getOwner()))
140
                                                        showPaste = true;
141
                                        }
142
                                        if (showPaste) {
143
                                            pasteItem = new MenuItem("<span id = 'folderContextMenu.paste'>" + AbstractImagePrototype.create(newImages.paste()).getHTML() + "&nbsp;Paste</span>", true, new PasteCommand(app, this, folder));
144
                                            contextMenu.addItem(pasteItem);
145
                                        }
146
                                }
147
        
148
                                    if (isFolderTreeSelected) {
149
                                            MenuItem moveToTrash = null;
150
                                            if (files != null && !files.isEmpty()) {
151
                                                    moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move files to Trash</span>", true, new ToTrashCommand(app, this, files));
152
                                            }
153
                                            else if (!folder.isContainer()) {
154
                                                    moveToTrash = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.emptyTrash()).getHTML() + "&nbsp;Move folder to Trash</span>", true, new ToTrashCommand(app, this, folder));
155
                                            }
156
                                            if (moveToTrash != null)
157
                                                    contextMenu.addItem(moveToTrash);
158
                                
159
                                        MenuItem delete = null;
160
                                        if (files != null && !files.isEmpty()) {
161
                                                delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete files</span>", true, new DeleteCommand(app, this, files, MessagePanel.images));
162
                                        }
163
                                        else if (!folder.isContainer())
164
                                                delete = new MenuItem("<span>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete folder</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
165
                                        if (delete != null)
166
                                                contextMenu.addItem(delete);
167
                                
168
                                        if (files != null && files.size() == 1) {
169
                                                contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;File properties</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PROPERTIES)));
170
                                                contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.group()).getHTML() + "&nbsp;Sharing</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.PERMISSIONS)));
171
                                                contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.versions()).getHTML() + "&nbsp;Versions</span>", true, new PropertiesCommand(app, this, files, PropertiesCommand.VERSIONS)));
172
                                        }
173
                                        else if (!folder.isContainer()) {
174
                                                contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.viewText()).getHTML() + "&nbsp;Folder properties</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PROPERTIES)));
175
                                                contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.sharing()).getHTML() + "&nbsp;Folder sharing</span>", true, new PropertiesCommand(app, this, folder, PropertiesCommand.PERMISSIONS)));
176
                                        }
177
                                    }
178
                                if (files != null && !files.isEmpty()) {
179
                                            contextMenu.addItem(new MenuItem("<span>" + AbstractImagePrototype.create(newImages.download()).getHTML() + "&nbsp;Download</span>", true, new Command() {
180
                                                        
181
                                                        @Override
182
                                                        public void execute() {
183
                                                        for (File f: files)
184
                                                                Window.open(app.getApiPath() + f.getOwner() + f.getUri() + "?X-Auth-Token=" + app.getToken(), "_blank", "");
185
                                                        }
186
                                                }));
187
                                }
188
                        }
189
                }
190
                else {
191
                        if (!folder.isTrash()) {
192
                                MenuItem restore = null;
193
                                if (files != null && !files.isEmpty())
194
                                        restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, files));
195
                                else
196
                                        restore = new MenuItem("<span>" + AbstractImagePrototype.create(images.versions()).getHTML() + "&nbsp;Restore</span>", true, new RestoreTrashCommand(app, this, folder));
197
                                contextMenu.addItem(restore);
198
        
199
                                    MenuItem delete = new MenuItem("<span id = 'folderContextMenu.delete'>" + AbstractImagePrototype.create(newImages.delete()).getHTML() + "&nbsp;Delete</span>", true, new DeleteCommand(app, this, folder, MessagePanel.images));
200
                                contextMenu.addItem(delete);
201
                        }
202
                        else {
203
                                    MenuItem emptyTrash = new MenuItem("<span>" + AbstractImagePrototype.create(images.emptyTrash()).getHTML() + "&nbsp;Empty Trash</span>", true, new EmptyTrashCommand(app, this));
204
                                    contextMenu.addItem(emptyTrash);
205
                        }
206
                }
207
        }
208
        else {
209
                Object o = app.getGroupTreeView().getSelected();
210
                if (o != null) {
211
                        if (o instanceof User) {
212
                        MenuItem removeUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Remove User</span>", true, new RemoveUserCommand(app, this, (User) o));
213
                        contextMenu.addItem(removeUser);
214
                        }
215
                        else if (o instanceof Group) {
216
                            MenuItem addUser = new MenuItem("<span>" + AbstractImagePrototype.create(images.user()).getHTML() + "&nbsp;Add User</span>", true, new AddUserCommand(app, this, (Group) o));
217
                            contextMenu.addItem(addUser);
218
                    
219
                            MenuItem deleteGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.delete()).getHTML() + "&nbsp;Delete Group</span>", true, new DeleteGroupCommand(app, this, (Group) o));
220
                            contextMenu.addItem(deleteGroup);
221
                        }
222
                        else {
223
                            MenuItem createGroup = new MenuItem("<span>" + AbstractImagePrototype.create(images.group()).getHTML() + "&nbsp;Create Group</span>", true, new CreateGroupCommand(app, this));
224
                            contextMenu.addItem(createGroup);
225
                        }
226
                }
227
                else
228
                        empty = true;
229
        }
230
                add(contextMenu);
231
        }
232
        
233
        public boolean isEmpty() {
234
                return empty;
235
        }
236
}